Skip to content

glm coefficients

6 messages · Scott Chamberlain, Chris Swan, Aitor Gastón

1 day later
#
In your example there is not categorical variables and you need at least one 
for ANCOVA.
Try the following, is the same dataset using factor() to create a 
categorical variable (named CatVar):

dat <- data.frame(response = rnorm(9), size = rnorm(9),  CatVar =
factor(c(1,1,1,1,0,0,0,0,0)))
model <- glm(response ~ size * CatVar, data = dat)
coef(model)

You will get:

(Intercept)         size      CatVar1 size:CatVar1
   0.2141371   -0.7847063   -1.8409264    3.3637699

The first coefficient (labeled "Intercept") is the intercept for CatVar = 0.
The second coefficient (labeled "size") is the slope for CatVar=0
The third coefficient (labeled "CatVar1") is the difference between the 
intercept for CatVar = 0 and the intercept for CatVar = 1
The fourth coefficient ("labeled size:CatVar") is the difference between the 
slope for CatVar = 0 and the slope for CatVar = 1

You can check it plotting this:

plot(dat$size,dat$response,col=dat$CatVar)
abline(a=coef(model)[1],b=coef(model)[2],col=1)
abline(a=sum(coef(model)[c(1,3)]),b=sum(coef(model)[c(2,4)]),col=2)

This way of presenting coefficients is the default in R (treatment contrast) 
but there are other alternatives, see ?contr.treatment.

Hope it helps,

Aitor


--------------------------------------------------
From: "Scott Chamberlain" <scttchamberlain4 at gmail.com>
Sent: Friday, September 23, 2011 5:59 PM
To: <R-sig-ecology at r-project.org>
Subject: [R-sig-eco] glm coefficients
#
I am following this and have two other questions.

1) The first is how to get the correct standard errors for each of the coefficients?  In the example offered, asking for summary(model) will yield, in part, the following (please understand my output will differ given the data generated is random):

Coefficients:
             Estimate Std. Error t value Pr(>|t|)
(Intercept)    0.7988     0.5801   1.377    0.227
size          -0.5437     0.7997  -0.680    0.527
CatVar1       -2.0183     1.0573  -1.909    0.115
size:CatVar1  -0.5047     1.2368  -0.408    0.700


To get the intercept for the CatVar1=1 line, one needs 0.7988-(-2.0183) = 2.18.  Is the standard error for this estimate 1.0573?


2) Suppose there were three or more categorical variables.  How can one perform pairwise comparisons between coefficients AND get the associated SE of the difference?  It seems that this would be quite desirable, yet I cannot find a package for this. I am looking for a method similar to the SAS implementation of the ESTIMATE statement.
#
Chris,

In your example, the intercept for CatVar1=1  is 0.7988+(-2.0183).
The standar error of the CatVar1 line is the standard error of the 
difference

You can use the multcomp package for pairwise comparisons, try this:

set.seed(100) #fixed seed to get reproducible results
dat <- data.frame(response = rnorm(100), size = rnorm(100),  CatVar =
sample(c("A","B","C"),100,replace=T))
model <- glm(response ~ size * CatVar, data = dat)
coef(model)

plot(dat$size,dat$response,col=dat$CatVar)
abline(a=coef(model)[1],b=coef(model)[2],col=1)
abline(a=sum(coef(model)[c(1,3)]),b=sum(coef(model)[c(2,5)]),col=2)
abline(a=sum(coef(model)[c(1,4)]),b=sum(coef(model)[c(2,6)]),col=3)

require(multcomp)
summary(glht(model,linfct=mcp(CatVar="Tukey")))

--------------------------------------------------
From: "Chris Swan" <cmswan at umbc.edu>
Sent: Sunday, September 25, 2011 4:08 PM
To: "Aitor Gast?n" <aitor.gaston at upm.es>
Cc: "Scott Chamberlain" <scttchamberlain4 at gmail.com>; 
<R-sig-ecology at r-project.org>
Subject: Re: [R-sig-eco] glm coefficients