setwd("~/My Stuff/Masters/MPA/PAD 6833/Data")
tab1<-read.delim("PUDF_base1_1q2020_tab.txt")
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ lubridate 1.9.5     ✔ tibble    3.3.1
## ✔ purrr     1.2.2     ✔ tidyr     1.3.2
## ✔ readr     2.2.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
diabetes_subset <- subset(
  tab1,
  grepl("^E11", PRINC_DIAG_CODE)
)
illness_model <- lm(ILLNESS_SEVERITY ~ PAT_AGE, data=diabetes_subset)
summary(illness_model)
## 
## Call:
## lm(formula = ILLNESS_SEVERITY ~ PAT_AGE, data = diabetes_subset)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.5588 -0.4582 -0.1909  0.5531  2.3023 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)  
## (Intercept)  1.000e+00  7.549e-01   1.325   0.1853  
## PAT_AGE02    1.250e+00  8.440e-01   1.481   0.1386  
## PAT_AGE03    1.333e+00  8.716e-01   1.530   0.1261  
## PAT_AGE04    9.091e-01  7.662e-01   1.186   0.2355  
## PAT_AGE05    6.977e-01  7.636e-01   0.914   0.3609  
## PAT_AGE06    8.056e-01  7.653e-01   1.053   0.2925  
## PAT_AGE07    1.017e+00  7.581e-01   1.342   0.1796  
## PAT_AGE08    1.102e+00  7.570e-01   1.456   0.1454  
## PAT_AGE09    1.109e+00  7.562e-01   1.466   0.1426  
## PAT_AGE10    1.183e+00  7.556e-01   1.565   0.1175  
## PAT_AGE11    1.237e+00  7.554e-01   1.637   0.1016  
## PAT_AGE12    1.337e+00  7.552e-01   1.771   0.0766 .
## PAT_AGE13    1.316e+00  7.552e-01   1.743   0.0813 .
## PAT_AGE14    1.421e+00  7.551e-01   1.882   0.0599 .
## PAT_AGE15    1.447e+00  7.551e-01   1.916   0.0554 .
## PAT_AGE16    1.501e+00  7.551e-01   1.988   0.0468 *
## PAT_AGE17    1.530e+00  7.552e-01   2.025   0.0428 *
## PAT_AGE18    1.458e+00  7.553e-01   1.931   0.0535 .
## PAT_AGE19    1.493e+00  7.554e-01   1.976   0.0481 *
## PAT_AGE20    1.494e+00  7.558e-01   1.976   0.0482 *
## PAT_AGE21    1.506e+00  7.572e-01   1.989   0.0467 *
## PAT_AGE22   -1.972e-11  1.068e+00   0.000   1.0000  
## PAT_AGE23    1.191e+00  7.560e-01   1.575   0.1152  
## PAT_AGE24    1.408e+00  7.555e-01   1.864   0.0624 .
## PAT_AGE25    1.488e+00  7.580e-01   1.963   0.0497 *
## PAT_AGE26    1.559e+00  7.659e-01   2.035   0.0418 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7549 on 13497 degrees of freedom
## Multiple R-squared:  0.02897,    Adjusted R-squared:  0.02717 
## F-statistic: 16.11 on 25 and 13497 DF,  p-value: < 2.2e-16
c_diabetes<- diabetes_subset |> drop_na(PAT_AGE)
c_diabetes$PAT_AGE <- as.numeric(c_diabetes$PAT_AGE)
## Warning: NAs introduced by coercion
illness_model <- lm(ILLNESS_SEVERITY ~ PAT_AGE, data=c_diabetes)
summary(illness_model)
## 
## Call:
## lm(formula = ILLNESS_SEVERITY ~ PAT_AGE, data = c_diabetes)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.6111 -0.4077 -0.3059  0.5923  1.8365 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 2.082158   0.026727   77.91   <2e-16 ***
## PAT_AGE     0.020344   0.001691   12.03   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7612 on 13520 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.01059,    Adjusted R-squared:  0.01052 
## F-statistic: 144.7 on 1 and 13520 DF,  p-value: < 2.2e-16
c_diabetes <- c_diabetes %>%
  filter(!FIRST_PAYMENT_SRC %in% c("`", "", "ZZ", NA)) %>%
  mutate(
    INSURED = ifelse(FIRST_PAYMENT_SRC == "09", 0, 1)
  )
