我正在創建一個配方,以便我首先創建一個名為“回應”的計算列,如下所示:
rec <- recipe( ~., data = training) %>%
step_mutate(response = as.integer(all(c('A', 'B') %in% Col4) & Col4 == 'A'))
我現在想將這個新的計算列指定為 recipe() 函式中的回應變數,如下所示。我將對其進行一系列操作,例如第一個使用 step_naomit 的操作。如何將 recipe() 中的回應重新指定為使用配方的上一步(以上)的計算列?
recipe <- recipe(response ~ ., data = training) %>%
step_naomit(recipe, response)
uj5u.com熱心網友回復:
您可以step_mutate()通過顯式設定引數來為函式中的新列設定role=角色。
rec <- recipe( ~., data = iris) %>%
step_mutate(SepalSquared= Sepal.Length ^ 2, role="outcome")
然后檢查它是否與 summary(prep(rec))
variable type role source
<chr> <chr> <chr> <chr>
1 Sepal.Length numeric predictor original
2 Sepal.Width numeric predictor original
3 Petal.Length numeric predictor original
4 Petal.Width numeric predictor original
5 Species nominal predictor original
6 SepalSquared numeric outcome derived
uj5u.com熱心網友回復:
這與tidymodel 錯誤有關,當呼叫 predict 函式時要求目標變數
通常不建議修改配方中的回應。這是因為回應變數在某些情況下對配方不可用,例如使用 {tune} 時。我建議您在將資料傳遞給配方之前執行此轉換。如果您在驗證拆分之前執行此操作,那就更好了。
set.seed(1234)
data_split <- my_data %>%
step_mutate(response = as.integer(all(c('A', 'B') %in% Col4) & Col4 == 'A')) %>%
initial_split()
training <- training(data_split)
testing <- testing(data_split)
rec <- recipe(response ~., data = training)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/336893.html
