Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: How do I loop computations over a matrix of numbers?


From   Francesca Colantuoni <[email protected]>
To   [email protected]
Subject   st: How do I loop computations over a matrix of numbers?
Date   Thu, 12 Sep 2013 15:56:43 -0400

Hello all!
After I have obtained my model parameters, I would like to compute the
predicted depent variable (quantity) by  plugging the estimated values
of the parameters in the equation, using observed independent
variables (my data set). I want to repeat this procedure a number of
times, so that I can compute mean and confidence intervals for the
dependent variable without resampling. Specifically:
I obtained vectors from a multivariate normal distribution for each of
the 10 model parameters (matrix 1000by10).
Now I have to compute totQnull and totQprime (quantities before and
after a policy change, say, a tax), for each row of the generated
matrix of coefficients. This way I should get 1000 values for
totQprime and 1000 for totQnull. For each of these values I have to
compute DeltaQ (so to have 1000 DeltaQs), and then compute mean and
confidence interval from the 1000 DeltaQs.

My question is: how can I import the generated matrix of coefficients
in my (panel) data set so that I can loop over it?

If I use the predicted model parameters and want to compute Qprime
only one time, the code is pretty much the following:

use "C:Users\dataset.dta", clear

gen beta1=-1.64
.....
gen beta10=-2.27

tempvar Qn ob Q_diff1 Q_diff2 Q_diff mQ_diff mlqt fix_eff
generate  Qn = price1*beta1 + price2*beta3 + price4*beta4
replace Qn = price1*beta3 + price2*beta2 + price4*beta5 if brand==147
generate  Qs = price1*beta6 + pe2_0*beta7 + price4*beta4
replace Qs = pe1_0*beta7 + price2*beta8+ price4*beta5 if brand==147

generate  ob=beta9+(beta10)*(28)

generate  Q_diff1 = ln(ob*Qn + (1 - ob)*Qs)

generate  Q_diff2 = ln(ob*Qn)

generate  Q_diff = Q_diff1*(state1==1) + Q_diff2*(state1==2)
replace Q_diff = Q_diff1*(state2==1) + Q_diff2*(state2==2)  if brand==147

egen  mQ_diff = mean(Q_diff), by(store brand)
egen  mlqt = mean(lqt), by(store brand)

generate  fix_eff = mlqt - mQ_diff  /*for this I would like to
generate 1000 fix_eff, one for each row of the matrix of
coefficients*/

generate lqt_null = Q_diff + fix_eff

egen totQ_null=sum(lqt_null) /*I would like to generate 1000
totQ_null, one for each row of the matrix of coefficients*/



I need to save the fix_eff1-1000 to compute, with the same procedure,
totQ_prime.

Any hint on this issue would be greatly appreciated!

Cheers,

Francesca
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index