0

Background: I have the following data that I run a glm function on:

location = c("DH", "Bos", "Beth")
count = c(166, 57, 38)

#make into df
df = data.frame(location, count) 

#poisson
summary(glm(count ~ location, family=poisson))

Output:

Coefficients:

            Estimate Std. Error z value Pr(>|z|)    
(Intercept)   3.6376     0.1622  22.424  < 2e-16 ***
locationBos   0.4055     0.2094   1.936   0.0529 .  
locationDH    1.4744     0.1798   8.199 2.43e-16 ***

Problem: I would like to change the (Intercept) so I can get all my values relative to Bos

I looked Change reference group using glm with binomial family and How to force R to use a specified factor level as reference in a regression?. I tried there method and it did not work, and I am not sure why.

Tried:

df1 <- within(df, location <- relevel(location, ref = 1))

#poisson
summary(glm(count ~ location, family=poisson, data = df1))

Desired Output:

Coefficients:

            Estimate Std. Error z value Pr(>|z|)    
(Intercept)    ...
locationBeth   ...
locationDH     ...

Question: How do I solve this problem?

1 Answers1

4

I think your problem is that you are modifying the data frame, but in your model you are not using the data frame. Use the data argument in the model to use the data in the data frame.

location = c("DH", "Bos", "Beth")
count = c(166, 57, 38)
# make into df
df = data.frame(location, count) 

Note that location by itself is a character vector. data.frame() coerces it to a factor by default in the data frame. After this conversion, we can use relevel to specify the reference level.

df$location = relevel(df$location, ref = "Bos") # set Bos as reference
summary(glm(count ~ location, family=poisson, data = df))

    # Call:
    # glm(formula = count ~ location, family = poisson, data = df)
    # ...
    # Coefficients:
    #              Estimate Std. Error z value Pr(>|z|)    
    # (Intercept)    4.0431     0.1325  30.524  < 2e-16 ***
    # locationBeth  -0.4055     0.2094  -1.936   0.0529 .  
    # locationDH     1.0689     0.1535   6.963 3.33e-12 ***
    # ...
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294