【发布时间】:2019-11-20 11:41:59
【问题描述】:
我正在尝试运行一个查询,创建一个 for 循环,以使用来自包 rattle.data 的数据创建引导程序(以 RainTomorrow 作为目标列的天气数据)。我正在尝试为每个引导样本选择一个具有最大概率的类,然后预测具有最大票数的类。
使用此代码,我不断收到错误提示
if(!require(rpart)) install.packages("rpart")
if(!require(rpart.plot)) install.packages("rpart.plot")
if(!require(caret)) install.packages("caret")
if(!require(rattle.data)) install.packages("rattle.data")
if(!require(tidyverse)) install.packages("tidyverse")
if(!require(ipred)) install.packages("ipred")
if(!require(Metrics)) install.packages("Metrics")
library(rpart)
library(rpart.plot)
library(rattle.data)
library(tidyverse)
library(caret)
library(ipred)
library(Metrics)
set.seed(500)
data <- weather
# creating train and test data
index <- createDataPartition(data$RainTomorrow, p = .6, list = FALSE)
train_data <- data[ index, ]
test_data <- data[-index, ]
## b ukol -> error in for each loop
nBoot = 10 #nr bootstrap samples
#create empty matrix [nr test data x nr bootstrap samples]to store bootstrap predictions
pred = matrix(data = NA, nrow = nrow(test_data), ncol = nBoot)
train_controls = rpart.control(minsplit = 6, maxdepth = 3)
for(b in 1:nBoot){
#create bootstrap sample
index.boot = sample(x=nrow(train_data), replace = T, size = nrow(train_data))
data_boot = train_data[index.boot,]
#fit data for the bootstrap sample
boot.model = rpart(RainTomorrow ~ .,
data =data_boot,
method = "anova",
control = train_controls)
#rpart.plot(boot.model)
#save prediction for bootstrap
pred[,b] = predict(boot.model, newdata= test_data )
}
#calculate prediction as mean of bootstrap predictions
pred.bagged = rowMeans(pred)
print(rmse(actual = test_data$RainTomorrow, predicted = pred.bagged))
但运行此查询会给我一条警告消息:
在 Ops.factor(actual, predict) 中:'-' 对因子没有意义
我一辈子都想不出原因(机器学习的新手)。
编辑:仍在寻找有效答案
【问题讨论】:
标签: r machine-learning statistics-bootstrap