【问题标题】:logistic regression choosing the right variables in R逻辑回归在 R 中选择正确的变量
【发布时间】:2020-12-30 07:36:20
【问题描述】:

好的,我有这样的数据,但有更多类似类型的变量

Company         Job  Month  Reported  Injury.Loc  Age
      1  Cartpenter      2         0         Leg   23
      2    Mechanic     12         1         Arm   33
      3       Legal      1         1         Arm   24
      4   Carpenter      1         1         Leg   75
      5       Legal      4         0        Head   23
      3      Dental      6         1       Wrist   40

由于变量的分类性质,我无法对其进行以下逻辑回归

log_m1 <- glm(Reported ~. , data = df, family = "binomial")

有什么方法可以一次分解所有分类变量并保留/保留所有数字变量?

所以基本上,代码来保持我需要的日志 reg 工作。

错误:

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

【问题讨论】:

    标签: r logistic-regression dummy-variable


    【解决方案1】:

    可以对数字和分类自变量的混合运行逻辑回归 - 这不是您收到错误消息的原因。

    让我们首先证明我们可以毫无问题地运行这样的回归:

    set.seed(69)
    
    df <- data.frame(sex = factor(sample(c("Male", "Female"), 100, TRUE)),
                     age = sample(21:90, 100, TRUE),
                     outcome = sample(0:1, 100, TRUE))
    
    glm(outcome ~ ., data = df, family = "binomial")
    #> 
    #> Call:  glm(formula = outcome ~ ., family = "binomial", data = df)
    #> 
    #> Coefficients:
    #> (Intercept)      sexMale          age  
    #>    0.169183     0.019774    -0.003115  
    #> 
    #> Degrees of Freedom: 99 Total (i.e. Null);  97 Residual
    #> Null Deviance:       138.6 
    #> Residual Deviance: 138.5     AIC: 144.5
    

    但如果我们将 sex 的所有值设为相同,我们可以复制您的错误:

    df2 <- within(df, sex <- rep("Male", 100))
    
    glm(outcome ~ ., data = df2, family = "binomial")
    #> Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]): 
    #> contrasts can be applied only to factors with 2 or more levels
    

    因此,您的数据中可能有一列只有一个因子水平(或只有一个唯一的非 NA 值)。删除它,您的回归应该会按预期运行。

    【讨论】:

      【解决方案2】:

      您可以对vtreatmagritt 包以及dplyr 使用下一个方法。代码如下:

      library(vtreat)
      library(dplyr)
      library(magrittr)
      #Data
      df <- structure(list(Company = c(1L, 2L, 3L, 4L, 5L, 3L), Job = c("Cartpenter", 
      "Mechanic", "Legal", "Carpenter", "Legal", "Dental"), Month = c(2L, 
      12L, 1L, 1L, 4L, 6L), Reported = c(0L, 1L, 1L, 1L, 0L, 1L), Injury.Loc = c("Leg", 
      "Arm", "Arm", "Leg", "Head", "Wrist"), Age = c(23L, 33L, 24L, 
      75L, 23L, 40L)), class = "data.frame", row.names = c(NA, -6L))
      

      首先,我们必须在不同的数据帧中隔离要转换和分离的变量(df 将保留数字变量和df2 分类变量):

      #Isolate data variables of type character
      vars <- c("Job","Injury.Loc")
      df2 <- df[,vars]
      df <- df[,-which(names(df) %in% vars)]
      

      完成后,我们使用designTreatmentsZ()use_series 处理变量并在新的数据帧中赋值:

      #Code for dummy vars
      treatplan <- designTreatmentsZ(df2, vars)
      #Process
      scoreFrame <- treatplan %>%
          use_series(scoreFrame) %>%
          select(varName, origName, code)
      

      现在,我们使用filter() 隔离newvars 中处理过的变量:

      #Select
      newvars <- scoreFrame %>%
          filter(code %in% c("clean", "lev")) %>%
          use_series(varName)
      

      我们在新的数据框中提取新变量:

      #Create new data
      dframe.treat <- prepare(treatplan, df2, varRestriction = newvars)
      

      最后,我们将数值变量添加到数据框中:

      #Bind with original df
      newdf <- cbind(df,dframe.treat)
      

      数据会是这样的(由于空间关系只显示了一些变量):

        Company Month Reported Age Job_lev_x_Carpenter Job_lev_x_Cartpenter Job_lev_x_Dental
      1       1     2        0  23                   0                    1                0
      2       2    12        1  33                   0                    0                0
      3       3     1        1  24                   0                    0                0
      4       4     1        1  75                   1                    0                0
      5       5     4        0  23                   0                    0                0
      6       3     6        1  40                   0                    0                1
      

      然后您可以创建模型。小心奇点,否则模型会得出错误的结论。

      #Model
      log_m1 <- glm(Reported ~. , data = newdf, family = "binomial")
      

      【讨论】:

      • 您不认为错误是由其中一列中的单个因素水平引起的吗? (这当然是重现错误的最简单方法)如果是这样,大概创建这样的虚拟变量虽然是一个巧妙的技巧,但不能解决问题,还是我错过了这里的重点?
      • @AllanCameron 我也很困惑 OP 想要虚拟变量来运行模型,但它可以产生 NA 的系数。
      猜你喜欢
      • 1970-01-01
      • 2015-01-03
      • 2020-08-19
      • 2021-11-23
      • 2016-06-30
      • 1970-01-01
      • 2018-01-26
      • 2021-06-10
      • 1970-01-01
      相关资源
      最近更新 更多