简体   繁体   中英

Strange classification in R and Error in eval(predvars, data, env) : object […] not found error (no typo!)

I am very new to R and have spent hours trying to solve the following problems (which I sense may be interrelated). I have read other answers mainly suggesting that there may be a typo in the DRB1 column. This is definitely now the case here and I am wondering if the error is earlier on since the confusion matrix shows that everything is classified as "-" while about 20% of factors in "Response" are "+":

Call:
 randomForest(formula = Response ~ ., data = training, ntree = 21, importance = TRUE) 
               Type of random forest: classification
                     Number of trees: 21
No. of variables tried at each split: 1

OOB estimate of  error rate: 20.24%
Confusion matrix:

  

   -     +   class.error
 - 17504 0           0
 +  4443 0           1

I am trying to use the random forest fn to predict the response variable "Response" by column DRB1:

library(randomForest)
#library(xlsx) 
library(xlsx)

# load data
path <- "C:/Users/[...].xlsx" #I have only removed the path for privacy reasons
data <- read.xlsx(path, sheetIndex = 2)

# show data [FINE]
head(data)

# make some data categorical
data$DRB1=as.factor(data$DRB1)
data$Response=as.factor(data$Response)

# to check data structure [FINE]
str(data)

# define 20% testing, 80% training data set etc. 
data_set_size=floor(nrow(data)*0.80)
index <- sample(1:nrow(data), size = data_set_size)

# training set up to first set of index 
training <- data[index,]

# testing set up to first set of index 
testing <- data[-index,]

head(testing,100)  #FINE!

# now apply random forest
rf <- randomForest(Response ~ ., data = training, ntree=21, importance=TRUE)
rf
plot(rf)

# testing$DRB1 [FINE]
str(testing)

# predict runs random forest
result <- data.frame(testing$Response, predict(rf, testing[,2], type = "response")) 
result
plot(result)

Here is a scaled-down version of the information I get when running the code:

> library(randomForest)
> #library(xlsx) 
> library(xlsx)
> 
> # load data
> path <- "C:/Users/[...].xlsx"
> data <- read.xlsx(path, sheetIndex = 2)
> 
> # show data
> head(data)
  Response  DRB1
1        - 08_01
2        - 11_01
3        - 07_01
4        - 11_01
5        - 04_04
6        - 07_01
> 
> # make some data categorical
> 
> data$DRB1=as.factor(data$DRB1)
> data$Response=as.factor(data$Response)
> 
> # to check data structure
> str(data)
'data.frame':   27438 obs. of  2 variables:
 $ Response: Factor w/ 2 levels "-","+": 1 1 1 1 1 1 1 1 1 1 ...
 $ DRB1    : Factor w/ 47 levels "0","01_01","01_02",..: 19 26 18 26 10 18 18 19 31 18 ...
> 
> # define 20% testing, 80% training data set etc. 
> data_set_size=floor(nrow(data)*0.80)
> index <- sample(1:nrow(data), size = data_set_size)
> 
> # training set up to first set of index 
> training <- data[index,]
> 
> # testing set up to first set of index 
> testing <- data[-index,]
> 
> head(testing,20)
    Response  DRB1
2          - 11_01
6          - 07_01
9          - 12_01
13         - 03_01
14         - 12_01
27         - 08_01
28         + 04_02
31         - 04_04
33         - 03_01
39         - 14_54
54         - 01_01
55         - 03_01
60         - 04_02
69         - 03_02
81         - 08_01
83         - 11_01
88         - 04_04
90         - 04_07
104        - 15_03
115        - 11_01
> 
> # now apply random forest
> rf <- randomForest(Response ~ ., data = training, ntree=21, importance=TRUE)
> rf

Call:
 randomForest(formula = Response ~ ., data = training, ntree = 21,      importance = TRUE) 
               Type of random forest: classification
                     Number of trees: 21
No. of variables tried at each split: 1

        OOB estimate of  error rate: 20.24%
Confusion matrix:
      - + class.error
- 17504 0           0
+  4443 0           1

Question 1: Why are there no + predictions?

> plot(rf)
> 
> # testing$DRB1
> 
> str(testing)
'data.frame':   5488 obs. of  2 variables:
 $ Response: Factor w/ 2 levels "-","+": 1 1 1 1 1 1 2 1 1 1 ...
 $ DRB1    : Factor w/ 47 levels "0","01_01","01_02",..: 26 18 31 5 31 19 8 10 5 40 ...

Question2: If DRB1 has some NA data, is this counted as a category like any other and does the randomForest take it into account as a category or ignore it?

> # predict runs random forest
> result <- data.frame(testing$Response, predict(rf, testing[,2], type = "response")) 
Error in eval(predvars, data, env) : object 'DRB1' not found

Question 3: I have been trying so hard to understand this error message but cannot. Can you help me?

> result
     testing.Response predict.rf..testing...2.3...type....response..
7                   -                                              -
11                  -                                              -
13                  -                                              -
16                  -                                              -
24                  -                                              -
36                  -                                              -
56                  -                                              -
58                  -                                              -
59                  -                                              -
65                  -                                              -
72                  -                                              -
73                  -                                              -
75                  -                                              -
77                  -                                              -
87                  -                                              -
89                  -                                              -
95                  -                                              -
101                 -                                              -
108                 -                                              -
111                 +                                              -
118                 -                                              -
123                 -                                              -
124                 -                                              -
126                 -                                              -
127                 -                                              -
130                 -                                              -
143                 -                                              -
149                 -                                              -
154                 -                                              -
155                 -                                              -
167                 -                                              -
169                 -                                              -
174                 -                                              -
175                 -                                              -
177                 -                                              -
199                 -                                              -
201                 -                                              -
202                 -                                              -
213                 -                                              -
220                 -                                              -
229                 -                                              -
231                 +                                              -
256                 -                                              -
259                 -                                              -
268                 -                                              -
278                 -                                              -
281                 -                                              -
291                 -                                              -
296                 +                                              -
297                 +                                              -
298                 -                                              -
301                 +                                              -
302                 -                                              -
303                 +                                              -
[...]
 [ reached 'max' / getOption("max.print") -- omitted 2244 rows ]
> plot(result)

The result plot stays the same as from a previous simulation...

I am SUPER grateful for any help/answers to even just 1 of the 3 questions.

Thanks a lot !

You have only one variable DRB1 for making the prediction, so most likely random forest is an overkill.

I would check by simple plots whether there is any association between categories of DRB1 and your response, for example doing:

library(ggplot2)
library(dplyr)
data %>% group_by(DRB1) %>% 
count(Response) %>% 
mutate(prop=n/sum(n)) %>% 
ggplot(aes(x=DRB1,y=prop,fill=Response)) + geom_col()

As for NAs.. it is no used in the model, but I don't know how applicable is this given that your data doesn't really need a ML model

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM