I want to predict $y$ with $x_1$ and $x_2$, including an out of sample prediction interval. However, $y$ has large outliers, so I log transform $y$ and estimate $\log(y) = a + b_1 x_1 + b_2 x_2 + e$, where $e$ is my normally distributed error term. Here are some data.
. clear
. set obs 2001
number of observations (_N) was 0, now 2,001
. global X x_1 x_2
.
. generate x_1 = runiform()
. generate x_2 = runiform()
. generate y = exp(5*x_1 + 5*x_2 + rnormal())
. replace x_1 = 1 if (_n == 1)
(1 real change made)
. replace x_2 = 1 if (_n == 1)
(1 real change made)
. replace y = exp(5*x_1 + 5*x_2 + 0) if (_n == 1)
(1 real change made)
. generate logy = log(y)
In Stata, normally, I would use predict
to estimate $\hat y$ and predict, stdf
to estimate prediction error $SE$, then $\hat y \pm 2 \times SE$ to generate upper and lower bounds for my prediction interval.
. regress y $X if (_n > 1)
Source | SS df MS Number of obs = 2,000
-------------+---------------------------------- F(2, 1997) = 47.60
Model | 1.9090e+10 2 9.5451e+09 Prob > F = 0.0000
Residual | 4.0041e+11 1,997 200507843 R-squared = 0.0455
-------------+---------------------------------- Adj R-squared = 0.0446
Total | 4.1950e+11 1,999 209857103 Root MSE = 14160
------------------------------------------------------------------------------
y | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
x_1 | 7837.173 1104.463 7.10 0.000 5671.154 10003.19
x_2 | 7390.569 1084.627 6.81 0.000 5263.45 9517.687
_cons | -5763.221 849.7131 -6.78 0.000 -7429.638 -4096.804
------------------------------------------------------------------------------
. predict yhat_1, xb
. predict yhat_1_se, stdf
. generate yhat_1_lb = yhat_1 - 2*yhat_1_se
. generate yhat_1_ub = yhat_1 + 2*yhat_1_se
. list y yhat_1* if (_n == 1)
+------------------------------------------------------+
| y yhat_1 yhat_1~e yhat_~lb yhat_~ub |
|------------------------------------------------------|
1. | 22026.46 9464.521 14184.66 -18904.8 37833.84 |
+------------------------------------------------------+
However, because I log transformed $y$, my code above is not correct and I can't apply this technique to estimate a $\hat y$ prediction interval. I can use predictnl
to estimate $\hat y$. But there is no , stdf
option for predictnl
. How can I adjust predictnl
's confidence interval to generate a prediction error? Is there a manual solution?
. regress logy $X if (_n > 1)
Source | SS df MS Number of obs = 2,000
-------------+---------------------------------- F(2, 1997) = 4246.62
Model | 8975.08597 2 4487.54298 Prob > F = 0.0000
Residual | 2110.29548 1,997 1.05673284 R-squared = 0.8096
-------------+---------------------------------- Adj R-squared = 0.8094
Total | 11085.3814 1,999 5.54546346 Root MSE = 1.028
------------------------------------------------------------------------------
logy | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
x_1 | 5.237723 .0801803 65.32 0.000 5.080477 5.394969
x_2 | 5.203039 .0787403 66.08 0.000 5.048617 5.357461
_cons | -.200541 .0616864 -3.25 0.001 -.3215174 -.0795646
------------------------------------------------------------------------------
. predictnl yhat_2 = exp(xb()), ci(yhat_2_lb yhat_2_ub) level(95)
note: confidence intervals calculated using Z critical values
. list y yhat_2* if (_n == 1)
+-------------------------------------------+
| y yhat_2 yha~2_lb yha~2_ub |
|-------------------------------------------|
1. | 22026.46 28007.31 24680.86 31333.77 |
+-------------------------------------------+
How can I adjust predictnl
's confidence interval to generate a prediction error? Is there a manual solution?
predictnl
. But, you're right, my code generates confidence interval because I don't know how to get prediction interval frompredictnl
. $\endgroup$ – Richard Herron Nov 30 '18 at 21:50