【问题标题】:How to change the reference level for Risk Ratio in logistic regression in R?如何更改R中逻辑回归中风险比的参考水平?
【发布时间】:2021-11-03 22:04:29
【问题描述】:
  Survey.ID          Quit        Boss            Subord Subord2 Subord3       Subord4
1         1             0         0               0     0            1          0
2         2             1         0               0     1            0          0
3         3             0         0               0     0            0          0
4         4             0         0               0     0            1          0
5         5             0         0               0     0            1          0
6         6             1         0               0     0            1          0

我上面有一个df。每个变量都是一个二元变量,用于对某人是老板还是某个级别的下属进行分类。我想看看什么是最能预测某人在过去一个月辞职的。我正在使用逻辑回归

model  <- glm(Quit ~ Subord, family=binomial, data = df)
summary(model)
exp(cbind(RR = coef(model), confint.default(model)))

我想找出每组员工的相对风险 (RR):Boss、Subord、Subord2、Subord3、Subord4。但是,我想将组称为 Subord4。我相信现在,参考设置为老板?我该如何解决这个问题?

【问题讨论】:

    标签: r reference regression logistic-regression


    【解决方案1】:

    我认为这可能会有所帮助

    library(tidyverse)
    

    样本数据

    df <-
     structure(list(id = 1:6, Quit = c(0L, 1L, 0L, 0L, 0L, 1L), Boss = c(0L, 
     0L, 0L, 0L, 0L, 0L), Subord = c(0L, 0L, 0L, 0L, 0L, 0L), Subord2 = c(0L, 
     1L, 0L, 0L, 0L, 0L), Subord3 = c(1L, 0L, 0L, 1L, 1L, 1L), Subord4 = c(0L, 
     0L, 0L, 0L, 0L, 0L)), class = "data.frame", row.names = c(NA, 
    -6L))
    

    代码

    df %>% 
      #Create a single column of variables: Boss Subord Subord2 Subord3 Subord4
      pivot_longer(cols = -c(id,Quit)) %>% 
      #Keeping only those with value = 1
      filter(value == 1) %>% 
      #Making "Subord4" the baseline, as the first level of the factor
      mutate(name = fct_relevel(as.factor(name),"Subord4")) %>% 
      glm(data = .,formula = Quit~name, family=binomial)
    

    【讨论】:

      猜你喜欢
      • 2014-06-10
      • 2021-03-25
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      • 2020-03-16
      • 2020-03-02
      • 2012-06-15
      • 2020-05-11
      相关资源
      最近更新 更多