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)
tab_clean <- tab1[complete.cases(tab1$LENGTH_OF_STAY, tab1$TOTAL_CHARGES_ANCIL, tab1$FIRST_PAYMENT_SRC), ]

cor(tab_clean$LENGTH_OF_STAY, tab_clean$TOTAL_CHARGES_ANCIL)
## [1] 0.2026408
View(tab_clean)
tab_subset <- tab_clean %>% filter(FIRST_PAYMENT_SRC %in% c("09", "12", "14", "16", "DS", "MA", "MB", "MC", "CI")) 
tab_subset <- tab_subset %>% mutate(FIRST_PAYMENT_SRC_NUM = recode (tab_subset$FIRST_PAYMENT_SRC,
    "09" = 1,
    "12" = 2,
    "14" = 3,
    "16" = 4,
    "DS" = 5,
    "MA" = 6,
    "MB" = 7,
    "MC" = 8,
    "CI" = 9
  )
)
class(tab_subset$LENGTH_OF_STAY)
## [1] "integer"
class(tab_subset$TOTAL_CHARGES_ANCIL)
## [1] "numeric"
class(tab_subset$FIRST_PAYMENT_SRC_NUM)
## [1] "numeric"
tab_subset$LENGTH_OF_STAY <- as.numeric(tab_subset$LENGTH_OF_STAY)
cor(
tab_subset[, c(
"LENGTH_OF_STAY",
"TOTAL_CHARGES_ANCIL",
"FIRST_PAYMENT_SRC_NUM"
)],
use = "complete.obs"
)
##                       LENGTH_OF_STAY TOTAL_CHARGES_ANCIL FIRST_PAYMENT_SRC_NUM
## LENGTH_OF_STAY            1.00000000          0.50112864            0.02061089
## TOTAL_CHARGES_ANCIL       0.50112864          1.00000000           -0.08440577
## FIRST_PAYMENT_SRC_NUM     0.02061089         -0.08440577            1.00000000
pairs(~ LENGTH_OF_STAY + TOTAL_CHARGES_ANCIL + FIRST_PAYMENT_SRC_NUM,
  data = tab_subset)

cor.test(tab_subset$LENGTH_OF_STAY, tab_subset$TOTAL_CHARGES_ANCIL,method="spearman")
## Warning in cor.test.default(tab_subset$LENGTH_OF_STAY,
## tab_subset$TOTAL_CHARGES_ANCIL, : cannot compute exact p-value with ties
## 
##  Spearman's rank correlation rho
## 
## data:  tab_subset$LENGTH_OF_STAY and tab_subset$TOTAL_CHARGES_ANCIL
## S = 1.8542e+16, p-value < 2.2e-16
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
##       rho 
## 0.3244623

#A correlation test was done on the length of stay and total charges ancillary using the spearman method because the data is not normally distributed and there are a high number of observations. From the Spearman’s test we can see there is a somewhat positive correlation between length of stay and the total charges. The relationship is not strong as seen by the rho = 0.32 but the P-vlaue does indicate a strong significance at 2.2e-16.