python - Gamma 回归仅截距

标签 python r regression glm

我是 python 的新手,我正在尝试进行 Gamma 回归,我希望获得与 R 类似的估计,但我无法理解 python 的语法并且它会产生错误,关于如何解决它的一些想法。

我的 R 代码:

set.seed(1)
y = rgamma(18,10,.1)
print(y)
[1]  76.67251 140.40808 138.26660 108.20993  53.46417 110.61754 119.11950 113.57558  85.82045  71.96892
[11]  76.81693  86.00139  93.62010  69.49795 121.99775 114.18707 125.43608 120.63640

# Option 1
model = glm(y~1,family=Gamma)
summary(model)

# Option 2
# x = rep(1,18)
# summary(glm(y~x,family=Gamma))

输出:

summary(model)

Call:
glm(formula = y ~ 1, family = Gamma)

Deviance Residuals: 
     Min        1Q    Median        3Q       Max  
-0.57898  -0.24017   0.07637   0.17489   0.34345  

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 0.009856   0.000581   16.96 4.33e-12 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for Gamma family taken to be 0.06255708)

    Null deviance: 1.1761  on 17  degrees of freedom
Residual deviance: 1.1761  on 17  degrees of freedom
AIC: 171.3

Number of Fisher Scoring iterations: 4

Python 代码

y = [76.67251,140.40808,138.26660,108.20993,53.46417,110.61754,
 119.11950,113.57558,85.82045,71.96892,76.81693,86.00139,
 93.62010,69.49795,121.99775,114.18707,125.43608,120.63640]

x = np.repeat(1,18)

import numpy
import statsmodels.api as sm

model = sm.GLM(x,y, family=sm.families.Gamma()).fit()
print(model.summary())

我希望得到类似于 R 的输出

最佳答案

您需要在 python 代码中更改 x 和 y 变量的顺序,然后您将看到完全相同的结果(尽管输出中显示的有效数字位数与 R 中的输出不同:

 sm.GLM(y,x, family=sm.families.Gamma()).fit().summary()

<class 'statsmodels.iolib.summary.Summary'>
"""
                 Generalized Linear Model Regression Results
==============================================================================
Dep. Variable:                      y   No. Observations:                   18
Model:                            GLM   Df Residuals:                       17
Model Family:                   Gamma   Df Model:                            0
Link Function:          inverse_power   Scale:                 0.0625558699706
Method:                          IRLS   Log-Likelihood:                -83.656
Date:                Sun, 20 May 2018   Deviance:                       1.1761
Time:                        17:59:04   Pearson chi2:                     1.06
No. Iterations:                     4
==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------
const          0.0099      0.001     16.963      0.000       0.009       0.011
==============================================================================
"""

各种 python 包都有自己的语法。这是一个很好的链接,其中包含一些如何在 Python 中使用公式语法的示例: http://www.statsmodels.org/dev/example_formulas.html enter link description here

关于python - Gamma 回归仅截距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50439178/

相关文章:

python - pyinstaller 无法看到 configparser

r - 如何将两列与数组组合

Java 8 在 UTF-8 解码方面的变化

python - 将十六进制字符串拆分为 Python 中的列表 - 如何?

Python - 向生成器添加 bool 条件

Python - 如何加速具有函数和多个返回值的嵌套 for 循环

r - 计算变量的频率

正则表达式选择多个组

r - 尝试运行固定效应逻辑回归时出错

java - 多维多项式回归(最好是 C/C++、Java 或 Scala)