Calculating prediction accuracy of a tree using rpart's predict method
Try calculating the confusion matrix first:
confMat <- table(test$class,t_pred)
Now you can calculate the accuracy by dividing the sum diagonal of the matrix - which are the correct predictions - by the total sum of the matrix:
accuracy <- sum(diag(confMat))/sum(confMat)
Gibberish Output in RPart plot in R
The algorithm replaces the levels of each factor by lower and upper case letters in the alphabet. If there are more than 56 levels in a factor, the Z letter is repeated, so it is not recommended to use factors with more than 56 levels as input to a rpart model.
However, it is possible to avoid the unwanted "gibberish" output: if you are using plot() + text(), try using the "pretty" parameter in the text() function. Example:
plot(tree)
text(tree, pretty=1)
Other output functions have their specific parameter for that. "labels()" for instance, has the "minlength" parameter:
labels(tree)
labels(tree,minlength=0)
I hope that helps.
Strange Behavior for the predict() function
Unfortunately, even though rpart
only used the wt
variable in splits, prediction still requires the others to be present. Use a data set with the sample columns:
> predict(model, mtcars[1,])
[1] 0.8571429
Max
Related Topics
Condition Filter in Dplyr Based on Shiny Input
Plot Line on Top of Stacked Bar Chart in Ggplot2
Installing R Studio with Anaconda
Use Loop to Split a List into Multiple Dataframes
R - Download Filtered Datatable
Use of .By and .Eachi in the Data.Table Package
R Reshape2 'Aggregation Function Missing: Defaulting to Length'
R: How to Aggregate Some Columns While Keeping Other Columns
Format Axis Tick Labels to Percentage in Plotly
Ggplot Piecharts on a Ggmap: Labels Destroy the Small Plots
Inserting Stargazer or Xable Table into Knitr Document
How to Get a List of All Possible Partitions of a Vector in R
Height' Must Be a Vector or a Matrix. Barplot Error
Labelling the Plots with Images on Graph in Ggplot2
How to Force Seasonality from Auto.Arima
Inserting a Table Under the Legend in a Ggplot2 and Saving Everything to a File
Note or Warning from Package Check When Readme.Md Includes Images