missing value where TRUE/FALSE needed with Caret

时间:2015-11-13 08:22:54

标签: r-caret

I have a data frame, which contains the "date variable". (the test data and code is available here时,为什么兄弟元素和父元素会移动

但是,我使用“function = caretFunc”。它显示错误消息。

    Error in { : task 1 failed - "missing value where TRUE/FALSE needed"
In addition: Warning messages:
1: In FUN(newX[, i], ...) : NAs introduced by coercion
2: In FUN(newX[, i], ...) : NAs introduced by coercion
3: In FUN(newX[, i], ...) : NAs introduced by coercion
4: In FUN(newX[, i], ...) : NAs introduced by coercion
5: In FUN(newX[, i], ...) : NAs introduced by coercion
6: In FUN(newX[, i], ...) : NAs introduced by coercion
7: In FUN(newX[, i], ...) : NAs introduced by coercion
8: In FUN(newX[, i], ...) : NAs introduced by coercion
9: In FUN(newX[, i], ...) : NAs introduced by coercion
10: In FUN(newX[, i], ...) : NAs introduced by coercion

我该怎么办?

重现错误的代码:

library(mlbench)
library(caret)
library(maps)
library(rgdal)
library(raster)
library(sp)
library(spdep)
library(GWmodel)
library(e1071)
library(plyr)
library(kernlab)
library(zoo)

mydata <- read.csv("Realestatedata_all_delete_date.csv", header=TRUE)
mydata$estate_TransDate <- as.Date(paste(mydata$estate_TransDate,1,sep="-"),format="%Y-%m-%d")
mydata$estate_HouseDate <- as.Date(mydata$estate_HouseDate,format="%Y-%m-%d")

rfectrl <- rfeControl(functions=caretFuncs, method="cv",number=10,verbose=TRUE,returnResamp = "final")
results <- rfe(mydata[,1:48],mydata[,49],sizes = c(1:48),rfeControl=rfectrl,method = "svmRadial")

print(results)
predictors(results)
plot(results, type=c("g", "o"))

1 个答案:

答案 0 :(得分:0)

以下输入变量(您提供给分类器)NAsmydata(缺少值):

colnames(mydata)[unique(which(is.na(mydata[,1:48]), arr.ind = TRUE)[,2])]

给出:

 [1] "Aport_Distance"       "Univ_Distance"        "ParkR_Distance"
 [4] "TRA_StationDistance"  "THSR_StationDistance" "Schools_Distance"
 [7] "Lib_Distance"         "Sport_Distance"       "ParkS_Distance"
[10] "Hyper_Distance"       "Shop_Distance"        "Post_Distance"
[13] "Hosp_Distance"        "Gas_Distance"         "Incin_Distance"
[16] "Mort_Distance" 

此外,您的日期变量(交易日期和房屋日期)似乎在NAs内转换为rfe(..)

SVM回归量似乎无法按原样处理NAs

我会将日期转换为自给定参考资料以来的年份:

mydata$estate_TransAge <- as.numeric(as.Date("2015-11-01") - mydata$estate_TransDate) / 365.25
mydata$estate_HouseAge <- as.numeric(as.Date("2015-11-01") - mydata$estate_HouseDate) / 365.25

# define the set of input variables
inputVars = setdiff(colnames(mydata),

                    # exclude these
                    c("estate_TransDate", "estate_HouseDate", "estate_TotalPrice")
                   )

并且还会删除您用作回归量输入的任何列中任何NA的条目:

traindata <- mydata[complete.cases(mydata[,inputVars]),]

然后用:

运行rfe
rfectrl <- rfeControl(functions=caretFuncs, method="cv",number=10,verbose=TRUE,returnResamp = "final")
results <- rfe(
               traindata[,inputVars], 
               traindata[,"estate_TotalPrice"],
               rfeControl=rfectrl,
               method = "svmRadial"
              )

就我而言,这需要很长时间才能完成,所以我只使用以下百分之一的数据对其进行了测试:

traindata <- sample_frac(traindata, 0.01)

如果您获得数据来预测某些输入变量的价格NA,那么问题仍然是该怎么办。