在使用插入符创建训练和测试数据时缺少值

时间:2015-03-03 11:54:59

标签: r statistics r-caret

我的问题是如何使用火车拟合带有插入符号的模型来处理缺失值。 我的数据的一小部分就是这样:

       df <- dput(dat)
       structure(list(LagO3 = c(NA, NA, NA, 40, 45, NA), RH = c(69.4087524414062, 
       79.9608383178711, 64.4592437744141, 66.4207077026367, 66.0899200439453, 
       91.3353729248047), SR = c(298.928888888889, 300.128888888889, 
       303.688888888889, 304.521111111111, 303.223333333333, 294.716666666667
       ), ST = c(317.9917578125, 317.448253038194, 311.039059244792, 
       312.557927517361, 321.252841796875, 330.512212456597), Tmx = c(294.770359293045, 
       294.897191864461, 295.674552786042, 296.247345044048, 296.108238352818, 
       294.594430242372), CWTE = c(0, 1, 0, 0, 0, 0), CWTW = c(0, 0, 
       0, 0, 0, 0), o3 = c(NA, NA, NA, 52, 55, NA)), .Names = c("LagO3", 
       "RH", "SR", "ST", "Tmx", "CWTE", "CWTW", "o3"), row.names = c("1", 
       "2", "3", "4", "5", "6"), class = "data.frame")

问题是,对于我的一个预测变量中的几个位置,我有NA,而预测(o3)也有NA(但在不同的位置)。然后,我试过了:

model <- train(x = na.omit(x.training), y = na.omit(training$o3), method = "lmStepAIC",
               direction="backward", trControl = control)

但是,我会有不同的长度... 我试着用:

 model <- train(x = x.training, y = training$o3,na.action=na.pass, 
                method = "lmStepAIC",direction="backward",trControl = control)

出现以下错误:

Error in quantile.default(y, probs = seq(0, 1, length = cuts)) : 
  missing values and NaN's not allowed if 'na.rm' is FALSE

我很感激任何建议!

非常感谢。

1 个答案:

答案 0 :(得分:1)

您需要将na.action参数与na.omit函数的 train 一起使用。正如文档中针对na.action(类型?train)所说的那样:

  

用于指定在找到NA时要采取的操作的函数。默认操作是过程失败。另一种选择是na.omit,它导致拒绝任何所需变量上缺少值的情况。 (注意:如果给出,则必须命名此参数。)

以下内容将起作用:

model <- train(x = x.training, y = training$o3, 
              method = "lmStepAIC",direction="backward", 
              trControl = control, na.action=na.omit)

输出:

> model <- train(x = x.training, y = y.training, method = "lmStepAIC",direction="backward",
+                na.action=na.omit)
Start:  AIC=-129.7
.outcome ~ LagO3 + RH + SR + ST + Tmx + CWTE + CWTW


Step:  AIC=-129.7
.outcome ~ LagO3 + RH + SR + ST + Tmx + CWTE


Step:  AIC=-129.7
.outcome ~ LagO3 + RH + SR + ST + Tmx


Step:  AIC=-129.7
.outcome ~ LagO3 + RH + SR + ST


Step:  AIC=-129.7
.outcome ~ LagO3 + RH + SR


Step:  AIC=-129.7
.outcome ~ LagO3 + RH


Step:  AIC=-129.7
.outcome ~ LagO3


Step:  AIC=-129.7
.outcome ~ 1
...
...
...