# Step 0: Load Required Libraries
library(class)
library(rpart)
library(caret)
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.3.2
## Loading required package: lattice
library(gains)
# Step 1: Import Data
death_df <- read.csv("2005_data.csv", nrows = 50000)
# Step 2: Explore Data
str(death_df)
## 'data.frame': 50000 obs. of 77 variables:
## $ resident_status : int 1 1 1 1 1 1 1 1 1 3 ...
## $ education_1989_revision : int 11 13 12 12 14 3 12 12 14 8 ...
## $ education_2003_revision : logi NA NA NA NA NA NA ...
## $ education_reporting_flag : int 0 0 0 0 0 0 0 0 0 0 ...
## $ month_of_death : int 1 1 1 1 1 1 1 1 1 1 ...
## $ sex : chr "F" "M" "F" "M" ...
## $ detail_age_type : int 1 1 1 1 1 1 1 1 1 1 ...
## $ detail_age : int 45 61 79 50 68 89 68 61 73 85 ...
## $ age_substitution_flag : logi NA NA NA NA NA NA ...
## $ age_recode_52 : int 35 38 41 36 39 43 39 38 40 43 ...
## $ age_recode_27 : int 15 18 21 16 19 23 19 18 20 23 ...
## $ age_recode_12 : int 7 8 10 7 9 11 9 8 9 11 ...
## $ infant_age_recode_22 : int NA NA NA NA NA NA NA NA NA NA ...
## $ place_of_death_and_decedents_status : int 1 1 6 1 1 6 1 1 1 7 ...
## $ marital_status : chr "M" "D" "D" "S" ...
## $ day_of_week_of_death : int 2 7 1 4 2 7 7 6 6 6 ...
## $ current_data_year : int 2005 2005 2005 2005 2005 2005 2005 2005 2005 2005 ...
## $ injury_at_work : chr "U" "U" "U" "U" ...
## $ manner_of_death : int 7 7 7 7 7 7 7 7 7 7 ...
## $ method_of_disposition : chr "U" "U" "U" "U" ...
## $ autopsy : chr "N" "N" "N" "N" ...
## $ activity_code : int NA NA NA NA NA NA NA NA NA NA ...
## $ place_of_injury_for_causes_w00_y34_except_y06_and_y07_: int NA NA NA NA NA NA NA NA NA NA ...
## $ icd_code_10th_revision : chr "C439" "J439" "I698" "E119" ...
## $ X358_cause_recode : int 98 266 239 159 93 239 266 267 266 125 ...
## $ X113_cause_recode : int 28 84 70 46 27 70 84 86 84 43 ...
## $ X130_infant_cause_recode : int NA NA NA NA NA NA NA NA NA NA ...
## $ X39_cause_recode : int 15 28 24 16 8 24 28 28 28 15 ...
## $ number_of_entity_axis_conditions : int 1 1 5 4 3 3 1 3 1 1 ...
## $ entity_condition_1 : chr "11C439" "11J439" "11R628" "11I469" ...
## $ entity_condition_2 : chr "" "" "21I698" "61E119" ...
## $ entity_condition_3 : chr "" "" "61J449" "62I500" ...
## $ entity_condition_4 : chr "" "" "62M199" "63K862" ...
## $ entity_condition_5 : chr "" "" "63R568" "" ...
## $ entity_condition_6 : chr "" "" "" "" ...
## $ entity_condition_7 : chr "" "" "" "" ...
## $ entity_condition_8 : chr "" "" "" "" ...
## $ entity_condition_9 : chr "" "" "" "" ...
## $ entity_condition_10 : chr "" "" "" "" ...
## $ entity_condition_11 : chr "" "" "" "" ...
## $ entity_condition_12 : chr "" "" "" "" ...
## $ entity_condition_13 : chr "" "" "" "" ...
## $ entity_condition_14 : chr "" "" "" "" ...
## $ entity_condition_15 : logi NA NA NA NA NA NA ...
## $ entity_condition_16 : logi NA NA NA NA NA NA ...
## $ entity_condition_17 : logi NA NA NA NA NA NA ...
## $ entity_condition_18 : logi NA NA NA NA NA NA ...
## $ entity_condition_19 : logi NA NA NA NA NA NA ...
## $ entity_condition_20 : logi NA NA NA NA NA NA ...
## $ number_of_record_axis_conditions : int 1 1 5 4 3 3 1 3 1 1 ...
## $ record_condition_1 : chr "C439" "J439" "I698" "E119" ...
## $ record_condition_2 : chr "" "" "J449" "I469" ...
## $ record_condition_3 : chr "" "" "M199" "I500" ...
## $ record_condition_4 : chr "" "" "R568" "K862" ...
## $ record_condition_5 : chr "" "" "R628" "" ...
## $ record_condition_6 : chr "" "" "" "" ...
## $ record_condition_7 : chr "" "" "" "" ...
## $ record_condition_8 : chr "" "" "" "" ...
## $ record_condition_9 : chr "" "" "" "" ...
## $ record_condition_10 : chr "" "" "" "" ...
## $ record_condition_11 : chr "" "" "" "" ...
## $ record_condition_12 : chr "" "" "" "" ...
## $ record_condition_13 : chr "" "" "" "" ...
## $ record_condition_14 : chr "" "" "" "" ...
## $ record_condition_15 : logi NA NA NA NA NA NA ...
## $ record_condition_16 : logi NA NA NA NA NA NA ...
## $ record_condition_17 : logi NA NA NA NA NA NA ...
## $ record_condition_18 : logi NA NA NA NA NA NA ...
## $ record_condition_19 : logi NA NA NA NA NA NA ...
## $ record_condition_20 : logi NA NA NA NA NA NA ...
## $ race : int 1 1 1 1 1 3 1 3 1 1 ...
## $ bridged_race_flag : logi NA NA NA NA NA NA ...
## $ race_imputation_flag : int NA NA NA NA NA NA NA NA NA NA ...
## $ race_recode_3 : int 1 1 1 1 1 2 1 2 1 1 ...
## $ race_recode_5 : int 1 1 1 1 1 3 1 3 1 1 ...
## $ hispanic_origin : int 100 100 100 100 100 100 100 100 100 100 ...
## $ hispanic_originrace_recode : int 6 6 6 6 6 8 6 8 6 6 ...
colSums(is.na(death_df))
## resident_status
## 0
## education_1989_revision
## 0
## education_2003_revision
## 50000
## education_reporting_flag
## 0
## month_of_death
## 0
## sex
## 0
## detail_age_type
## 0
## detail_age
## 0
## age_substitution_flag
## 50000
## age_recode_52
## 0
## age_recode_27
## 0
## age_recode_12
## 0
## infant_age_recode_22
## 49367
## place_of_death_and_decedents_status
## 0
## marital_status
## 0
## day_of_week_of_death
## 0
## current_data_year
## 0
## injury_at_work
## 0
## manner_of_death
## 10352
## method_of_disposition
## 0
## autopsy
## 0
## activity_code
## 46108
## place_of_injury_for_causes_w00_y34_except_y06_and_y07_
## 46918
## icd_code_10th_revision
## 0
## X358_cause_recode
## 0
## X113_cause_recode
## 0
## X130_infant_cause_recode
## 49367
## X39_cause_recode
## 0
## number_of_entity_axis_conditions
## 0
## entity_condition_1
## 0
## entity_condition_2
## 0
## entity_condition_3
## 0
## entity_condition_4
## 0
## entity_condition_5
## 0
## entity_condition_6
## 0
## entity_condition_7
## 0
## entity_condition_8
## 0
## entity_condition_9
## 0
## entity_condition_10
## 0
## entity_condition_11
## 0
## entity_condition_12
## 0
## entity_condition_13
## 0
## entity_condition_14
## 0
## entity_condition_15
## 50000
## entity_condition_16
## 50000
## entity_condition_17
## 50000
## entity_condition_18
## 50000
## entity_condition_19
## 50000
## entity_condition_20
## 50000
## number_of_record_axis_conditions
## 0
## record_condition_1
## 0
## record_condition_2
## 0
## record_condition_3
## 0
## record_condition_4
## 0
## record_condition_5
## 0
## record_condition_6
## 0
## record_condition_7
## 0
## record_condition_8
## 0
## record_condition_9
## 0
## record_condition_10
## 0
## record_condition_11
## 0
## record_condition_12
## 0
## record_condition_13
## 0
## record_condition_14
## 0
## record_condition_15
## 50000
## record_condition_16
## 50000
## record_condition_17
## 50000
## record_condition_18
## 50000
## record_condition_19
## 50000
## record_condition_20
## 50000
## race
## 0
## bridged_race_flag
## 50000
## race_imputation_flag
## 49975
## race_recode_3
## 0
## race_recode_5
## 0
## hispanic_origin
## 0
## hispanic_originrace_recode
## 0
# Step 3: Preprocess data
death_df$Above_Avg_Age <- ifelse(death_df$detail_age > 77.4, 1, 0)
death_df$Above_Avg_Age = as.factor(death_df$Above_Avg_Age)
head(death_df)
## resident_status education_1989_revision education_2003_revision
## 1 1 11 NA
## 2 1 13 NA
## 3 1 12 NA
## 4 1 12 NA
## 5 1 14 NA
## 6 1 3 NA
## education_reporting_flag month_of_death sex detail_age_type detail_age
## 1 0 1 F 1 45
## 2 0 1 M 1 61
## 3 0 1 F 1 79
## 4 0 1 M 1 50
## 5 0 1 F 1 68
## 6 0 1 F 1 89
## age_substitution_flag age_recode_52 age_recode_27 age_recode_12
## 1 NA 35 15 7
## 2 NA 38 18 8
## 3 NA 41 21 10
## 4 NA 36 16 7
## 5 NA 39 19 9
## 6 NA 43 23 11
## infant_age_recode_22 place_of_death_and_decedents_status marital_status
## 1 NA 1 M
## 2 NA 1 D
## 3 NA 6 D
## 4 NA 1 S
## 5 NA 1 M
## 6 NA 6 W
## day_of_week_of_death current_data_year injury_at_work manner_of_death
## 1 2 2005 U 7
## 2 7 2005 U 7
## 3 1 2005 U 7
## 4 4 2005 U 7
## 5 2 2005 U 7
## 6 7 2005 U 7
## method_of_disposition autopsy activity_code
## 1 U N NA
## 2 U N NA
## 3 U N NA
## 4 U N NA
## 5 U N NA
## 6 U N NA
## place_of_injury_for_causes_w00_y34_except_y06_and_y07_ icd_code_10th_revision
## 1 NA C439
## 2 NA J439
## 3 NA I698
## 4 NA E119
## 5 NA C349
## 6 NA I679
## X358_cause_recode X113_cause_recode X130_infant_cause_recode X39_cause_recode
## 1 98 28 NA 15
## 2 266 84 NA 28
## 3 239 70 NA 24
## 4 159 46 NA 16
## 5 93 27 NA 8
## 6 239 70 NA 24
## number_of_entity_axis_conditions entity_condition_1 entity_condition_2
## 1 1 11C439
## 2 1 11J439
## 3 5 11R628 21I698
## 4 4 11I469 61E119
## 5 3 11C349 61F179
## 6 3 11N19 21I679
## entity_condition_3 entity_condition_4 entity_condition_5 entity_condition_6
## 1
## 2
## 3 61J449 62M199 63R568
## 4 62I500 63K862
## 5 62J449
## 6 61F03
## entity_condition_7 entity_condition_8 entity_condition_9 entity_condition_10
## 1
## 2
## 3
## 4
## 5
## 6
## entity_condition_11 entity_condition_12 entity_condition_13
## 1
## 2
## 3
## 4
## 5
## 6
## entity_condition_14 entity_condition_15 entity_condition_16
## 1 NA NA
## 2 NA NA
## 3 NA NA
## 4 NA NA
## 5 NA NA
## 6 NA NA
## entity_condition_17 entity_condition_18 entity_condition_19
## 1 NA NA NA
## 2 NA NA NA
## 3 NA NA NA
## 4 NA NA NA
## 5 NA NA NA
## 6 NA NA NA
## entity_condition_20 number_of_record_axis_conditions record_condition_1
## 1 NA 1 C439
## 2 NA 1 J439
## 3 NA 5 I698
## 4 NA 4 E119
## 5 NA 3 C349
## 6 NA 3 F03
## record_condition_2 record_condition_3 record_condition_4 record_condition_5
## 1
## 2
## 3 J449 M199 R568 R628
## 4 I469 I500 K862
## 5 F179 J449
## 6 I679 N19
## record_condition_6 record_condition_7 record_condition_8 record_condition_9
## 1
## 2
## 3
## 4
## 5
## 6
## record_condition_10 record_condition_11 record_condition_12
## 1
## 2
## 3
## 4
## 5
## 6
## record_condition_13 record_condition_14 record_condition_15
## 1 NA
## 2 NA
## 3 NA
## 4 NA
## 5 NA
## 6 NA
## record_condition_16 record_condition_17 record_condition_18
## 1 NA NA NA
## 2 NA NA NA
## 3 NA NA NA
## 4 NA NA NA
## 5 NA NA NA
## 6 NA NA NA
## record_condition_19 record_condition_20 race bridged_race_flag
## 1 NA NA 1 NA
## 2 NA NA 1 NA
## 3 NA NA 1 NA
## 4 NA NA 1 NA
## 5 NA NA 1 NA
## 6 NA NA 3 NA
## race_imputation_flag race_recode_3 race_recode_5 hispanic_origin
## 1 NA 1 1 100
## 2 NA 1 1 100
## 3 NA 1 1 100
## 4 NA 1 1 100
## 5 NA 1 1 100
## 6 NA 2 3 100
## hispanic_originrace_recode Above_Avg_Age
## 1 6 0
## 2 6 0
## 3 6 1
## 4 6 0
## 5 6 0
## 6 8 1
# Partition the data into training and validation sets using selected predictors.
death_df2 <- death_df[ , c(1,5,7,16,71,78)]
# Later variables
set.seed(1)
train_index <- sample(c(1:dim(death_df2)[1]), dim(death_df2)[1]*0.7)
valid_index <- setdiff(c(1:dim(death_df2)[1]), train_index)
train_df <- death_df2[train_index, ]
valid_df <- death_df2[valid_index, ]
head(train_df)
## resident_status month_of_death detail_age_type day_of_week_of_death race
## 24388 1 6 1 1 1
## 43307 1 11 1 2 1
## 4050 1 1 1 2 1
## 11571 3 3 1 7 1
## 25173 1 6 1 5 1
## 32618 1 8 1 2 1
## Above_Avg_Age
## 24388 1
## 43307 0
## 4050 0
## 11571 0
## 25173 0
## 32618 0
# Logistic model
reg <- glm(Above_Avg_Age ~., data = train_df, family = "binomial")
confusionMatrix(factor(ifelse(predict(reg, valid_df, type = "response")>0.5, 1, 0)),
valid_df$Above_Avg_Age, positive = "1")
## Warning in confusionMatrix.default(factor(ifelse(predict(reg, valid_df, :
## Levels are not in the same order for reference and data. Refactoring data to
## match.
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 8461 6539
## 1 0 0
##
## Accuracy : 0.5641
## 95% CI : (0.5561, 0.572)
## No Information Rate : 0.5641
## P-Value [Acc > NIR] : 0.5034
##
## Kappa : 0
##
## Mcnemar's Test P-Value : <2e-16
##
## Sensitivity : 0.0000
## Specificity : 1.0000
## Pos Pred Value : NaN
## Neg Pred Value : 0.5641
## Prevalence : 0.4359
## Detection Rate : 0.0000
## Detection Prevalence : 0.0000
## Balanced Accuracy : 0.5000
##
## 'Positive' Class : 1
##
# Tree model
tr <- rpart(Above_Avg_Age ~., data = death_df2)
confusionMatrix(factor(ifelse(predict(tr, valid_df)[,2]>0.5, 1, 0)),
valid_df$Above_Avg_Age, positive = "1")
## Warning in confusionMatrix.default(factor(ifelse(predict(tr, valid_df)[, :
## Levels are not in the same order for reference and data. Refactoring data to
## match.
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 8461 6539
## 1 0 0
##
## Accuracy : 0.5641
## 95% CI : (0.5561, 0.572)
## No Information Rate : 0.5641
## P-Value [Acc > NIR] : 0.5034
##
## Kappa : 0
##
## Mcnemar's Test P-Value : <2e-16
##
## Sensitivity : 0.0000
## Specificity : 1.0000
## Pos Pred Value : NaN
## Neg Pred Value : 0.5641
## Prevalence : 0.4359
## Detection Rate : 0.0000
## Detection Prevalence : 0.0000
## Balanced Accuracy : 0.5000
##
## 'Positive' Class : 1
##
res <- data.frame(
ActualClass = valid_df$Above_Avg_Age,
LRProb = predict(reg, valid_df, type = "response"),
LRPred = ifelse(predict(reg, valid_df, type = 'response') > 0.5, 1, 0),
TREEProb = predict(tr, valid_df)[,2],
TREEPred = ifelse(predict(tr, valid_df)[,2] > 0.5, 1, 0)
)
options(digits = 1, scipen = 2)
head(res, 10)
## ActualClass LRProb LRPred TREEProb TREEPred
## 3 1 0.5 0 0.4 0
## 6 1 0.5 0 0.4 0
## 14 0 0.5 0 0.4 0
## 16 0 0.5 0 0.4 0
## 19 0 0.5 0 0.4 0
## 23 0 0.5 0 0.4 0
## 28 0 0.5 0 0.4 0
## 31 1 0.5 0 0.4 0
## 32 0 0.5 0 0.4 0
## 37 0 0.5 0 0.4 0
# Ensemble model
res$majority <- rowMeans(data.frame(res$LRPred,
res$TREEPred))>0.5
res$avg <- rowMeans(data.frame(res$LRProb, res$TREEProb))
head(res)
## ActualClass LRProb LRPred TREEProb TREEPred majority avg
## 3 1 0.5 0 0.4 0 FALSE 0.5
## 6 1 0.5 0 0.4 0 FALSE 0.4
## 14 0 0.5 0 0.4 0 FALSE 0.5
## 16 0 0.5 0 0.4 0 FALSE 0.5
## 19 0 0.5 0 0.4 0 FALSE 0.5
## 23 0 0.5 0 0.4 0 FALSE 0.5
confusionMatrix(factor(res$majority * 1), valid_df[,6], positive = "1")
## Warning in confusionMatrix.default(factor(res$majority * 1), valid_df[, :
## Levels are not in the same order for reference and data. Refactoring data to
## match.
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 8461 6539
## 1 0 0
##
## Accuracy : 0.564
## 95% CI : (0.556, 0.572)
## No Information Rate : 0.564
## P-Value [Acc > NIR] : 0.503
##
## Kappa : 0
##
## Mcnemar's Test P-Value : <2e-16
##
## Sensitivity : 0.000
## Specificity : 1.000
## Pos Pred Value : NaN
## Neg Pred Value : 0.564
## Prevalence : 0.436
## Detection Rate : 0.000
## Detection Prevalence : 0.000
## Balanced Accuracy : 0.500
##
## 'Positive' Class : 1
##
confusionMatrix(factor((res$avg > 0.5)* 1), valid_df[,6], positive = "1")
## Warning in confusionMatrix.default(factor((res$avg > 0.5) * 1), valid_df[, :
## Levels are not in the same order for reference and data. Refactoring data to
## match.
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 8461 6539
## 1 0 0
##
## Accuracy : 0.564
## 95% CI : (0.556, 0.572)
## No Information Rate : 0.564
## P-Value [Acc > NIR] : 0.503
##
## Kappa : 0
##
## Mcnemar's Test P-Value : <2e-16
##
## Sensitivity : 0.000
## Specificity : 1.000
## Pos Pred Value : NaN
## Neg Pred Value : 0.564
## Prevalence : 0.436
## Detection Rate : 0.000
## Detection Prevalence : 0.000
## Balanced Accuracy : 0.500
##
## 'Positive' Class : 1
##