Hi!
I am trying to do a regression imputation on my educational variable (I know that multiple imputation is better, but due to time limits of the deadline I have to use this one).
I got some help writing the loop, because I found it really hard. I works for the most part, but I keep getting an error.
First:
egen lw = group(S003 wave)
summ lw
local Nlw = r(max)
forvalues i = 1/`Nlw' {
summ educ if lw==`i'
if (r(N) <= 20) {
break
}
regress educ X047 X003 female if lw==`i'
predict educ_hat if lw==`i', xb
replace educ = educ_hat if missing(educ) & lw==`i'
drop educ_hat
}
When I run it, I get the error 'no observations'. I looked it up and saw that many others solved it by using 'capture'. I tried it, but it doesn't seem to work as I get the error 'no last observations' (when capture is placed before regress).
The second part of the syntax gets the same error:
egen land = group(S003)
summ land
local Nland = r(max)
forvalues i = 1/`Nland' {
summ educ if land==`i'
if (r(N) <= 20) {
break
}
regress educ (c.X047 c.X003 c.female)##c.wave if land==`i'
predict educ_hat if land==`i', xb
replace educ = educ_hat if missing(educ) & land==`i'
drop educ_hat
}
The first syntax should impute missing values on education when not a complete country (S003) have missing values on this variable. The second syntax should impute education for respondents in countries where nobody has a value on this variable.
I really hope someone is able to help me!
Best,
Lisa
I am trying to do a regression imputation on my educational variable (I know that multiple imputation is better, but due to time limits of the deadline I have to use this one).
I got some help writing the loop, because I found it really hard. I works for the most part, but I keep getting an error.
First:
egen lw = group(S003 wave)
summ lw
local Nlw = r(max)
forvalues i = 1/`Nlw' {
summ educ if lw==`i'
if (r(N) <= 20) {
break
}
regress educ X047 X003 female if lw==`i'
predict educ_hat if lw==`i', xb
replace educ = educ_hat if missing(educ) & lw==`i'
drop educ_hat
}
When I run it, I get the error 'no observations'. I looked it up and saw that many others solved it by using 'capture'. I tried it, but it doesn't seem to work as I get the error 'no last observations' (when capture is placed before regress).
The second part of the syntax gets the same error:
egen land = group(S003)
summ land
local Nland = r(max)
forvalues i = 1/`Nland' {
summ educ if land==`i'
if (r(N) <= 20) {
break
}
regress educ (c.X047 c.X003 c.female)##c.wave if land==`i'
predict educ_hat if land==`i', xb
replace educ = educ_hat if missing(educ) & land==`i'
drop educ_hat
}
The first syntax should impute missing values on education when not a complete country (S003) have missing values on this variable. The second syntax should impute education for respondents in countries where nobody has a value on this variable.
I really hope someone is able to help me!
Best,
Lisa
Comment