用Caret来估算缺失值

mch*_*gun 6 r r-caret

我正在参加Kaggle Titanic比赛,我有一个关于输入缺失值的问题.我正在尝试使用Caret包,我的训练集包括因素和数字.

我想使用preProcessCaret中的函数来估算缺失值,但在使用preProcess之前,我需要将所有因子转换为带有该dummyVars函数的虚拟变量.

dummies  = dummyVars(survived ~ . -1, data = train, na.action = na.pass)
xtrain = predict(dummies, train)
Run Code Online (Sandbox Code Playgroud)

然而,在使用dummyVars转换因子的过程中,所有的NA都是通过一些未知的算法预测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")
Run Code Online (Sandbox Code Playgroud)

top*_*epo 4

第一部分是一个错误;NA 值不应该是 1(显然)。同时,您可以用来model.matrix生成虚拟变量,但您可能必须立即对所有数据执行此操作。另外,如果您使用train,则可以使用公式方法。总的来说,这是一个更好的方法。

我将在接下来的几周内解决这个问题。我即将发布 caret 的一个版本,这个版本加上 UserR,会耽误我一些时间。

编辑:下周将发布修复该错误的新版本

最大限度