# [R-sig-eco] glm coefficients

Aitor Gastón aitor.gaston at upm.es
Sun Sep 25 17:09:19 CEST 2011

```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

> 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.
>
>
>
> --
> Christopher M. Swan, Ph.D.
> Associate Professor
> Dept. of Geography & Environmental Systems
> University of Maryland, Baltimore County
> 211 Sondheim Hall
> 1000 Hilltop Circle
> Baltimore, MD 21250
> Chris.Swan at umbc.edu
> http://www.umbc.edu/people/cmswan
> (410) 455-3957
>
>
>
>
>
>
>
>
>
> On Sep 24, 2011, at 4:52 PM, Aitor Gastón wrote:
>
>> 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
>>
>>> Hello,
>>>
>>>
>>> Is there a way to add up coefficients from a glm model for an ANCOVA to
>>> get
>>> the coefficients for each term?
>>>
>>> For example, in the following:
>>> dat <- data.frame(response = rnorm(9), size = rnorm(9),  covariate =
>>> c(1,1,1,1,0,0,0,0,0))
>>> model <- glm(response ~ size * covariate, data = dat)
>>>> coef(model)
>>>  (Intercept)           size                covariate
>>> size:covariate
>>>   -0.2995964     -0.1969266      0.7158756      1.2829886
>>>
>>> we are interested in the coefficients for the intercepts for size at
>>> both
>>> levels of the covariate  (0 and 1), and the slopes for each line. This
>>> requires adding up coefficients from the above output.
>>>
>>> Are there built in base functions or in other packages that do these
>>>
>>>
>>> Thanks!
>>> __
>>> Scott Chamberlain
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> _______________________________________________
>>> R-sig-ecology mailing list
>>> R-sig-ecology at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>>>
>>
>> _______________________________________________
>> R-sig-ecology mailing list
>> R-sig-ecology at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>>
>

```