Showing posts with label SOLS. Show all posts
Showing posts with label SOLS. Show all posts

Thursday, February 7, 2013

2SLS with multiple endogenous variables


* I am wondering if when using 2SLS you must use a multivariate OLS in the reduced form or if you can just do each individual endogenous variable.

* Let's see!

clear
set obs 10000

* First generate the instruments
gen z1 = rnormal()
gen z2 = rnormal()

* Now the error.  u1 and u2 are the parts of w1 and w2 correlated with the error u.
gen u1 = rnormal()
gen u2 = rnormal()
gen u3 = rnormal()*3

gen w1 = 1*z1 + .5*z2 + rnormal() + 2*u1
gen w2 = -.5*z1 + 1.5*z2 + rnormal() - 2*u2 + .2*u1

gen u = u1 + u2 + u3
gen y = 4 + w1*1 + w2*1 + u

* We can see because u is correlated with w1 and w2, OLS will be biased and inconsistent.

reg y w1 w2

* Instrumental variable regression could solve this problem

ivreg y (w1 w2 = z1 z2)

* Let's see about our 2SLS (which should be the same as IV)

reg w1 z1 z2
predict w1_hat

reg w2 z1 z2
predict w2_hat

reg y w1_hat w2_hat
* It seems that 2SLS using separate regressors is producing the same results.

* This is probably because in SOLS if you use the same regressors then I think the coefficients are the same but the standard errors may be adjusted for cross equation correlations (I think I recall).

Tuesday, May 29, 2012

Write your own System OLS in Mata

* This is a method related to Seemingly Unrelated Regression (SUR) referred to in a previous post.

* Imagine that you have three different equations that have different dependent variables but the same explanatory variables.

* Let us think of the example of the demand for capturing and mining an astroid and the research that goes into it. See Tony Cookson's post on the subject

* In general we may think that might have three different relationships that we are trying to uncover but we don't understand the relationship between them.

* In equation 1: we have the price of platnium
* In equation 2: we have millions of dollars of investments in private spacecraft
* In equation 3: we have an index prepresenting the probability of making a launch attempt

* This will specify the means of the three variables to be drawn jointly
matrix m = (0,0,0)

* This will specify the covariance matrix for the errors
matrix C = (12, -5, 5 \ -5, 12, -3 \ 5, -3, 6)
matrix list C

* Clear the memory and tell stata to prepare for 1000 observations
clear
set obs 100

* Draw three variablesb
drawnorm u1 u2 u3, means(m) cov(C)

* Now let's generate our exogenous variables
gen platnum_Q = 10 + rnormal()
  label var platnum_Q "The current quantity of plantinum available on the market"
gen invest_age = 50 + rnormal()
  label var invest_age "Average age of investors"
gen national_news_coverage = rpoisson(10)
  label var national_news_coverage "The current amount of national news coverage of space crafts."

* Now generate the dependent variables
gen platnum_price = 3*platnum_Q-0*invest_age-.1*national_news_coverage+u1*20
gen space_investment = 2*platnum_Q+3.5*invest_age +5.2*national_news_coverage+u2*5
gen launch_index = 6.01*platnum_Q+5.3*invest_age-10*national_news_coverage+u3*10

* send variables to mata.

putmata x1=platnum_Q x2=invest_age x3=national_news_coverage, replace
putmata y1=platnum_price y2=space_investment y3=launch_index, replace

mata
N=rows(x1)
cons = J(N, 1, 1)

// Save the explanatory variables to a matrix
X=(x1 , x2 , x3, cons)

// Save the dependent variables to a matrix
// Notice that at this point this is the same as OLS except that in OLS there is only one Y.
Y=(y1 , y2 , y3)

// alternatively the Sytem OLS estimator is:
B_OLS = invsym(X'*X) * (X'*Y)
B_OLS

end

reg platnum_price platnum_Q invest_age national_news_coverage
reg space_investment platnum_Q invest_age national_news_coverage
reg launch_index platnum_Q invest_age national_news_coverage

* This is the same as using seemingly unrelated regression.
mvreg platnum_price  space_investment launch_index = platnum_Q invest_age national_news_coverage

* However, seemlingl unrelated regression allows for restrictions on coefficients.
sureg (platnum_price=platnum_Q invest_age national_news_coverage) ///
     (space_investment=platnum_Q invest_age national_news_coverage) ///
(launch_index=platnum_Q invest_age national_news_coverage)