6
我正在參加Kaggle泰坦尼克號競賽,並且有關於插入缺失值的問題。我正在嘗試使用Caret軟件包,我的訓練集由因素和數字組成。插值與插入符號缺失值
我想使用Caret中的preProcess
函數來計算缺失值,但在使用preProcess之前,我需要使用dummyVars
函數將所有因子轉換爲虛擬變量。
dummies = dummyVars(survived ~ . -1, data = train, na.action = na.pass)
xtrain = predict(dummies, train)
然而,在使用dummyVars
轉換的因素的過程中,所有來港是由一些不知名的算法預測和失蹤age
列都成爲即使我已經指定na.action = na.pass
1的。我想將我的因素轉換爲虛擬變量,但沒有觸及NA,因此我可以使用preProcess
函數來計算它們。我怎樣才能做到這一點?
謝謝。
dput這裏:
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")
感謝您的答覆被釋放。 'model.matrix'似乎並沒有這樣做,因爲它省略了NA中的任何行。請讓我知道是否有辦法讓它工作。謝謝。 – mchangun