library(readxl)
## Warning: package 'readxl' was built under R version 4.1.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.1.3

Regresión con todos los Datos

Import Data

#file.choose()
claims_data <- read_xlsx("C:\\Users\\jesus\\Documents\\Actividades\\ClaimsData2018.xlsx")
transactions <- read_xlsx("C:\\Users\\jesus\\Documents\\Actividades\\TransactionsSummary2018.xlsx")

Clean Data

claims_data$TotalReserves <- as.numeric(claims_data$TotalReserves)
claims_data$IndemnityPaid <- as.numeric(claims_data$IndemnityPaid)
claims_data$OtherPaid <- as.numeric(claims_data$OtherPaid)
claims_data$TotalRecovery <- as.numeric(claims_data$TotalRecovery)
claims_data$ClaimantOpenedDate <- as.Date(claims_data$ClaimantOpenedDate, format = "%Y/%m/%d")
claims_data$ClaimantClosedDate <- as.Date(claims_data$ClaimantClosedDate, format = "%Y/%m/%d")
claims_data$ClaimantAge_at_DOI <- as.numeric(claims_data$ClaimantAge_at_DOI)
## Warning: NAs introducidos por coerción
claims_data$AverageWeeklyWage <- as.numeric(claims_data$AverageWeeklyWage)
## Warning: NAs introducidos por coerción

Merge

df <- merge(claims_data, transactions, by="ClaimID", all = TRUE)

##Analysis

Create Total Incurred Cost per claim

df2 <- df
#summary(df2)
df2$TotalIncurredCost <- with(df2, TotalReserves + IndemnityPaid + OtherPaid - TotalRecovery)

Obtain the difference between closed and open dates to obtain the Claim Duration

df2$ClaimDuration  <- as.numeric(difftime(df2$ClaimantClosedDate, df2$ClaimantOpenedDate, units = c("days")))
#df2$ClaimDuration <- seq(from = df2$ClaimantOpenedDate, to = df2$ClaimantClosedDate, by = "day")
#summary(df2$ClaimDuration)

Multiple Regression

