0
對不起,如果它感覺像一個重複的問題,但說實話,我已經花了超過12個小時,並且還沒有發現容易理解和易於應用的方法。如何完美地將(創建的)模型應用於R中的新數據?
情況很簡單,我創建了2個模型,並且需要將它們應用於測試數據。
#Model 1 -
reg5 <- glm(train$survived ~ train$pclass_str + train$sex +
train$age_2 + train$sibsp + train$pclass_str*train$sex,
family = "binomial")
#Model 2 -
reg6 <- randomForest(train$survived_str ~ train$pclass_str + train$sex +
train$age_2 + train$sibsp, ntree=5000)
應用它 -
test$pred_reg5 <- predict(reg5, newdata = test, type="response")
test$pred_reg6 <- predict(reg6, newdata = test, type="response")
什麼我可以保證的是,無論訓練和測試數據包含由同一個名字用在模型中的變量。儘管還有其他未使用的變量。
我得到的錯誤:
Error in `[<-.factor`(`*tmp*`, keep, value = c("0", "1", "1", "1", "0", :
NAs are not allowed in subscripted assignments
In addition: Warning message:
'newdata' had 418 rows but variables found have 891 rows
感謝您的幫助!
請在您的代碼中放入換行符。 – Roland
@Roland - 我只是在編輯OP的代碼。但是,好吧... – jlhoward
謝謝,如果你解釋爲什麼使用'data'參數會更好。第一個公式也可以簡化爲'age_2 + sibsp + pclass_str * sex'。 – Roland