为R中的随机统一森林留出一个ID交叉验证

时间:2015-11-13 17:11:55

标签: r random-forest cross-validation

我正在使用数据框df

df<-structure(list(ID = structure(c(1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L), .Label = c("AU-Tum", 
"AU-Wac", "BE-Bra", "BE-Jal", "BR-Cax", "BR-Sa3", "CA-Ca1", "CA-Ca2", 
"CA-Ca3", "CA-Gro", "Ca-Man", "CA-NS1", "CA-NS2", "CA-NS3", "CA-NS4", 
"CA-NS5", "CA-NS6", "CA-NS7", "CA-Oas", "CA-Obs", "CA-Ojp", "CA-Qcu", 
"CA-Qfo", "CA-SF1", "CA-SF2", "CA-SF3", "CA-SJ1", "CA-SJ2", "CA-SJ3", 
"CA-TP1", "CA-TP2", "CA-TP4", "CN-Cha", "CN-Ku1", "CZ-Bk1", "De-Bay", 
"DE-Har", "DE-Tha", "DE-Wet", "DK-Sor", "FI-Hyy", "FI-Sod", "FR-Hes", 
"FR-Pue", "GF-Guy", "ID-Pag", "IL-Yat", "IT-Col", "IT-Lav", "IT-Non", 
"IT-Ro1", "IT-Ro2", "IT-Sro", "JP-Tak", "JP-Tef", "JP-Tom", "NL-Loo", 
"PT-Esp", "RU-Fyo", "SE-Abi", "SE-Fla", "SE-Nor", "SE-Sk1", "SE-Sk2", 
"SE-St1", "UK-Gri", "UK-Ham", "US-Blo", "US-Bn1", "US-Bn2", "Us-Bn3", 
"US-Dk3", "US-Fmf", "US-Fwf", "US-Ha1", "US-Ha2", "US-Ho1", "US-Ho2", 
"US-Lph", "US-Me1", "US-Me3", "US-Nc2", "US-NR1", "US-Oho", "US-Sp1", 
"US-Sp2", "US-Sp3", "US-Syv", "US-Umb", "US-Wcr", "US-Wi0", "US-Wi1", 
"US-Wi2", "US-Wi4", "US-Wi8", "VU-Coc", "Austin", "Caxiuana", 
"Mae Klong", "Niwot Ridge", "Sky Oaks old", "Sky Oaks young", 
"Sodankylä", "Tomakomai", "Yenisey Abies", "Yenisey Betula", 
"Yenisey Mixed"), class = "factor"), P = c(1241.59999960661, 
1282.40000277758, 0, 895, 0, 960.399999260902, 988.300011262298, 
778.211069688201, 0, 676.725008800626, 1750.51986303926, 1614.11541634798, 
951.847023338079, 1119.3682884872, 1112.38984390156, 1270.65773075982, 
1234.72262170166, 1338.46096616983, 1136.69287367165, 1265.46480803983
), Te = c(9.20406423444821, 9.58323018294185, NaN, 12.1362462834136, 
NaN, 10.6474634506736, 10.2948508957069, 11.3363996068107, NaN, 
11.9457507949986, 9.10006221322572, 7.65505467142961, 8.21480062559674, 
8.09251754304318, 8.466220758789, 8.48094407814006, 8.77304120569444, 
8.31727518543397, 8.80921738865237, 9.04091478341757), Y = c(2172.34112930298, 
2479.44521586597, 1027.63470042497, 2342.35314202309, 868.4010617733, 
1157.13594430499, 1118.60130960867, 1100.47051284742, 1072.57190890331, 
1228.25697739795, 2268.14043972082, 2147.62290922552, 2269.1387550775, 
2247.31983098201, 1903.39138268307, 2174.78291538358, 2359.51909126411, 
2488.39004804939, 461.398994384333, 567.150629704352)), .Names = c("ID", 
"P", "Te", "Y"), row.names = 307:326, class = "data.frame")

我想在离开一个ID输出模式时执行一个随机的统一森林。基本上,我有一个循环,每次我删除具有相同ID的数据,我用其他ID训练我的模型。然后我通过对我为训练删除的数据进行预测来测试我的模型。我已完成下面的命令,但它不起作用。

library(randomUniformForest)    
ruf_pred<-c()
    for (i in df$ID){
      ruf = randomUniformForest(Y~ P+Te, 
                                    data= df[df$ID != i, ],   
                                    importance = T,
                                    ntree = 100)
      ruf_pred[[i]] = predict(ruf, newdata=df[df$ID != i, ], type="all")
    }

我收到此错误消息:error in is.vector(X) : argument "X" is missing, with no default

任何人都知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

而不是使用newdata = in预测你需要使用X =或不提X.

另见?predict.randomUniformForest

运行下面的代码可以解决问题:

ruf_pred<-c()
    for (i in df$ID){
      ruf = randomUniformForest(Y~ P+Te, 
                                    data= df[df$ID != i, ],   
                                    importance = T,
                                    ntree = 100)
      ruf_pred[[i]] = predict(ruf, X = df[df$ID != i, ], type = "all")
    }