df2.lm <- lm(TotalIncurredCost ~ ClaimStatus + ClaimantAge_at_DOI + Gender + AverageWeeklyWage + BodyPartRegion + BodyPart + InjuryNature, data = df2)
summary(df2.lm)
## 
## Call:
## lm(formula = TotalIncurredCost ~ ClaimStatus + ClaimantAge_at_DOI + 
##     Gender + AverageWeeklyWage + BodyPartRegion + BodyPart + 
##     InjuryNature, data = df2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -289205  -16987   -3489    5766 4757653 
## 
## Coefficients: (7 not defined because of singularities)
##                                                                           Estimate
## (Intercept)                                                             -1.766e+04
## ClaimStatusO                                                             1.650e+05
## ClaimStatusR                                                             1.754e+05
## ClaimantAge_at_DOI                                                       4.046e+00
## GenderMale                                                               2.605e+03
## GenderNot Available                                                      2.422e+04
## AverageWeeklyWage                                                        2.618e-03
## BodyPartRegionLower Extremities                                          1.458e+04
## BodyPartRegionMultiple Body Parts                                        5.952e+04
## BodyPartRegionNeck                                                       4.562e+04
## BodyPartRegionNon-Standard Code                                          6.831e+03
## BodyPartRegionNot Available                                              2.438e+04
## BodyPartRegionTrunk                                                      6.148e+03
## BodyPartRegionUpper Extremities                                          1.420e+04
## BodyPartAnkle                                                           -1.337e+03
## BodyPartArtificial Appliance                                            -4.247e+04
## BodyPartBody Systems and Multiple Body Systems                          -4.695e+04
## BodyPartBrain                                                            7.347e+04
## BodyPartButtocks                                                        -2.985e+02
## BodyPartChest                                                           -3.626e+02
## BodyPartDisc-Trunk                                                       3.403e+04
## BodyPartEar(S)                                                           1.445e+03
## BodyPartElbow                                                           -4.576e+03
## BodyPartEyes                                                             5.443e+03
## BodyPartFacial Bones                                                     1.300e+04
## BodyPartFinger(S)                                                       -1.013e+04
## BodyPartFoot                                                            -6.831e+03
## BodyPartGreat Toe                                                       -1.018e+04
## BodyPartHand                                                            -9.000e+03
## BodyPartHeart                                                            4.469e+03
## BodyPartHip                                                              4.306e+03
## BodyPartInsufficient Info to Properly Identify?Unclassified             -4.100e+04
## BodyPartInternal Organs                                                  1.903e+04
## BodyPartKnee                                                             4.109e+03
## BodyPartLarynx                                                          -3.683e+04
## BodyPartLower Arm                                                       -7.120e+03
## BodyPartLower Back Area                                                  3.467e+04
## BodyPartLower Leg                                                        8.986e+02
## BodyPartLumbar and/or Sacral Vertebrae (Vertebra NOC Trunk)              3.072e+04
## BodyPartLungs                                                           -1.036e+03
## BodyPartMouth                                                            9.086e+03
## BodyPartMultiple Body Parts (Including Body Systems and Body Parts)     -2.590e+04
## BodyPartMultiple Head Injury                                             2.008e+04
## BodyPartMultiple Lower Extremities                                       1.203e+04
## BodyPartMultiple Neck Injury                                            -1.754e+04
## BodyPartMultiple Trunk                                                   1.723e+04
## BodyPartMultiple Upper Extremities                                       9.815e+03
## BodyPartNo Physical Injury                                              -4.008e+04
## BodyPartNon-Standard Code                                                       NA
## BodyPartNose                                                            -1.718e+03
## BodyPartNot Available                                                           NA
## BodyPartPelvis                                                           3.667e+03
## BodyPartSacrum And Coccyx                                                2.023e+04
## BodyPartShoulder(S)                                                      7.246e+03
## BodyPartSkull                                                            4.599e+03
## BodyPartSoft Tissue-Head                                                -2.094e+04
## BodyPartSoft Tissue-Neck                                                -5.241e+04
## BodyPartSpinal Cord-Trunk                                                1.404e+05
## BodyPartTeeth                                                                   NA
## BodyPartThumb                                                           -7.328e+03
## BodyPartToes                                                            -9.862e+03
## BodyPartTrachea                                                         -3.939e+04
## BodyPartUpper Arm                                                        5.325e+03
## BodyPartUpper Back Area                                                  2.012e+04
## BodyPartUpper Leg                                                               NA
## BodyPartVertebrae                                                               NA
## BodyPartWhole Body                                                              NA
## BodyPartWrist                                                                   NA
## InjuryNatureAll Other Cumulative Injury, NOC                            -1.911e+04
## InjuryNatureAll Other Occupational Disease Injury, NOC                   1.069e+05
## InjuryNatureAll Other Specific Injuries, Noc                             1.434e+04
## InjuryNatureAmputation                                                   2.370e+04
## InjuryNatureAngina Pectoris                                              9.385e+03
## InjuryNatureAsbestosis                                                   1.830e+04
## InjuryNatureAsphyxiation                                                 3.520e+04
## InjuryNatureBurn                                                         1.256e+04
## InjuryNatureCancer                                                       2.455e+04
## InjuryNatureCarpal Tunnel Syndrome                                       1.035e+04
## InjuryNatureConcussion                                                   3.092e+04
## InjuryNatureContagious Disease                                           1.683e+04
## InjuryNatureContusion                                                    1.007e+04
## InjuryNatureCrushing                                                     6.976e+03
## InjuryNatureDermatitis                                                  -3.936e+03
## InjuryNatureDislocation                                                  4.992e+03
## InjuryNatureDust Disease, NOC                                           -1.884e+03
## InjuryNatureElectric Shock                                               4.657e+03
## InjuryNatureForeign Body                                                 6.205e+03
## InjuryNatureFracture                                                     1.778e+04
## InjuryNatureFreezing                                                    -1.010e+04
## InjuryNatureHearing Loss Or Impairment                                   1.282e+04
## InjuryNatureHeat Prostration                                            -7.442e+03
## InjuryNatureHernia                                                       1.008e+04
## InjuryNatureInfection                                                   -4.112e+03
## InjuryNatureInflammation                                                 9.181e+03
## InjuryNatureLaceration                                                   7.542e+03
## InjuryNatureLoss of Hearing                                              1.831e+04
## InjuryNatureMental Disorder                                              9.832e+03
## InjuryNatureMental Stress                                                2.095e+04
## InjuryNatureMultiple Injuries Including Both Physical and Psychological  1.056e+05
## InjuryNatureMultiple Physical Injuries Only                              3.242e+03
## InjuryNatureMyocardial Infarction                                        8.407e+03
## InjuryNatureNo Physical Injury                                           4.773e+02
## InjuryNatureNon-Standard Code                                           -9.568e+03
## InjuryNatureNot Available                                                3.816e+03
## InjuryNaturePoisoning?Chemical (Other Than Metals)                       2.088e+02
## InjuryNaturePoisoning?General (NOT OD or Cumulative Injury)             -1.108e+04
## InjuryNaturePuncture                                                     8.092e+03
## InjuryNatureRadiation                                                    3.064e+04
## InjuryNatureRespiratory Disorders                                        1.329e+04
## InjuryNatureRupture                                                      1.216e+04
## InjuryNatureSeverance                                                    5.789e+04
## InjuryNatureSilicosis                                                   -7.988e+02
## InjuryNatureSprain                                                       4.469e+02
## InjuryNatureStrain                                                       3.497e+03
## InjuryNatureSyncope                                                      4.440e+03
## InjuryNatureVascular                                                     5.007e+03
## InjuryNatureVDT-Related Disease                                          6.596e+03
## InjuryNatureVision Loss                                                  1.075e+04
##                                                                         Std. Error
## (Intercept)                                                              5.532e+04
## ClaimStatusO                                                             1.874e+03
## ClaimStatusR                                                             3.334e+03
## ClaimantAge_at_DOI                                                       5.594e+00
## GenderMale                                                               9.881e+02
## GenderNot Available                                                      5.941e+03
## AverageWeeklyWage                                                        3.835e-02
## BodyPartRegionLower Extremities                                          1.082e+04
## BodyPartRegionMultiple Body Parts                                        1.580e+04
## BodyPartRegionNeck                                                       2.777e+04
## BodyPartRegionNon-Standard Code                                          1.134e+04
## BodyPartRegionNot Available                                              9.506e+04
## BodyPartRegionTrunk                                                      1.036e+04
## BodyPartRegionUpper Extremities                                          9.546e+03
## BodyPartAnkle                                                            6.092e+03
## BodyPartArtificial Appliance                                             6.798e+04
## BodyPartBody Systems and Multiple Body Systems                           1.465e+04
## BodyPartBrain                                                            1.948e+04
## BodyPartButtocks                                                         8.420e+03
## BodyPartChest                                                            5.958e+03
## BodyPartDisc-Trunk                                                       7.543e+03
## BodyPartEar(S)                                                           1.292e+04
## BodyPartElbow                                                            4.333e+03
## BodyPartEyes                                                             9.955e+03
## BodyPartFacial Bones                                                     1.105e+04
## BodyPartFinger(S)                                                        3.327e+03
## BodyPartFoot                                                             6.265e+03
## BodyPartGreat Toe                                                        1.228e+04
## BodyPartHand                                                             3.232e+03
## BodyPartHeart                                                            2.306e+04
## BodyPartHip                                                              6.985e+03
## BodyPartInsufficient Info to Properly Identify?Unclassified              1.433e+04
## BodyPartInternal Organs                                                  1.117e+04
## BodyPartKnee                                                             5.816e+03
## BodyPartLarynx                                                           3.872e+04
## BodyPartLower Arm                                                        4.063e+03
## BodyPartLower Back Area                                                  4.894e+03
## BodyPartLower Leg                                                        6.471e+03
## BodyPartLumbar and/or Sacral Vertebrae (Vertebra NOC Trunk)              6.323e+03
## BodyPartLungs                                                            9.280e+03
## BodyPartMouth                                                            1.204e+04
## BodyPartMultiple Body Parts (Including Body Systems and Body Parts)      1.291e+04
## BodyPartMultiple Head Injury                                             9.846e+03
## BodyPartMultiple Lower Extremities                                       7.644e+03
## BodyPartMultiple Neck Injury                                             2.644e+04
## BodyPartMultiple Trunk                                                   1.174e+04
## BodyPartMultiple Upper Extremities                                       5.474e+03
## BodyPartNo Physical Injury                                               1.405e+04
## BodyPartNon-Standard Code                                                       NA
## BodyPartNose                                                             1.192e+04
## BodyPartNot Available                                                           NA
## BodyPartPelvis                                                           1.190e+04
## BodyPartSacrum And Coccyx                                                1.787e+04
## BodyPartShoulder(S)                                                      3.136e+03
## BodyPartSkull                                                            1.141e+04
## BodyPartSoft Tissue-Head                                                 1.049e+04
## BodyPartSoft Tissue-Neck                                                 2.633e+04
## BodyPartSpinal Cord-Trunk                                                1.455e+04
## BodyPartTeeth                                                                   NA
## BodyPartThumb                                                            4.580e+03
## BodyPartToes                                                             8.960e+03
## BodyPartTrachea                                                          4.671e+04
## BodyPartUpper Arm                                                        4.733e+03
## BodyPartUpper Back Area                                                  6.883e+03
## BodyPartUpper Leg                                                               NA
## BodyPartVertebrae                                                               NA
## BodyPartWhole Body                                                              NA
## BodyPartWrist                                                                   NA
## InjuryNatureAll Other Cumulative Injury, NOC                             5.704e+04
## InjuryNatureAll Other Occupational Disease Injury, NOC                   6.401e+04
## InjuryNatureAll Other Specific Injuries, Noc                             5.458e+04
## InjuryNatureAmputation                                                   5.684e+04
## InjuryNatureAngina Pectoris                                              5.934e+04
## InjuryNatureAsbestosis                                                   5.699e+04
## InjuryNatureAsphyxiation                                                 6.896e+04
## InjuryNatureBurn                                                         5.475e+04
## InjuryNatureCancer                                                       7.220e+04
## InjuryNatureCarpal Tunnel Syndrome                                       5.478e+04
## InjuryNatureConcussion                                                   5.493e+04
## InjuryNatureContagious Disease                                           5.535e+04
## InjuryNatureContusion                                                    5.455e+04
## InjuryNatureCrushing                                                     5.486e+04
## InjuryNatureDermatitis                                                   5.469e+04
## InjuryNatureDislocation                                                  5.486e+04
## InjuryNatureDust Disease, NOC                                            6.011e+04
## InjuryNatureElectric Shock                                               5.598e+04
## InjuryNatureForeign Body                                                 5.476e+04
## InjuryNatureFracture                                                     5.459e+04
## InjuryNatureFreezing                                                     7.713e+04
## InjuryNatureHearing Loss Or Impairment                                   5.899e+04
## InjuryNatureHeat Prostration                                             5.544e+04
## InjuryNatureHernia                                                       5.527e+04
## InjuryNatureInfection                                                    5.501e+04
## InjuryNatureInflammation                                                 5.467e+04
## InjuryNatureLaceration                                                   5.457e+04
## InjuryNatureLoss of Hearing                                              5.917e+04
## InjuryNatureMental Disorder                                              6.532e+04
## InjuryNatureMental Stress                                                5.511e+04
## InjuryNatureMultiple Injuries Including Both Physical and Psychological  5.619e+04
## InjuryNatureMultiple Physical Injuries Only                              5.460e+04
## InjuryNatureMyocardial Infarction                                        6.147e+04
## InjuryNatureNo Physical Injury                                           5.468e+04
## InjuryNatureNon-Standard Code                                            5.492e+04
## InjuryNatureNot Available                                                5.508e+04
## InjuryNaturePoisoning?Chemical (Other Than Metals)                       5.568e+04
## InjuryNaturePoisoning?General (NOT OD or Cumulative Injury)              5.847e+04
## InjuryNaturePuncture                                                     5.462e+04
## InjuryNatureRadiation                                                    6.681e+04
## InjuryNatureRespiratory Disorders                                        5.490e+04
## InjuryNatureRupture                                                      5.523e+04
## InjuryNatureSeverance                                                    6.097e+04
## InjuryNatureSilicosis                                                    7.712e+04
## InjuryNatureSprain                                                       5.456e+04
## InjuryNatureStrain                                                       5.455e+04
## InjuryNatureSyncope                                                      5.562e+04
## InjuryNatureVascular                                                     6.681e+04
## InjuryNatureVDT-Related Disease                                          8.622e+04
## InjuryNatureVision Loss                                                  5.925e+04
##                                                                         t value
## (Intercept)                                                              -0.319
## ClaimStatusO                                                             88.042
## ClaimStatusR                                                             52.623
## ClaimantAge_at_DOI                                                        0.723
## GenderMale                                                                2.636
## GenderNot Available                                                       4.077
## AverageWeeklyWage                                                         0.068
## BodyPartRegionLower Extremities                                           1.348
## BodyPartRegionMultiple Body Parts                                         3.768
## BodyPartRegionNeck                                                        1.643
## BodyPartRegionNon-Standard Code                                           0.602
## BodyPartRegionNot Available                                               0.256
## BodyPartRegionTrunk                                                       0.593
## BodyPartRegionUpper Extremities                                           1.487
## BodyPartAnkle                                                            -0.219
## BodyPartArtificial Appliance                                             -0.625
## BodyPartBody Systems and Multiple Body Systems                           -3.204
## BodyPartBrain                                                             3.771
## BodyPartButtocks                                                         -0.035
## BodyPartChest                                                            -0.061
## BodyPartDisc-Trunk                                                        4.511
## BodyPartEar(S)                                                            0.112
## BodyPartElbow                                                            -1.056
## BodyPartEyes                                                              0.547
## BodyPartFacial Bones                                                      1.177
## BodyPartFinger(S)                                                        -3.045
## BodyPartFoot                                                             -1.090
## BodyPartGreat Toe                                                        -0.829
## BodyPartHand                                                             -2.785
## BodyPartHeart                                                             0.194
## BodyPartHip                                                               0.616
## BodyPartInsufficient Info to Properly Identify?Unclassified              -2.861
## BodyPartInternal Organs                                                   1.703
## BodyPartKnee                                                              0.707
## BodyPartLarynx                                                           -0.951
## BodyPartLower Arm                                                        -1.753
## BodyPartLower Back Area                                                   7.085
## BodyPartLower Leg                                                         0.139
## BodyPartLumbar and/or Sacral Vertebrae (Vertebra NOC Trunk)               4.859
## BodyPartLungs                                                            -0.112
## BodyPartMouth                                                             0.755
## BodyPartMultiple Body Parts (Including Body Systems and Body Parts)      -2.006
## BodyPartMultiple Head Injury                                              2.040
## BodyPartMultiple Lower Extremities                                        1.574
## BodyPartMultiple Neck Injury                                             -0.664
## BodyPartMultiple Trunk                                                    1.468
## BodyPartMultiple Upper Extremities                                        1.793
## BodyPartNo Physical Injury                                               -2.852
## BodyPartNon-Standard Code                                                    NA
## BodyPartNose                                                             -0.144
## BodyPartNot Available                                                        NA
## BodyPartPelvis                                                            0.308
## BodyPartSacrum And Coccyx                                                 1.132
## BodyPartShoulder(S)                                                       2.311
## BodyPartSkull                                                             0.403
## BodyPartSoft Tissue-Head                                                 -1.995
## BodyPartSoft Tissue-Neck                                                 -1.990
## BodyPartSpinal Cord-Trunk                                                 9.652
## BodyPartTeeth                                                                NA
## BodyPartThumb                                                            -1.600
## BodyPartToes                                                             -1.101
## BodyPartTrachea                                                          -0.843
## BodyPartUpper Arm                                                         1.125
## BodyPartUpper Back Area                                                   2.923
## BodyPartUpper Leg                                                            NA
## BodyPartVertebrae                                                            NA
## BodyPartWhole Body                                                           NA
## BodyPartWrist                                                                NA
## InjuryNatureAll Other Cumulative Injury, NOC                             -0.335
## InjuryNatureAll Other Occupational Disease Injury, NOC                    1.670
## InjuryNatureAll Other Specific Injuries, Noc                              0.263
## InjuryNatureAmputation                                                    0.417
## InjuryNatureAngina Pectoris                                               0.158
## InjuryNatureAsbestosis                                                    0.321
## InjuryNatureAsphyxiation                                                  0.510
## InjuryNatureBurn                                                          0.229
## InjuryNatureCancer                                                        0.340
## InjuryNatureCarpal Tunnel Syndrome                                        0.189
## InjuryNatureConcussion                                                    0.563
## InjuryNatureContagious Disease                                            0.304
## InjuryNatureContusion                                                     0.185
## InjuryNatureCrushing                                                      0.127
## InjuryNatureDermatitis                                                   -0.072
## InjuryNatureDislocation                                                   0.091
## InjuryNatureDust Disease, NOC                                            -0.031
## InjuryNatureElectric Shock                                                0.083
## InjuryNatureForeign Body                                                  0.113
## InjuryNatureFracture                                                      0.326
## InjuryNatureFreezing                                                     -0.131
## InjuryNatureHearing Loss Or Impairment                                    0.217
## InjuryNatureHeat Prostration                                             -0.134
## InjuryNatureHernia                                                        0.182
## InjuryNatureInfection                                                    -0.075
## InjuryNatureInflammation                                                  0.168
## InjuryNatureLaceration                                                    0.138
## InjuryNatureLoss of Hearing                                               0.309
## InjuryNatureMental Disorder                                               0.151
## InjuryNatureMental Stress                                                 0.380
## InjuryNatureMultiple Injuries Including Both Physical and Psychological   1.880
## InjuryNatureMultiple Physical Injuries Only                               0.059
## InjuryNatureMyocardial Infarction                                         0.137
## InjuryNatureNo Physical Injury                                            0.009
## InjuryNatureNon-Standard Code                                            -0.174
## InjuryNatureNot Available                                                 0.069
## InjuryNaturePoisoning?Chemical (Other Than Metals)                        0.004
## InjuryNaturePoisoning?General (NOT OD or Cumulative Injury)              -0.190
## InjuryNaturePuncture                                                      0.148
## InjuryNatureRadiation                                                     0.459
## InjuryNatureRespiratory Disorders                                         0.242
## InjuryNatureRupture                                                       0.220
## InjuryNatureSeverance                                                     0.950
## InjuryNatureSilicosis                                                    -0.010
## InjuryNatureSprain                                                        0.008
## InjuryNatureStrain                                                        0.064
## InjuryNatureSyncope                                                       0.080
## InjuryNatureVascular                                                      0.075
## InjuryNatureVDT-Related Disease                                           0.076
## InjuryNatureVision Loss                                                   0.182
##                                                                         Pr(>|t|)
## (Intercept)                                                             0.749540
## ClaimStatusO                                                             < 2e-16
## ClaimStatusR                                                             < 2e-16
## ClaimantAge_at_DOI                                                      0.469599
## GenderMale                                                              0.008391
## GenderNot Available                                                     4.58e-05
## AverageWeeklyWage                                                       0.945567
## BodyPartRegionLower Extremities                                         0.177755
## BodyPartRegionMultiple Body Parts                                       0.000165
## BodyPartRegionNeck                                                      0.100442
## BodyPartRegionNon-Standard Code                                         0.546850
## BodyPartRegionNot Available                                             0.797603
## BodyPartRegionTrunk                                                     0.552907
## BodyPartRegionUpper Extremities                                         0.136934
## BodyPartAnkle                                                           0.826310
## BodyPartArtificial Appliance                                            0.532099
## BodyPartBody Systems and Multiple Body Systems                          0.001355
## BodyPartBrain                                                           0.000163
## BodyPartButtocks                                                        0.971725
## BodyPartChest                                                           0.951476
## BodyPartDisc-Trunk                                                      6.46e-06
## BodyPartEar(S)                                                          0.910929
## BodyPartElbow                                                           0.290868
## BodyPartEyes                                                            0.584517
## BodyPartFacial Bones                                                    0.239139
## BodyPartFinger(S)                                                       0.002332
## BodyPartFoot                                                            0.275565
## BodyPartGreat Toe                                                       0.407044
## BodyPartHand                                                            0.005362
## BodyPartHeart                                                           0.846339
## BodyPartHip                                                             0.537572
## BodyPartInsufficient Info to Properly Identify?Unclassified             0.004225
## BodyPartInternal Organs                                                 0.088511
## BodyPartKnee                                                            0.479840
## BodyPartLarynx                                                          0.341562
## BodyPartLower Arm                                                       0.079672
## BodyPartLower Back Area                                                 1.42e-12
## BodyPartLower Leg                                                       0.889560
## BodyPartLumbar and/or Sacral Vertebrae (Vertebra NOC Trunk)             1.18e-06
## BodyPartLungs                                                           0.911069
## BodyPartMouth                                                           0.450487
## BodyPartMultiple Body Parts (Including Body Systems and Body Parts)     0.044873
## BodyPartMultiple Head Injury                                            0.041391
## BodyPartMultiple Lower Extremities                                      0.115405
## BodyPartMultiple Neck Injury                                            0.507006
## BodyPartMultiple Trunk                                                  0.142101
## BodyPartMultiple Upper Extremities                                      0.072988
## BodyPartNo Physical Injury                                              0.004345
## BodyPartNon-Standard Code                                                     NA
## BodyPartNose                                                            0.885414
## BodyPartNot Available                                                         NA
## BodyPartPelvis                                                          0.758063
## BodyPartSacrum And Coccyx                                               0.257593
## BodyPartShoulder(S)                                                     0.020855
## BodyPartSkull                                                           0.686873
## BodyPartSoft Tissue-Head                                                0.045997
## BodyPartSoft Tissue-Neck                                                0.046570
## BodyPartSpinal Cord-Trunk                                                < 2e-16
## BodyPartTeeth                                                                 NA
## BodyPartThumb                                                           0.109607
## BodyPartToes                                                            0.271051
## BodyPartTrachea                                                         0.399162
## BodyPartUpper Arm                                                       0.260544
## BodyPartUpper Back Area                                                 0.003466
## BodyPartUpper Leg                                                             NA
## BodyPartVertebrae                                                             NA
## BodyPartWhole Body                                                            NA
## BodyPartWrist                                                                 NA
## InjuryNatureAll Other Cumulative Injury, NOC                            0.737666
## InjuryNatureAll Other Occupational Disease Injury, NOC                  0.094923
## InjuryNatureAll Other Specific Injuries, Noc                            0.792729
## InjuryNatureAmputation                                                  0.676655
## InjuryNatureAngina Pectoris                                             0.874325
## InjuryNatureAsbestosis                                                  0.748108
## InjuryNatureAsphyxiation                                                0.609757
## InjuryNatureBurn                                                        0.818582
## InjuryNatureCancer                                                      0.733794
## InjuryNatureCarpal Tunnel Syndrome                                      0.850073
## InjuryNatureConcussion                                                  0.573506
## InjuryNatureContagious Disease                                          0.761027
## InjuryNatureContusion                                                   0.853542
## InjuryNatureCrushing                                                    0.898810
## InjuryNatureDermatitis                                                  0.942629
## InjuryNatureDislocation                                                 0.927506
## InjuryNatureDust Disease, NOC                                           0.974993
## InjuryNatureElectric Shock                                              0.933699
## InjuryNatureForeign Body                                                0.909779
## InjuryNatureFracture                                                    0.744607
## InjuryNatureFreezing                                                    0.895808
## InjuryNatureHearing Loss Or Impairment                                  0.827956
## InjuryNatureHeat Prostration                                            0.893215
## InjuryNatureHernia                                                      0.855272
## InjuryNatureInfection                                                   0.940411
## InjuryNatureInflammation                                                0.866624
## InjuryNatureLaceration                                                  0.890093
## InjuryNatureLoss of Hearing                                             0.757051
## InjuryNatureMental Disorder                                             0.880357
## InjuryNatureMental Stress                                               0.703833
## InjuryNatureMultiple Injuries Including Both Physical and Psychological 0.060153
## InjuryNatureMultiple Physical Injuries Only                             0.952654
## InjuryNatureMyocardial Infarction                                       0.891225
## InjuryNatureNo Physical Injury                                          0.993035
## InjuryNatureNon-Standard Code                                           0.861708
## InjuryNatureNot Available                                               0.944763
## InjuryNaturePoisoning?Chemical (Other Than Metals)                      0.997008
## InjuryNaturePoisoning?General (NOT OD or Cumulative Injury)             0.849692
## InjuryNaturePuncture                                                    0.882230
## InjuryNatureRadiation                                                   0.646486
## InjuryNatureRespiratory Disorders                                       0.808660
## InjuryNatureRupture                                                     0.825795
## InjuryNatureSeverance                                                   0.342350
## InjuryNatureSilicosis                                                   0.991737
## InjuryNatureSprain                                                      0.993465
## InjuryNatureStrain                                                      0.948886
## InjuryNatureSyncope                                                     0.936375
## InjuryNatureVascular                                                    0.940265
## InjuryNatureVDT-Related Disease                                         0.939023
## InjuryNatureVision Loss                                                 0.855965
##                                                                            
## (Intercept)                                                                
## ClaimStatusO                                                            ***
## ClaimStatusR                                                            ***
## ClaimantAge_at_DOI                                                         
## GenderMale                                                              ** 
## GenderNot Available                                                     ***
## AverageWeeklyWage                                                          
## BodyPartRegionLower Extremities                                            
## BodyPartRegionMultiple Body Parts                                       ***
## BodyPartRegionNeck                                                         
## BodyPartRegionNon-Standard Code                                            
## BodyPartRegionNot Available                                                
## BodyPartRegionTrunk                                                        
## BodyPartRegionUpper Extremities                                            
## BodyPartAnkle                                                              
## BodyPartArtificial Appliance                                               
## BodyPartBody Systems and Multiple Body Systems                          ** 
## BodyPartBrain                                                           ***
## BodyPartButtocks                                                           
## BodyPartChest                                                              
## BodyPartDisc-Trunk                                                      ***
## BodyPartEar(S)                                                             
## BodyPartElbow                                                              
## BodyPartEyes                                                               
## BodyPartFacial Bones                                                       
## BodyPartFinger(S)                                                       ** 
## BodyPartFoot                                                               
## BodyPartGreat Toe                                                          
## BodyPartHand                                                            ** 
## BodyPartHeart                                                              
## BodyPartHip                                                                
## BodyPartInsufficient Info to Properly Identify?Unclassified             ** 
## BodyPartInternal Organs                                                 .  
## BodyPartKnee                                                               
## BodyPartLarynx                                                             
## BodyPartLower Arm                                                       .  
## BodyPartLower Back Area                                                 ***
## BodyPartLower Leg                                                          
## BodyPartLumbar and/or Sacral Vertebrae (Vertebra NOC Trunk)             ***
## BodyPartLungs                                                              
## BodyPartMouth                                                              
## BodyPartMultiple Body Parts (Including Body Systems and Body Parts)     *  
## BodyPartMultiple Head Injury                                            *  
## BodyPartMultiple Lower Extremities                                         
## BodyPartMultiple Neck Injury                                               
## BodyPartMultiple Trunk                                                     
## BodyPartMultiple Upper Extremities                                      .  
## BodyPartNo Physical Injury                                              ** 
## BodyPartNon-Standard Code                                                  
## BodyPartNose                                                               
## BodyPartNot Available                                                      
## BodyPartPelvis                                                             
## BodyPartSacrum And Coccyx                                                  
## BodyPartShoulder(S)                                                     *  
## BodyPartSkull                                                              
## BodyPartSoft Tissue-Head                                                *  
## BodyPartSoft Tissue-Neck                                                *  
## BodyPartSpinal Cord-Trunk                                               ***
## BodyPartTeeth                                                              
## BodyPartThumb                                                              
## BodyPartToes                                                               
## BodyPartTrachea                                                            
## BodyPartUpper Arm                                                          
## BodyPartUpper Back Area                                                 ** 
## BodyPartUpper Leg                                                          
## BodyPartVertebrae                                                          
## BodyPartWhole Body                                                         
## BodyPartWrist                                                              
## InjuryNatureAll Other Cumulative Injury, NOC                               
## InjuryNatureAll Other Occupational Disease Injury, NOC                  .  
## InjuryNatureAll Other Specific Injuries, Noc                               
## InjuryNatureAmputation                                                     
## InjuryNatureAngina Pectoris                                                
## InjuryNatureAsbestosis                                                     
## InjuryNatureAsphyxiation                                                   
## InjuryNatureBurn                                                           
## InjuryNatureCancer                                                         
## InjuryNatureCarpal Tunnel Syndrome                                         
## InjuryNatureConcussion                                                     
## InjuryNatureContagious Disease                                             
## InjuryNatureContusion                                                      
## InjuryNatureCrushing                                                       
## InjuryNatureDermatitis                                                     
## InjuryNatureDislocation                                                    
## InjuryNatureDust Disease, NOC                                              
## InjuryNatureElectric Shock                                                 
## InjuryNatureForeign Body                                                   
## InjuryNatureFracture                                                       
## InjuryNatureFreezing                                                       
## InjuryNatureHearing Loss Or Impairment                                     
## InjuryNatureHeat Prostration                                               
## InjuryNatureHernia                                                         
## InjuryNatureInfection                                                      
## InjuryNatureInflammation                                                   
## InjuryNatureLaceration                                                     
## InjuryNatureLoss of Hearing                                                
## InjuryNatureMental Disorder                                                
## InjuryNatureMental Stress                                                  
## InjuryNatureMultiple Injuries Including Both Physical and Psychological .  
## InjuryNatureMultiple Physical Injuries Only                                
## InjuryNatureMyocardial Infarction                                          
## InjuryNatureNo Physical Injury                                             
## InjuryNatureNon-Standard Code                                              
## InjuryNatureNot Available                                                  
## InjuryNaturePoisoning?Chemical (Other Than Metals)                         
## InjuryNaturePoisoning?General (NOT OD or Cumulative Injury)                
## InjuryNaturePuncture                                                       
## InjuryNatureRadiation                                                      
## InjuryNatureRespiratory Disorders                                          
## InjuryNatureRupture                                                        
## InjuryNatureSeverance                                                      
## InjuryNatureSilicosis                                                      
## InjuryNatureSprain                                                         
## InjuryNatureStrain                                                         
## InjuryNatureSyncope                                                        
## InjuryNatureVascular                                                       
## InjuryNatureVDT-Related Disease                                            
## InjuryNatureVision Loss                                                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 94380 on 38269 degrees of freedom
##   (148297 observations deleted due to missingness)
## Multiple R-squared:  0.2321, Adjusted R-squared:  0.2299 
## F-statistic: 105.2 on 110 and 38269 DF,  p-value: < 2.2e-16
par(mfrow=c(2,2))
plot(df2.lm)
## Warning in sqrt(crit * p * (1 - hh)/hh): Se han producido NaNs

