'confusionMatrix Error in newdata.df$Propensity : $ operator is invalid for atomic vectors
A resulting set of predictive model validation for a classification model includes both actual values and propensities.
Create a data frame using cbind() command, include Propensity and Actual values in the data frame.
Propensity <- c(0.53, 0.42, 0.38, 0.76, 0.33, 0.42, 0.55, 0.59, 0.09, 0.21, 0.32, 0.52, 0.03, 0.13, 0.01, 0.68, 0.48, 0.29, 0.03, 0.02)
Actual <- c(1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0)
newdata.df <- cbind(Propensity,Actual)
confusionMatrix(as.factor(ifelse(newdata.df$Propensity > 0.5, 'Propensity', 'Actual')),newdata.df$Actual)
Solution 1:[1]
First, you created a matrix with cbind() but the $colname works only with data frames. Second, confusionMatrix wants two factors that have the same levels, both 1, 0 or both Actual, Propensity:
library(caret) # Always include packages loaded. The `confusionMatrix` function is not part of base R
Prop <- as.factor(ifelse(newdata.df$Propensity > 0.5, 'Propensity', 'Actual'))
Act <- as.factor(ifelse(newdata.df$Actual > 0.5, 'Propensity', 'Actual'))
newdata.df <- data.frame(Prop, Act)
confusionMatrix(newdata.df$Prop, newdata.df$Act)
# Confusion Matrix and Statistics
#
# Reference
# Prediction Actual Propensity
# Actual 10 4
# Propensity 1 5
#
# Accuracy : 0.75
# 95% CI : (0.509, 0.9134)
# No Information Rate : 0.55
# P-Value [Acc > NIR] : 0.05533
#
# Kappa : 0.4792
#
# Mcnemar's Test P-Value : 0.37109
#
# Sensitivity : 0.9091
# Specificity : 0.5556
# Pos Pred Value : 0.7143
# Neg Pred Value : 0.8333
# Prevalence : 0.5500
# Detection Rate : 0.5000
# Detection Prevalence : 0.7000
# Balanced Accuracy : 0.7323
#
# 'Positive' Class : Actual
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dcarlson |
