I'm trying to run a relatively simple model in R such as fitTree<-rpart(Event~ACTIVITY_X+ACTIVITY_Y)
. Here's my code with associated data:
> library(data.table)
> library(tree)
> library(rpart)
> #From the file "KNNCollar_#.txt", just select the columns ACTIVITY_X, ACTIVITY_Y, ACTIVITY_Z and Event
> dataraw<-fread("KNNCollar_41365.txt", select = c("ACTIVITY_X","ACTIVITY_Y","Event"),stringsAsFactors = F,header = TRUE)
> dataset <- matrix(NA,nrow=0,ncol=3)
> #Now, delete all rows containg the string "End"
> dataset<-dataraw[!grepl("End", dataraw$Event),]
> head(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
6: 33 20 Vigilance
> names(dataset)<-c("ACTIVITY_X", "ACTIVITY_Y","Event")
> fitTree<-rpart(Event~ACTIVITY_X+ACTIVITY_Y)
Error in eval(predvars, data, env) : object 'Event' not found
As you can see, I'm gettin the error Error in eval(predvars, data, env) : object 'Event' not found
.
Other people in the forum have been asking about this, and I've tried to adjust by converting my dataframe into a matrix, reading it from a .txt
, using names
etc. as suggested in other posts.
None of this is working for me. Now my question is if that's just a syntax error or there's a real issue with my code?
Hope somebody can help as any suggestion is appreciated!
I searched the internet for "rpart cran", clicked the https://cran.r-project.org/web/packages/rpart link and opened up https://cran.r-project.org/web/packages/rpart/rpart.pdf. Jumping to page where rpart
is documented it shows that this function accepts formula. If you are specifying a formula AND your data is in a data.frame, you need to pass in the data.frame using data
argument. In short:
fitTree <- rpart(Event ~ ACTIVITY_X + ACTIVITY_Y, data = dataset)
Notice that I added spaces which, arguably, enhances readability.