## Warning in sqrt(crit * p * (1 - hh)/hh): Se han producido NaNs

Regresión con merge all FALSE

library(readxl)
library(dplyr)

Regresión

Analysis

Create Total Incurred Cost per claim

df2 <- df
#summary(df2)
df2$TotalIncurredCost <- with(df2, TotalReserves + IndemnityPaid + OtherPaid - TotalRecovery)

Obtain the difference between closed and open dates to obtain the Claim Duration

df2$ClaimDuration  <- as.numeric(difftime(df2$ClaimantClosedDate, df2$ClaimantOpenedDate, units = c("days")))
#df2$ClaimDuration <- seq(from = df2$ClaimantOpenedDate, to = df2$ClaimantClosedDate, by = "day")
#summary(df2$ClaimDuration)

Multiple Regression

df2.lm <- lm(TotalIncurredCost ~ ClaimStatus + ClaimantAge_at_DOI + Gender + AverageWeeklyWage + BodyPartRegion + BodyPart + InjuryNature, data = df2)
summary(df2.lm)
## 
## Call:
## lm(formula = TotalIncurredCost ~ ClaimStatus + ClaimantAge_at_DOI + 
##     Gender + AverageWeeklyWage + BodyPartRegion + BodyPart + 
##     InjuryNature, data = df2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -289205  -16987   -3489    5766 4757653 
## 
## Coefficients: (7 not defined because of singularities)
##                                                                           Estimate
## (Intercept)                                                             -1.766e+04
## ClaimStatusO                                                             1.650e+05
## ClaimStatusR                                                             1.754e+05
## ClaimantAge_at_DOI                                                       4.046e+00
## GenderMale                                                               2.605e+03
## GenderNot Available                                                      2.422e+04
## AverageWeeklyWage                                                        2.618e-03
## BodyPartRegionLower Extremities                                          1.458e+04
## BodyPartRegionMultiple Body Parts                                        5.952e+04
## BodyPartRegionNeck                                                       4.562e+04
## BodyPartRegionNon-Standard Code                                          6.831e+03
## BodyPartRegionNot Available                                              2.438e+04
## BodyPartRegionTrunk                                                      6.148e+03
## BodyPartRegionUpper Extremities                                          1.420e+04
## BodyPartAnkle                                                           -1.337e+03
## BodyPartArtificial Appliance                                            -4.247e+04
## BodyPartBody Systems and Multiple Body Systems                          -4.695e+04
## BodyPartBrain                                                            7.347e+04
## BodyPartButtocks                                                        -2.985e+02
## BodyPartChest                                                           -3.626e+02
## BodyPartDisc-Trunk                                                       3.403e+04
## BodyPartEar(S)                                                           1.445e+03
## BodyPartElbow                                                           -4.576e+03
## BodyPartEyes                                                             5.443e+03
## BodyPartFacial Bones                                                     1.300e+04
## BodyPartFinger(S)                                                       -1.013e+04
## BodyPartFoot                                                            -6.831e+03
## BodyPartGreat Toe                                                       -1.018e+04
## BodyPartHand                                                            -9.000e+03
## BodyPartHeart                                                            4.469e+03
## BodyPartHip                                                              4.306e+03
## BodyPartInsufficient Info to Properly Identify?Unclassified             -4.100e+04
## BodyPartInternal Organs                                                  1.903e+04
## BodyPartKnee                                                             4.109e+03
## BodyPartLarynx                                                          -3.683e+04
## BodyPartLower Arm                                                       -7.120e+03
## BodyPartLower Back Area                                                  3.467e+04
## BodyPartLower Leg                                                        8.986e+02
## BodyPartLumbar and/or Sacral Vertebrae (Vertebra NOC Trunk)              3.072e+04
## BodyPartLungs                                                           -1.036e+03
## BodyPartMouth                                                            9.086e+03
## BodyPartMultiple Body Parts (Including Body Systems and Body Parts)     -2.590e+04
## BodyPartMultiple Head Injury                                             2.008e+04
## BodyPartMultiple Lower Extremities                                       1.203e+04
## BodyPartMultiple Neck Injury                                            -1.754e+04
## BodyPartMultiple Trunk                                                   1.723e+04
## BodyPartMultiple Upper Extremities                                       9.815e+03
## BodyPartNo Physical Injury                                              -4.008e+04
## BodyPartNon-Standard Code                                                       NA
## BodyPartNose                                                            -1.718e+03
## BodyPartNot Available                                                           NA
## BodyPartPelvis                                                           3.667e+03
## BodyPartSacrum And Coccyx                                                2.023e+04
## BodyPartShoulder(S)                                                      7.246e+03
## BodyPartSkull                                                            4.599e+03
## BodyPartSoft Tissue-Head                                                -2.094e+04
## BodyPartSoft Tissue-Neck                                                -5.241e+04
## BodyPartSpinal Cord-Trunk                                                1.404e+05
## BodyPartTeeth                                                                   NA
## BodyPartThumb                                                           -7.328e+03
## BodyPartToes                                                            -9.862e+03
## BodyPartTrachea                                                         -3.939e+04
## BodyPartUpper Arm                                                        5.325e+03
## BodyPartUpper Back Area                                                  2.012e+04
## BodyPartUpper Leg                                                               NA
## BodyPartVertebrae                                                               NA
## BodyPartWhole Body                                                              NA
## BodyPartWrist                                                                   NA
## InjuryNatureAll Other Cumulative Injury, NOC                            -1.911e+04
## InjuryNatureAll Other Occupational Disease Injury, NOC                   1.069e+05
## InjuryNatureAll Other Specific Injuries, Noc                             1.434e+04
## InjuryNatureAmputation                                                   2.370e+04
## InjuryNatureAngina Pectoris                                              9.385e+03
## InjuryNatureAsbestosis                                                   1.830e+04
## InjuryNatureAsphyxiation                                                 3.520e+04
## InjuryNatureBurn                                                         1.256e+04
## InjuryNatureCancer                                                       2.455e+04
## InjuryNatureCarpal Tunnel Syndrome                                       1.035e+04
## InjuryNatureConcussion                                                   3.092e+04
## InjuryNatureContagious Disease                                           1.683e+04
## InjuryNatureContusion                                                    1.007e+04
## InjuryNatureCrushing                                                     6.976e+03
## InjuryNatureDermatitis                                                  -3.936e+03
## InjuryNatureDislocation                                                  4.992e+03
## InjuryNatureDust Disease, NOC                                           -1.884e+03
## InjuryNatureElectric Shock                                               4.657e+03
## InjuryNatureForeign Body                                                 6.205e+03
## InjuryNatureFracture                                                     1.778e+04
## InjuryNatureFreezing                                                    -1.010e+04
## InjuryNatureHearing Loss Or Impairment                                   1.282e+04
## InjuryNatureHeat Prostration                                            -7.442e+03
## InjuryNatureHernia                                                       1.008e+04
## InjuryNatureInfection                                                   -4.112e+03
## InjuryNatureInflammation                                                 9.181e+03
## InjuryNatureLaceration                                                   7.542e+03
## InjuryNatureLoss of Hearing                                              1.831e+04
## InjuryNatureMental Disorder                                              9.832e+03
## InjuryNatureMental Stress                                                2.095e+04
## InjuryNatureMultiple Injuries Including Both Physical and Psychological  1.056e+05
## InjuryNatureMultiple Physical Injuries Only                              3.242e+03
## InjuryNatureMyocardial Infarction                                        8.407e+03
## InjuryNatureNo Physical Injury                                           4.773e+02
## InjuryNatureNon-Standard Code                                           -9.568e+03
## InjuryNatureNot Available                                                3.816e+03
## InjuryNaturePoisoning?Chemical (Other Than Metals)                       2.088e+02
## InjuryNaturePoisoning?General (NOT OD or Cumulative Injury)             -1.108e+04
## InjuryNaturePuncture                                                     8.092e+03
## InjuryNatureRadiation                                                    3.064e+04
## InjuryNatureRespiratory Disorders                                        1.329e+04
## InjuryNatureRupture                                                      1.216e+04
## InjuryNatureSeverance                                                    5.789e+04
## InjuryNatureSilicosis                                                   -7.988e+02
## InjuryNatureSprain                                                       4.469e+02
## InjuryNatureStrain                                                       3.497e+03
## InjuryNatureSyncope                                                      4.440e+03
## InjuryNatureVascular                                                     5.007e+03
## InjuryNatureVDT-Related Disease                                          6.596e+03
## InjuryNatureVision Loss                                                  1.075e+04
##                                                                         Std. Error
## (Intercept)                                                              5.532e+04
## ClaimStatusO                                                             1.874e+03
## ClaimStatusR                                                             3.334e+03
## ClaimantAge_at_DOI                                                       5.594e+00
## GenderMale                                                               9.881e+02
## GenderNot Available                                                      5.941e+03
## AverageWeeklyWage                                                        3.835e-02
## BodyPartRegionLower Extremities                                          1.082e+04
## BodyPartRegionMultiple Body Parts                                        1.580e+04
## BodyPartRegionNeck                                                       2.777e+04
## BodyPartRegionNon-Standard Code                                          1.134e+04
## BodyPartRegionNot Available                                              9.506e+04
## BodyPartRegionTrunk                                                      1.036e+04
## BodyPartRegionUpper Extremities                                          9.546e+03
## BodyPartAnkle                                                            6.092e+03
## BodyPartArtificial Appliance                                             6.798e+04
## BodyPartBody Systems and Multiple Body Systems                           1.465e+04
## BodyPartBrain                                                            1.948e+04
## BodyPartButtocks                                                         8.420e+03
## BodyPartChest                                                            5.958e+03
## BodyPartDisc-Trunk                                                       7.543e+03
## BodyPartEar(S)                                                           1.292e+04
## BodyPartElbow                                                            4.333e+03
## BodyPartEyes                                                             9.955e+03
## BodyPartFacial Bones                                                     1.105e+04
## BodyPartFinger(S)                                                        3.327e+03
## BodyPartFoot                                                             6.265e+03
## BodyPartGreat Toe                                                        1.228e+04
## BodyPartHand                                                             3.232e+03
## BodyPartHeart                                                            2.306e+04
## BodyPartHip                                                              6.985e+03
## BodyPartInsufficient Info to Properly Identify?Unclassified              1.433e+04
## BodyPartInternal Organs                                                  1.117e+04
## BodyPartKnee                                                             5.816e+03
## BodyPartLarynx                                                           3.872e+04
## BodyPartLower Arm                                                        4.063e+03
## BodyPartLower Back Area                                                  4.894e+03
## BodyPartLower Leg                                                        6.471e+03
## BodyPartLumbar and/or Sacral Vertebrae (Vertebra NOC Trunk)              6.323e+03
## BodyPartLungs                                                            9.280e+03
## BodyPartMouth                                                            1.204e+04
## BodyPartMultiple Body Parts (Including Body Systems and Body Parts)      1.291e+04
## BodyPartMultiple Head Injury                                             9.846e+03
## BodyPartMultiple Lower Extremities                                       7.644e+03
## BodyPartMultiple Neck Injury                                             2.644e+04
## BodyPartMultiple Trunk                                                   1.174e+04
## BodyPartMultiple Upper Extremities                                       5.474e+03
## BodyPartNo Physical Injury                                               1.405e+04
## BodyPartNon-Standard Code                                                       NA
## BodyPartNose                                                             1.192e+04
## BodyPartNot Available                                                           NA
## BodyPartPelvis                                                           1.190e+04
## BodyPartSacrum And Coccyx                                                1.787e+04
## BodyPartShoulder(S)                                                      3.136e+03
## BodyPartSkull                                                            1.141e+04
## BodyPartSoft Tissue-Head                                                 1.049e+04
## BodyPartSoft Tissue-Neck                                                 2.633e+04
## BodyPartSpinal Cord-Trunk                                                1.455e+04
## BodyPartTeeth                                                                   NA
## BodyPartThumb                                                            4.580e+03
## BodyPartToes                                                             8.960e+03
## BodyPartTrachea                                                          4.671e+04
## BodyPartUpper Arm                                                        4.733e+03
## BodyPartUpper Back Area                                                  6.883e+03
## BodyPartUpper Leg                                                               NA
## BodyPartVertebrae                                                               NA
## BodyPartWhole Body                                                              NA
## BodyPartWrist                                                                   NA
## InjuryNatureAll Other Cumulative Injury, NOC                             5.704e+04
## InjuryNatureAll Other Occupational Disease Injury, NOC                   6.401e+04
## InjuryNatureAll Other Specific Injuries, Noc                             5.458e+04
## InjuryNatureAmputation                                                   5.684e+04
## InjuryNatureAngina Pectoris                                              5.934e+04
## InjuryNatureAsbestosis                                                   5.699e+04
## InjuryNatureAsphyxiation                                                 6.896e+04
## InjuryNatureBurn                                                         5.475e+04
## InjuryNatureCancer                                                       7.220e+04
## InjuryNatureCarpal Tunnel Syndrome                                       5.478e+04
## InjuryNatureConcussion                                                   5.493e+04
## InjuryNatureContagious Disease                                           5.535e+04
## InjuryNatureContusion                                                    5.455e+04
## InjuryNatureCrushing                                                     5.486e+04
## InjuryNatureDermatitis                                                   5.469e+04
## InjuryNatureDislocation                                                  5.486e+04
## InjuryNatureDust Disease, NOC                                            6.011e+04
## InjuryNatureElectric Shock                                               5.598e+04
## InjuryNatureForeign Body                                                 5.476e+04
## InjuryNatureFracture                                                     5.459e+04
## InjuryNatureFreezing                                                     7.713e+04
## InjuryNatureHearing Loss Or Impairment                                   5.899e+04
## InjuryNatureHeat Prostration                                             5.544e+04
## InjuryNatureHernia                                                       5.527e+04
## InjuryNatureInfection                                                    5.501e+04
## InjuryNatureInflammation                                                 5.467e+04
## InjuryNatureLaceration                                                   5.457e+04
## InjuryNatureLoss of Hearing                                              5.917e+04
## InjuryNatureMental Disorder                                              6.532e+04
## InjuryNatureMental Stress                                                5.511e+04
## InjuryNatureMultiple Injuries Including Both Physical and Psychological  5.619e+04
## InjuryNatureMultiple Physical Injuries Only                              5.460e+04
## InjuryNatureMyocardial Infarction                                        6.147e+04
## InjuryNatureNo Physical Injury                                           5.468e+04
## InjuryNatureNon-Standard Code                                            5.492e+04
## InjuryNatureNot Available                                                5.508e+04
## InjuryNaturePoisoning?Chemical (Other Than Metals)                       5.568e+04
## InjuryNaturePoisoning?General (NOT OD or Cumulative Injury)              5.847e+04
## InjuryNaturePuncture                                                     5.462e+04
## InjuryNatureRadiation                                                    6.681e+04
## InjuryNatureRespiratory Disorders                                        5.490e+04
## InjuryNatureRupture                                                      5.523e+04
## InjuryNatureSeverance                                                    6.097e+04
## InjuryNatureSilicosis                                                    7.712e+04
## InjuryNatureSprain                                                       5.456e+04
## InjuryNatureStrain                                                       5.455e+04
## InjuryNatureSyncope                                                      5.562e+04
## InjuryNatureVascular                                                     6.681e+04
## InjuryNatureVDT-Related Disease                                          8.622e+04
## InjuryNatureVision Loss                                                  5.925e+04
##                                                                         t value
## (Intercept)                                                              -0.319
## ClaimStatusO                                                             88.042
## ClaimStatusR                                                             52.623
## ClaimantAge_at_DOI                                                        0.723
## GenderMale                                                                2.636
## GenderNot Available                                                       4.077
## AverageWeeklyWage                                                         0.068
## BodyPartRegionLower Extremities                                           1.348
## BodyPartRegionMultiple Body Parts                                         3.768
## BodyPartRegionNeck                                                        1.643
## BodyPartRegionNon-Standard Code                                           0.602
## BodyPartRegionNot Available                                               0.256
## BodyPartRegionTrunk                                                       0.593
## BodyPartRegionUpper Extremities                                           1.487
## BodyPartAnkle                                                            -0.219
## BodyPartArtificial Appliance                                             -0.625
## BodyPartBody Systems and Multiple Body Systems                           -3.204
## BodyPartBrain                                                             3.771
## BodyPartButtocks                                                         -0.035
## BodyPartChest                                                            -0.061
## BodyPartDisc-Trunk                                                        4.511
## BodyPartEar(S)                                                            0.112
## BodyPartElbow                                                            -1.056
## BodyPartEyes                                                              0.547
## BodyPartFacial Bones                                                      1.177
## BodyPartFinger(S)                                                        -3.045
## BodyPartFoot                                                             -1.090
## BodyPartGreat Toe                                                        -0.829
## BodyPartHand                                                             -2.785
## BodyPartHeart                                                             0.194
## BodyPartHip                                                               0.616
## BodyPartInsufficient Info to Properly Identify?Unclassified              -2.861
## BodyPartInternal Organs                                                   1.703
## BodyPartKnee                                                              0.707
## BodyPartLarynx                                                           -0.951
## BodyPartLower Arm                                                        -1.753
## BodyPartLower Back Area                                                   7.085
## BodyPartLower Leg                                                         0.139
## BodyPartLumbar and/or Sacral Vertebrae (Vertebra NOC Trunk)               4.859
## BodyPartLungs                                                            -0.112
## BodyPartMouth                                                             0.755
## BodyPartMultiple Body Parts (Including Body Systems and Body Parts)      -2.006
## BodyPartMultiple Head Injury                                              2.040
## BodyPartMultiple Lower Extremities                                        1.574
## BodyPartMultiple Neck Injury                                             -0.664
## BodyPartMultiple Trunk                                                    1.468
## BodyPartMultiple Upper Extremities                                        1.793
## BodyPartNo Physical Injury                                               -2.852
## BodyPartNon-Standard Code                                                    NA
## BodyPartNose                                                             -0.144
## BodyPartNot Available                                                        NA
## BodyPartPelvis                                                            0.308
## BodyPartSacrum And Coccyx                                                 1.132
## BodyPartShoulder(S)                                                       2.311
## BodyPartSkull                                                             0.403
## BodyPartSoft Tissue-Head                                                 -1.995
## BodyPartSoft Tissue-Neck                                                 -1.990
## BodyPartSpinal Cord-Trunk                                                 9.652
## BodyPartTeeth                                                                NA
## BodyPartThumb                                                            -1.600
## BodyPartToes                                                             -1.101
## BodyPartTrachea                                                          -0.843
## BodyPartUpper Arm                                                         1.125
## BodyPartUpper Back Area                                                   2.923
## BodyPartUpper Leg                                                            NA
## BodyPartVertebrae                                                            NA
## BodyPartWhole Body                                                           NA
## BodyPartWrist                                                                NA
## InjuryNatureAll Other Cumulative Injury, NOC                             -0.335
## InjuryNatureAll Other Occupational Disease Injury, NOC                    1.670
## InjuryNatureAll Other Specific Injuries, Noc                              0.263
## InjuryNatureAmputation                                                    0.417
## InjuryNatureAngina Pectoris                                               0.158
## InjuryNatureAsbestosis                                                    0.321
## InjuryNatureAsphyxiation                                                  0.510
## InjuryNatureBurn                                                          0.229
## InjuryNatureCancer                                                        0.340
## InjuryNatureCarpal Tunnel Syndrome                                        0.189
## InjuryNatureConcussion                                                    0.563
## InjuryNatureContagious Disease                                            0.304
## InjuryNatureContusion                                                     0.185
## InjuryNatureCrushing                                                      0.127
## InjuryNatureDermatitis                                                   -0.072
## InjuryNatureDislocation                                                   0.091
## InjuryNatureDust Disease, NOC                                            -0.031
## InjuryNatureElectric Shock                                                0.083
## InjuryNatureForeign Body                                                  0.113
## InjuryNatureFracture                                                      0.326
## InjuryNatureFreezing                                                     -0.131
## InjuryNatureHearing Loss Or Impairment                                    0.217
## InjuryNatureHeat Prostration                                             -0.134
## InjuryNatureHernia                                                        0.182
## InjuryNatureInfection                                                    -0.075
## InjuryNatureInflammation                                                  0.168
## InjuryNatureLaceration                                                    0.138
## InjuryNatureLoss of Hearing                                               0.309
## InjuryNatureMental Disorder                                               0.151
## InjuryNatureMental Stress                                                 0.380
## InjuryNatureMultiple Injuries Including Both Physical and Psychological   1.880
## InjuryNatureMultiple Physical Injuries Only                               0.059
## InjuryNatureMyocardial Infarction                                         0.137
## InjuryNatureNo Physical Injury                                            0.009
## InjuryNatureNon-Standard Code                                            -0.174
## InjuryNatureNot Available                                                 0.069
## InjuryNaturePoisoning?Chemical (Other Than Metals)                        0.004
## InjuryNaturePoisoning?General (NOT OD or Cumulative Injury)              -0.190
## InjuryNaturePuncture                                                      0.148
## InjuryNatureRadiation                                                     0.459
## InjuryNatureRespiratory Disorders                                         0.242
## InjuryNatureRupture                                                       0.220
## InjuryNatureSeverance                                                     0.950
## InjuryNatureSilicosis                                                    -0.010
## InjuryNatureSprain                                                        0.008
## InjuryNatureStrain                                                        0.064
## InjuryNatureSyncope                                                       0.080
## InjuryNatureVascular                                                      0.075
## InjuryNatureVDT-Related Disease                                           0.076
## InjuryNatureVision Loss                                                   0.182
##                                                                         Pr(>|t|)
## (Intercept)                                                             0.749540
## ClaimStatusO                                                             < 2e-16
## ClaimStatusR                                                             < 2e-16
## ClaimantAge_at_DOI                                                      0.469599
## GenderMale                                                              0.008391
## GenderNot Available                                                     4.58e-05
## AverageWeeklyWage                                                       0.945567
## BodyPartRegionLower Extremities                                         0.177755
## BodyPartRegionMultiple Body Parts                                       0.000165
## BodyPartRegionNeck                                                      0.100442
## BodyPartRegionNon-Standard Code                                         0.546850
## BodyPartRegionNot Available                                             0.797603
## BodyPartRegionTrunk                                                     0.552907
## BodyPartRegionUpper Extremities                                         0.136934
## BodyPartAnkle                                                           0.826310
## BodyPartArtificial Appliance                                            0.532099
## BodyPartBody Systems and Multiple Body Systems                          0.001355
## BodyPartBrain                                                           0.000163
## BodyPartButtocks                                                        0.971725
## BodyPartChest                                                           0.951476
## BodyPartDisc-Trunk                                                      6.46e-06
## BodyPartEar(S)                                                          0.910929
## BodyPartElbow                                                           0.290868
## BodyPartEyes                                                            0.584517
## BodyPartFacial Bones                                                    0.239139
## BodyPartFinger(S)                                                       0.002332
## BodyPartFoot                                                            0.275565
## BodyPartGreat Toe                                                       0.407044
## BodyPartHand                                                            0.005362
## BodyPartHeart                                                           0.846339
## BodyPartHip                                                             0.537572
## BodyPartInsufficient Info to Properly Identify?Unclassified             0.004225
## BodyPartInternal Organs                                                 0.088511
## BodyPartKnee                                                            0.479840
## BodyPartLarynx                                                          0.341562
## BodyPartLower Arm                                                       0.079672
## BodyPartLower Back Area                                                 1.42e-12
## BodyPartLower Leg                                                       0.889560
## BodyPartLumbar and/or Sacral Vertebrae (Vertebra NOC Trunk)             1.18e-06
## BodyPartLungs                                                           0.911069
## BodyPartMouth                                                           0.450487
## BodyPartMultiple Body Parts (Including Body Systems and Body Parts)     0.044873
## BodyPartMultiple Head Injury                                            0.041391
## BodyPartMultiple Lower Extremities                                      0.115405
## BodyPartMultiple Neck Injury                                            0.507006
## BodyPartMultiple Trunk                                                  0.142101
## BodyPartMultiple Upper Extremities                                      0.072988
## BodyPartNo Physical Injury                                              0.004345
## BodyPartNon-Standard Code                                                     NA
## BodyPartNose                                                            0.885414
## BodyPartNot Available                                                         NA
## BodyPartPelvis                                                          0.758063
## BodyPartSacrum And Coccyx                                               0.257593
## BodyPartShoulder(S)                                                     0.020855
## BodyPartSkull                                                           0.686873
## BodyPartSoft Tissue-Head                                                0.045997
## BodyPartSoft Tissue-Neck                                                0.046570
## BodyPartSpinal Cord-Trunk                                                < 2e-16
## BodyPartTeeth                                                                 NA
## BodyPartThumb                                                           0.109607
## BodyPartToes                                                            0.271051
## BodyPartTrachea                                                         0.399162
## BodyPartUpper Arm                                                       0.260544
## BodyPartUpper Back Area                                                 0.003466
## BodyPartUpper Leg                                                             NA
## BodyPartVertebrae                                                             NA
## BodyPartWhole Body                                                            NA
## BodyPartWrist                                                                 NA
## InjuryNatureAll Other Cumulative Injury, NOC                            0.737666
## InjuryNatureAll Other Occupational Disease Injury, NOC                  0.094923
## InjuryNatureAll Other Specific Injuries, Noc                            0.792729
## InjuryNatureAmputation                                                  0.676655
## InjuryNatureAngina Pectoris                                             0.874325
## InjuryNatureAsbestosis                                                  0.748108
## InjuryNatureAsphyxiation                                                0.609757
## InjuryNatureBurn                                                        0.818582
## InjuryNatureCancer                                                      0.733794
## InjuryNatureCarpal Tunnel Syndrome                                      0.850073
## InjuryNatureConcussion                                                  0.573506
## InjuryNatureContagious Disease                                          0.761027
## InjuryNatureContusion                                                   0.853542
## InjuryNatureCrushing                                                    0.898810
## InjuryNatureDermatitis                                                  0.942629
## InjuryNatureDislocation                                                 0.927506
## InjuryNatureDust Disease, NOC                                           0.974993
## InjuryNatureElectric Shock                                              0.933699
## InjuryNatureForeign Body                                                0.909779
## InjuryNatureFracture                                                    0.744607
## InjuryNatureFreezing                                                    0.895808
## InjuryNatureHearing Loss Or Impairment                                  0.827956
## InjuryNatureHeat Prostration                                            0.893215
## InjuryNatureHernia                                                      0.855272
## InjuryNatureInfection                                                   0.940411
## InjuryNatureInflammation                                                0.866624
## InjuryNatureLaceration                                                  0.890093
## InjuryNatureLoss of Hearing                                             0.757051
## InjuryNatureMental Disorder                                             0.880357
## InjuryNatureMental Stress                                               0.703833
## InjuryNatureMultiple Injuries Including Both Physical and Psychological 0.060153
## InjuryNatureMultiple Physical Injuries Only                             0.952654
## InjuryNatureMyocardial Infarction                                       0.891225
## InjuryNatureNo Physical Injury                                          0.993035
## InjuryNatureNon-Standard Code                                           0.861708
## InjuryNatureNot Available                                               0.944763
## InjuryNaturePoisoning?Chemical (Other Than Metals)                      0.997008
## InjuryNaturePoisoning?General (NOT OD or Cumulative Injury)             0.849692
## InjuryNaturePuncture                                                    0.882230
## InjuryNatureRadiation                                                   0.646486
## InjuryNatureRespiratory Disorders                                       0.808660
## InjuryNatureRupture                                                     0.825795
## InjuryNatureSeverance                                                   0.342350
## InjuryNatureSilicosis                                                   0.991737
## InjuryNatureSprain                                                      0.993465
## InjuryNatureStrain                                                      0.948886
## InjuryNatureSyncope                                                     0.936375
## InjuryNatureVascular                                                    0.940265
## InjuryNatureVDT-Related Disease                                         0.939023
## InjuryNatureVision Loss                                                 0.855965
##                                                                            
## (Intercept)                                                                
## ClaimStatusO                                                            ***
## ClaimStatusR                                                            ***
## ClaimantAge_at_DOI                                                         
## GenderMale                                                              ** 
## GenderNot Available                                                     ***
## AverageWeeklyWage                                                          
## BodyPartRegionLower Extremities                                            
## BodyPartRegionMultiple Body Parts                                       ***
## BodyPartRegionNeck                                                         
## BodyPartRegionNon-Standard Code                                            
## BodyPartRegionNot Available                                                
## BodyPartRegionTrunk                                                        
## BodyPartRegionUpper Extremities                                            
## BodyPartAnkle                                                              
## BodyPartArtificial Appliance                                               
## BodyPartBody Systems and Multiple Body Systems                          ** 
## BodyPartBrain                                                           ***
## BodyPartButtocks                                                           
## BodyPartChest                                                              
## BodyPartDisc-Trunk                                                      ***
## BodyPartEar(S)                                                             
## BodyPartElbow                                                              
## BodyPartEyes                                                               
## BodyPartFacial Bones                                                       
## BodyPartFinger(S)                                                       ** 
## BodyPartFoot                                                               
## BodyPartGreat Toe                                                          
## BodyPartHand                                                            ** 
## BodyPartHeart                                                              
## BodyPartHip                                                                
## BodyPartInsufficient Info to Properly Identify?Unclassified             ** 
## BodyPartInternal Organs                                                 .  
## BodyPartKnee                                                               
## BodyPartLarynx                                                             
## BodyPartLower Arm                                                       .  
## BodyPartLower Back Area                                                 ***
## BodyPartLower Leg                                                          
## BodyPartLumbar and/or Sacral Vertebrae (Vertebra NOC Trunk)             ***
## BodyPartLungs                                                              
## BodyPartMouth                                                              
## BodyPartMultiple Body Parts (Including Body Systems and Body Parts)     *  
## BodyPartMultiple Head Injury                                            *  
## BodyPartMultiple Lower Extremities                                         
## BodyPartMultiple Neck Injury                                               
## BodyPartMultiple Trunk                                                     
## BodyPartMultiple Upper Extremities                                      .  
## BodyPartNo Physical Injury                                              ** 
## BodyPartNon-Standard Code                                                  
## BodyPartNose                                                               
## BodyPartNot Available                                                      
## BodyPartPelvis                                                             
## BodyPartSacrum And Coccyx                                                  
## BodyPartShoulder(S)                                                     *  
## BodyPartSkull                                                              
## BodyPartSoft Tissue-Head                                                *  
## BodyPartSoft Tissue-Neck                                                *  
## BodyPartSpinal Cord-Trunk                                               ***
## BodyPartTeeth                                                              
## BodyPartThumb                                                              
## BodyPartToes                                                               
## BodyPartTrachea                                                            
## BodyPartUpper Arm                                                          
## BodyPartUpper Back Area                                                 ** 
## BodyPartUpper Leg                                                          
## BodyPartVertebrae                                                          
## BodyPartWhole Body                                                         
## BodyPartWrist                                                              
## InjuryNatureAll Other Cumulative Injury, NOC                               
## InjuryNatureAll Other Occupational Disease Injury, NOC                  .  
## InjuryNatureAll Other Specific Injuries, Noc                               
## InjuryNatureAmputation                                                     
## InjuryNatureAngina Pectoris                                                
## InjuryNatureAsbestosis                                                     
## InjuryNatureAsphyxiation                                                   
## InjuryNatureBurn                                                           
## InjuryNatureCancer                                                         
## InjuryNatureCarpal Tunnel Syndrome                                         
## InjuryNatureConcussion                                                     
## InjuryNatureContagious Disease                                             
## InjuryNatureContusion                                                      
## InjuryNatureCrushing                                                       
## InjuryNatureDermatitis                                                     
## InjuryNatureDislocation                                                    
## InjuryNatureDust Disease, NOC                                              
## InjuryNatureElectric Shock                                                 
## InjuryNatureForeign Body                                                   
## InjuryNatureFracture                                                       
## InjuryNatureFreezing                                                       
## InjuryNatureHearing Loss Or Impairment                                     
## InjuryNatureHeat Prostration                                               
## InjuryNatureHernia                                                         
## InjuryNatureInfection                                                      
## InjuryNatureInflammation                                                   
## InjuryNatureLaceration                                                     
## InjuryNatureLoss of Hearing                                                
## InjuryNatureMental Disorder                                                
## InjuryNatureMental Stress                                                  
## InjuryNatureMultiple Injuries Including Both Physical and Psychological .  
## InjuryNatureMultiple Physical Injuries Only                                
## InjuryNatureMyocardial Infarction                                          
## InjuryNatureNo Physical Injury                                             
## InjuryNatureNon-Standard Code                                              
## InjuryNatureNot Available                                                  
## InjuryNaturePoisoning?Chemical (Other Than Metals)                         
## InjuryNaturePoisoning?General (NOT OD or Cumulative Injury)                
## InjuryNaturePuncture                                                       
## InjuryNatureRadiation                                                      
## InjuryNatureRespiratory Disorders                                          
## InjuryNatureRupture                                                        
## InjuryNatureSeverance                                                      
## InjuryNatureSilicosis                                                      
## InjuryNatureSprain                                                         
## InjuryNatureStrain                                                         
## InjuryNatureSyncope                                                        
## InjuryNatureVascular                                                       
## InjuryNatureVDT-Related Disease                                            
## InjuryNatureVision Loss                                                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 94380 on 38269 degrees of freedom
##   (148297 observations deleted due to missingness)
## Multiple R-squared:  0.2321, Adjusted R-squared:  0.2299 
## F-statistic: 105.2 on 110 and 38269 DF,  p-value: < 2.2e-16
par(mfrow=c(2,2))
plot(df2.lm)
## Warning in sqrt(crit * p * (1 - hh)/hh): Se han producido NaNs

