Re: st: How to skip logit when there is no variation in the dependent variable

 From n j cox To statalist@hsphsun2.harvard.edu Subject Re: st: How to skip logit when there is no variation in the dependent variable Date Mon, 19 Mar 2007 14:01:01 +0000

This is good but can I think be improved by correcting
what looks like a little slip and moving more work outside
the loop.

mark touse
markout touse <predictors>
replace touse = 0 if !drought
levelsof country if touse, local(K)
foreach k of local K {
logit drought <predictors> if country == `k' & touse
}

If -country- is a string variable, use "`k'" instead.

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

Ulrich Kohler replied to Thomas de Hoop

>> I have created dummy variables for 214 countries. For each of these
>> countries I want to estimate several logit models to estimate chances
>> of certain situations to occur, and create chance variables from the
>> prediction of these models. I estimate these chances using data from
>> the past. I have data of these situation For this I created loops with
>> the foreach and forvalue commands from stata. The problem is that in
>> some countries a certain situation never occurred. For these
>> situations there are only 0 value observations. Of course logistic
>> regression is not possible in these kind of situations. For this
>> reason I would like to skip these equations in the loop.
>> For this reason I counted the number of times a situation did occur in
>> the past and created a variable.
>>
>> I tried to skip the equation as follows:
>>
>> forval i=1/214 {
>> logit drought`i' variable1 variable 2 etc. if droughtcount`i'>0
>> }
>>
>> i= the country number
>> variable1 variable2 etc are the explanatory variables.
>>
>> drought`i'=the situation in a certain country
>> droughtcount`i'=the number of times a situation did occur in the past.
>>
>> For country 3 this results in the following error:
>> no observations
>> r(2000);

If you _know_ that all obs are zero in the situation were outcome does not
vary, one way would be:

levelsof country, local(K)
foreach k of local K {
count if drought & country == `k'
if r(N) > 0 logit drought variable1 variable2
}

Problems might still arise as a result of missings in the dependent variables.
A more general approach would be something like

mark touse
markout touse variable1 variable2 variable3 <list of independent variables>
levelsof country, local(K)
foreach k of local K {
count if drought & country == `k' & touse
if r(N) > 0 logit drought variable1 variable2
}

It might be also sensible to set the level for calculating the equation a
little higher by using -if r(N) > 30-, for example.

*
* 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/