Hi, I would like to calculate cumulative security residual return, which is residual in a regression of a firm's monthly stock return(variable"ret") on the market monthly returns (variable "vwretd") over the 12-month period ( estimation_window) before the date of filing of the financial statement (event_window). I have prepared my data and am working on the regression loop. my codes are shown below:

gen predicted_return=.
egen id=group(company_id)

forvalues i=1(1)N { /*note: replace N with the highest value of id */

reg ret vwretd if id==`i' & estimation_window==1
predict p if id==`i'
replace predicted_return = p if id==`i' & event_window==1
drop p
}


initially, it worked well, but when it comes to id==15, it showed the following error and stopped running the loop:

(option xb assumed; fitted values)
(8,074,563 missing values generated)
(0 real changes made)

insufficient observations
(option xb assumed; fitted values)
(8,074,563 missing values generated)
(0 real changes made)


I search the forum and tried to fix the problem by using the codes below but it does work. Can I get some help?

forvalues i=1(1)N { /*note: replace N with the highest value of id */

capture noisily reg ret vwretd if id==`i' & estimation_window==1
capture noisily predict p if id==`i'
capture noisly replace predicted_return = p if id==`i' & event_window==1
capture noisly drop p
}