2  How-to in practice

GLMs in R are typically performed with the function “glm()”. This function, as a regular linear regressions, needs a model express through a formula and some data. One more argument needs to be specified: the family. The family is the description of the error distribution and link function to be used in the model. In practice, you will set this argument to “binomial” for a logistic regression, to “poisson” for a Poisson regression, or even to “gaussian” for a linear regression if you feel cheeky, you crazy kid! More details and options about the families available are presented in the help of the “family()” function, but those are a good start for most of the common problems you will be facing. What about a couple of practical examples?

2.1 Quick reminder : “Normal” linear regression

n=10

# Generating simulated data
control <- rnorm(10,5) 
treatment<- rnorm(10,7) 
Y <- c(control, treatment) 

# Generating Factor Levels
labels = c("Control","Treatment")
group <- gl(length(labels), n, labels = labels) 


lm.D9 <- lm(Y ~ group) 
summary(lm.D9)

Call:
lm(formula = Y ~ group)

Residuals:
    Min      1Q  Median      3Q     Max 
-2.1522 -0.9011  0.1384  0.9046  2.1439 

Coefficients:
               Estimate Std. Error t value Pr(>|t|)    
(Intercept)      4.5627     0.3633  12.559 2.42e-10 ***
groupTreatment   2.5679     0.5138   4.998 9.33e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.149 on 18 degrees of freedom
Multiple R-squared:  0.5812,    Adjusted R-squared:  0.5579 
F-statistic: 24.98 on 1 and 18 DF,  p-value: 9.327e-05
glm.D9 <- glm(Y ~ group, family = "gaussian") 
summary(glm.D9)

Call:
glm(formula = Y ~ group, family = "gaussian")

Coefficients:
               Estimate Std. Error t value Pr(>|t|)    
(Intercept)      4.5627     0.3633  12.559 2.42e-10 ***
groupTreatment   2.5679     0.5138   4.998 9.33e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for gaussian family taken to be 1.319915)

    Null deviance: 56.728  on 19  degrees of freedom
Residual deviance: 23.758  on 18  degrees of freedom
AIC: 66.202

Number of Fisher Scoring iterations: 2
Note

If you need a refresher on the concepts behind linear regression and multiple linear regression, and how to perform them in R, or assess the quality of the results, feel free to visit the dedicated section of the Prelude in R website (shameless self-promotion plug).