Generalized Linear Model

Summary

Sponsored link

function “lm”

:
:
:(General Linear Model)

# sample data
plot(Y ~ X, dat1)

plot of chunk lm

# run lm
fit <- lm(Y ~ X, data = dat1)
summary(fit)
## 
## Call:
## lm(formula = Y ~ X, data = dat1)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.20570 -0.05521 -0.00719  0.05772  0.37109 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.998986   0.018630   53.62   <2e-16 ***
## X           0.100417   0.003047   32.96   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.09342 on 98 degrees of freedom
## Multiple R-squared:  0.9172, Adjusted R-squared:  0.9164 
## F-statistic:  1086 on 1 and 98 DF,  p-value: < 2.2e-16

function “glm”

:
:
:

Poisson

# sample data
plot(Y ~ X, dat2)

plot of chunk glm_poisson

# run poisson model
fit <- glm(Y ~ X, family = poisson, data = dat2)
summary(fit)
## 
## Call:
## glm(formula = Y ~ X, family = poisson, data = dat2)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -2.2549  -0.6981  -0.2130   0.5192   2.2705  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  0.30021    0.13408   2.239   0.0252 *  
## X            0.18878    0.01937   9.744   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 208.64  on 99  degrees of freedom
## Residual deviance: 107.75  on 98  degrees of freedom
## AIC: 405.17
## 
## Number of Fisher Scoring iterations: 5

Binomial

# sample data
plot(Y ~ X, dat3)

plot of chunk glm_binomial

# run binomial model (N is the number of trials)
fit <- glm(cbind(Y, N-Y) ~ X, family = binomial, data = dat3)
summary(fit)
## 
## Call:
## glm(formula = cbind(Y, N - Y) ~ X, family = binomial, data = dat3)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -2.8832  -0.7536  -0.2680   0.6691   1.7401  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -5.22638    0.29749  -17.57   <2e-16 ***
## X            0.83371    0.04804   17.36   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 680.830  on 99  degrees of freedom
## Residual deviance:  89.731  on 98  degrees of freedom
## AIC: 237.04
## 
## Number of Fisher Scoring iterations: 5

function “glm.nb”

:
:MASS
:

# sample data
plot(Y ~ X, dat4)

plot of chunk glm_negative-binomial

# run negative-binomial model
fit <- glm.nb(Y ~ X, data = dat4)
summary(fit)
## 
## Call:
## glm.nb(formula = Y ~ X, data = dat4, init.theta = 1.446107716, 
##     link = log)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -2.5771  -1.0159  -0.3357   0.4103   2.4534  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  0.14780    0.21082   0.701    0.483    
## X            0.50781    0.03262  15.569   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Negative Binomial(1.4461) family taken to be 1)
## 
##     Null deviance: 340.04  on 99  degrees of freedom
## Residual deviance: 109.02  on 98  degrees of freedom
## AIC: 789
## 
## Number of Fisher Scoring iterations: 1
## 
## 
##               Theta:  1.446 
##           Std. Err.:  0.222 
## 
##  2 x log-likelihood:  -782.995
Posted in: R

Leave a Reply

Your email address will not be published. Required fields are marked *