confidence and prediction intervals with StatsModels

前端 未结 6 1132
青春惊慌失措
青春惊慌失措 2020-11-27 11:32

I do this linear regression with StatsModels:

import numpy as np
import statsmodels.api as sm
from statsmodels.sandbox.regression.p         


        
6条回答
  •  执念已碎
    2020-11-27 12:04

    You can calculate them based on results given by statsmodel and the normality assumptions.

    Here is an example for OLS and CI for the mean value:

    import statsmodels.api as sm
    import numpy as np
    from scipy import stats
    
    #Significance level:
    sl = 0.05
    #Evaluate mean value at a required point x0. Here, at the point (0.0,2.0) for N_model=2:
    x0 = np.asarray([1.0, 0.0, 2.0])# If you have no constant in your model, remove the first 1.0. For more dimensions, add the desired values.
    
    #Get an OLS model based on output y and the prepared vector X (as in your notation):
    model = sm.OLS(endog = y, exog = X )
    results = model.fit()
    #Get two-tailed t-values:
    (t_minus, t_plus) = stats.t.interval(alpha = (1.0 - sl), df =  len(results.resid) - len(x0) )
    y_value_at_x0 = np.dot(results.params, x0)
    lower_bound = y_value_at_x0 + t_minus*np.sqrt(results.mse_resid*( np.dot(np.dot(x0.T,results.normalized_cov_params),x0) ))
    upper_bound = y_value_at_x0 +  t_plus*np.sqrt(results.mse_resid*( np.dot(np.dot(x0.T,results.normalized_cov_params),x0) ))
    

    You can wrap a nice function around this with input results, point x0 and significance level sl.

    I am unsure now if you can use this for WLS() since there are extra things happening there.

    Ref: Ch3 in [D.C. Montgomery and E.A. Peck. “Introduction to Linear Regression Analysis.” 4th. Ed., Wiley, 1992].

提交回复
热议问题