0

¿Saben si se puede definir un Modelo Lineal Generalizado para una variable aleatoria con distribución Gamma en Python? ¿O tendré que conformarme con R? Si acaso es posible por favor adjunten tutoriales, guías, documentación, ejemplos...

1

1 respuesta 1

2

Hola claro que los GLM si funcionan en python mira este ejemplo. Para una variable y con 20 valores de la distribución gamma.

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

La salida que genera r es la siguiente:

summary(glm(y~1,family=Gamma))

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

Ahora miremos con python:

import numpy as np
import statsmodels.api as sm

x = np.repeat(1,18)
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]

La salida es la siguiente:

sm.GLM(y,x, family=sm.families.Gamma()).fit().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.062556
Method:                          IRLS   Log-Likelihood:                -83.656
Date:                dom, 20 may 2018   Deviance:                       1.1761
Time:                        17:12:44   Pearson chi2:                     1.06
No. Iterations:                     6   Covariance Type:             nonrobust
==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------
const          0.0099      0.001     16.963      0.000       0.009       0.011
==============================================================================

Ahora comparemos los resultados como vez, son muy parecidos solo que en python hay menos dígitos.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.