Happy New Year! I need help.
I have missing values in the emission (panel) data and they have been replaced by rnormal(sample mean, sample sd). Unfortunately, sd is large that these missing values are replaced with negative values. Since the emission can't be negative, I want to re-replace these negative values with rnromal(sample mean, sample sd) until they are replaced with positive values. Unfortunately, the loop stops after replacing negative values a single time.
Just to let yall know that although it may sound as if I am trying to fabricate data, it is not the case here. I am simply trying to see how drastically different the tsplot would be with "complete" data when compared to the original data.
The following is my code:
tsset id date, daily
tsfill, full
foreach var in emitCOD emitDUST {
egen `var'mean = mean(`var'), by(Province)
egen `var'sd = sd(`var'), by(Province)
tsset id date
bysort id: carryforward `var'mean, replace
bysort id: carryforward `var'sd, replace
gsort id-date
bysort id: carryforward `var'mean, replace
bysort id: carryforward `var'sd, replace
forvalues i = 1/2 {
gen `var'_`i' = `var'
replace `var'_`i' = rnormal(`var'mean, `var'sd) if `var' == .
while (`var'_`i' < 0) {
replace `var'_`i' = rnormal(`var'mean, `var'sd)
if `var'_`i' >= 0 {
continue, break
}
}
}
}
Thanks.