c_diabetes <- c_diabetes %>% filter (!SEX_CODE %in% c("U", "'", NA)) %>% mutate(SEX = ifelse(SEX_CODE == "M", 0, 1))
c_diabetes <- c_diabetes %>% filter (!RACE %in% c("'","5", NA)) %>% mutate(WHITE= ifelse(SEX_CODE == "4", 1, 0))
c_diabetes <- c_diabetes %>% filter (!ETHNICITY %in% c("'", NA)) %>% mutate(HISPANIC= ifelse(ETHNICITY == "1", 1, 0))
illness_model <- lm(ILLNESS_SEVERITY ~ PAT_AGE + PAT_AGE + SEX_CODE + RACE + ETHNICITY + FIRST_PAYMENT_SRC, data=c_diabetes)
summary(illness_model)
## 
## Call:
## lm(formula = ILLNESS_SEVERITY ~ PAT_AGE + PAT_AGE + SEX_CODE + 
##     RACE + ETHNICITY + FIRST_PAYMENT_SRC, data = c_diabetes)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.7207 -0.4829 -0.2168  0.5276  2.0315 
## 
## Coefficients: (1 not defined because of singularities)
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          1.209340   0.542910   2.228  0.02594 *  
## PAT_AGE              0.020217   0.003125   6.470 1.03e-10 ***
## SEX_CODEF            0.142735   0.044128   3.235  0.00122 ** 
## SEX_CODEM            0.215530   0.043896   4.910 9.26e-07 ***
## RACE1                0.683593   0.557017   1.227  0.21976    
## RACE2                0.547819   0.537143   1.020  0.30781    
## RACE3                0.535417   0.532455   1.006  0.31465    
## RACE4                0.461062   0.532308   0.866  0.38643    
## ETHNICITY`           0.005805   0.251216   0.023  0.98156    
## ETHNICITY1           0.084164   0.018869   4.460 8.28e-06 ***
## ETHNICITY2                 NA         NA      NA       NA    
## FIRST_PAYMENT_SRC12  0.144410   0.079496   1.817  0.06932 .  
## FIRST_PAYMENT_SRC13  0.040307   0.110596   0.364  0.71553    
## FIRST_PAYMENT_SRC14 -0.221935   0.537359  -0.413  0.67961    
## FIRST_PAYMENT_SRC15  0.138838   0.092327   1.504  0.13268    
## FIRST_PAYMENT_SRC16  0.251665   0.078351   3.212  0.00132 ** 
## FIRST_PAYMENT_SRCBL  0.054311   0.082061   0.662  0.50809    
## FIRST_PAYMENT_SRCCH  0.073328   0.122604   0.598  0.54980    
## FIRST_PAYMENT_SRCCI  0.151344   0.085043   1.780  0.07517 .  
## FIRST_PAYMENT_SRCHM  0.173289   0.082801   2.093  0.03639 *  
## FIRST_PAYMENT_SRCMA  0.326040   0.077160   4.226 2.41e-05 ***
## FIRST_PAYMENT_SRCMB -0.192488   0.146860  -1.311  0.18999    
## FIRST_PAYMENT_SRCMC  0.206706   0.079438   2.602  0.00928 ** 
## FIRST_PAYMENT_SRCOF  0.031861   0.132501   0.240  0.80998    
## FIRST_PAYMENT_SRCVA  0.041431   0.137732   0.301  0.76357    
## FIRST_PAYMENT_SRCWC  0.458060   0.239149   1.915  0.05547 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7522 on 9612 degrees of freedom
## Multiple R-squared:  0.03121,    Adjusted R-squared:  0.02879 
## F-statistic:  12.9 on 24 and 9612 DF,  p-value: < 2.2e-16
illness_model <- lm(ILLNESS_SEVERITY ~ PAT_AGE + SEX + HISPANIC + WHITE + INSURED, data=c_diabetes)
summary(illness_model)
## 
## Call:
## lm(formula = ILLNESS_SEVERITY ~ PAT_AGE + SEX + HISPANIC + WHITE + 
##     INSURED, data = c_diabetes)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.6333 -0.4642 -0.3172  0.5568  1.8479 
## 
## Coefficients: (2 not defined because of singularities)
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  2.128289   0.035160  60.532  < 2e-16 ***
## PAT_AGE      0.020994   0.002191   9.581  < 2e-16 ***
## SEX         -0.081209   0.015741  -5.159 2.53e-07 ***
## HISPANIC     0.061396   0.017954   3.420  0.00063 ***
## WHITE              NA         NA      NA       NA    
## INSURED            NA         NA      NA       NA    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.759 on 9633 degrees of freedom
## Multiple R-squared:  0.01141,    Adjusted R-squared:  0.0111 
## F-statistic: 37.06 on 3 and 9633 DF,  p-value: < 2.2e-16

#This model has an r-squared of 0.01141 suggesting it only covers about 1% of the data set. This it is overall significant with the p-value: < 2.2e-16. This is a really big data set with a lot of different factors that can relate to illness severity so that makes sense that I am not covering much with my model, but that seems kind of crazy.

#The model created does suggest that patient age, sex, and hispanic status are significant. I am not sure why NA appears for some of my variables. Patient age is a misnomer because it is suggesting that each section is one year so that as the year increases by 1, the illness severity increases by 0.02. Looking at the dictionary though, the numbers correlate to an age range so 11 actually covers 40-44 and 12 covers 45-49 (looking at it now I also need to cut out 22-26 in the future because those are specifically HIV and drug/alcohol use patients). Would this work for the binomials like sex? If the patient is male then they it is predicted their illness severity would go down 0.08 and if they are hispanic then their severity increases by 0.06?

table(c_diabetes$FIRST_PAYMENT_SRC)
## 
##   11   12   13   14   15   16   BL   CH   CI   HM   MA   MB   MC   OF   VA   WC 
##   99  947   87    2  202 1786  557   61  378  503 3904   36  973   48   43   11
table(c_diabetes$FIRST_PAYMENT_SRC)
## 
##   11   12   13   14   15   16   BL   CH   CI   HM   MA   MB   MC   OF   VA   WC 
##   99  947   87    2  202 1786  557   61  378  503 3904   36  973   48   43   11

I really wanted to look and see if insurance status would change the severity of Type 2 Diabetes but since some of the uninsured patients might be coded as ZZ and that is mixed with patients that are unknown, I don’t think I can do that. It appears there is a difference between the different insurances though potentially when I ran things as not a binomial?