简体   繁体   中英

"NAs introduced by coercion" Error when using tree function

I've been classifying events using the KNN algorithm but that has not led to high accuracies of classification. I've been told by some collegues that the tree () function in R (from the tree package) could help with this.

Here's a sample of my data. I'm trying to classify different events (I have 8 different classes of events), based on the values from the first two columns "ACTIVITY_X" and "ACTIVITY_Y" :

> print(dataset)
     ACTIVITY_X ACTIVITY_Y     Event
  1:         19         21 Vigilance
  2:         20         14 Vigilance
  3:         34         35 Vigilance
  4:         18          5 Vigilance
  5:         23         27 Vigilance
 ---                                
426:          9         25 Vigilance
427:          0          0   Head-up
428:          0          0   Head-up
429:          3          3   Head-up
430:          0          0 Vigilance

Ideally, I would like to find different threshold values between the different classes (Head-up, Vigilance etc..) which should help classifying them when "Event" data is not available and I only have "ACTIVITY_X" and "ACTIVITY_Y" data. I guess I should be using the tree() function as:

xtree <- tree(Head-up~ACTIVITY_X+ACTIVITY_Y,data=dataset)
plot(xtree)
title("Head_up")
text(xtree)

xtree <- tree(Vigilance~ACTIVITY_X+ACTIVITY_Y,data=dataset)
plot(xtree)
title("Vigilance")
text(xtree)

etc..

However, I'm having different errors when running the analysis, main one being "NAs introduced by coercion" . These errors are unexistant when I'm using the rpart() function, which is also a classifying algorithm.

> xtree <- tree(Vigilance~ACTIVITY_X+ACTIVITY_Y,data=dataset)
Warning message:
In tree(Vigilance ~ ACTIVITY_X + ACTIVITY_Y, data = dataset) :
  NAs introduced by coercion
> plot(xtree)
Error in plot.tree(xtree) : cannot plot singlenode tree
> title("Vigilance")
Error in title("Vigilance") : plot.new has not been called yet
> text(xtree)
Error in text.tree(xtree) : cannot plot singlenode tree

Any help would be appreciated. I'm very new to R so I hope this question is still of interest to other users.

I am a little unsure if the structure of my data is the same as yours, but the error was the same for me:

Binary | X1  | X2
No     | 6.3 | 8.3
Yes    | 7.2 | 9.8
Yes    | 5.0 | 3.8

x = tree(Binary ~ . , data)

NAs introduced by coercion

For me, this error was because the 'Binary' variable in the data set was of format "character" rather than the required "factor" format

class(data$Binary)
"character"

data$Binary = as.factor(data$Binary)

class(data$Binary)
"factor"

After this transformation, running the tree function does not give the error anymore.

yes changing variable to factor from from other classes such as binary, character etc works thank..!!!

> class(train_data$saleCAT)

[1] "character"

data$saleCAT <- as.factor(data_cat) 
> class(train_data$saleCAT)

[1] "factor"

关于这个主题的评论有点旧,但我认为旧版本的 R 在分配时会自动分配因子,我认为这会更好。

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