setwd("/Users/lululemken/Desktop/Data Mining/Homework")
library(class) library(rpart)
library(caret) library(gains)
death_df <- read.csv("2005_data.csv", nrows = 50000)
str(death_df) colSums(is.na(death_df))
Based on the Dataset provided here (2005_Data.csv) about the death report, split the data by 70% and 30% into training and validation.
train_index <- sample(c(1:dim(new_death_df)[1]),dim(new_death_df)[1]*0.7)
valid_index <- setdiff(c(1:dim(new_death_df)[1]), train_index)
train_df <- new_death_df[train_index,]
valid_df <- new_death_df[valid_index,]
## Loading required package: ggplot2
## Loading required package: lattice
You will need to pick 5 appropriate predictors. Use KNN, Classification Tree, and Logistic Regression to create an ensemble model to predict if the person is going to out live the average age or not.
new_death_df <- death_df[,c("education_1989_revision", "marital_status", "hispanic_origin", "month_of_death", "detail_age", "injury_at_work")]
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 7015 2458
## 1 1426 4101
##
## Accuracy : 0.7411
## 95% CI : (0.734, 0.7481)
## No Information Rate : 0.5627
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.4645
##
## Mcnemar's Test P-Value : < 2.2e-16
##
## Sensitivity : 0.6252
## Specificity : 0.8311
## Pos Pred Value : 0.7420
## Neg Pred Value : 0.7405
## Prevalence : 0.4373
## Detection Rate : 0.2734
## Detection Prevalence : 0.3685
## Balanced Accuracy : 0.7282
##
## 'Positive' Class : 1
##
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 6828 2260
## 1 1613 4299
##
## Accuracy : 0.7418
## 95% CI : (0.7347, 0.7488)
## No Information Rate : 0.5627
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.4695
##
## Mcnemar's Test P-Value : < 2.2e-16
##
## Sensitivity : 0.6554
## Specificity : 0.8089
## Pos Pred Value : 0.7272
## Neg Pred Value : 0.7513
## Prevalence : 0.4373
## Detection Rate : 0.2866
## Detection Prevalence : 0.3941
## Balanced Accuracy : 0.7322
##
## 'Positive' Class : 1
##
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 7041 2440
## 1 1400 4119
##
## Accuracy : 0.744
## 95% CI : (0.7369, 0.751)
## No Information Rate : 0.5627
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.4705
##
## Mcnemar's Test P-Value : < 2.2e-16
##
## Sensitivity : 0.6280
## Specificity : 0.8341
## Pos Pred Value : 0.7463
## Neg Pred Value : 0.7426
## Prevalence : 0.4373
## Detection Rate : 0.2746
## Detection Prevalence : 0.3679
## Balanced Accuracy : 0.7311
##
## 'Positive' Class : 1
##
## ActualClass LRProb LRPred KNNProb KNNPred TREEProb TREEPred
## 2 0 0.2 0 0.2 0 0.3 0
## 3 1 0.2 0 0.2 0 0.3 0
## 9 0 0.2 0 0.2 0 0.3 0
## 13 1 0.2 0 0.4 1 0.3 0
## 15 1 0.7 1 0.3 1 0.7 1
## 21 1 0.3 0 0.3 0 0.3 0
## 23 0 0.3 0 0.3 0 0.3 0
## 25 1 0.7 1 0.3 1 0.7 1
## 27 0 0.3 0 0.5 0 0.3 0
## 29 0 0.3 0 0.3 1 0.3 0
## ActualClass LRProb LRPred KNNProb KNNPred TREEProb TREEPred majority avg
## 2 0 0.2 0 0.2 0 0.3 0 FALSE 0.2
## 3 1 0.2 0 0.2 0 0.3 0 FALSE 0.2
## 9 0 0.2 0 0.2 0 0.3 0 FALSE 0.2
## 13 1 0.2 0 0.4 1 0.3 0 TRUE 0.3
## 15 1 0.7 1 0.3 1 0.7 1 TRUE 0.6
## 21 1 0.3 0 0.3 0 0.3 0 FALSE 0.3
Error in predict(tr, valid_df)[, 5] : incorrect number of dimensions
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 6770 2232
## 1 1671 4327
##
## Accuracy : 0.7398
## 95% CI : (0.7327, 0.7468)
## No Information Rate : 0.5627
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.4662
##
## Mcnemar's Test P-Value : < 2.2e-16
##
## Sensitivity : 0.6597
## Specificity : 0.8020
## Pos Pred Value : 0.7214
## Neg Pred Value : 0.7521
## Prevalence : 0.4373
## Detection Rate : 0.2885
## Detection Prevalence : 0.3999
## Balanced Accuracy : 0.7309
##
## 'Positive' Class : 1
##
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 7073 2496
## 1 1368 4063
##
## Accuracy : 0.7424
## 95% CI : (0.7353, 0.7494)
## No Information Rate : 0.5627
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.4663
##
## Mcnemar's Test P-Value : < 2.2e-16
##
## Sensitivity : 0.6195
## Specificity : 0.8379
## Pos Pred Value : 0.7481
## Neg Pred Value : 0.7392
## Prevalence : 0.4373
## Detection Rate : 0.2709
## Detection Prevalence : 0.3621
## Balanced Accuracy : 0.7287
##
## 'Positive' Class : 1
##