6
$\begingroup$

I'm working with R and confirming my results in Python with the overwhelming majority of the work matching between the two quite well. There are two outputs coming out of R that I'm not seeing how to get in Python and for now I'm looking for pre-packaged calls but if I have to do it manually so be it. Still, I would expect the pre-packaged calls to be available since practically everything else that is in R is in Python.

First question: How do you get the R 'Residual standard error'(see the red box) in Python?

Here is the R code and below that the results:

fit_1a <- lm(rpaapl~rpsp, data=df_returns)
summary(fit_1a)

regression in R

Here is the Python/statsmodels.ols code and below that the results:

est_1a = smf.ols(formula='rpaapl ~ rpsp', data=xl).fit()
print(est_1a.summary())

enter image description here

$\color{red}{\text{So how can I get this residual standard error in Python?}}$

Second question: How do you get the R 'standard error of each prediction' in Python?

Here is the R code and below that the results:

forecast_1d <- data.frame(predict(fit_1a, newdata=data.frame(rpsp=mrp), se.fit=TRUE))
forecast_1d

enter image description here

Here is the Python/statsmodels.ols code and below that the results:

df_1d["estimate"] = est_1a.predict(df_1d)
print(type(est_1a.predict(df_1d)))
df_1d["estimate"]

enter image description here

$\color{red}{\text{So how can I get these standard errors for each prediction in Python?}}$

Please note that the est_1a object has a bunch of values but I'm not finding the standard error. Also, est_1a.predict only returns a timeseries so the predict call does not seem to calculate the standard error (se.fit in R). Any help is much appreciated.

$\endgroup$
1
$\begingroup$

For your first question, I think what R calls the "residual standard error" is the square root of the scale parameter:

np.sqrt(est_1a.scale)
$\endgroup$
1
  • $\begingroup$ Thank you, that is correct. What remains now is my second question. Any info is most appreciated. $\endgroup$ – ghbcode Feb 14 '18 at 23:45
1
$\begingroup$

To the last part

Several models have now a get_prediction method that provide standard errors and confidence interval for predicted mean and prediction intervals for new observations.

pred = results.get_prediction(x_predict)
pred_df = pred.summary_frame()

some examples are in this gist https://gist.github.com/josef-pkt/1417e0473c2a87e14d76b425657342f5

$\endgroup$
1
  • $\begingroup$ Thank you very much. I believe that is it. Much appreciated. $\endgroup$ – ghbcode Feb 15 '18 at 23:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.