如何使用 Statsmodels params 参数将我自己的系数传递给预测方法?

问题描述 投票:0回答:0

我正在尝试创建图表来可视化 GLM 中每个系数的置信区间。我想看到的是,当使用给定系数的不同值而其余系数保持不变时,我的最终预测会受到怎样的影响。例如,如果我使用日历年的第 2.5 个百分位数系数值,同时将其余系数保持在其估计值,我的预测会是什么样子?这与使用日历年的第 50 个和第 97.5 个百分位数相比如何?

我得到了像这样的估计值和 95% 置信区间值

poisson_results.params
poisson_results.conf_int()
。看着docs,我想创建一个这样的数组:

new_params = ['0.266308','0.307143','0.694031','1.90103','2.729616','3.766581','4.317322','4.784669','5.566175','0.514219','0.120122','-0.122659','0.228447','0.179789','0.180528','0.152507','-0.009693','-0.01597','-0.046785','-0.081709','-0.087051','0.001042','-0.011786',]``` 

# converting the list to an array
new_params = np.array(new_params, dtype=float)


predicted_counts = poisson_results.predict(
    params=new_params,
    exog=df.select(
        pl.col(
            [
                "mortality_rate",
                "issue_age_clipped_45offset",
                "calendar_year_clipped_2012offset",
                "dur_cap20",
                "rate_up_group",
                "Factoring1",
                "qx",
                "RU_20_29",
                "RU_30_39",
                "RU_40_84",
                "imp_wtw",
                "attained_age_clipped_45offset",
                "att_Age",
            ]
        )
    ).to_pandas(),
    offset=df.select(pl.col("qx").log()).to_numpy().ravel()
)

运行上面的代码片段后,我得到这个错误:

TypeError: predict() got multiple values for argument 'params'
.

我该如何解决这个错误?

注意,我的一些特征,例如 rate_up_group,是分类的,这就是为什么我的数组中的值比我的

exog
数据框中列出的特征更多的原因。

另外,请注意,这是用 Polars 语法编写的。

python statsmodels glm
© www.soinside.com 2019 - 2024. All rights reserved.