Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: Loop-within-loop error while looping over observations for a microsimulation


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Loop-within-loop error while looping over observations for a microsimulation
Date   Tue, 7 Aug 2012 11:09:35 +0100

I am surprised at your syntax

... in `N'+`j'

as I didn't know that Stata would do the mathematics there. But
regardless of that, observation `N' + `j' is at position _N + 1 ... _N
+ 12 and so always outside your dataset, so nothing would be done Your
logic appears to call for

local k = `i' + `j'
if childbirth[`i']==1 replace childbirth= .i in`k'

I note also that

        if childbirth[`i']== .f replace childbirth= rbinomial(1,
probabilityconceive)

-replace-s in every observation, including those earlier declared
times when infertile. That can't be what you intend, so you need to
specify -in- here too.

That said, I don't see that you need to reach for the exotic machinery
of extended missing values. Infertility post partum would seem to be
represented naturally by 0; the value is known and zero, and for a
specific reason, not unknown for a specific reason.

Nick

On Tue, Aug 7, 2012 at 12:31 AM, Stephen Cranney
<cranney.stephen@gmail.com> wrote:
> Thank you all so much, that worked to make the syntax legal. I'm
> almost there, but it's not quite doing what I want it to. To recap,
> the data is sorted by year and month. When the "birth" is created out
> of the binomial function, I want the code to detect it and make a
> number of the subsequent months "infertile" so that binomial
> probability of births won't work during those months. .f= fertile
> missing, .i is missing for after-birth infertility.
> "monthsofpostpartum" is a local macro that is simply the number of
> months of after-birth infertility. This just gives me a sequence of 1s
> and 0s; it doesn't seem to be replacing the subsequent months after
> birth with the .i
>
> local N = _N
> local  monthsofpostpartum =  12
> forvalues i = 1/`N' {
>         forvalues j= 1/`monthsofpostpartum' {
>                 if childbirth[`i']== .f replace childbirth= rbinomial(1, probabilityconceive)
>                 if childbirth[`i']==1 replace childbirth= .i in`N'+`j'
>                 }
>         }
>
> Thanks in advance,
>
>
>
> On Mon, Aug 6, 2012 at 1:45 PM, Nick Cox <njcoxstata@gmail.com> wrote:
>> You still have a problem.
>>
>>           replace childbirth[_n+j]== .i if (childbirth==1) in `i'
>>
>> is illegal, as explained.
>>
>> This kind of statement would be legal
>>
>>  if (childbirth[<subscript>]  == 1)  replace childbirth = .i in <something>
>>
>> but I am not certain that it is what you want.
>>
>> On Mon, Aug 6, 2012 at 8:08 PM, Stephen Cranney
>> <cranney.stephen@gmail.com> wrote:
>>> But with the current syntax isn't that not a problem? If I input the
>>> numbers directly I have the following:
>>>
>>> local N = _N
>>> forvalues i = 1/`N' {
>>>         forvalues j= 0/12 {
>>>
>>>                 replace childbirth= rbinomial(1, probabilityconceive)
>>> if childbirth== .f
>>>                 replace childbirth[_n+j]== .i if (childbirth==1) in `i'
>>>                 }
>>>         }
>>>
>>>
>>> Here 1/`N' is directly computed before the loop starts, and the j
>>> values are inputted directly (with the higher bound being the number
>>> of postpartum months), so I'm still not seeing exactly how the syntax
>>> has a problem. I've closed off the space that was between childbirth
>>> and the _n, so it's not that it's accidentally recognizing the value
>>> as a weight.
>>>
>>> Thanks,
>>>
>>> Stephen
>>>
>>> On Mon, Aug 6, 2012 at 11:59 AM, Nick Cox <njcoxstata@gmail.com> wrote:
>>>>
>>>> No "seems to indicate" here: simply, subscripts are not allowed to the
>>>> left of the assignment in Stata.
>>>>
>>>> The syntax diagram shows the form -replace oldvar = exp-. Stuff can
>>>> follow that, but nothing else can be included.
>>>>
>>>> Nick
>>>>
>>>> On Mon, Aug 6, 2012 at 4:53 PM, Stephen Cranney
>>>> <cranney.stephen@gmail.com> wrote:
>>>> > Thanks for catching that; it's still giving me the "weights not
>>>> > allowed" error message though, even when I delete the extra space.
>>>> >
>>>> > I found this prior Statlist post that seems to indicate that Stata
>>>> > triggers the no weights allowed warning when the `i' is placed to the
>>>> > left of the equals sign:
>>>> >
>>>> > http://www.stata.com/statalist/archive/2006-08/msg00976.html
>>>> >
>>>> > So I changed my code to the following, but I'm still getting the "no
>>>> > weights allowed" message.
>>>> >
>>>> > local N = _N
>>>> > forvalues i = 1/`N' {
>>>> >         forvalues j= 0/12 {
>>>> >                 replace childbirth= rbinomial(1, probabilityconceive) if childbirth== .f
>>>> >                 replace childbirth[_n+j]== .i if (childbirth==1) in `i'
>>>> >                 }
>>>> >         }
>>>> >
>>>> > Best,
>>>> >
>>>> >
>>>> >
>>>> > On Mon, Aug 6, 2012 at 9:32 AM, daniel klein
>>>> > <klein.daniel.81@googlemail.com> wrote:
>>>> >> I did not follow closely, but there is a space between <childbirth>
>>>> >> and  <[`i']> in your code, probably causing Stata to interpret <[`i']>
>>>> >> as some kind of weight, rather than a subscript.
>>>> >>
>>>> >> Best
>>>> >> Daniel
>>>> >>
>>>> >> --
>>>> >> [...]
>>>> >> However, when I change the
>>>> >> monthsofpostpartum variable to a constant (in this case 12), it's
>>>> >> giving me a "weights not allowed" sign.
>>>> *
*
*   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   |   Site index