## Warning in sqrt(crit * p * (1 - hh)/hh): Se han producido NaNs

Árbol de decisión

1. Importar bases de datos

library(readxl)
claims <- claims_data
transactions <- transactions

Unir bases de datos

df <- merge(claims, transactions, by="ClaimID", all=TRUE)
View(df)

2. Entender la base de datos

summary(df)
##     ClaimID          TotalPaid         TotalReserves     TotalRecovery      
##  Min.   :  633915   Length:186677      Min.   :      0   Min.   :     0.00  
##  1st Qu.:  810246   Class :character   1st Qu.:      0   1st Qu.:     0.00  
##  Median :  856915   Mode  :character   Median :      0   Median :     0.00  
##  Mean   :12344572                      Mean   :   2233   Mean   :    68.88  
##  3rd Qu.:22716420                      3rd Qu.:      0   3rd Qu.:     0.00  
##  Max.   :62246496                      Max.   :2069575   Max.   :130541.03  
##                                        NA's   :52673     NA's   :52673      
##  IndemnityPaid      OtherPaid       TotalIncurredCost ClaimStatus       
##  Min.   :  -475   Min.   :  -7820   Min.   : -11775   Length:186677     
##  1st Qu.:     0   1st Qu.:     58   1st Qu.:     59   Class :character  
##  Median :     0   Median :    230   Median :    234   Mode  :character  
##  Mean   :  3061   Mean   :   3685   Mean   :   8910                     
##  3rd Qu.:     0   3rd Qu.:    855   3rd Qu.:    965                     
##  Max.   :640732   Max.   :4129915   Max.   :5054823                     
##  NA's   :52673    NA's   :52673     NA's   :52673                       
##   IncidentDate                 IncidentDescription
##  Min.   :1947-02-24 00:00:00   Length:186677      
##  1st Qu.:1998-12-21 00:00:00   Class :character   
##  Median :2004-01-05 00:00:00   Mode  :character   
##  Mean   :2003-12-08 16:48:47                      
##  3rd Qu.:2009-02-02 00:00:00                      
##  Max.   :2014-06-27 00:00:00                      
##  NA's   :52673                                    
##  ReturnToWorkDate              AverageWeeklyWage   ClaimantOpenedDate  
##  Min.   :1976-10-29 00:00:00   Min.   :      0.0   Min.   :1947-02-24  
##  1st Qu.:2002-04-25 00:00:00   1st Qu.:    300.0   1st Qu.:1999-02-09  
##  Median :2007-07-09 00:00:00   Median :    492.0   Median :2004-02-17  
##  Mean   :2006-06-01 15:30:12   Mean   :    587.3   Mean   :2004-01-23  
##  3rd Qu.:2011-06-01 00:00:00   3rd Qu.:    660.4   3rd Qu.:2009-04-09  
##  Max.   :2015-05-07 00:00:00   Max.   :2024000.0   Max.   :2014-06-30  
##  NA's   :111310                NA's   :137597      NA's   :52673       
##  ClaimantClosedDate   EmployerNotificationDate     
##  Min.   :1999-06-01   Min.   :1972-09-10 00:00:00  
##  1st Qu.:2005-03-31   1st Qu.:2000-03-13 00:00:00  
##  Median :2006-04-04   Median :2004-12-28 00:00:00  
##  Mean   :2007-05-24   Mean   :2005-08-29 04:49:34  
##  3rd Qu.:2009-11-11   3rd Qu.:2009-11-03 00:00:00  
##  Max.   :2014-06-30   Max.   :9999-07-21 00:00:00  
##  NA's   :57351        NA's   :74961                
##   ReceivedDate                   IsDenied         ClaimantAge_at_DOI
##  Min.   :1947-02-24 00:00:00   Length:186677      Min.   :-8000.00  
##  1st Qu.:1999-02-09 00:00:00   Class :character   1st Qu.:   33.00  
##  Median :2004-02-13 00:00:00   Mode  :character   Median :   42.00  
##  Mean   :2004-07-19 11:29:03                      Mean   :   39.85  
##  3rd Qu.:2009-02-27 00:00:00                      3rd Qu.:   51.00  
##  Max.   :9999-07-21 00:00:00                      Max.   :   94.00  
##  NA's   :52673                                    NA's   :97751     
##     Gender          ClaimantType       InjuryNature       BodyPartRegion    
##  Length:186677      Length:186677      Length:186677      Length:186677     
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##    BodyPart         BillReviewALE        Hospital         PhysicianOutpatient
##  Length:186677      Min.   : -456.0   Min.   : -12570.4   Min.   :  -4655.7  
##  Class :character   1st Qu.:   16.0   1st Qu.:    193.9   1st Qu.:    107.6  
##  Mode  :character   Median :   32.0   Median :    559.1   Median :    221.6  
##                     Mean   :  191.2   Mean   :   4394.7   Mean   :   1752.3  
##                     3rd Qu.:   80.0   3rd Qu.:   2253.4   3rd Qu.:    710.5  
##                     Max.   :56475.3   Max.   :2759604.0   Max.   :1481468.5  
##                     NA's   :139865    NA's   :145262      NA's   :84986      
##        Rx          
##  Min.   :  -469.5  
##  1st Qu.:    23.3  
##  Median :    58.3  
##  Mean   :  1140.4  
##  3rd Qu.:   174.5  
##  Max.   :631635.5  
##  NA's   :145752

