我正在参加 Kaggle Titanic 比赛,我有一个关于估算缺失值的问题。我正在尝试使用 Caret 包,我的训练集由因素和数字组成。
我想使用preProcess
Caret 中的函数来估算缺失值,但在使用 preProcess 之前,我需要使用函数将所有因子转换为虚拟变量dummyVars
。
dummies = dummyVars(survived ~ . -1, data = train, na.action = na.pass)
xtrain = predict(dummies, train)
然而,在使用dummyVars
转换因子的过程中,所有的 NAs 都是由一些未知的算法预测age
的,即使我指定了缺失的列也都变成了 1 na.action = na.pass
。我想在不触及 NA 的情况下将我的因子转换为虚拟变量,以便我可以使用然后使用该preProcess
函数来估算它们。我怎样才能做到这一点?
谢谢你。
在这里输入:
structure(list(survived = structure(c(1L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("0",
"1"), class = "factor"), pclass = structure(c(3L, 1L, 3L, 1L,
3L, 3L, 1L, 3L, 3L, 2L, 3L, 1L, 3L, 3L, 3L, 2L, 3L, 2L, 3L, 3L
), .Label = c("1", "2", "3"), class = "factor"), sex = structure(c(2L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L,
2L, 1L, 1L), .Label = c("female", "male"), class = "factor"),
age = c(22, 38, 26, 35, 35, NA, 54, 2, 27, 14, 4, 58, 20,
39, 14, 55, 2, NA, 31, NA), sibsp = c(1, 1, 0, 1, 0, 0, 0,
3, 0, 1, 1, 0, 0, 1, 0, 0, 4, 0, 1, 0), parch = c(0, 0, 0,
0, 0, 0, 0, 1, 2, 0, 1, 0, 0, 5, 0, 0, 1, 0, 0, 0), fare = c(7.25,
71.2833, 7.925, 53.1, 8.05, 8.4583, 51.8625, 21.075, 11.1333,
30.0708, 16.7, 26.55, 8.05, 31.275, 7.8542, 16, 29.125, 13,
18, 7.225), embarked = structure(c(4L, 2L, 4L, 4L, 4L, 3L,
4L, 4L, 4L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 2L), .Label = c("",
"C", "Q", "S"), class = "factor")), .Names = c("survived",
"pclass", "sex", "age", "sibsp", "parch", "fare", "embarked"), row.names = c(NA,
20L), class = "data.frame")