forked from NLMichaud/WeeklyCDCPlot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.Rhistory
More file actions
512 lines (512 loc) · 28 KB
/
Copy path.Rhistory
File metadata and controls
512 lines (512 loc) · 28 KB
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
levels(d$Disease)[41] <- "Strep toxic shock synd"
levels(d$Disease)[42] <- "Syphilis congenital <1yr"
levels(d$Disease)[42] <- "Toxic shock synd staph"
levels(d$Disease)[47] <- "Vanco Interm Staph A"
levels(d$Disease)[48] <- "Vanco Resist Staph A"
d$threshold[is.na(d$threshold)]<-d$c[is.na(d$threshold)]
d$alert[is.na(d$alert)]<-"N"
write.table(d, file="infreq.txt", row.names=FALSE, col.names=TRUE)
write.table(unique(d$Disease), file="inf_dis.txt",row.names=FALSE, col.names=TRUE)
shiny::runApp('Documents/PhD/Spring 15/mmwr scrape/CDCPlot')
getwd()
setwd("/home/nick/Documents/PhD/Spring 15/mmwr scrape/CDCPlot"")
setwd("/home/nick/Documents/PhD/Spring 15/mmwr scrape/CDCPlot")
all <- matrix(c(
c("Cryptosporidiosis","Cryptosporidiosis", "b36e-ru3r", "2014"),
c("Cryptosporidiosis","Cryptosporidiosis", "9n3x-apcd", "2015"),
c("Salmonellosis", "Salmonellosis", "52cr-rw4k", "2014"),
c("Salmonellosis", "Salmonellosis", "d6kj-devz", "2105"),
c("Shigellosis","Shigellosis", "52cr-rw4k", "2014"),
c("Shigellosis","Shigellosis", "n3wf-wtep", "2015"),
c("Pertussis","Pertussis", "8rkx-vimh", "2014"),
c("Pertussis","Pertussis", "d69q-iyrb", "2015"),
c("Malaria","Malaria", "y6uv-t34t", "2014"),
c("Malaria","Malaria", "7pb7-w9us", "2015"),
c("Legionellosis","Legionellosis", "23gt-ssfe", "2014"),
c("Legionellosis","Legionellosis", "ydsy-yh5w", "2015"),
c("Hepatitis A", "Hepatitis..viral..acute...type.A","rg4j-6mcc", "2014"),
c("Hepatitis A", "Hepatitis..viral..acute...type.A","65xe-6neq", "2015"),
c("Hepatitis B, Acute", "Hepatitis..viral..acute...type.B","rg4j-6mcc","2014"),
c("Hepatitis B, Acute", "Hepatitis..viral..acute...type.B","65xe-6neq","2015"),
c("Hepatitis C, Acute", "Hepatitis..viral..acute...type.C","rg4j-6mcc","2014"),
c("Hepatitis C, Acute", "Hepatitis..viral..acute...type.C","65xe-6neq","2015"),
c("Giardiasis", "Giardiasis", "9ix3-ryt6","2014"),
c("Giardiasis", "Giardiasis", "mpdg-hf57","2015"),
c("Meningococcal Disease Invasive (all serogroups)", "Meningococcal.disease..invasive...All.serogroups", "y6uv-t34t","2014"),
c("Meningococcal Disease Invasive (all serogroups)", "Meningococcal.disease..invasive...All.serogroups", "7pb7-w9us","2015"),
c("Mumps", "Mumps", "8rkx-vimh","2014"),
c("Mumps", "Mumps", "d69q-iyrb","2015"),
#c("Pneumonia and Influenza Mortality Reports by City/Region, 2014", "P.I..Total","qpap-3u8w"),
#leave out pneumonia for now, format is too different
c("Shiga toxin-producing E. coli (STEC)", "Shiga.toxin.producing.E..coli..STEC..", "52cr-rw4k","2014"),
c("Shiga toxin-producing E. coli (STEC)", "Shiga.toxin.producing.E..coli..STEC..", "n3wf-wtep","2015"),
c("P&I MORT", "P&I MORT", "qpap-3u8w", "2014"),
c("P&I MORT", "P&I MORT", "7esm-uptm", "2015")
)
,ncol=4, byrow=T)
#Name matrix columns and write to csv file
URL_NAMES <- data.frame(display_name=all[,1],data_name=all[,2],url=all[,3],year=all[,4])
write.table(URL_NAMES, file="urldat.txt", row.names=FALSE, col.names=TRUE)
# Read in data with disease names and corresponding urls. This data is created from the url_names.R file, which should be run first.
urldat <- read.table("urldat.txt", header=T)
# A function to help deal with NA values when calculating thresholds. NA's occur when we try to
# calculate running standard deviations with only one data point, and cause an error in the cumsum function.
# Args:
# x: A vector of disease occurance data that we wish to calculate an alert threshold for
# days: an integer for the number of days to calculate the threshold over
newthresh <- function(x,days){
thresh <-runmean(x, days,endrule="NA")+2*runsd(x, days,endrule="sd",align="right")
thresh[is.na(thresh)]<-x[is.na(thresh)]
return(thresh)
}
# This function takes each url and corresponding disease name and gets data from CDC. It then combines multiple years worth of data,
# calculates alert thresholds and cumulative sums and returns the columns of interest from the CDC data.
# Args:
# url_data: the rows of the url_data.txt file which contain the urls for a given disease
url_func <- function(url_data){
# Construct actual CDC website url name and get data for 2014 and 2015
curl <- url_data$url
URL <- paste( "https://data.cdc.gov/api/views/",curl, "/rows.csv?accessType=DOWNLOAD",sep="")
nndss14 <-read.csv(textConnection(getURL(URL[1],ssl.verifypeer=FALSE)),strip.white=T,stringsAsFactors=F)
nndss15 <- read.csv(textConnection(getURL(URL[2],ssl.verifypeer=FALSE)),strip.white=T,stringsAsFactors=F)
# Some diseases have a slightly different name for MMWR.Week and MMWR.Year, so we standardize the names here
if("MMWRWeek"%in%names(nndss14)){nndss14<- dplyr::rename(nndss14,MMWR.Week=MMWRWeek )}
if("MMWR.WEEK"%in%names(nndss14)){nndss14<- dplyr::rename(nndss14,MMWR.Week=MMWR.WEEK )}
if("MMWRYear"%in%names(nndss14)){nndss14<- dplyr::rename(nndss14, MMWR.Year=MMWRYear )}
if("MMWR.YEAR"%in%names(nndss14)){nndss14<- dplyr::rename(nndss14, MMWR.Year=MMWR.YEAR )}
if("MMWRWeek"%in%names(nndss15)){nndss15<- dplyr::rename(nndss15, MMWR.Week=MMWRWeek )}
if("MMWR.WEEK"%in%names(nndss15)){nndss15<- dplyr::rename(nndss15, MMWR.Week=MMWR.WEEK )}
if("MMWRYear"%in%names(nndss15)){nndss15<- dplyr::rename(nndss15, MMWR.Year=MMWRYear )}
if("MMWR.YEAR"%in%names(nndss15)){nndss15<- dplyr::rename(nndss15, MMWR.Year=MMWR.YEAR )}
# dname is the name of the column in the nndss file which contains weekly data for the disease of interest
dname <- c(paste(url_data$data_name[1],"..Current.week",sep=""))
#special column name for P&I mortality data
if(url_data$data_name[1]=="P&I MORT")dname <- "P.I..Total"
# Select relevant columns from both the 2014 and 2015 data and rbind them together
nndss <- rbind(select(nndss14, contains(dname), contains("MMWR"), contains("Reporting"), -contains("flag")),
select(nndss15, contains(dname), contains("MMWR"), contains("Reporting"), -contains("flag")))
# set NA values to 0, maybe not a great idea, but useful for calculating thresholds and cumulative sums
names(nndss)[which(dname==names(nndss))] <- "c"
nndss$c <- as.numeric(nndss$c)
nndss$c[is.na(nndss$c)]<-0
nndss$display_name <- url_data$display_name[1]
# Create columns for 10 and 14 week thresholds and 10 and 14 week alerts, grouping by reporting area.
nndss <- nndss %>% group_by(Reporting.Area) %>% mutate(fourteenwk.thresh=newthresh(c,14),
tenwk.thresh=newthresh(c,10),
fourteenwk.alert=c>fourteenwk.thresh,
tenwk.alert=c>tenwk.thresh)
# Create columns for cumulative sum along with cumulative threshold values, grouping both by reporting area and year
nndss <- group_by(nndss, Reporting.Area, MMWR.Year) %>% mutate(cumulate=cumsum(c),
cumu10=cumulate+(tenwk.thresh-c),
cumu14=cumulate+(fourteenwk.thresh-c))
#select and return relevant columns of data table
nndss<- select(nndss, one_of("c","Reporting.Area", "MMWR.Year", "MMWR.Week","display_name"), contains("thresh"),contains("cumu"),contains("alert"))
return(nndss)
}
# Run the url_func function for each different disease name in our urldat.txt data file
output <- ddply(urldat, .(data_name), url_func)
Encoding(output$Reporting.Area) <- "latin1"
output$Reporting.Area <- iconv(output$Reporting.Area, "latin1", "ASCII", sub="")
# Write output as plotdat.csv
write.table(output, file="plotdat.txt", row.names=FALSE, col.names=TRUE)
# Separate output file which contains all disease names called disease_names.csv
write.table(unique(output$display_name), file="disease_names.txt", row.names=FALSE, col.names=TRUE)
# Separate output file which contains locations and location types (state, region, or country) called location_names.cd, doesn't include p&i data
regions <-c("NEW ENGLAND", "MID. ATLANTIC", "E.N. CENTRAL", "W.N. CENTRAL", "S. ATLANTIC",
"E.S. CENTRAL", "W.S. CENTRAL", "MOUNTAIN", "PACIFIC", "TERRITORIES")
loc_type <- rep("state", length(unique(output$Reporting.Area[output$data_name!="P&I MORT"])))
loc_type[which(unique(output$Reporting.Area[output$data_name!="P&I MORT"])%in%regions)] <- "region"
loc_type[1] <- "country"
# Also include, for state locations, which region the state falls under. Thankfully, the CDC data table is ordered so that it first lists a region, then
# all the states in that region, then the next region, and so on. So, between each region name, all states will be in the same region
region_num=0
loc_reg <- rep("NONE", length(loc_type))
for(i in 1:62){
if(loc_type[i]=="region"){
region_num = region_num+1
}
if(loc_type[i]=="state"){
loc_reg[i]=regions[region_num]
}
}
loc_reg[63:67] <- "TERRITORIES"
all_locs<-data.frame(location=unique(output$Reporting.Area[output$data_name!="P&I MORT"]),type=loc_type, region=loc_reg)
write.table(all_locs, file="location_names.txt", row.names=FALSE, col.names=TRUE)
pi_loc <- rep("city", length(unique(output$Reporting.Area[output$data_name=="P&I MORT"])))
pi_loc[which(tolower(unique(output$Reporting.Area[output$data_name=="P&I MORT"]))%in%tolower(regions))] <- "region"
pi_loc[length(pi_loc)] <- "country"
region_num=0
pi_reg <- rep("NONE", length(pi_loc))
for(i in 1:length(pi_loc)){
if(pi_loc[i]=="region"){
region_num = region_num+1
}
if(pi_loc[i]=="city"){
pi_reg[i]=regions[region_num]
}
}
pi_locs<-data.frame(location=unique(output$Reporting.Area[output$data_name=="P&I MORT"]),type=pi_loc, region=pi_reg)
pi_locs <- data.frame(lapply(pi_locs, as.character), stringsAsFactors=FALSE)
write.table(pi_locs, file="pi_names.txt", row.names=FALSE, col.names=TRUE)
#separate code for infrequent diseases.
URL <- c("https://data.cdc.gov/api/views/wcwi-x3uk/rows.csv?accessType=DOWNLOAD",
"https://data.cdc.gov/api/views/pb4z-432k/rows.csv?accessType=DOWNLOAD")
nndss14 <-read.csv(textConnection(getURL(URL[1],ssl.verifypeer=FALSE)),strip.white=T,stringsAsFactors=F)
nndss15 <- read.csv(textConnection(getURL(URL[2],ssl.verifypeer=FALSE)),strip.white=T,stringsAsFactors=F)
nndss <- rbind(select(nndss14, contains("Current.week"), contains("MMWR"), contains("Disease"), -contains("flag")),
select(nndss15, contains("Current.week"),contains("MMWR"), contains("Disease"), -contains("flag")))
#disease names are different bewteen years, try to clean some disease names up
Encoding(nndss$Disease) <- "latin1"
nndss$Disease <- iconv(nndss$Disease, "latin1", "ASCII", sub="")
nndss$Disease <- gsub(":","",nndss$Disease)
nndss$Disease <- gsub(",","",nndss$Disease)
nndss$Disease <- gsub("\\*","",nndss$Disease)
#remove all disease names which aren't present in both years
nndss <- nndss[-which(nndss$Disease%in%names(which(table(nndss$Disease)<54))),]
d <- nndss %>% group_by(Disease, MMWR.week, MMWR.year)%>%summarise(c=Current.week)
d$c <- as.numeric(d$c)
d <- d %>% mutate(c = ifelse(is.na(c),0,c))
d <- d %>% group_by(Disease) %>% mutate("fourteenweekmean"=runmean(c, 2, align="right"))
d <- d %>% mutate(fourteenweekmean = ifelse(is.na(fourteenweekmean),0,fourteenweekmean))
d <- d%>% group_by(Disease) %>% mutate ("fourteenweeksd"=runsd(c, 14,center=runmean(c,14),align="right"))
d$sd2 <- d$fourteenweeksd*2
d$threshold <- d$fourteenweekmean + d$sd2
d$alert <- ifelse(d$c > d$threshold, "Y", "N")
d$Disease <- as.factor(d$Disease)
levels(d$Disease)[3] <- "Arbo,EEE"
levels(d$Disease)[2] <- "Arbo,CA serogroup"
levels(d$Disease)[4] <- "Arbo,Powassan"
levels(d$Disease)[5] <- "Arbo,St Louis"
levels(d$Disease)[6] <- "Arbo,WEE"
levels(d$Disease)[9] <- "Botulism other"
levels(d$Disease)[14] <- "Cyclosporiasis"
levels(d$Disease)[16] <- "H flu <5 non-b"
levels(d$Disease)[17] <- "H flu <5 b"
levels(d$Disease)[18] <- "H flu <5 unknown"
levels(d$Disease)[19] <- "Hansen Disease"
levels(d$Disease)[20] <- "HUS,postdiarrheal"
levels(d$Disease)[21] <- "HBV,perinatal"
levels(d$Disease)[22] <- "Influenza ped mort"
levels(d$Disease)[25] <- "Measles"
levels(d$Disease)[26] <- "Mening a,c,y,w-135"
levels(d$Disease)[27] <- "Mening other"
levels(d$Disease)[28] <- "Mening serogroup b"
levels(d$Disease)[29] <- "Mening unknown"
levels(d$Disease)[30] <- "Novel influenza A"
levels(d$Disease)[32] <- "Polio nonparalytic"
levels(d$Disease)[34] <- "Psittacosis"
levels(d$Disease)[37] <- "Q Fever, Total"
levels(d$Disease)[39] <- "SARS-CoV"
levels(d$Disease)[40] <- "Smallpox"
levels(d$Disease)[41] <- "Strep toxic shock synd"
levels(d$Disease)[42] <- "Syphilis congenital <1yr"
levels(d$Disease)[42] <- "Toxic shock synd staph"
levels(d$Disease)[47] <- "Vanco Interm Staph A"
levels(d$Disease)[48] <- "Vanco Resist Staph A"
d$threshold[is.na(d$threshold)]<-d$c[is.na(d$threshold)]
d$alert[is.na(d$alert)]<-"N"
write.table(d, file="infreq.txt", row.names=FALSE, col.names=TRUE)
write.table(unique(d$Disease), file="inf_dis.txt",row.names=FALSE, col.names=TRUE)
shiny::runApp()
pi_names <- read.table("pi_names.txt", header=T,colClasses=c("character","character"))
pi_names$region
shiny::runApp()
sort(filter(pi_names, type=="region")$location)
sort(filter(location_names, type=="region")$location)
location_names <- read.table("location_names.txt", header=T, colClasses=c("character","character"))
sort(filter(location_names, type=="region")$location)
shiny::runApp()
?toupper
toupper(pi_names$location[which(pi_names$type=="region")])
pi_names$location[which(pi_names$type=="region")]
shiny::runApp()
pi_names$type
pi_names$location
pi_names$location[which(pi_names$type=="region")]<- toupper(pi_names$location[which(pi_names$type=="region")])
pi_names$location
filter(cdcdata, display_name == "P&I MORT", Reporting.Area %in% pi_names$location[which(pi_names$type=="region")])
cdcdata <- read.table("plotdat.txt", header=T)
filter(cdcdata, display_name == "P&I MORT", Reporting.Area %in% pi_names$location[which(pi_names$type=="region")])
pi_names$location[which(pi_names$type=="region")]
unique(cdcdata$Reporting.Area)
pi_names <- read.table("pi_names.txt", header=T,colClasses=c("character","character"))
pi_names
shiny::runApp()
cdcdata$Reporting.Area
location_names
toupper(cdcdata$Reporting.Area[toupper(cdcdata$Reporting.Area)%in%location_names$region])
shiny::runApp()
infreq <- read.table("infreq.txt", header=T)
infreq
head(infreq)
head()
head(d)
d <- d %>% group_by(Disease, MMWR.Year) %>% mutate(cumulate=cumsum(c),
cumu14=cumulate+(threshold-c))
?mutate
d <- d %>% group_by(Disease, MMWR.year) %>% mutate(cumulate=cumsum(c),
cumu14=cumulate+(threshold-c))
d$Disease <- as.factor(d$Disease)
levels(d$Disease)[3] <- "Arbo,EEE"
levels(d$Disease)[2] <- "Arbo,CA serogroup"
levels(d$Disease)[4] <- "Arbo,Powassan"
levels(d$Disease)[5] <- "Arbo,St Louis"
levels(d$Disease)[6] <- "Arbo,WEE"
levels(d$Disease)[9] <- "Botulism other"
levels(d$Disease)[14] <- "Cyclosporiasis"
levels(d$Disease)[16] <- "H flu <5 non-b"
levels(d$Disease)[17] <- "H flu <5 b"
levels(d$Disease)[18] <- "H flu <5 unknown"
levels(d$Disease)[19] <- "Hansen Disease"
levels(d$Disease)[20] <- "HUS,postdiarrheal"
levels(d$Disease)[21] <- "HBV,perinatal"
levels(d$Disease)[22] <- "Influenza ped mort"
levels(d$Disease)[25] <- "Measles"
levels(d$Disease)[26] <- "Mening a,c,y,w-135"
levels(d$Disease)[27] <- "Mening other"
levels(d$Disease)[28] <- "Mening serogroup b"
levels(d$Disease)[29] <- "Mening unknown"
levels(d$Disease)[30] <- "Novel influenza A"
levels(d$Disease)[32] <- "Polio nonparalytic"
levels(d$Disease)[34] <- "Psittacosis"
levels(d$Disease)[37] <- "Q Fever, Total"
levels(d$Disease)[39] <- "SARS-CoV"
levels(d$Disease)[40] <- "Smallpox"
levels(d$Disease)[41] <- "Strep toxic shock synd"
levels(d$Disease)[42] <- "Syphilis congenital <1yr"
levels(d$Disease)[42] <- "Toxic shock synd staph"
levels(d$Disease)[47] <- "Vanco Interm Staph A"
levels(d$Disease)[48] <- "Vanco Resist Staph A"
d$threshold[is.na(d$threshold)]<-d$c[is.na(d$threshold)]
d$alert[is.na(d$alert)]<-"N"
write.table(d, file="infreq.txt", row.names=FALSE, col.names=TRUE)
write.table(unique(d$Disease), file="inf_dis.txt",row.names=FALSE, col.names=TRUE)
shiny::runApp()
d <- nndss %>% group_by(Disease, MMWR.week, MMWR.year)%>%summarise(c=Current.week)
d$c <- as.numeric(d$c)
d <- d %>% mutate(c = ifelse(is.na(c),0,c))
d <- d %>% group_by(Disease) %>% mutate("fourteenweekmean"=runmean(c, 14, align="right"))
d <- d %>% mutate(fourteenweekmean = ifelse(is.na(fourteenweekmean),0,fourteenweekmean))
d <- d%>% group_by(Disease) %>% mutate ("fourteenweeksd"=runsd(c, 14,center=runmean(c,14),align="right"))
d$sd2 <- d$fourteenweeksd*2
d$threshold <- d$fourteenweekmean + d$sd2
d$alert <- ifelse(d$c > d$threshold, "Y", "N")
d <- d %>% group_by(Disease, MMWR.year) %>% mutate(cumulate=cumsum(c),
cumu14=cumulate+(threshold-c))
d$Disease <- as.factor(d$Disease)
levels(d$Disease)[3] <- "Arbo,EEE"
levels(d$Disease)[2] <- "Arbo,CA serogroup"
levels(d$Disease)[4] <- "Arbo,Powassan"
levels(d$Disease)[5] <- "Arbo,St Louis"
levels(d$Disease)[6] <- "Arbo,WEE"
levels(d$Disease)[9] <- "Botulism other"
levels(d$Disease)[14] <- "Cyclosporiasis"
levels(d$Disease)[16] <- "H flu <5 non-b"
levels(d$Disease)[17] <- "H flu <5 b"
levels(d$Disease)[18] <- "H flu <5 unknown"
levels(d$Disease)[19] <- "Hansen Disease"
levels(d$Disease)[20] <- "HUS,postdiarrheal"
levels(d$Disease)[21] <- "HBV,perinatal"
levels(d$Disease)[22] <- "Influenza ped mort"
levels(d$Disease)[25] <- "Measles"
levels(d$Disease)[26] <- "Mening a,c,y,w-135"
levels(d$Disease)[27] <- "Mening other"
levels(d$Disease)[28] <- "Mening serogroup b"
levels(d$Disease)[29] <- "Mening unknown"
levels(d$Disease)[30] <- "Novel influenza A"
levels(d$Disease)[32] <- "Polio nonparalytic"
levels(d$Disease)[34] <- "Psittacosis"
levels(d$Disease)[37] <- "Q Fever, Total"
levels(d$Disease)[39] <- "SARS-CoV"
levels(d$Disease)[40] <- "Smallpox"
levels(d$Disease)[41] <- "Strep toxic shock synd"
levels(d$Disease)[42] <- "Syphilis congenital <1yr"
levels(d$Disease)[42] <- "Toxic shock synd staph"
levels(d$Disease)[47] <- "Vanco Interm Staph A"
levels(d$Disease)[48] <- "Vanco Resist Staph A"
d$threshold[is.na(d$threshold)]<-d$c[is.na(d$threshold)]
d$alert[is.na(d$alert)]<-"N"
write.table(d, file="infreq.txt", row.names=FALSE, col.names=TRUE)
write.table(unique(d$Disease), file="inf_dis.txt",row.names=FALSE, col.names=TRUE)
shiny::runApp()
pi_names
shiny::runApp()
?h4
shiny::runApp()
shiny::runApp()
URL <- "https://ibis.health.state.nm.us/resources/MMWRWeekCalendar.html"
getURL(URL,ssl.verifypeer=FALSE))
getURL(URL,ssl.verifypeer=FALSE)
library(XML)
readHTMLTable(URL)
htmlParse(URL)
URL <- "https://ibis.health.state.nm.us/resources/MMWRWeekCalendar.html"
htmlParse(URL)
getURL(URL)
dates <-getURL(URL)
readHTMLTable(dates)
htmlParse(dates)
readHTMLTable(dates)
readHTMLTable(dates)$'NULL'
str(readHTMLTable(dates))
readHTMLTable(dates)[[7]]
URL <- "https://ibis.health.state.nm.us/resources/MMWRWeekCalendar.html"
dates <-getURL(URL)
dates <- readHTMLTable(dates)[[7]]
str(dates)
library(lubridate)
?readHTMLTable
dates <- readHTMLTable(dates, header=T)[[7]]
dates <-getURL(URL)
dates <- readHTMLTable(dates, header=T)[[7]]
dates
names(dates) <- c("MMWR.Week", "2011", "2012", "2013", "2014", "2015")
dates
dates <- dates[-1,]
dates
dates$2014
names(dates) <- c("MMWR.Week", "y2011", "y2012", "y2013", "y2014", "y2015")
dates$y2014
?which
getwd()
write.table(dates, file="dates.txt", row.names=FALSE, col.names=TRUE)
dates <- read.table("dates.txt", header=T)
dates
newthresh <- function(x,days){
thresh <-runmean(x, days,endrule="NA")+2*runsd(x, days,endrule="sd",align="right")
thresh[is.na(thresh)]<-x[is.na(thresh)]
return(thresh)
}
# This function takes each url and corresponding disease name and gets data from CDC. It then combines multiple years worth of data,
# calculates alert thresholds and cumulative sums and returns the columns of interest from the CDC data.
# Args:
# url_data: the rows of the url_data.txt file which contain the urls for a given disease
url_func <- function(url_data){
# Construct actual CDC website url name and get data for 2014 and 2015
curl <- url_data$url
URL <- paste( "https://data.cdc.gov/api/views/",curl, "/rows.csv?accessType=DOWNLOAD",sep="")
nndss14 <-read.csv(textConnection(getURL(URL[1],ssl.verifypeer=FALSE)),strip.white=T,stringsAsFactors=F)
nndss15 <- read.csv(textConnection(getURL(URL[2],ssl.verifypeer=FALSE)),strip.white=T,stringsAsFactors=F)
# Some diseases have a slightly different name for MMWR.Week and MMWR.Year, so we standardize the names here
if("MMWRWeek"%in%names(nndss14)){nndss14<- dplyr::rename(nndss14,MMWR.Week=MMWRWeek )}
if("MMWR.WEEK"%in%names(nndss14)){nndss14<- dplyr::rename(nndss14,MMWR.Week=MMWR.WEEK )}
if("MMWRYear"%in%names(nndss14)){nndss14<- dplyr::rename(nndss14, MMWR.Year=MMWRYear )}
if("MMWR.YEAR"%in%names(nndss14)){nndss14<- dplyr::rename(nndss14, MMWR.Year=MMWR.YEAR )}
if("MMWRWeek"%in%names(nndss15)){nndss15<- dplyr::rename(nndss15, MMWR.Week=MMWRWeek )}
if("MMWR.WEEK"%in%names(nndss15)){nndss15<- dplyr::rename(nndss15, MMWR.Week=MMWR.WEEK )}
if("MMWRYear"%in%names(nndss15)){nndss15<- dplyr::rename(nndss15, MMWR.Year=MMWRYear )}
if("MMWR.YEAR"%in%names(nndss15)){nndss15<- dplyr::rename(nndss15, MMWR.Year=MMWR.YEAR )}
# dname is the name of the column in the nndss file which contains weekly data for the disease of interest
dname <- c(paste(url_data$data_name[1],"..Current.week",sep=""))
#special column name for P&I mortality data
if(url_data$data_name[1]=="P&I MORT")dname <- "P.I..Total"
# Select relevant columns from both the 2014 and 2015 data and rbind them together
nndss <- rbind(select(nndss14, contains(dname), contains("MMWR"), contains("Reporting"), -contains("flag")),
select(nndss15, contains(dname), contains("MMWR"), contains("Reporting"), -contains("flag")))
# set NA values to 0, maybe not a great idea, but useful for calculating thresholds and cumulative sums
names(nndss)[which(dname==names(nndss))] <- "c"
nndss$c <- as.numeric(nndss$c)
nndss$c[is.na(nndss$c)]<-0
nndss$display_name <- url_data$display_name[1]
# Create columns for 10 and 14 week thresholds and 10 and 14 week alerts, grouping by reporting area.
nndss <- nndss %>% group_by(Reporting.Area) %>% mutate(fourteenwk.thresh=newthresh(c,14),
tenwk.thresh=newthresh(c,10),
fourteenwk.alert=c>fourteenwk.thresh,
tenwk.alert=c>tenwk.thresh)
# Create columns for cumulative sum along with cumulative threshold values, grouping both by reporting area and year
nndss <- group_by(nndss, Reporting.Area, MMWR.Year) %>% mutate(cumulate=cumsum(c),
cumu10=cumulate+(tenwk.thresh-c),
cumu14=cumulate+(fourteenwk.thresh-c))
#select and return relevant columns of data table
nndss<- select(nndss, one_of("c","Reporting.Area", "MMWR.Year", "MMWR.Week","display_name"), contains("thresh"),contains("cumu"),contains("alert"))
nndss$date <- apply(nndss,1, function(x){return(dates[which(dates$MMWR.Week==x$MMWR.Week),5+(x$MMWR.Year-2014)])}
return(nndss)
}
# Run the url_func function for each different disease name in our urldat.txt data file
output <- ddply(urldat, .(data_name), url_func)
url_func <- function(url_data){
# Construct actual CDC website url name and get data for 2014 and 2015
curl <- url_data$url
URL <- paste( "https://data.cdc.gov/api/views/",curl, "/rows.csv?accessType=DOWNLOAD",sep="")
nndss14 <-read.csv(textConnection(getURL(URL[1],ssl.verifypeer=FALSE)),strip.white=T,stringsAsFactors=F)
nndss15 <- read.csv(textConnection(getURL(URL[2],ssl.verifypeer=FALSE)),strip.white=T,stringsAsFactors=F)
# Some diseases have a slightly different name for MMWR.Week and MMWR.Year, so we standardize the names here
if("MMWRWeek"%in%names(nndss14)){nndss14<- dplyr::rename(nndss14,MMWR.Week=MMWRWeek )}
if("MMWR.WEEK"%in%names(nndss14)){nndss14<- dplyr::rename(nndss14,MMWR.Week=MMWR.WEEK )}
if("MMWRYear"%in%names(nndss14)){nndss14<- dplyr::rename(nndss14, MMWR.Year=MMWRYear )}
if("MMWR.YEAR"%in%names(nndss14)){nndss14<- dplyr::rename(nndss14, MMWR.Year=MMWR.YEAR )}
if("MMWRWeek"%in%names(nndss15)){nndss15<- dplyr::rename(nndss15, MMWR.Week=MMWRWeek )}
if("MMWR.WEEK"%in%names(nndss15)){nndss15<- dplyr::rename(nndss15, MMWR.Week=MMWR.WEEK )}
if("MMWRYear"%in%names(nndss15)){nndss15<- dplyr::rename(nndss15, MMWR.Year=MMWRYear )}
if("MMWR.YEAR"%in%names(nndss15)){nndss15<- dplyr::rename(nndss15, MMWR.Year=MMWR.YEAR )}
# dname is the name of the column in the nndss file which contains weekly data for the disease of interest
dname <- c(paste(url_data$data_name[1],"..Current.week",sep=""))
#special column name for P&I mortality data
if(url_data$data_name[1]=="P&I MORT")dname <- "P.I..Total"
# Select relevant columns from both the 2014 and 2015 data and rbind them together
nndss <- rbind(select(nndss14, contains(dname), contains("MMWR"), contains("Reporting"), -contains("flag")),
select(nndss15, contains(dname), contains("MMWR"), contains("Reporting"), -contains("flag")))
# set NA values to 0, maybe not a great idea, but useful for calculating thresholds and cumulative sums
names(nndss)[which(dname==names(nndss))] <- "c"
nndss$c <- as.numeric(nndss$c)
nndss$c[is.na(nndss$c)]<-0
nndss$display_name <- url_data$display_name[1]
# Create columns for 10 and 14 week thresholds and 10 and 14 week alerts, grouping by reporting area.
nndss <- nndss %>% group_by(Reporting.Area) %>% mutate(fourteenwk.thresh=newthresh(c,14),
tenwk.thresh=newthresh(c,10),
fourteenwk.alert=c>fourteenwk.thresh,
tenwk.alert=c>tenwk.thresh)
# Create columns for cumulative sum along with cumulative threshold values, grouping both by reporting area and year
nndss <- group_by(nndss, Reporting.Area, MMWR.Year) %>% mutate(cumulate=cumsum(c),
cumu10=cumulate+(tenwk.thresh-c),
cumu14=cumulate+(fourteenwk.thresh-c))
#select and return relevant columns of data table
nndss<- select(nndss, one_of("c","Reporting.Area", "MMWR.Year", "MMWR.Week","display_name"), contains("thresh"),contains("cumu"),contains("alert"))
nndss$date <- apply(nndss,1, function(x){return(dates[which(dates$MMWR.Week==x$MMWR.Week),5+(x$MMWR.Year-2014)])})
return(nndss)
}
output <- ddply(urldat, .(data_name), url_func)
dates$MMWR.Week
?apply
url_func <- function(url_data){
# Construct actual CDC website url name and get data for 2014 and 2015
curl <- url_data$url
URL <- paste( "https://data.cdc.gov/api/views/",curl, "/rows.csv?accessType=DOWNLOAD",sep="")
nndss14 <-read.csv(textConnection(getURL(URL[1],ssl.verifypeer=FALSE)),strip.white=T,stringsAsFactors=F)
nndss15 <- read.csv(textConnection(getURL(URL[2],ssl.verifypeer=FALSE)),strip.white=T,stringsAsFactors=F)
# Some diseases have a slightly different name for MMWR.Week and MMWR.Year, so we standardize the names here
if("MMWRWeek"%in%names(nndss14)){nndss14<- dplyr::rename(nndss14,MMWR.Week=MMWRWeek )}
if("MMWR.WEEK"%in%names(nndss14)){nndss14<- dplyr::rename(nndss14,MMWR.Week=MMWR.WEEK )}
if("MMWRYear"%in%names(nndss14)){nndss14<- dplyr::rename(nndss14, MMWR.Year=MMWRYear )}
if("MMWR.YEAR"%in%names(nndss14)){nndss14<- dplyr::rename(nndss14, MMWR.Year=MMWR.YEAR )}
if("MMWRWeek"%in%names(nndss15)){nndss15<- dplyr::rename(nndss15, MMWR.Week=MMWRWeek )}
if("MMWR.WEEK"%in%names(nndss15)){nndss15<- dplyr::rename(nndss15, MMWR.Week=MMWR.WEEK )}
if("MMWRYear"%in%names(nndss15)){nndss15<- dplyr::rename(nndss15, MMWR.Year=MMWRYear )}
if("MMWR.YEAR"%in%names(nndss15)){nndss15<- dplyr::rename(nndss15, MMWR.Year=MMWR.YEAR )}
# dname is the name of the column in the nndss file which contains weekly data for the disease of interest
dname <- c(paste(url_data$data_name[1],"..Current.week",sep=""))
#special column name for P&I mortality data
if(url_data$data_name[1]=="P&I MORT")dname <- "P.I..Total"
# Select relevant columns from both the 2014 and 2015 data and rbind them together
nndss <- rbind(select(nndss14, contains(dname), contains("MMWR"), contains("Reporting"), -contains("flag")),
select(nndss15, contains(dname), contains("MMWR"), contains("Reporting"), -contains("flag")))
# set NA values to 0, maybe not a great idea, but useful for calculating thresholds and cumulative sums
names(nndss)[which(dname==names(nndss))] <- "c"
nndss$c <- as.numeric(nndss$c)
nndss$c[is.na(nndss$c)]<-0
nndss$display_name <- url_data$display_name[1]
# Create columns for 10 and 14 week thresholds and 10 and 14 week alerts, grouping by reporting area.
nndss <- nndss %>% group_by(Reporting.Area) %>% mutate(fourteenwk.thresh=newthresh(c,14),
tenwk.thresh=newthresh(c,10),
fourteenwk.alert=c>fourteenwk.thresh,
tenwk.alert=c>tenwk.thresh)
# Create columns for cumulative sum along with cumulative threshold values, grouping both by reporting area and year
nndss <- group_by(nndss, Reporting.Area, MMWR.Year) %>% mutate(cumulate=cumsum(c),
cumu10=cumulate+(tenwk.thresh-c),
cumu14=cumulate+(fourteenwk.thresh-c))
#select and return relevant columns of data table
nndss<- select(nndss, one_of("c","Reporting.Area", "MMWR.Year", "MMWR.Week","display_name"), contains("thresh"),contains("cumu"),contains("alert"))
nndss$date <- apply(nndss,1, function(x){return(dates[which(datesMMWR.Week==x['MMWR.Week']),5+(x['MMWR.Year']-2014)])})
return(nndss)
}
output <- ddply(urldat, .(data_name), url_func)