3. Limpiar la base de datos

#library(dplyr)
#Convertir a numericos
df$IndemnityPaid <- as.numeric(df$IndemnityPaid)
df$TotalPaid <- as.numeric(df$TotalPaid)
df$TotalReserves <- as.numeric(df$TotalReserves)
df$TotalRecovery <- as.numeric(df$TotalRecovery)
df$IndemnityPaid <- as.numeric(df$IndemnityPaid)
df$OtherPaid <- as.numeric(df$OtherPaid)
df$AverageWeeklyWage <- as.numeric(df$AverageWeeklyWage)
df$ClaimantAge_at_DOI <- as.numeric(df$ClaimantAge_at_DOI)

df$TotalIncurredCost <- with(df, TotalReserves + IndemnityPaid + OtherPaid - TotalRecovery)

# Eliminar Totales negativos
df <- df[df$TotalIncurredCost>=0,]
df <- df[df$BillReviewALE>=0,]
df <- df[df$Hospital>=0,]
df <- df[df$PhysicianOutpatient>=0,]
df <- df[df$Rx>=0,]
# Cuantos NA's hay por variable
sapply(df, function(x) sum(is.na(x)))
##                  ClaimID                TotalPaid            TotalReserves 
##                   181492                   181492                   181492 
##            TotalRecovery            IndemnityPaid                OtherPaid 
##                   181492                   181492                   181492 
##        TotalIncurredCost              ClaimStatus             IncidentDate 
##                   181492                   181492                   181492 
##      IncidentDescription         ReturnToWorkDate        AverageWeeklyWage 
##                   181492                   184160                   184958 
##       ClaimantOpenedDate       ClaimantClosedDate EmployerNotificationDate 
##                   181492                   181529                   181685 
##             ReceivedDate                 IsDenied       ClaimantAge_at_DOI 
##                   181492                   181492                   183099 
##                   Gender             ClaimantType             InjuryNature 
##                   181492                   181492                   181492 
##           BodyPartRegion                 BodyPart            BillReviewALE 
##                   181492                   181492                   181492 
##                 Hospital      PhysicianOutpatient                       Rx 
##                   181492                   181492                   181492
summary(df)
##     ClaimID           TotalPaid        TotalReserves    TotalRecovery     
##  Min.   :  777625   Min.   :     0.0   Min.   :     0   Min.   :    0.00  
##  1st Qu.:  793154   1st Qu.:    67.4   1st Qu.:     0   1st Qu.:    0.00  
##  Median :  803511   Median :   240.7   Median :     0   Median :    0.00  
##  Mean   : 1672130   Mean   :  6187.5   Mean   :  1707   Mean   :   50.73  
##  3rd Qu.:  813440   3rd Qu.:   851.9   3rd Qu.:     0   3rd Qu.:    0.00  
##  Max.   :36094375   Max.   :755038.3   Max.   :899411   Max.   :25044.20  
##  NA's   :181492     NA's   :181492     NA's   :181492   NA's   :181492    
##  IndemnityPaid      OtherPaid        TotalIncurredCost   ClaimStatus       
##  Min.   :     0   Min.   :     0.0   Min.   :      0.0   Length:186571     
##  1st Qu.:     0   1st Qu.:    66.7   1st Qu.:     65.3   Class :character  
##  Median :     0   Median :   235.7   Median :    235.1   Mode  :character  
##  Mean   :  2852   Mean   :  3335.7   Mean   :   7843.8                     
##  3rd Qu.:     0   3rd Qu.:   783.3   3rd Qu.:    832.8                     
##  Max.   :314577   Max.   :623298.0   Max.   :1654449.4                     
##  NA's   :181492   NA's   :181492     NA's   :181492                        
##   IncidentDate                 IncidentDescription
##  Min.   :1982-09-16 00:00:00   Length:186571      
##  1st Qu.:2001-05-24 00:00:00   Class :character   
##  Median :2003-06-03 00:00:00   Mode  :character   
##  Mean   :2002-12-19 00:57:33                      
##  3rd Qu.:2005-02-23 00:00:00                      
##  Max.   :2013-09-12 00:00:00                      
##  NA's   :181492                                   
##  ReturnToWorkDate              AverageWeeklyWage ClaimantOpenedDate  
##  Min.   :1992-05-01 00:00:00   Min.   :   0.23   Min.   :1982-09-16  
##  1st Qu.:2003-05-28 00:00:00   1st Qu.: 346.40   1st Qu.:2001-06-19  
##  Median :2004-11-18 00:00:00   Median : 472.79   Median :2003-07-11  
##  Mean   :2004-03-29 03:13:30   Mean   : 490.53   Mean   :2003-02-07  
##  3rd Qu.:2005-10-25 00:00:00   3rd Qu.: 616.81   3rd Qu.:2005-04-11  
##  Max.   :2013-09-12 00:00:00   Max.   :7409.77   Max.   :2013-09-18  
##  NA's   :184160                NA's   :184958    NA's   :181492      
##  ClaimantClosedDate   EmployerNotificationDate     
##  Min.   :1999-07-12   Min.   :1990-10-10 00:00:00  
##  1st Qu.:2005-03-31   1st Qu.:2001-08-01 12:00:00  
##  Median :2005-04-25   Median :2003-07-17 00:00:00  
##  Mean   :2006-05-07   Mean   :2003-04-23 08:55:30  
##  3rd Qu.:2007-07-20   3rd Qu.:2005-03-22 00:00:00  
##  Max.   :2014-06-25   Max.   :2013-09-12 00:00:00  
##  NA's   :181529       NA's   :181685               
##   ReceivedDate                   IsDenied         ClaimantAge_at_DOI
##  Min.   :1982-09-16 00:00:00   Length:186571      Min.   :-80.00    
##  1st Qu.:2001-06-19 00:00:00   Class :character   1st Qu.: 33.00    
##  Median :2003-07-10 00:00:00   Mode  :character   Median : 42.00    
##  Mean   :2003-10-13 12:02:24                      Mean   : 41.46    
##  3rd Qu.:2005-04-06 00:00:00                      3rd Qu.: 50.00    
##  Max.   :5202-01-10 00:00:00                      Max.   : 94.00    
##  NA's   :181492                                   NA's   :183099    
##     Gender          ClaimantType       InjuryNature       BodyPartRegion    
##  Length:186571      Length:186571      Length:186571      Length:186571     
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##    BodyPart         BillReviewALE        Hospital         PhysicianOutpatient
##  Length:186571      Min.   :    0.0   Min.   :      0.0   Min.   :      0    
##  Class :character   1st Qu.:   40.0   1st Qu.:    775.9   1st Qu.:    627    
##  Mode  :character   Median :  144.0   Median :   3609.9   Median :   3897    
##                     Mean   :  722.6   Mean   :  14363.7   Mean   :  10604    
##                     3rd Qu.:  516.8   3rd Qu.:  12562.1   3rd Qu.:  10990    
##                     Max.   :56475.3   Max.   :2759604.0   Max.   :1481469    
##                     NA's   :181492    NA's   :181492      NA's   :181492     
##        Rx          
##  Min.   :     0.0  
##  1st Qu.:    48.3  
##  Median :   183.3  
##  Mean   :  4900.2  
##  3rd Qu.:   953.9  
##  Max.   :631635.5  
##  NA's   :181492
library(rpart)
## Warning: package 'rpart' was built under R version 4.1.3
library(rpart.plot)
## Warning: package 'rpart.plot' was built under R version 4.1.3
arbol1 <- rpart(formula = InjuryNature~TotalIncurredCost, data=df)
rpart.plot(arbol1)

