Tuesday, February 21, 2006

st: RE: writing mata code more effeciently

From your code I suspect that you are trying to create a vector y from "f_week_hours" and a matrix x from "f_lnwage", "m_lnwage", and "assetinc" where y and x should contain the actual values for cases with "s_init" equal to 1 and should be 0 otherwise. In Mata this could be done as follows

void fe() { stata("drop _all") stata("use c:\data\nicolas\select\sas\testing") y = st_data(., "f_week_hours") x = st_data(., ("f_lnwage", "m_lnwage", "assetinc")) s = st_data(., "s_init") s = s:==1 //skip this line if s is 0/1 already y = y:*s x = x:*s }

although it may be faster to first create modified variables in Stata (i.e. set the variables to zero for cases with "s_init" unequal 1) and then just read these variables.

But do you really need the cases with "s_init" unequal 1 in y and x? Wouldn't it be better for your application to just include cases with s_init==1 in y and x? Assuming that "s_init" is 0/1, the code for this would be

void fe() { stata("drop _all") stata("use c:\data\nicolas\select\sas\testing") y = st_data(., "f_week_hours", "s_init") x = st_data(., ("f_lnwage", "m_lnwage", "assetinc"), "s_init") }

or if you prefer views

void fe() { stata("drop _all") stata("use c:\data\nicolas\select\sas\testing") st_view(y=., ., "f_week_hours", "s_init") st_view(x=., ., ("f_lnwage", "m_lnwage", "assetinc"), "s_init") }

If "s_init" isn't 0/1, then do something like

void fe() { stata("drop _all") stata("use c:\data\nicolas\select\sas\testing") stata("gen byte touse = s_init==1") y = st_data(., "f_week_hours", "touse") x = st_data(., ("f_lnwage", "m_lnwage", "assetinc"), "touse") }

ben

> -----Original Message----- > From: owner-statalist@hsphsun2.harvard.edu [mailto:owner- > statalist@hsphsun2.harvard.edu] On Behalf Of AbdelRahmen El Lahga > Sent: Tuesday, February 21, 2006 1:03 AM > To: statalist@hsphsun2.harvard.edu > Subject: st: writing mata code more effeciently > > Is there any solution to rewrite this fragment of my mata code more > efficiently. My old laptop runs more then 1 hours without finishing the > job > on 50591 obs. > below the code: > version 9.1 > mata: > mata clear > void fe () { > stata ("drop _all") > stata ("use c:\data\nicolas\select\sas\testing") > y_init=x_init=s_init=. > st_view(y_init, ., "f_week_hours") > st_view(x_init, ., ("f_lnwage", "m_lnwage", "assetinc")) > st_view(s_init, ., "s_init") > tmax=21 // tmax > n=rows(x_init)/tmax // nombre d'individus > nt=rows(x_init) //nombre total des lignes > k=cols(x_init) // nombre de regresseurs > k > y=J(nt,1,0) > x=J(nt,k,0) > s=s_init > for (i=1;i=nt;i++) { > if (s[i]=1) { > y[i,.]= y_init[i,.] > x[i,.]= x_init[i,.] > } > } > } > fe () > end > PS: When i use st_data instead of st_view I get the follwing error: > fe(): 3257 nonpointer found where pointer required > <istmt>: - function returned error > Any help > > * > * For searches and help try: > * http://www.stata.com/support/faqs/res/findit.html > * http://www.stata.com/support/statalist/faq > * http://www.ats.ucla.edu/stat/stata/

* * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/


Tag:


Links to this post:

Create a Link



<< Home

This page is powered by Blogger. Isn't yours?