Data Summary

outcome <- read.csv("C:/Users/angul/OneDrive/R/outcome-of-care-measures.csv", colClasses = "character")
# head(outcome)
# str(outcome[,c(2,7,11,17,19,23)])
summary(outcome[,c(2,7,11,17,19,23)])
##  Hospital.Name         State          
##  Length:4706        Length:4706       
##  Class :character   Class :character  
##  Mode  :character   Mode  :character  
##  Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack
##  Length:4706                                              
##  Class :character                                         
##  Mode  :character                                         
##  Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure
##  Length:4706                                               
##  Class :character                                          
##  Mode  :character                                          
##  Lower.Mortality.Estimate...Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure
##  Length:4706                                                                          
##  Class :character                                                                     
##  Mode  :character                                                                     
##  Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia
##  Length:4706                                           
##  Class :character                                      
##  Mode  :character

30-day mortality rates plot for heart attack

outcome[, 11] <- as.numeric(outcome[, 11])
## ignore NA warning ;/
hist(outcome[, 11])

Function 1:

Returns the state’s hospital with lowest 30-day death rate

best <- function(state, outcome) {
  
  ## Read the outcome data
  dat <- read.csv("C:/Users/angul/OneDrive/R/outcome-of-care-measures.csv", colClasses = "character")
  ## Check that state and outcome are valid
  if (!state %in% unique(dat[, 7])) {
    stop("invalid state")
  }
  switch(outcome, `heart attack` = {
    col = 11
  }, `heart failure` = {
    col = 17
  }, pneumonia = {
    col = 23
  }, stop("invalid outcome"))
  ## Return hospital name in that state with lowest 30-day death rate
  df = dat[dat$State == state, c(2, col)]
  df[which.min(df[, 2]), 1]
}

Finding the best hospital in a state

source("best.R")
# best("SC", "heart attack")
# best("NY", "pneumonia")
# best("AK", "pneumonia")

Function 2:

Returns the state’s hospital with the given rank 30-day death rate

rankhospital <- function(state, outcome, num = "best") {
  
  ## Read the outcome data
  dat <- read.csv("C:/Users/angul/OneDrive/R/outcome-of-care-measures.csv", colClasses = "character")
  ## Check that state and outcome are valid
  if (!state %in% unique(dat[, 7])) {
    stop("invalid state")
  }
  switch(outcome, `heart attack` = {
    col = 11
  }, `heart failure` = {
    col = 17
  }, pneumonia = {
    col = 23
  }, stop("invalid outcome"))
  dat[, col] = as.numeric(dat[, col])
  df = dat[dat[, 7] == state, c(2, col)]
  df = na.omit(df)
  nhospital = nrow(df)
  switch(num, best = {
    num = 1
  }, worst = {
    num = nhospital
  })
  if (num > nhospital) {
    return(NA)
  }
  ## Return hospital name in that state with the given rank 30-day death rate
  
  o = order(df[, 2], df[, 1])
  df[o, ][num, 1]
}
# rankhospital("NC", "heart attack", "worst")
# rankhospital("TX", "pneumonia", 10)
rankhospital("NY", "heart attack", 7)
## [1] "BELLEVUE HOSPITAL CENTER"

Function 3:

Ranks hospitals in all states

rankall <- function(outcome, num = "best") {
  ## Read the outcome data
  dat <- read.csv("C:/Users/angul/OneDrive/R/outcome-of-care-measures.csv", colClasses = "character")
  ## Check that state and outcome are valid
  states = unique(dat[, 7])
  switch(outcome, `heart attack` = {
    col = 11
  }, `heart failure` = {
    col = 17
  }, pneumonia = {
    col = 23
  }, stop("invalid outcome"))
  
  ## Return hospital name in that state with the given rank 30-day death rate
  dat[, col] = as.numeric(dat[, col])
  dat = dat[, c(2, 7, col)]  # leave only name, state, and death rate
  dat = na.omit(dat)
  # head(dat) Hospital.Name State 1 SOUTHEAST ALABAMA MEDICAL CENTER AL 2
  # MARSHALL MEDICAL CENTER SOUTH AL 3 ELIZA COFFEE MEMORIAL HOSPITAL AL 7 ST
  # VINCENT'S EAST AL 8 DEKALB REGIONAL MEDICAL CENTER AL 9 SHELBY BAPTIST
  # MEDICAL CENTER AL
  # Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack 1 14.3 2 18.5 3
  # 18.1 7 17.7 8 18.0 9 15.9
  rank_in_state <- function(state) {
    df = dat[dat[, 2] == state, ]
    nhospital = nrow(df)
    switch(num, best = {
      num = 1
    }, worst = {
      num = nhospital
    })
    if (num > nhospital) {
      result = NA
    }
    o = order(df[, 3], df[, 1])
    result = df[o, ][num, 1]
    c(result, state)
  }
  output = do.call(rbind, lapply(states, rank_in_state))
  output = output[order(output[, 2]), ]
  rownames(output) = output[, 2]
  colnames(output) = c("hospital", "state")
  data.frame(output)
}
head(rankall("heart attack", 20), 10)
## Warning in rankall("heart attack", 20): NAs introduced by coercion
r <- rankall("heart attack", 4)
as.character(subset(r, state == "HI")$hospital)
## [1] "CASTLE MEDICAL CENTER"
r <- rankall("pneumonia", "worst")
as.character(subset(r, state == "NJ")$hospital)
## [1] "BERGEN REGIONAL MEDICAL CENTER"
r <- rankall("heart failure", 10)
as.character(subset(r, state == "NV")$hospital)
## [1] "RENOWN SOUTH MEADOWS MEDICAL CENTER"