arbol2 <- rpart(formula = AverageWeeklyWage~TotalIncurredCost, data=df)
rpart.plot(arbol2)

Clusters

1. Imputando la base de datos

library(ggplot2)
library(factoextra)
## Warning: package 'factoextra' was built under R version 4.1.3
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
quejas <- read_excel("C:\\Users\\jesus\\Documents\\Actividades\\ClaimsData2018.xlsx")

# Base de datos del resumen de la transacción
resumen <- read_excel("C:\\Users\\jesus\\Documents\\Actividades\\TransactionsSummary2018.xlsx")

Unir las bases de datos

# Unir las bases de datos a partir de la columna de `ClaimID'
bdc <- merge(quejas, resumen, by= "ClaimID", all= TRUE)

Entendimiento y limpieza de la base de datos

summary(bdc)
##     ClaimID          TotalPaid         TotalReserves      TotalRecovery     
##  Min.   :  633915   Length:186677      Length:186677      Length:186677     
##  1st Qu.:  810246   Class :character   Class :character   Class :character  
##  Median :  856915   Mode  :character   Mode  :character   Mode  :character  
##  Mean   :12344572                                                           
##  3rd Qu.:22716420                                                           
##  Max.   :62246496                                                           
##                                                                             
##  IndemnityPaid       OtherPaid         TotalIncurredCost ClaimStatus       
##  Length:186677      Length:186677      Min.   : -11775   Length:186677     
##  Class :character   Class :character   1st Qu.:     59   Class :character  
##  Mode  :character   Mode  :character   Median :    234   Mode  :character  
##                                        Mean   :   8910                     
##                                        3rd Qu.:    965                     
##                                        Max.   :5054823                     
##                                        NA's   :52673                       
##   IncidentDate                 IncidentDescription
##  Min.   :1947-02-24 00:00:00   Length:186677      
##  1st Qu.:1998-12-21 00:00:00   Class :character   
##  Median :2004-01-05 00:00:00   Mode  :character   
##  Mean   :2003-12-08 16:48:47                      
##  3rd Qu.:2009-02-02 00:00:00                      
##  Max.   :2014-06-27 00:00:00                      
##  NA's   :52673                                    
##  ReturnToWorkDate              AverageWeeklyWage  ClaimantOpenedDate           
##  Min.   :1976-10-29 00:00:00   Length:186677      Min.   :1947-02-24 00:00:00  
##  1st Qu.:2002-04-25 00:00:00   Class :character   1st Qu.:1999-02-09 00:00:00  
##  Median :2007-07-09 00:00:00   Mode  :character   Median :2004-02-17 00:00:00  
##  Mean   :2006-06-01 15:30:12                      Mean   :2004-01-23 11:32:07  
##  3rd Qu.:2011-06-01 00:00:00                      3rd Qu.:2009-04-09 06:00:00  
##  Max.   :2015-05-07 00:00:00                      Max.   :2014-06-30 00:00:00  
##  NA's   :111310                                   NA's   :52673                
##  ClaimantClosedDate            EmployerNotificationDate     
##  Min.   :1999-06-01 00:00:00   Min.   :1972-09-10 00:00:00  
##  1st Qu.:2005-03-31 00:00:00   1st Qu.:2000-03-13 00:00:00  
##  Median :2006-04-04 12:00:00   Median :2004-12-28 00:00:00  
##  Mean   :2007-05-24 02:11:30   Mean   :2005-08-29 04:49:34  
##  3rd Qu.:2009-11-11 00:00:00   3rd Qu.:2009-11-03 00:00:00  
##  Max.   :2014-06-30 00:00:00   Max.   :9999-07-21 00:00:00  
##  NA's   :57351                 NA's   :74961                
##   ReceivedDate                   IsDenied         ClaimantAge_at_DOI
##  Min.   :1947-02-24 00:00:00   Length:186677      Length:186677     
##  1st Qu.:1999-02-09 00:00:00   Class :character   Class :character  
##  Median :2004-02-13 00:00:00   Mode  :character   Mode  :character  
##  Mean   :2004-07-19 11:29:03                                        
##  3rd Qu.:2009-02-27 00:00:00                                        
##  Max.   :9999-07-21 00:00:00                                        
##  NA's   :52673                                                      
##     Gender          ClaimantType       InjuryNature       BodyPartRegion    
##  Length:186677      Length:186677      Length:186677      Length:186677     
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##    BodyPart         BillReviewALE        Hospital         PhysicianOutpatient
##  Length:186677      Min.   : -456.0   Min.   : -12570.4   Min.   :  -4655.7  
##  Class :character   1st Qu.:   16.0   1st Qu.:    193.9   1st Qu.:    107.6  
##  Mode  :character   Median :   32.0   Median :    559.1   Median :    221.6  
##                     Mean   :  191.2   Mean   :   4394.7   Mean   :   1752.3  
##                     3rd Qu.:   80.0   3rd Qu.:   2253.4   3rd Qu.:    710.5  
##                     Max.   :56475.3   Max.   :2759604.0   Max.   :1481468.5  
##                     NA's   :139865    NA's   :145262      NA's   :84986      
##        Rx          
##  Min.   :  -469.5  
##  1st Qu.:    23.3  
##  Median :    58.3  
##  Mean   :  1140.4  
##  3rd Qu.:   174.5  
##  Max.   :631635.5  
##  NA's   :145752
grupos <- 3
str(bdc)
## 'data.frame':    186677 obs. of  27 variables:
##  $ ClaimID                 : num  633915 633917 633918 633919 633920 ...
##  $ TotalPaid               : chr  NA NA NA NA ...
##  $ TotalReserves           : chr  NA NA NA NA ...
##  $ TotalRecovery           : chr  NA NA NA NA ...
##  $ IndemnityPaid           : chr  NA NA NA NA ...
##  $ OtherPaid               : chr  NA NA NA NA ...
##  $ TotalIncurredCost       : num  NA NA NA NA NA ...
##  $ ClaimStatus             : chr  NA NA NA NA ...
##  $ IncidentDate            : POSIXct, format: NA NA ...
##  $ IncidentDescription     : chr  NA NA NA NA ...
##  $ ReturnToWorkDate        : POSIXct, format: NA NA ...
##  $ AverageWeeklyWage       : chr  NA NA NA NA ...
##  $ ClaimantOpenedDate      : POSIXct, format: NA NA ...
##  $ ClaimantClosedDate      : POSIXct, format: NA NA ...
##  $ EmployerNotificationDate: POSIXct, format: NA NA ...
##  $ ReceivedDate            : POSIXct, format: NA NA ...
##  $ IsDenied                : chr  NA NA NA NA ...
##  $ ClaimantAge_at_DOI      : chr  NA NA NA NA ...
##  $ Gender                  : chr  NA NA NA NA ...
##  $ ClaimantType            : chr  NA NA NA NA ...
##  $ InjuryNature            : chr  NA NA NA NA ...
##  $ BodyPartRegion          : chr  NA NA NA NA ...
##  $ BodyPart                : chr  NA NA NA NA ...
##  $ BillReviewALE           : num  437.6 311.3 74.7 231.3 24.8 ...
##  $ Hospital                : num  2035 6540 721 107 NA ...
##  $ PhysicianOutpatient     : num  4742 2247 231 1078 251 ...
##  $ Rx                      : num  65 0 NA 1629.2 56.5 ...

