首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Poisson回归中的多项式回归

Poisson回归中的多项式回归
EN

Stack Overflow用户
提问于 2021-03-04 18:48:13
回答 1查看 433关注 0票数 0

我想要创建一个模型,它从多项式回归中获取值,并根据预测值(通过多项式)创建Poisson回归。

我只得到了R码,就像这样

代码语言:javascript
复制
glm(y ~ poly(x, 6), family = Poisson, data = data_set)

到目前为止,我的方法是首先计算离散数据点的多项式预测,并在此基础上运行Poisson回归。然而,我的结果有点差。

代码语言:javascript
复制
import pandas as pd
from statsmodels.formula.api import glm
polynomial_model = np.poly1d(np.polyfit(x=bin_mids, y=count_per_bin, deg = 6))
model_values = [polynomial_model(i) for i in bin_mids]
df_1["model_values"] = model_values
poisson_model = glm("df_1['count_per_bin'] ~ df_1['model_values']" , data = df_1 ,family = sm.families.Poisson()).fit()

如果你们中有人看到我的错误,我很想知道我哪里出了问题。

干杯

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-04 21:34:47

不太确定您在使用二进制代码和代码的其余部分做什么。您在R中所做的是对正交多项式的回归。有关使用的更多信息,您可以看到它。

如果您使用的是状态模型,则只需提供具有转换后的值x、x^2、x^3的输入矩阵,然后执行QR分解,以及在这篇文章中提到。您可以使用sklearn获得矩阵:

代码语言:javascript
复制
import numpy as np
import statsmodels.api as sm
import pandas as pd
from sklearn.preprocessing import PolynomialFeatures

np.random.seed(111)
df = pd.DataFrame({'x':np.random.uniform(0,1,50),'y':np.random.poisson(5,50)})

xp = PolynomialFeatures(degree=6).fit_transform(df[['x']])
xp = np.linalg.qr(xp)[0][:,1:]

model = sm.GLM(df['y'],sm.add_constant(xp),family=sm.families.Poisson()).fit()
model.summary()

                     Generalized Linear Model Regression Results                  
==============================================================================
Dep. Variable:                      y   No. Observations:                   50
Model:                            GLM   Df Residuals:                       43
Model Family:                 Poisson   Df Model:                            6
Link Function:                    log   Scale:                          1.0000
Method:                          IRLS   Log-Likelihood:                -100.45
Date:                Thu, 04 Mar 2021   Deviance:                       33.846
Time:                        22:32:57   Pearson chi2:                     31.8
No. Iterations:                     4                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------
const          1.5458      0.066     23.495      0.000       1.417       1.675
x1            -0.2059      0.439     -0.469      0.639      -1.067       0.655
x2             0.8169      0.466      1.754      0.079      -0.096       1.730
x3             0.1178      0.442      0.267      0.790      -0.748       0.984
x4            -0.5503      0.454     -1.212      0.226      -1.440       0.340
x5             0.0035      0.457      0.008      0.994      -0.892       0.899
x6            -0.6878      0.455     -1.512      0.131      -1.579       0.204
==============================================================================

我们将数据写入csv

代码语言:javascript
复制
df.to_csv("data.csv")

用R来拟合,你可以看到我们得到了相同的系数:

代码语言:javascript
复制
df = read.csv("data.csv",row.names=1)
summary(glm(y ~ poly(x, 6), family = poisson, data = df))

Call:
glm(formula = y ~ poly(x, 6), family = poisson, data = df)

Deviance Residuals: 
     Min        1Q    Median        3Q       Max  
-2.42648  -0.73970  -0.04625   0.61351   1.81234  

Coefficients:
             Estimate Std. Error z value Pr(>|z|)    
(Intercept)  1.545808   0.065794  23.495   <2e-16 ***
poly(x, 6)1 -0.205940   0.439457  -0.469   0.6393    
poly(x, 6)2  0.816910   0.465770   1.754   0.0794 .  
poly(x, 6)3  0.117815   0.441847   0.267   0.7897    
poly(x, 6)4 -0.550271   0.454099  -1.212   0.2256    
poly(x, 6)5 -0.003508   0.456691  -0.008   0.9939    
poly(x, 6)6  0.687751   0.454953   1.512   0.1306    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 40.443  on 49  degrees of freedom
Residual deviance: 33.846  on 43  degrees of freedom
AIC: 214.91
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66481212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档