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)

1 comment:

  1. How can I do the same with a fixed effects estimation? B_FE = ...

    ReplyDelete