-
Notifications
You must be signed in to change notification settings - Fork 0
/
rankall.R
150 lines (127 loc) · 5.78 KB
/
rankall.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
rank <- function(state, outcome) {
## Read outcome data
## Check that state and outcome are valid
## Return adata frame with hospital name
## in that state, the rate
measures <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
states <- unique(measures$State)
if(!(state %in% states)){
stop("invalid state")
}
outcomelevels <- c("heart attack", "heart failure", "pneumonia")
if(!(outcome %in% outcomelevels)){
stop("invalid outcome")
}
outcomecol <- factor(outcome, outcomelevels, labels=c("Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack", "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure", "Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia"))
outcomecol <- as.character(outcomecol)
suppressWarnings(measures[, outcomecol] <- as.numeric(measures[, outcomecol]))
rescol <- "Hospital.Name"
observations <- subset(measures,(measures$State==state & !is.na(measures[outcomecol])), c(rescol,outcomecol))
observations <- observations[order(observations[outcomecol],observations[rescol]),]
observations
}
rankall <- function(outcome, num = "best"){
## Read outcome data
## Check that state and outcome are valid
## For each state, find the hospital of the given rank
## Return a data frame with the hospital names and the
## (abbreviated) state name
measures <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
states <- unique(measures$State)
outcomelevels <- c("heart attack", "heart failure", "pneumonia")
if(!(outcome %in% outcomelevels)){
stop("invalid outcome")
}
outcomecol <- factor(outcome, outcomelevels, labels=c("Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack", "Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure", "Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia"))
outcomecol <- as.character(outcomecol)
suppressWarnings(measures[, outcomecol] <- as.numeric(measures[, outcomecol]))
hnamecol <- "Hospital.Name"
statecol <- "State"
observations <- subset(measures,(!is.na(measures[outcomecol])), c(hnamecol,statecol,outcomecol))
observationsOrder <- order(observations[statecol],observations[outcomecol],observations[hnamecol])
observations <- observations[observationsOrder,]
observationsParts <- split(observations,observations[statecol])
selfunc <- function(obs){
i <- NULL
dftemp <- as.data.frame(obs)
rowcount <- nrow(dftemp)
if(is.numeric(num) && num > 0){
if(num <= rowcount)
i <- num
else
i <- NA
}
else if(is.character(num) && num == "best"){
i <- 1
}
else if(is.character(num) && num == "worst"){
i <- rowcount
}
else{
stop("invalid number")
}
if(!is.na(i))
dftemp[i,c(hnamecol,statecol)]
else{
dftemp <- dftemp[1,c(hnamecol,statecol)]
dftemp[1,hnamecol] <- NA
dftemp
}
}
observations <- sapply(observationsParts, selfunc)
observations <- t(observations)
colnames(observations) <- c("hospital","state")
as.data.frame(observations)
}
rankhospital <- function(state, outcome, num = "best") {
observations <- rank(state, outcome)
i <- NULL
rowcount <- nrow(observations)
if(is.numeric(num) && num > 0){
if(num <= rowcount)
i <- num
else
i <- NA
}
else if(is.character(num) && num == "best"){
i <- 1
}
else if(is.character(num) && num == "worst"){
i <- rowcount
}
else{
stop("invalid number")
}
if(is.na(i))
NA
else
observations[i,1]
}
best <- function(state, outcome){
## Return hospital name in that state with lowest 30-day death
## rateout
observations <- rank(state, outcome)
observations[1,1]
}
test <- function(){
r <- c()
test <- best("TX", "heart attack") == "CYPRESS FAIRBANKS MEDICAL CENTER"
r <- c(r, best1 = test)
test <- best("TX", "heart failure") == "FORT DUNCAN MEDICAL CENTER"
r <- c(r, best2 = test)
test <- best("MD", "heart attack") == "JOHNS HOPKINS HOSPITAL, THE"
r <- c(r, best3 = test)
test <- best("MD", "pneumonia") == "GREATER BALTIMORE MEDICAL CENTER"
r <- c(r, best4 = test)
test <- tryCatch(best("BB", "heart attack"), error = function(err) err$message == 'invalid state')
r <- c(r, best5 = test)
test <- tryCatch(best("NY", "hert attack"), error = function(err) err$message == 'invalid outcome')
r <- c(r, best6 = test)
test <- rankhospital("TX", "heart failure", 4) == "DETAR HOSPITAL NAVARRO"
r <- c(r, rankhospital1 = test)
test <- rankhospital("MD", "heart attack", "worst") == "HARFORD MEMORIAL HOSPITAL"
r <- c(r, rankhospital2 = test)
test <- is.na(rankhospital("MN", "heart attack", 5000))
r <- c(r, rankhospital3 = test)
r
}