I wrote some code to compress the weekly data into bi-weeks (26 bi-weeks per year). The climate data is average max and min temperatures per bi-week.
## this is the code I used to compress the weekly data into biweeks
BAL <- read.csv("C:/Users/Lisa/Desktop/677/Baltimore.csv", header = T, stringsAsFactors = FALSE)
## split into an array by year
BAL.yr <- split(BAL, f = BAL$YEAR)
## find number of weeks in each year
WPY <- rep(NA, 34)
for (i in 1:34) {
WPY[i] <- length(BAL.yr[[i]]$WEEK)
}
## since some years have 53 weeks instead of 52, I removed all the week
## 53s we have so much data I don't think this will affect our results
## much the number of weeks per year must be even to use biweeks!
BAL.1 <- BAL[BAL$WEEK != 53, ]
BIWEEK <- rep(rep(1:26, each = 2), 34)
BIWEEK.p <- (BIWEEK/26) - (1/26)
BAL.1 <- data.frame(BAL.1, BIWEEK, BIWEEK.p)
YR.BI <- BAL.1$YEAR + BAL.1$BIWEEK.p
BAL.1 <- data.frame(BAL.1, YR.BI)
BAL.1$CASES <- as.numeric(BAL.1$CASES)
## Warning: NAs introduced by coercion
CASES.BI <- rep(NA, length(BAL.1$WEEK))
for (i in seq(1, length(BAL.1$WEEK), by = 2)) {
CASES.BI[i] <- BAL.1$CASES[i] + BAL.1$CASES[i + 1]
CASES.BI[i + 1] <- CASES.BI[i]
}
TMAXAV.BI <- rep(NA, length(BAL.1$WEEK))
TMINAV.BI <- rep(NA, length(BAL.1$WEEK))
for (i in seq(1, length(BAL.1$WEEK), by = 2)) {
TMAXAV.BI[i] <- (BAL.1$TMAXAV[i] + BAL.1$TMAXAV[i + 1])/2
TMAXAV.BI[i + 1] <- TMAXAV.BI[i]
}
for (i in seq(1, length(BAL.1$WEEK), by = 2)) {
TMINAV.BI[i] <- (BAL.1$TMINAV[i] + BAL.1$TMINAV[i + 1])/2
TMINAV.BI[i + 1] <- TMINAV.BI[i]
}
for (i in seq(1, length(BAL.1$WEEK), by = 2)) {
CASES.BI[i] <- BAL.1$CASES[i] + BAL.1$CASES[i + 1]
CASES.BI[i + 1] <- CASES.BI[i]
}
BAL.1 <- data.frame(BAL.1, CASES.BI, TMAXAV.BI, TMINAV.BI)
plot(BAL.1$YR.BI, BAL.1$CASES.BI, type = "l")
BAL.BWK <- BAL.1[, c(1, 22:27)]
names(BAL.BWK) <- c("YEAR", "BIWEEK", "BIWEEK.p", "YR.BWK", "CASES", "TMAXAV",
"TMINAV")
## remove duplicate rows ##
BAL.BWK <- BAL.BWK[seq(1, length(BAL.BWK$YEAR), by = 2), ]
## this is what the final dataset looks like ##
head(BAL.BWK, 52)
## YEAR BIWEEK BIWEEK.p YR.BWK CASES TMAXAV TMINAV
## 1 1920 1 0.00000 1920 181 24.214 -46.1429
## 3 1920 2 0.03846 1920 274 4.429 -61.7857
## 5 1920 3 0.07692 1920 270 47.929 -32.1429
## 7 1920 4 0.11538 1920 323 46.786 -26.5714
## 9 1920 5 0.15385 1920 308 46.786 -48.8571
## 11 1920 6 0.19231 1920 372 133.357 23.0000
## 13 1920 7 0.23077 1920 380 172.143 71.8571
## 15 1920 8 0.26923 1920 528 155.143 52.7857
## 17 1920 9 0.30769 1920 560 183.714 90.4286
## 19 1920 10 0.34615 1920 560 195.214 101.0714
## 21 1920 11 0.38462 1920 503 244.000 146.3571
## 23 1920 12 0.42308 1920 306 275.000 170.1429
## 25 1920 13 0.46154 1920 158 275.929 177.7143
## 27 1920 14 0.50000 1920 100 294.857 205.5714
## 29 1920 15 0.53846 1921 42 283.357 190.0714
## 31 1920 16 0.57692 1921 18 288.500 194.4286
## 33 1920 17 0.61538 1921 16 272.143 205.1429
## 35 1920 18 0.65385 1921 16 259.071 175.3571
## 37 1920 19 0.69231 1921 15 250.714 155.8571
## 39 1920 20 0.73077 1921 6 237.214 144.8571
## 41 1920 21 0.76923 1921 12 250.500 135.2143
## 43 1920 22 0.80769 1921 19 208.357 108.7143
## 45 1920 23 0.84615 1921 15 112.929 44.0714
## 47 1920 24 0.88462 1921 26 104.429 52.0714
## 49 1920 25 0.92308 1921 38 97.000 35.2857
## 51 1920 26 0.96154 1921 29 46.857 -17.9286
## 53 1921 1 0.00000 1921 NA 91.357 15.1429
## 55 1921 2 0.03846 1921 56 48.000 -31.7143
## 57 1921 3 0.07692 1921 74 70.357 4.0000
## 59 1921 4 0.11538 1921 145 83.357 -0.4286
## 61 1921 5 0.15385 1921 98 133.071 41.6429
## 63 1921 6 0.19231 1921 103 175.500 71.3571
## 65 1921 7 0.23077 1921 105 200.000 73.0714
## 67 1921 8 0.26923 1921 131 184.857 79.0000
## 69 1921 9 0.30769 1921 187 196.143 118.6429
## 71 1921 10 0.34615 1921 233 211.143 118.5714
## 73 1921 11 0.38462 1921 236 253.643 147.5714
## 75 1921 12 0.42308 1921 165 270.214 166.0000
## 77 1921 13 0.46154 1921 85 314.357 206.0714
## 79 1921 14 0.50000 1922 48 306.286 231.2857
## 81 1921 15 0.53846 1922 18 309.571 215.9286
## 83 1921 16 0.57692 1922 20 288.071 199.5714
## 85 1921 17 0.61538 1922 9 277.286 189.7857
## 87 1921 18 0.65385 1922 15 292.857 204.5000
## 89 1921 19 0.69231 1922 4 273.429 194.9286
## 91 1921 20 0.73077 1922 6 241.500 152.9286
## 93 1921 21 0.76923 1922 16 208.000 89.3571
## 95 1921 22 0.80769 1922 13 176.071 87.7143
## 97 1921 23 0.84615 1922 46 107.929 35.2143
## 99 1921 24 0.88462 1922 107 138.071 61.5000
## 101 1921 25 0.92308 1922 140 78.214 17.8571
## 103 1921 26 0.96154 1922 116 57.857 -24.2143
## plots of cases temps by decade ##
plot(BAL.BWK$YR.BWK, BAL.BWK$CASES, type = "l", lwd = 2, xlim = c(1920, 1930))
lines(BAL.BWK$YR.BWK, BAL.BWK$TMAXAV * 10, col = "skyblue")
lines(BAL.BWK$YR.BWK, BAL.BWK$TMINAV * 10, col = "orange")
plot(BAL.BWK$YR.BWK, BAL.BWK$CASES, type = "l", lwd = 2, xlim = c(1930, 1940))
lines(BAL.BWK$YR.BWK, BAL.BWK$TMAXAV * 10, col = "skyblue")
lines(BAL.BWK$YR.BWK, BAL.BWK$TMINAV * 10, col = "orange")
plot(BAL.BWK$YR.BWK, BAL.BWK$CASES, type = "l", lwd = 2, xlim = c(1940, 1950))
lines(BAL.BWK$YR.BWK, BAL.BWK$TMAXAV * 10, col = "skyblue")
lines(BAL.BWK$YR.BWK, BAL.BWK$TMINAV * 10, col = "orange")
plot(BAL.BWK$YR.BWK, BAL.BWK$CASES, type = "l", lwd = 2, xlim = c(1950, 1960))
lines(BAL.BWK$YR.BWK, BAL.BWK$TMAXAV * 10, col = "skyblue")
lines(BAL.BWK$YR.BWK, BAL.BWK$TMINAV * 10, col = "orange")
## plot cases by year ##
BAL.BWKs <- split(BAL.BWK, f = BAL.BWK$YEAR)
for (i in 1:29) {
plot(BAL.BWKs[[i]]$BIWEEK, BAL.BWKs[[i]]$CASES, type = "l", lwd = 2, xlab = BAL.BWKs[[i]]$YEAR[1],
ylab = "Cases")
}
for (i in 33:34) {
plot(BAL.BWKs[[i]]$BIWEEK, BAL.BWKs[[i]]$CASES, type = "l", lwd = 2, xlab = BAL.BWKs[[i]]$YEAR[1],
ylab = "Cases")
}