【问题标题】:recipes package cannot create interaction term in step_interact食谱包无法在 step_interact 中创建交互项
【发布时间】:2021-03-15 03:19:31
【问题描述】:

我正在使用医疗保险数据集来磨练我的建模技能,如下所示:

> insur_dt
      age    sex    bmi children smoker    region   charges
   1:  19 female 27.900        0    yes southwest 16884.924
   2:  18   male 33.770        1     no southeast  1725.552
   3:  28   male 33.000        3     no southeast  4449.462
   4:  33   male 22.705        0     no northwest 21984.471
   5:  32   male 28.880        0     no northwest  3866.855
  ---                                                      
1334:  50   male 30.970        3     no northwest 10600.548
1335:  18 female 31.920        0     no northeast  2205.981
1336:  18 female 36.850        0     no southeast  1629.833
1337:  21 female 25.800        0     no southwest  2007.945
1338:  61 female 29.070        0    yes northwest 29141.360

我使用recipes 作为tidymodels 元数据包的一部分来准备我的数据以用于模型,并且我已确定bmiagesmoker 形成交互术语。

insur_split <- initial_split(insur_dt)

insur_train <- training(insur_split)
insur_test <- testing(insur_split)

# we are going to do data processing and feature engineering with recipes

# below, we are going to predict charges using everything else(".")
insur_rec <- recipe(charges ~ age + bmi + smoker, data = insur_train) %>%
    step_dummy(all_nominal()) %>%
    step_zv(all_numeric()) %>%
    step_normalize(all_numeric()) %>%
    step_interact(~ bmi:smoker:age) %>% 
    prep()

根据tidymodels guide/documentation,我必须将交互指定为recipe 中的一个步骤step_interact。但是,当我尝试这样做时出现错误:

> insur_rec <- recipe(charges ~ age + bmi + smoker, data = insur_train) %>%
+     step_dummy(all_nominal()) %>%
+     step_zv(all_numeric()) %>%
+     step_normalize(all_numeric()) %>%
+     step_interact(~ bmi:smoker:age) %>% 
+     prep()
Interaction specification failed for: ~bmi:smoker:age. No interactions will be created.partial match of 'object' to 'objects'

我是建模新手,不太清楚为什么会出现此错误。我只是想说明charges 由所有其他预测变量解释,smoker(是/否因素)、age(数字)和bmi(双精度)都相互交互以告知结果。我做错了什么?

【问题讨论】:

    标签: r tidyverse tidymodels r-recipes


    【解决方案1】:

    来自documentation:

    step_interact 可以创建变量之间的交互。它主要用于数字数据;在用于交互之前,分类变量可能应该使用step_dummy() 转换为虚拟变量。

    step_dummy(all_nominal()) 将变量smoker 转换为smoker_yes。下面,你会看到我只是把交互术语中smoker的名字改成了smoker_yes

    insur_rec <- recipe(charges ~ bmi + age + smoker, data = insur_train) %>%
        step_dummy(all_nominal()) %>%
        step_normalize(all_numeric(), -all_outcomes()) %>%
        step_interact(terms = ~ bmi:age:smoker_yes) %>% 
        prep(verbose = TRUE, log_changes = TRUE)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      • 2016-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-18
      相关资源
      最近更新 更多