-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathjhu_prgwr_w4.Rmd
383 lines (339 loc) · 16.7 KB
/
jhu_prgwr_w4.Rmd
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
---
title: "JHU - Simulation & Profiling"
author: 郭耀仁
date: "`r Sys.Date()`"
output:
revealjs::revealjs_presentation:
theme: black
highlight: zenburn
center: true
---
## 作業來源
[Coursera](https://www.coursera.org/) 的 [Programming with R](https://www.coursera.org/learn/r-programming) 課程第四週作業
## 資料集
[Assignment3-data](https://storage.googleapis.com/jhu_coursera_data/Assignment3-data.zip)
## 資料內容
- outcome-of-care-measures.csv 30 天心臟病就診資料
- hospital-data.csv 4,000+ 醫院的資訊
- Hospital_Revised_Flatfiles.pdf 編碼手冊
# 練習一
## 練習一說明
- 將 outcome-of-care-measures.csv 讀入,並且觀察第 11 個變數的分佈
```{r}
library(ggplot2)
outcome <- read.csv("~/Downloads/Assignment3-data/outcome-of-care-measures.csv", stringsAsFactors = FALSE)
outcome[, 11] <- as.numeric(outcome[, 11])
```
## 練習一範例
```{r echo=FALSE, warning=FALSE}
ggplot(outcome, mapping = aes(x = Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack)) +
geom_histogram(bins = 40, fill = rgb(0.8, 0, 0, 0.5)) +
theme_minimal() +
ggtitle("30-day mortality rates for heart attack") +
xlab("30-day mortality rates")
```
# 練習二
## 練習二函數結構
`best(state, outcome)`
找出指定州別裡面最好的醫院
## 練習二函數提示
- `outcome` 參數可以輸入 "heart attack"、"heart failure" 或 "pneumonia"
- 如果有兩個以上醫院的 outcome 相同,則依照字母順序排序後回傳
- `suppressWarnings()` 可以消除警告訊息
- 假如輸入無效的 `state` 名稱,使用 `stop()` 函數回傳訊息 invalid state
- 假如輸入無效的 `outcome` 參數,使用 `stop()` 函數回傳訊息 invalid outcome
## 練習二函數範例
```{r echo=FALSE}
best <- function(state, outcome) {
outcome_df <- read.csv("~/Downloads/Assignment3-data/outcome-of-care-measures.csv", stringsAsFactors = FALSE)
unique_state_names <- unique(outcome_df$State)
unique_outcomes <- c("heart attack", "heart failure", "pneumonia")
cols_to_numeric <- 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")
for (char_col in cols_to_numeric) {
suppressWarnings(
outcome_df[, char_col] <- as.numeric(outcome_df[, char_col])
)
}
if (!(state %in% unique_state_names)) {
stop("invalid state")
} else if (!(outcome %in% unique_outcomes)) {
stop("invalid outcome")
} else {
state_outcome <- outcome_df[outcome_df$State == state, ]
if (outcome == "heart attack") {
lowest_indice <- which(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack == min(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack, na.rm = TRUE))
hospital_names <- state_outcome$Hospital.Name[lowest_indice]
hospital_names <- sort(hospital_names)
return(hospital_names)
} else if (outcome == "heart failure") {
lowest_indice <- which(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure == min(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure, na.rm = TRUE))
hospital_names <- state_outcome$Hospital.Name[lowest_indice]
hospital_names <- sort(hospital_names)
return(hospital_names)
} else if (outcome == "pneumonia") {
lowest_indice <- which(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia == min(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia, na.rm = TRUE))
hospital_names <- state_outcome$Hospital.Name[lowest_indice]
hospital_names <- sort(hospital_names)
return(hospital_names)
}
}
}
```
```{r}
best("TX", "heart attack")
best("TX", "heart failure")
best("MD", "heart attack")
```
----
```{r error=TRUE}
best("MD", "pneumonia")
best("BB", "heart attack")
best("NY", "hert attack")
```
# 練習三
## 練習三函數結構
`rankhospital(state, outcome, num = "best")`
將一個州依照 `outcome` 參數選出最好、最差或特定排名的醫院
## 練習三函數提示
- `outcome` 參數可以輸入 "heart attack"、"heart failure" 或 "pneumonia"
- 如果有兩個以上醫院的 outcome 相同,則依照字母順序排序後回傳
- 假如輸入無效的 `state` 名稱,使用 `stop()` 函數回傳訊息 invalid state
- 假如輸入無效的 `outcome` 參數,使用 `stop()` 函數回傳訊息 invalid outcome
## 練習三函數範例
```{r echo = FALSE}
rankhospital <- function(state, outcome, num = "best") {
outcome_df <- read.csv("~/Downloads/Assignment3-data/outcome-of-care-measures.csv", stringsAsFactors = FALSE)
unique_state_names <- unique(outcome_df$State)
unique_outcomes <- c("heart attack", "heart failure", "pneumonia")
cols_to_numeric <- 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")
for (char_col in cols_to_numeric) {
suppressWarnings(
outcome_df[, char_col] <- as.numeric(outcome_df[, char_col])
)
}
if (!(state %in% unique_state_names)) {
stop("invalid state")
} else if (!(outcome %in% unique_outcomes)) {
stop("invalid outcome")
} else {
state_outcome <- outcome_df[outcome_df$State == state, ]
if (outcome == "heart attack") {
if (num == "best") {
lowest_indice <- which(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack == min(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack, na.rm = TRUE))
hospital_names <- state_outcome$Hospital.Name[lowest_indice]
hospital_names <- sort(hospital_names)
return(hospital_names)
} else if (num == "worst") {
lowest_indice <- which(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack == max(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack, na.rm = TRUE))
hospital_names <- state_outcome$Hospital.Name[lowest_indice]
hospital_names <- sort(hospital_names)
return(hospital_names)
} else if (num > nrow(state_outcome)) {
return(NA)
} else if (is.numeric(num)) {
state_outcome_ordered <- state_outcome[order(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack, state_outcome$Hospital.Name), ]
return(state_outcome_ordered$Hospital.Name[num])
} else {
stop("invalid num")
}
} else if (outcome == "heart failure") {
if (num == "best") {
lowest_indice <- which(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure == min(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure, na.rm = TRUE))
hospital_names <- state_outcome$Hospital.Name[lowest_indice]
hospital_names <- sort(hospital_names)
return(hospital_names)
} else if (num == "worst") {
lowest_indice <- which(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure == max(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure, na.rm = TRUE))
hospital_names <- state_outcome$Hospital.Name[lowest_indice]
hospital_names <- sort(hospital_names)
return(hospital_names)
} else if (num > nrow(state_outcome)) {
return(NA)
} else if (is.numeric(num)) {
state_outcome_ordered <- state_outcome[order(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure, state_outcome$Hospital.Name), ]
return(state_outcome_ordered$Hospital.Name[num])
} else {
stop("invalid num")
}
} else if (outcome == "pneumonia") {
if (num == "best") {
lowest_indice <- which(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia == min(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia, na.rm = TRUE))
hospital_names <- state_outcome$Hospital.Name[lowest_indice]
hospital_names <- sort(hospital_names)
return(hospital_names)
} else if (num == "worst") {
lowest_indice <- which(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia == max(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia, na.rm = TRUE))
hospital_names <- state_outcome$Hospital.Name[lowest_indice]
hospital_names <- sort(hospital_names)
return(hospital_names)
} else if (num > nrow(state_outcome)) {
return(NA)
} else if (is.numeric(num)) {
state_outcome_ordered <- state_outcome[order(state_outcome$Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia, state_outcome$Hospital.Name), ]
return(state_outcome_ordered$Hospital.Name[num])
} else {
stop("invalid num")
}
}
}
}
```
```{r}
rankhospital("TX", "heart failure", 4)
rankhospital("MD", "heart attack", "worst")
rankhospital("MN", "heart attack", 5000)
```
# 練習四
## 練習四函數結構
`rankall(outcome, num = "best")`
將全國的醫院依照 `outcome` 參數選出各州最好、最差或特定排名的醫院
## 練習四函數提示
- `outcome` 參數可以輸入 "heart attack"、"heart failure" 或 "pneumonia"
- 如果有兩個以上醫院的 outcome 相同,則依照字母順序排序後回傳
- 假如輸入無效的 `outcome` 參數,使用 `stop()` 函數回傳訊息 invalid outcome
## 練習四函數範例
```{r echo = FALSE}
rankall <- function(outcome, num = "best") {
outcome_df <- read.csv("~/Downloads/Assignment3-data/outcome-of-care-measures.csv", stringsAsFactors = FALSE)
unique_state_names <- sort(unique(outcome_df$State))
unique_outcomes <- c("heart attack", "heart failure", "pneumonia")
cols_to_numeric <- 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")
for (char_col in cols_to_numeric) {
suppressWarnings(
outcome_df[, char_col] <- as.numeric(outcome_df[, char_col])
)
}
if (!(outcome %in% unique_outcomes)) {
stop("invalid outcome")
} else {
if (outcome == "heart attack") {
if (num == "best") {
result_df <- data.frame(best_hospitals = c(), states = c())
for (state in unique_state_names) {
each_state <- outcome_df[outcome_df$State == state, ]
each_state_ordered <- each_state[order(each_state$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack, each_state$Hospital.Name), ]
best_in_each_state <- each_state_ordered$Hospital.Name[1]
df_to_bind <- data.frame(best_hospitals = best_in_each_state, states = state)
result_df <- rbind(result_df, df_to_bind)
}
names(result_df) <- c("hospital", "state")
return(result_df)
} else if (num == "worst") {
result_df <- data.frame(worst_hospitals = c(), states = c())
for (state in unique_state_names) {
each_state <- outcome_df[outcome_df$State == state, ]
each_state_ordered <- each_state[order(each_state$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack, each_state$Hospital.Name, decreasing = TRUE), ]
worst_in_each_state <- each_state_ordered$Hospital.Name[length(each_state_ordered$Hospital.Name)]
df_to_bind <- data.frame(worst_hospitals = worst_in_each_state, states = state)
result_df <- rbind(result_df, df_to_bind)
}
names(result_df) <- c("hospital", "state")
return(result_df)
} else if (is.numeric(num)) {
result_df <- data.frame(best_hospitals = c(), states = c())
for (state in unique_state_names) {
each_state <- outcome_df[outcome_df$State == state, ]
ordered_in_each_state <- each_state[order(each_state$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Attack, each_state$Hospital.Name), ]
if (num > nrow(ordered_in_each_state)) {
df_to_bind <- data.frame(best_hospitals = NA, states = state)
result_df <- rbind(result_df, df_to_bind)
} else {
df_to_bind <- data.frame(best_hospitals = ordered_in_each_state$Hospital.Name[num], states = state)
result_df <- rbind(result_df, df_to_bind)
}
}
names(result_df) <- c("hospital", "state")
return(result_df)
}
} else if (outcome == "heart failure") {
if (num == "best") {
result_df <- data.frame(best_hospitals = c(), states = c())
for (state in unique_state_names) {
each_state <- outcome_df[outcome_df$State == state, ]
each_state_ordered <- each_state[order(each_state$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure, each_state$Hospital.Name), ]
best_in_each_state <- each_state_ordered$Hospital.Name[1]
df_to_bind <- data.frame(best_hospitals = best_in_each_state, states = state)
result_df <- rbind(result_df, df_to_bind)
}
names(result_df) <- c("hospital", "state")
return(result_df)
} else if (num == "worst") {
result_df <- data.frame(worst_hospitals = c(), states = c())
for (state in unique_state_names) {
each_state <- outcome_df[outcome_df$State == state, ]
each_state_ordered <- each_state[order(each_state$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure, each_state$Hospital.Name, decreasing = TRUE), ]
worst_in_each_state <- each_state_ordered$Hospital.Name[length(each_state_ordered$Hospital.Name)]
df_to_bind <- data.frame(worst_hospitals = worst_in_each_state, states = state)
result_df <- rbind(result_df, df_to_bind)
}
names(result_df) <- c("hospital", "state")
return(result_df)
} else if (is.numeric(num)) {
result_df <- data.frame(best_hospitals = c(), states = c())
for (state in unique_state_names) {
each_state <- outcome_df[outcome_df$State == state, ]
ordered_in_each_state <- each_state$Hospital.Name[order(each_state$Hospital.30.Day.Death..Mortality..Rates.from.Heart.Failure, each_state$Hospital.Name), ]
if (num > length(ordered_in_each_state)) {
df_to_bind <- data.frame(best_hospitals = NA, states = state)
result_df <- rbind(result_df, df_to_bind)
} else {
df_to_bind <- data.frame(best_hospitals = ordered_in_each_state[num], states = state)
result_df <- rbind(result_df, df_to_bind)
}
}
names(result_df) <- c("hospital", "state")
return(result_df)
}
} else if (outcome == "pneumonia") {
if (num == "best") {
result_df <- data.frame(best_hospitals = c(), states = c())
for (state in unique_state_names) {
each_state <- outcome_df[outcome_df$State == state, ]
each_state_ordered <- each_state[order(each_state$Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia, each_state$Hospital.Name), ]
best_in_each_state <- each_state_ordered$Hospital.Name[1]
df_to_bind <- data.frame(best_hospitals = best_in_each_state, states = state)
result_df <- rbind(result_df, df_to_bind)
}
names(result_df) <- c("hospital", "state")
return(result_df)
} else if (num == "worst") {
result_df <- data.frame(worst_hospitals = c(), states = c())
for (state in unique_state_names) {
each_state <- outcome_df[outcome_df$State == state, ]
each_state_ordered <- each_state[order(each_state$Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia, each_state$Hospital.Name, decreasing = TRUE), ]
worst_in_each_state <- each_state_ordered$Hospital.Name[1]
df_to_bind <- data.frame(worst_hospitals = worst_in_each_state, states = state)
result_df <- rbind(result_df, df_to_bind)
}
names(result_df) <- c("hospital", "state")
return(result_df)
} else if (is.numeric(num)) {
best_hospitals <- c()
states <- c()
result_df <- data.frame(best_hospitals, states)
for (state in unique_state_names) {
each_state <- outcome_df[outcome_df$State == state, ]
ordered_in_each_state <- each_state$Hospital.Name[order(each_state$Hospital.30.Day.Death..Mortality..Rates.from.Pneumonia, each_state$Hospital.Name), ]
if (num > length(ordered_in_each_state)) {
df_to_bind <- data.frame(best_hospitals = NA, states = state)
result_df <- rbind(result_df, df_to_bind)
} else {
df_to_bind <- data.frame(best_hospitals = ordered_in_each_state[num], states = state)
result_df <- rbind(result_df, df_to_bind)
}
}
names(result_df) <- c("hospital", "state")
return(result_df)
}
}
}
}
```
```{r}
head(rankall("heart attack", 20), 10)
tail(rankall("pneumonia", "worst"), 3)
```
----
```{r}
tail(rankall("heart failure"), 10)
```