Statalist


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

Re: st: RE: nested forvalues loop


From   Austin Nichols <[email protected]>
To   [email protected]
Subject   Re: st: RE: nested forvalues loop
Date   Mon, 5 Oct 2009 15:27:37 -0400

Ariel Linden <[email protected]>:

Probably you are not running any logits. Maybe you want:

g double ps=.
forv i=14/24 {
 loc j=`j'-14
 logit Y l(0/`j').X1 l(0/`j').X2 l(0/`j').X3 if month==`i'
 tempvar w
 predict double `w'
 replace ps=`w' if e(sample)
 drop `w'
}

On Mon, Oct 5, 2009 at 3:18 PM, Ariel Linden <[email protected]> wrote:
> 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" <[email protected]>
> 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
> [email protected]

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index