confidence and prediction intervals with StatsModels

前端 未结 6 1131
青春惊慌失措
青春惊慌失措 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:01

    For test data you can try to use the following.

    predictions = result.get_prediction(out_of_sample_df)
    predictions.summary_frame(alpha=0.05)
    

    I found the summary_frame() method buried here and you can find the get_prediction() method here. You can change the significance level of the confidence interval and prediction interval by modifying the "alpha" parameter.

    I am posting this here because this was the first post that comes up when looking for a solution for confidence & prediction intervals – even though this concerns itself with test data rather.

    Here's a function to take a model, new data, and an arbitrary quantile, using this approach:

    def ols_quantile(m, X, q):
      # m: OLS model.
      # X: X matrix.
      # q: Quantile.
      #
      # Set alpha based on q.
      a = q * 2
      if q > 0.5:
        a = 2 * (1 - q)
      predictions = m.get_prediction(X)
      frame = predictions.summary_frame(alpha=a)
      if q > 0.5:
        return frame.obs_ci_upper
      return frame.obs_ci_lower
    

提交回复
热议问题