Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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.

like image 904
juansalix Avatar asked Apr 21 '26 02:04

juansalix


1 Answers

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.

like image 96
user11877958 Avatar answered Apr 23 '26 17:04

user11877958



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!