#Imputando la base de datos

numeric_cols <- sapply(bdc, is.character)
bdc[numeric_cols] <- lapply(bdc[numeric_cols], as.numeric)
## Warning in lapply(bdc[numeric_cols], as.numeric): NAs introducidos por coerción

## Warning in lapply(bdc[numeric_cols], as.numeric): NAs introducidos por coerción

## Warning in lapply(bdc[numeric_cols], as.numeric): NAs introducidos por coerción

## Warning in lapply(bdc[numeric_cols], as.numeric): NAs introducidos por coerción

## Warning in lapply(bdc[numeric_cols], as.numeric): NAs introducidos por coerción

## Warning in lapply(bdc[numeric_cols], as.numeric): NAs introducidos por coerción

## Warning in lapply(bdc[numeric_cols], as.numeric): NAs introducidos por coerción

## Warning in lapply(bdc[numeric_cols], as.numeric): NAs introducidos por coerción

## Warning in lapply(bdc[numeric_cols], as.numeric): NAs introducidos por coerción
date_cols <- sapply(bdc, inherits, "POSIXct")
bdc[date_cols] <- lapply(bdc[date_cols], as.numeric)
bdc_imputed <- bdc
for (col in names(bdc)) {
  bdc_imputed[[col]][is.na(bdc[[col]])] <- mean(bdc[[col]], na.rm = TRUE)
}
sum(is.na(bdc_imputed))
## [1] 1306739

