Statalist


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

st: RE: nested forvalues loop


From   "Ariel Linden" <ariel.linden@gmail.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: nested forvalues loop
Date   Mon, 5 Oct 2009 12:18:14 -0700

Hi Nick,

Thank you for reviewing my code. I tried running it and it did not return
any value for any of the ps`j' 


Per your comment about the "data-dredging involved" this is not the case. I
am trying to predict the propensity score for each person-month of data, so
that I can ultimately create weights and then use them within a xtreg model.


This code is supposed to use the current and lagged months covariates in the
logistic model to account for past treatment and covariate history.

So, given that context, I would like to loop at each month, incorporating
the lags of all prior months starting at month 14, working up to the end of
the last period (month 24). Month 14 will only have 1 lag (this is where the
month 13 data comes in), while month 24 will contain covariate values from
month 13 all the way up to month 24.

Any help would be appreciated. Doing this manually is both tedious and
inefficient.

Thanks!

Ariel


Date: Fri, 2 Oct 2009 18:29:32 +0100
From: "Nick Cox" <n.j.cox@durham.ac.uk>
Subject: st: RE: nested forvalues loop

There is some inconsistency here over whether you want to start at 13 or 14.


Your -predict- command won't work. Second time around the inner loop, ps`i'
already exists. 

There might be some conservative ill-ease at the data-dredging involved but
that's a separate issue. 

Here's a guess at better code. 

forvalues j=1/10 {
	gen ps`j' = . 
	forvalues i=14/24 {
		capture logit  Y l(0/`j').X1 l(0/`j').X2 l(0/`j').X3 month
if month==`i'
		if _rc == 0 { 
			tempvar work
			predict `work' 
			replace ps`j' = `work' if month == `i'
			drop `work'
		} 
	}
}		
	

Nick
n.j.cox@durham.ac.uk 

******************************************************

Ariel Linden

I have a basic question about structuring this nested forvalues loop (Stata
v10.1).

I have a panel data set and I want to focus on months 13 thru 24. In each
loop I want to use the lagged values for all the prior months. So I have `i'
which loops through the months 13 thru 24, and `j' that is intended to
increase the lag at each iteration.

For example month 14 should have 0/1 lag and month 24 should have 0/10 lags.

I would appreciate feedback as to if I wrote this correctly. 

forvalues i=14/24 {

	forvalues j=1/10 {

capture logit  Y l(0/`j').X1 l(0/`j').X2 l(0/`j').X3 month if month==`i'

predict ps`i' if month == `i'
}
}





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



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index