Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中的randomForest:可以拟合模型并将其用于无误差的预测,但tuneRF给出了差异长度误差_R_Random Forest - Fatal编程技术网

R中的randomForest:可以拟合模型并将其用于无误差的预测,但tuneRF给出了差异长度误差

R中的randomForest:可以拟合模型并将其用于无误差的预测,但tuneRF给出了差异长度误差,r,random-forest,R,Random Forest,只是搞乱了UCI的心脏病数据:。数据格式如下: A tibble: 6 x 14 age sex cp trestbps chol fbs restecg thalach exang oldpeak <dbl> <dbl> <dbl> <int> <int> <dbl> <int> <int> <int> <dbl> 1 63

只是搞乱了UCI的心脏病数据:。数据格式如下:

A tibble: 6 x 14
    age   sex    cp trestbps  chol   fbs restecg thalach exang oldpeak
  <dbl> <dbl> <dbl>    <int> <int> <dbl>   <int>   <int> <int>   <dbl>
1    63     1     3      145   233     1       0     150     0     2.3
2    41     0     1      130   204     0       0     172     0     1.4
它是R3.5.0和randomForest 4.6-14

您将在代码中看到一些注释:

1) tuneRF命令使用同一数据集的子集,因此类标签是相同的

2) 在训练/测试划分之前,“目标”响应变量已转换为因子

我有一种感觉,这与我的子集设置方式有关,结果可能是列表而不是数据帧?但是我在前面的步骤中使用了相同的方法,没有错误。我以前发现了一个关于这个的问题,但现在在我的历史记录/谷歌中找不到。即使我能找到它,我也不明白它是如何应用的,因为我以前使用过相同的子集方法,没有任何问题

脚本:

library(tidyverse)
library(randomForest)
我已经添加了匈牙利数据,在通过运行以下命令插补缺失值(并且不想使用响应进行插补)之后:

hungar_heart <- cbind(impute(hungar_heart[,-14]),hungar_heart[,14])

hungar_heart太好了-多亏了一些帮助,才解决了这个问题

我应该在我的OP中明确说明我在使用dplyr

事实证明,尽管randomForest和predict在tibbles上可以很好地工作,但tuneRF(或者可能是tuneRF在我的子集设置方式之后)需要一个数据帧,否则会抛出一个错误

V简单修复:

train <- as.data.frame(train)

使用R版本培训
3..3.2
randomForest
版本
4.6-14
您的代码正常工作,没有任何错误。好的,很好,我将尝试使用这些版本。我肯定应该指定在我的OP中使用的版本,我将添加。也可能我省略了一些上下文所需的代码。
hungar_heart<-setNames(hungar_heart, c("age","sex","cp","trestbps","chol","fbs","restecg","thalach","exang","oldpeak","slope","ca","thal","target"))
heart_total<-rbind(heart_data,hungar_heart)

heart_total$target <- as.factor(heart_total$target)

#Partition new combined dataset into training and test sets after setting seed (123)
set.seed(123)
indicator <- sample(2, nrow(heart_total), replace = TRUE, prob = c(.7,.3))
train <- heart_total[indicator==1,]
test <- heart_total[indicator==2,]

#Fit random forest to training set, using default values to start.  
forest <- randomForest(target~., data=train)

#Use trained model on test set
predict_try <- predict(forest, test)

#so far so good.  now tuneRF gives error:

tune_RF <- tuneRF(train[,-14],train[,14],
   stepFactor = 0.5,
   plot = TRUE,
   ntreeTry = 300,
   improve = 0.05)

Error in randomForest.default(x, y, mtry = mtryStart, ntree = ntreeTry,  : 
length of response must be the same as predictors
In addition: Warning message:
In randomForest.default(x, y, mtry = mtryStart, ntree = ntreeTry,  :
  The response has five or fewer unique values.  Are you sure you want to do regression?

#FWIW, length:

length(train[,-14])
[1] 13

length(train[,14])
[1] 1
train <- as.data.frame(train)