简体   繁体   English

eval 中的错误(predvars、data、env):object 'Example' not found in RandomForest function

[英]Error in eval(predvars, data, env) : object 'Example' not found in RandomForest function

Im just playing with a Random Forest, but I seem to have an issue.我只是在玩随机森林,但我似乎有一个问题。 When I try use the randomForest() function, it returns the error: Error in eval(predvars, data, env): object '180018R' not found .当我尝试使用randomForest() function 时,它返回错误: Error in eval(predvars, data, env): object '180018R' not found Here are the latest (relevant) lines of code, followed by the structure() output.这是最新的(相关)代码行,后面是structure() output。

install.packages("randomForest")

# Random forest

data <- as.data.frame(pattern_mat)
str(data)

# Response variable is "Response" Column 313
data$Response <- as.factor(data$Response)
table(data$Response)

### Data Partition
set.seed(123)
ind <- sample(2, nrow(data), replace=TRUE, prob=(c(0.7, 0.3)))
train <- data[ind==1,]
test <- data[ind==2,]

### Random Forest
library(randomForest)
set.seed(222)
rf <- randomForest(Response~., data = train)

Structure info *I have shortened the output as it is unnecessary.结构信息*我已经缩短了 output 因为它是不必要的。

> str(train)
'data.frame':   145 obs. of  313 variables:
 $ 180018R : num  1 0 0 0 0 0 0 0 0 0 ...
 $ 217220R : num  1 0 0 0 0 0 0 0 0 0 ...
 $ 217300R : num  1 0 0 0 0 0 0 0 0 0 ...
 $ 281722R : num  0 1 1 1 1 1 1 1 1 0 ...
 $ 681714R : num  0 1 1 1 1 1 1 1 1 0 ...
 $ 281730R : num  0 1 1 1 1 1 1 1 1 0 ...
 $ 681715R : num  0 1 1 1 1 1 1 1 1 0 ...
 $ 411113  : num  0 0 0 0 0 0 0 0 0 1 ...
 $ 478105  : num  0 0 0 0 0 0 0 0 0 1 ...
     :        :   : : : : : : : : : :
     :        :   : : : : : : : : : :
     :        :   : : : : : : : : : :
 $ 641112  : num  0 0 0 0 0 0 0 0 0 0 ...
 $ 641170  : num  0 0 0 0 0 0 0 0 0 0 ...
 $ 641370  : num  0 0 0 0 0 0 0 0 0 0 ...
 $ 641611  : num  0 0 0 0 0 0 0 0 0 0 ...
 $ 645342  : num  0 0 0 0 0 0 0 0 0 0 ...
  [list output truncated]

So as you can see the error: Error in eval(predvars, data, env): object '180018R' not found mentioned 180018R which is the name of the first column.所以你可以看到错误: Error in eval(predvars, data, env): object '180018R' not found提到180018R这是第一列的名称。

Anyone knows what to do?有谁知道该怎么做?

As best as I can tell, the issue is with the columns' names starting with numbers, which is not the best practice in R (although allowed), and I guess randomForest is not correctly handling it with the ~.据我所知,问题在于以数字开头的列名,这不是 R 中的最佳实践(尽管允许),我猜 randomForest 没有正确处理它与~. syntax.句法。

Try to rename all of the columns so that they start with some generic letter like V and then see if your function now works.尝试重命名所有列,使它们以V之类的通用字母开头,然后查看您的 function 现在是否有效。 Here is a reproducible example that demonstrates this.这是一个可重现的示例,可以证明这一点。

set.seed(1)
data <- data.frame(x = rbinom(100, 1, 0.5))
data$`180018R` <- data$x
data$x <- NULL
data$Response <- as.factor(rbinom(100, 1, 0.2))
table(data$Response)

### Demonstrating error
library(randomForest)
set.seed(222)
rf <- randomForest(Response~., data = data)
# Produces error as in the original example
# Fixing the issue by adding a character to the column names except response
data2 <- data
response_col <- which(colnames(data2) == "Response")
colnames(data2)[-response_col] <- paste0( "V", colnames(data2)[-response_col])
set.seed(222)
rf <- randomForest(Response ~ ., data = data2)
# Runs with no issue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 eval(predvars,data,env)中的错误:找不到对象 - Error in eval(predvars, data, env) : object not found R:eval(predvars,data,env)中的错误:找不到对象&#39;x&#39; - R: Error in eval(predvars, data, env) : object 'x' not found 回归:评估错误(predvars,数据,env):未找到 object 'volt' - regression: Error in eval(predvars, data, env) : object 'volt' not found R eval(predvars, data, env) object 未通过在 function 中传递参数找到 - R eval(predvars, data, env) object not found by passing a pameter in a function eval(predvars, data, env) 中的错误:找不到对象“下水道” - Error in eval(predvars, data, env) : object 'Sewer' not found eval(predvars, data, env) 中的错误:找不到对象“BMI” - Error in eval(predvars, data, env) : object 'BMI' not found eval(predvars, data, env) 中的错误:找不到对象“有用” - Error in eval(predvars, data, env) : object 'helpfulness' not found eval(predvars,data,env)中的systemfit错误:找不到对象 - systemfit Error in eval(predvars, data, env) : object not found stepcAIC-eval(predvars,data,env)中的错误:找不到对象&#39;Color1&#39; - stepcAIC - Error in eval(predvars, data, env) : object 'Color1' not found eval(predvars, data, env) 中的错误:找不到对象“Rm” - Error in eval(predvars, data, env) : object 'Rm' not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM