【问题标题】:Subset data points outside confidence interval置信区间外的子集数据点
【发布时间】:2018-11-08 04:42:57
【问题描述】:

使用与之前question 相同的示例(代码粘贴在下面),我们可以通过statsmodels outliers_influence 中的summary_table 函数获得95% CI。但是现在,怎么可能只对置信区间之外的数据点(xy)进行子集化呢?

import numpy as np
import statsmodels.api as sm
from statsmodels.stats.outliers_influence import summary_table

#measurements genre
n = 100
x = np.linspace(0, 10, n)
e = np.random.normal(size=n)
y = 1 + 0.5*x + 2*e
X = sm.add_constant(x)
re = sm.OLS(y, X).fit()
st, data, ss2 = summary_table(re, alpha=0.05)
predict_ci_low, predict_ci_upp = data[:, 6:8].T

【问题讨论】:

    标签: python-3.x statsmodels


    【解决方案1】:

    这可能有点晚了,但您可以将其放入 pandas.DataFrame 并根据布尔值列表进行过滤。假设我得到了你的问题:

    import numpy as np
    import statsmodels.api as sm
    from statsmodels.stats.outliers_influence import summary_table
    import matplotlib.pyplot as plot
    
    ## Import pandas
    import pandas as pd
    
    #measurements genre
    n = 100
    x = np.linspace(0, 10, n)
    e = np.random.normal(size=n)
    y = 1 + 0.5*x + 2*e
    X = sm.add_constant(x)
    re = sm.OLS(y, X).fit()
    st, data, ss2 = summary_table(re, alpha=0.05)
    
    # Make prediction
    prediction = re.predict(X)
    predict_ci_low, predict_ci_upp = data[:, 6:8].T
    
    # Put y and x in a pd.DataFrame
    df = pd.DataFrame(y).set_index(x)
    
    # Get the y values that are out of the ci intervals. This could be done directly in the df indexer
    out_up = y > predict_ci_upp
    out_down = y < predict_ci_low
    
    # Plot everything
    plot.plot(x, y, label = 'train')
    plot.plot(df[out_up], marker = 'o', linewidth = 0)
    plot.plot(df[out_down], marker = 'o', linewidth = 0)
    plot.plot(x, predictionTrain, label = 'prediction')
    plot.plot(x, predict_ci_upp, label = 'ci_up')
    plot.plot(x, predict_ci_low, label = 'ci_low')
    plot.legend(loc='best')
    

    这是结果图:

    【讨论】:

      猜你喜欢
      • 2016-11-27
      • 2014-02-08
      • 1970-01-01
      • 1970-01-01
      • 2018-09-09
      • 1970-01-01
      • 2011-09-18
      • 2019-04-30
      • 1970-01-01
      相关资源
      最近更新 更多