Background :我有以下数据,我运行 glm
函数:
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: 我想更改 (Intercept)
所以我可以获得相对于 Bos
的所有值
我看了Change reference group using glm with binomial family和How to force R to use a specified factor level as reference in a regression? . 我尝试了那种方法,它没有用,我不知道为什么 .
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: 如何解决此问题?
1 回答
我认为您的问题是您正在修改数据框,但在您的模型中您没有使用数据框 . 使用模型中的
data
参数来使用数据框中的数据 .注意
location
本身就是一个character
向量 .data.frame()
默认情况下在数据框中将其强制转换为factor
. 转换后,我们可以使用relevel
来指定参考级别 .