首页 文章

在泊松GLM R中改变Y截距

提问于
浏览
0

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 familyHow 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 回答

  • 3

    我认为您的问题是您正在修改数据框,但在您的模型中您没有使用数据框 . 使用模型中的 data 参数来使用数据框中的数据 .

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

    注意 location 本身就是一个 character 向量 . data.frame() 默认情况下在数据框中将其强制转换为 factor . 转换后,我们可以使用 relevel 来指定参考级别 .

    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 ***
        # ...
    

相关问题