Wednesday, July 25, 2012
The Return Command
* In Stata the return command is an essential tool that any aspiring programmer should be aware of.
* Note, I have two lines of comments going on in this code. Hopefully, it is coherent.
* The comments that are commented out as such /* HELLO I am YOU COMMENT */ are referring
* to a method to identify coefficients when the error is multiplicative.
* Within Stata there are two methods by which commands yeild feedback to the user.
* One method is the visual display.
* Let's start with a little generated data
clear
set obs 10000
gen x = rnormal()*10
gen u = exp(cos(rnormal()*10))
gen y = 30*x*u /* Imagine we have a multiplicative error and we want to
identify the coefficient 30. */
* All of the above commands do not give any kind of return except visual ones (this is frustrating to me since sometimes)
* However, there is a lot of commands that do.
gen add_error = y-30*x
corr add_error x /* We can see that that the "addative" error is correlated with the
explanatory variable */
* corr is an rclass command the majority of commands return as rclass.
* to see the returns from corr
return list
* We can target the returns in the form that Stata lists:
di "We will have difficulty estimating the 30 coefficient on x"
di "because the addative error is correlated with x at a level = " r(rho)
reg y x /* This badly fails at identifying the 3 because the error is
correlated with explanatory variable x */
* Most regression/estimator commands are "eclass" commands and store their returns as ereturns.
* To see those returns:
ereturn list
* These returns are available to be targetted if you would like to store a value as a macro.
global rss_1 = e(rss)
global r2_a = e(r2_a)
* Matrices can be targetted in a similar fassion
matrix b1 = e(b)
matrix list b1
* Most coefficients are also saved as system variables
global bx = _b[x]
* When you do your next eclass command it will override the stored values from the previous
gen lny = ln(y) /* We may attempt to isolate the coefficient by using the natural log command
. This makes ln(y) = 30*x*u = ln(30) + ln(x) + ln(u) */
gen lnx = ln(x)
reg lny lnx
di "The " exp(_b[_cons]) " is closer to 30 than the coefficient in the direct estimation ${bx}"
/* Note that the only reason this works as well as it does is because u is defined so that
E(ln(u))=E(ln(exp(rnormal())))=E(rnormal())=0 and because there is not constant in the first equation.
If there were a constant in the first equation then we would have
y=c+bxu -> lny=ln(c+bxu) which does not help us. */
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment