Hi everyone, I am trying to to in-sample and out of sample prediction with lasso. The following steps worked perfectly find with linear regression but not with lasso, so I wonder why this is the case and how I can solve it.
Step 1. Generate data
matrix def c = J(8,8,0)
matrix list c
forvalues i = 1/8{
forvalues j = 1/8{
local toaddin = 0.5^(abs(`i' - `j'))
matrix c[`i',`j'] = `toaddin'
}
}
set seed 12345
set obs 20
drawnorm x1 x2 x3 x4 x5 x6 x7 x8, cov(c)
gen e = rnormal(0,1)
gen y = 3*x1 + 1.5*x2 + 2*x5 + e
Step 2. Save the model
lasso2 y x1 x2 x3 x4 x5 x6 x7 x8
lasso2, lic(ebic)
estimates store regression3
Note: the insample prediction as the following works perfectly fine
predict yhat_lasso, residual lic(ebic)
gen yhat_lasso_2 = yhat_lasso^2
summ yhat_lasso_2
local yhat_lasso_2_mean = r(mean)
di `yhat_lasso_2_mean'
Step 3. Generate new data for out-of-sample prediction
drop _all
set seed 12345
set obs 1000
drawnorm x1 x2 x3 x4 x5 x6 x7 x8, cov(c)
gen e = rnormal(0,1)
gen y = 3*x1 + 1.5*x2 + 2*x5 + e
Step 4: Try to do out-of-sample prediction
set seed 12345
estimates restore regression3
ereturn list
predict yhat_lasso, residual lic(ebic)
Here is where the error message "No observation" comes out.
The ereturn list commend works well, which means the regression result has been successfully restored. So I am unsure why can't I predict again here?
Thanks everyone who tries to help!
0 Response to Estimates Restore with Lasso - "no observations" error
Post a Comment