#Quitando NAs restantes

na_cols <- sapply(bdc_imputed, function(x) all(is.na(x)))
bdc_imputed <- bdc_imputed[, !na_cols]
sum(is.na(bdc_imputed))
## [1] 0
#Revisando la base de datos imputada
summary(bdc_imputed)
##     ClaimID           TotalPaid       TotalReserves     TotalRecovery      
##  Min.   :  633915   Min.   :   -270   Min.   :      0   Min.   :     0.00  
##  1st Qu.:  810246   1st Qu.:    123   1st Qu.:      0   1st Qu.:     0.00  
##  Median :  856915   Median :    629   Median :      0   Median :     0.00  
##  Mean   :12344572   Mean   :   6746   Mean   :   2233   Mean   :    68.88  
##  3rd Qu.:22716420   3rd Qu.:   6746   3rd Qu.:   2233   3rd Qu.:    68.88  
##  Max.   :62246496   Max.   :4527291   Max.   :2069575   Max.   :130541.03  
##  IndemnityPaid      OtherPaid       TotalIncurredCost  IncidentDate       
##  Min.   :  -475   Min.   :  -7820   Min.   : -11775   Min.   :-721180800  
##  1st Qu.:     0   1st Qu.:    120   1st Qu.:    121   1st Qu.: 974332800  
##  Median :     0   Median :    589   Median :    639   Median :1070902127  
##  Mean   :  3061   Mean   :   3685   Mean   :   8910   Mean   :1070902127  
##  3rd Qu.:  3061   3rd Qu.:   3685   3rd Qu.:   8910   3rd Qu.:1165795200  
##  Max.   :640732   Max.   :4129915   Max.   :5054823   Max.   :1403827200  
##  ReturnToWorkDate    AverageWeeklyWage   ClaimantOpenedDate  
##  Min.   :2.154e+08   Min.   :      0.0   Min.   :-721180800  
##  1st Qu.:1.149e+09   1st Qu.:    587.3   1st Qu.: 978393600  
##  Median :1.149e+09   Median :    587.3   Median :1074857528  
##  Mean   :1.149e+09   Mean   :    587.3   Mean   :1074857528  
##  3rd Qu.:1.149e+09   3rd Qu.:    587.3   3rd Qu.:1170806400  
##  Max.   :1.431e+09   Max.   :2024000.0   Max.   :1404086400  
##  ClaimantClosedDate  EmployerNotificationDate  ReceivedDate       
##  Min.   :9.282e+08   Min.   :8.493e+07        Min.   :-7.212e+08  
##  1st Qu.:1.112e+09   1st Qu.:1.055e+09        1st Qu.: 9.785e+08  
##  Median :1.180e+09   Median :1.125e+09        Median : 1.090e+09  
##  Mean   :1.180e+09   Mean   :1.125e+09        Mean   : 1.090e+09  
##  3rd Qu.:1.221e+09   3rd Qu.:1.151e+09        3rd Qu.: 1.169e+09  
##  Max.   :1.404e+09   Max.   :2.534e+11        Max.   : 2.534e+11  
##     IsDenied       ClaimantAge_at_DOI BillReviewALE        Hospital      
##  Min.   :0.00000   Min.   :-8000.00   Min.   : -456.0   Min.   : -12570  
##  1st Qu.:0.00000   1st Qu.:   39.85   1st Qu.:  191.2   1st Qu.:   4395  
##  Median :0.00000   Median :   39.85   Median :  191.2   Median :   4395  
##  Mean   :0.04474   Mean   :   39.85   Mean   :  191.2   Mean   :   4395  
##  3rd Qu.:0.04474   3rd Qu.:   42.00   3rd Qu.:  191.2   3rd Qu.:   4395  
##  Max.   :1.00000   Max.   :   94.00   Max.   :56475.3   Max.   :2759604  
##  PhysicianOutpatient       Rx          
##  Min.   :  -4655.7   Min.   :  -469.5  
##  1st Qu.:    196.2   1st Qu.:  1140.4  
##  Median :   1752.3   Median :  1140.4  
##  Mean   :   1752.3   Mean   :  1140.4  
##  3rd Qu.:   1752.3   3rd Qu.:  1140.4  
##  Max.   :1481468.5   Max.   :631635.5
#Buscando columnas sin varianza
zero_var_cols <- sapply(bdc_imputed, function(x) var(x) == 0)

#Elimina esas columnas
bdc_imputed_no_zero_var <- bdc_imputed[, !zero_var_cols]

Cantidad de grupos

grupos <- 7
segmentos <- kmeans(bdc_imputed_no_zero_var,grupos)
asignacion <- cbind(bdc_imputed_no_zero_var, cluster=segmentos$cluster)
#Graficando resultados
fviz_cluster(segmentos, bdc_imputed_no_zero_var)

library(cluster)
library(data.table)
## Warning: package 'data.table' was built under R version 4.1.3
## 
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
set.seed(123)

#optimizacion <- clusGap(bdc_imputed_no_zero_var, FUN=kmeans, nstart=1, K.max = 7)

#plot(optimizacion, xlab="Número de clusters K")
LS0tDQp0aXRsZTogIkFjdGl2aWRhZCA0LjYiDQphdXRob3I6ICJFcXVpcG8gMSINCmRhdGU6ICIyMDIzLTA5LTI2Ig0Kb3V0cHV0OiAgDQogIGh0bWxfZG9jdW1lbnQ6DQogICAgdG9jOiBUUlVFDQogICAgdG9jX2Zsb2F0OiBUUlVFDQogICAgY29kZV9kb3dubG9hZDogVFJVRQ0KICAgIHRoZW1lOiAic2ltcGxleCINCiAgICBoaWdobGlnaHQ6ICJtb25vY2hyb21lIg0KLS0tDQoNCmBgYHtyIG1lc3NhZ2U9RkFMU0V9DQpsaWJyYXJ5KHJlYWR4bCkNCmxpYnJhcnkoZHBseXIpDQpgYGANCg0KIyBSZWdyZXNpw7NuIGNvbiB0b2RvcyBsb3MgRGF0b3MNCg0KIyMgSW1wb3J0IERhdGENCg0KYGBge3J9DQojZmlsZS5jaG9vc2U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