alpha = 0.98
SAN <- read.csv("C:/Users/Lisa/Desktop/677/SAN.csv", header = TRUE, stringsAsFactors = FALSE)
SAN <- SAN[seq(1, length(SAN$CASES.i), by = 2), ]
## definition of school indicator
SCH <- c(-1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1, 1, 1, 1, 1, 1, -1)
SCH <- rep(SCH, length(SAN$CASES))[1:length(SAN$CASES)]
SAN$SCH <- SCH
## to analyze a subset of the data
SAN <- SAN[c(1:length(SAN$CASES)), ]
SAN$CASES <- as.numeric(SAN$CASES)
SAN$CASES.i <- as.numeric(SAN$CASES.i)
SAN$TMAX <- as.numeric(SAN$TMAX)
SAN$TMIN <- as.numeric(SAN$TMIN)
SAN$WEEK <- as.numeric(SAN$WEEK)
SAN$BWK <- as.numeric(SAN$BWK)
SAN$SCH <- as.numeric(SAN$SCH)
SAN$B <- as.numeric(SAN$B)
SAN$B.5 <- as.numeric(SAN$B.5)
## to change back and forth from B.5 vs. B SAN$B.5 <- SAN$B
## this is what the data looks like head(SAN)
plot(cumsum(SAN$B.5), cumsum(SAN$CASES.i), type = "l", xlab = "Cumulative sum of births (lagged 5 years)",
ylab = "Cumulative sum of reported cases")
x <- c(1:1e+06)
lines(x, x, col = "red")
legend("bottomright", c("incidence", "expected incidence"), col = c("black",
"red", "gray"), lty = 1)
## fit a smooth spline of cumulative measles on cumulative births with 2.5
## degrees of freedom
cum.reg <- smooth.spline(cumsum(SAN$B.5), cumsum(SAN$CASES.i), df = 2.5)
lm <- lm(cumsum(SAN$CASES.i) ~ cumsum(SAN$B.5))
# abline(lm, col='gray')
## predict points using the smooth spline and calculate residuals, D
D <- predict(cum.reg)$y - cumsum(SAN$CASES.i)
B <- SAN$B.5
# plot(D, type='l')
## under reporting is given by slope of smooth spline
u <- predict(cum.reg, deriv = 1)$y
rho <- mean(u)
legend("topleft", paste("rho", round(rho, 3), sep = " = "))
## Ic are actual cases - reported cases divided by u
Ic = SAN$CASES.i/u
# plot(Ic, type='l')
lIt = log(Ic[2:(length(SAN$CASES) + 1)])
lItm1 = log(Ic[1:length(SAN$CASES)])
Dtm1 = D[1:length(SAN$CASES)]
## remove values of -Inf from I - glm function does not like these!
for (i in 1:(length(lIt) - 1)) {
if (lIt[i] == -Inf) {
lIt[i] <- 0
}
}
for (i in 1:length(lItm1)) {
if (lItm1[i] == -Inf) {
lItm1[i] <- 0
}
}
## mean populaiton estimate
pop = mean(SAN$POP.5)
seas = rep(1:26, length(SAN$CASES))[1:length(SAN$CASES)]
seas <- as.factor(seas)
## test Smeans from 1% to whole population
Smean = seq(0.001, 1, by = 0.001) * pop
## this is a place to store the likelihoods of the data for each setting
## of Smean
llik = rep(NA, length(Smean))
## now perform the log linear regressions at each Smean
for (i in 1:length(Smean)) {
lStm1 = log(Smean[i] + Dtm1)
glmfit = glm(lIt ~ -1 + as.factor(seas) + lItm1 + offset(lStm1))
llik[i] = glmfit$deviance
}
## Warning: NaNs produced
sbar <- Smean[which(llik == min(llik))]
sbar
## [1] 1320
## plot likelihood curve
plot(Smean, llik, type = "l", xlim = c(0, (0.1 * pop)), xlab = "sbar", ylab = "deviance (- log likelihood)")
legend("topright", paste("sbar", round(sbar, 0), sep = " = "))
## The Smean we want to use is the one that minimizes the log likelihood
plot(D + sbar, type = "l", xlab = "Biweek", ylab = "S")
sbar.def <- sbar
D.def <- D
B.def <- SAN$B.5
alpha.def <- 0.98
## TSIR code
## pass B, sbar, and D results from above and guess at coefficients for
## Beta function
runTSIR <- function(alpha = alpha.def, B = B.def, sbar = sbar.def, D = D.def,
Beta = "TMP", guess = c(x1 = 3.8e-05, x2 = 0.4), initial.state = c(S = sbar.def -
1, I = 1, R = SAN$POP[1] - sbar.def - 1, CI = 1)) {
## create empty vectors to store S, I, R, B, Beta estimates
S <- rep(NA, length(SAN$CASES))
I <- rep(NA, length(SAN$CASES))
R <- rep(NA, length(SAN$CASES))
CI <- rep(NA, length(SAN$CASES))
## set time = 1 values to initial states
S[1] <- sbar + D[1]
I[1] <- initial.state["I"]
R[1] <- initial.state["R"]
CI[1] <- initial.state["CI"]
## betas are a function of the normalized climate data - I used tmax here.
## The x1-x2 are parameters to fit the seasonal forcing equation.
tmax <- (SAN$TMAX - 260)/105
Beta.TMP <- guess["x1"] * (1 + (guess["x2"] * (tmax)))
Beta.SCH <- guess["x1"] * (1 + (guess["x2"] * (SAN$SCH)))
Beta.CST <- rep(guess["x1"], length(SAN$CASES))
if (Beta == "TMP") {
for (t in 2:length(SAN$CASES)) {
S[t] <- D[t] + sbar
I[t] <- Beta.TMP[t] * S[t - 1] * (I[t - 1]^alpha)
R[t] <- I[t - 1] + R[t - 1]
CI[t] <- I[t] + CI[t - 1]
}
} else if (Beta == "SCH") {
for (t in 2:length(SAN$CASES)) {
S[t] <- D[t] + sbar
I[t] <- Beta.SCH[t] * S[t - 1] * (I[t - 1]^alpha)
R[t] <- I[t - 1] + R[t - 1]
CI[t] <- I[t] + CI[t - 1]
}
} else if (Beta == "CST") {
for (t in 2:length(SAN$CASES)) {
S[t] <- D[t] + sbar
I[t] <- Beta.CST[t] * S[t - 1] * (I[t - 1]^alpha)
R[t] <- I[t - 1] + R[t - 1]
CI[t] <- I[t] + CI[t - 1]
}
}
tsir.output <- data.frame(S, I, R, CI, Beta.TMP, Beta.SCH, Beta.CST)
}
######################## LS1 is temperature ##
LS1 <- function(x) {
sum((runTSIR(guess = x, Beta = "TMP")$I - SAN$CASES.i/u)^2)
}
g <- c(x1 = 2.5e-05, x2 = -0.4)
p <- optim(g, LS1)
## show optimal values
p$par
## x1 x2
## 0.0007651 -0.3632455
## show MSE
MSE1 <- LS1(p$par)
MSE1
## [1] 102888648
optimal1 <- as.vector(p$par)
out.opt1 <- runTSIR(guess = c(x1 = as.numeric(p$par[1]), x2 = as.numeric(p$par[2])),
Beta = "TMP")
head(out.opt1)
## S I R CI Beta.TMP Beta.SCH Beta.CST
## 1 1693 1.000 186237 1.000 0.0008957 0.0010430 0.0007651
## 2 1698 1.502 186238 2.502 0.0008870 0.0010430 0.0007651
## 3 1701 2.546 186239 5.048 0.0010067 0.0004872 0.0007651
## 4 1707 4.441 186242 9.489 0.0010445 0.0004872 0.0007651
## 5 1713 7.563 186246 17.052 0.0010276 0.0004872 0.0007651
## 6 1716 12.001 186254 29.053 0.0009647 0.0004872 0.0007651
######################## LS2 is school #######
LS2 <- function(x) {
sum((runTSIR(guess = x, Beta = "SCH")$I - SAN$CASES.i/u)^2)
}
g <- c(x1 = 2.5e-05, x2 = 0.4)
k <- optim(g, LS2)
par(mfrow = c(1, 3), mar = c(4, 4, 4, 2))
plot(SAN$BWK, out.opt1$S, type = "l", xlab = "Biweek", ylab = "Number of people",
main = "S")
plot(SAN$BWK, out.opt1$I, type = "l", xlab = "Biweek", ylab = "", main = "I")
plot(SAN$BWK, out.opt1$R, type = "l", xlab = "Biweek", ylab = "", main = "R")
par(mfrow = c(1, 1))
## show optimal values
k$par
## x1 x2
## 0.0007395 0.1961207
## show MSE
MSE2 <- LS2(k$par)
MSE2
## [1] 132625787
optimal2 <- as.vector(k$par)
out.opt2 <- runTSIR(guess = c(x1 = as.numeric(k$par[1]), x2 = as.numeric(k$par[2])),
Beta = "SCH")
head(out.opt2)
## S I R CI Beta.TMP Beta.SCH Beta.CST
## 1 1693 1.000 186237 1.000 0.0006713 0.0005945 0.0007395
## 2 1698 1.006 186238 2.006 0.0006759 0.0005945 0.0007395
## 3 1701 1.512 186239 3.518 0.0006134 0.0008845 0.0007395
## 4 1707 2.256 186241 5.774 0.0005937 0.0008845 0.0007395
## 5 1713 3.352 186243 9.126 0.0006025 0.0008845 0.0007395
## 6 1716 4.957 186246 14.083 0.0006353 0.0008845 0.0007395
########################## LS3 is constant #######
LS3 <- function(x) {
sum((runTSIR(guess = x, Beta = "CST")$I - SAN$CASES.i/u)^2)
}
g <- c(x1 = 2.5e-05, x2 = 0.8)
q <- optim(g, LS3)
## show optimal values
q$par
## x1 x2
## 0.0007352 0.8023559
## show MSE
MSE3 <- LS3(q$par)
MSE3
## [1] 137230951
optimal3 <- as.vector(q$par)
out.opt3 <- runTSIR(guess = c(x1 = as.numeric(q$par[1]), x2 = as.numeric(q$par[2])),
Beta = "CST")
head(out.opt3)
## S I R CI Beta.TMP Beta.SCH Beta.CST
## 1 1693 1.000 186237 1.000 0.0004579 0.0001453 0.0007352
## 2 1698 1.245 186238 2.245 0.0004763 0.0001453 0.0007352
## 3 1701 1.547 186239 3.791 0.0002223 0.0013250 0.0007352
## 4 1707 1.918 186241 5.709 0.0001421 0.0013250 0.0007352
## 5 1713 2.376 186243 8.086 0.0001778 0.0013250 0.0007352
## 6 1716 2.941 186245 11.027 0.0003114 0.0013250 0.0007352
## get average betas for each biweek
Beta.TMP <- rep(NA, 26)
for (i in 1:26) {
Beta.TMP[i] <- mean(out.opt1$Beta.TMP[rep(i, length(out.opt1$Beta.TMP),
by = 26)])
}
Beta.SCH <- rep(NA, 26)
for (i in 1:26) {
Beta.SCH[i] <- mean(out.opt2$Beta.SCH[rep(i, length(out.opt2$Beta.SCH),
by = 26)])
}
Beta.CST <- rep(NA, 26)
for (i in 1:26) {
Beta.CST[i] <- mean(out.opt3$Beta.CST[rep(i, length(out.opt3$Beta.CST),
by = 26)])
}
## Betas.26 is dataframe of average betas for 1 year
Betas.26 <- data.frame(Beta.TMP, Beta.SCH, Beta.CST)
plot(Betas.26$Beta.TMP, col = "magenta", type = "l", ylab = "Beta", xlab = "Biweek",
xlim = c(1, 26), lwd = 2, pch = 16, ylim = c(min(Beta.SCH, Beta.TMP), max(Beta.TMP,
Beta.SCH)))
lines(Betas.26$Beta.SCH, col = "darkgreen", lwd = 2)
lines(Betas.26$Beta.CST, col = "blue", lwd = 2)
legend("topright", c("Constant ", "School", "Temperature "), col = c("blue",
"darkgreen", "magenta"), lwd = 2)
## Betas is all betas
Betas <- data.frame(Beta.TMP = out.opt1$Beta.TMP, Beta.SCH = out.opt2$Beta.SCH,
Beta.CST = out.opt3$Beta.CST)
# plot(SAN$BWK,out.opt1$I, type='l', col='purple',
# ylim=c(0,4000),xlim=c(0,26*10)) lines(SAN$BWK, out.opt2$I, type='l',
# col='orange')
## Fourier analysis - plot spectrum and ID dominant frequencies
freq <- spectrum(SAN$CASES.i)$freq
spec <- spectrum(SAN$CASES.i)$spec
data <- data.frame(freq, spec, period = 1/freq, period.yrs = (1/freq)/26)
data <- data[order(-spec), ]
head(data)
## freq spec period period.yrs
## 26 0.03852 16551 25.96 0.9985
## 13 0.01926 7818 51.92 1.9970
## 8 0.01185 5695 84.38 3.2452
## 9 0.01333 4403 75.00 2.8846
## 24 0.03556 4310 28.12 1.0817
## 15 0.02222 4080 45.00 1.7308
par(mar = c(4, 4, 2, 2))
spectrum(SAN$CASES.i, main = "", xlab = "Frequency (1/Period)", ylab = "Spectral Density",
ylim = c(10, 1e+08), xlim = c(-0.01, 0.5))
text(data[1, 1], data[1, 2], paste(round(data[1, 3], 1), "bwks", sep = "\n"),
pos = 4, cex = 0.7)
text(data[2, 1], data[2, 2], paste(round(data[2, 3], 1), "bwks", sep = "\n"),
pos = 3, cex = 0.7)
## parameters
param <- data.frame(TEMP = p$par, SCHOOL = k$par, CONSTANT = c(q$par[1], NA))
row.names(param) <- c("beta_0", "beta_1")
## plot fits against data
plot(SAN$BWK, out.opt1$I, type = "l", col = "red", lwd = 2)
lines(SAN$BWK, SAN$CASES.i)
legend("topright", c(paste("MSE:", round(MSE1, 0), sep = "")))
# plot(SAN$BWK, SAN$CASES.i, type='l') lines(SAN$BWK, out.opt2$I,
# type='l', col='red', lwd=2)
plot(SAN$BWK, out.opt2$I, type = "l", col = "red", lwd = 2)
lines(SAN$BWK, SAN$CASES.i)
legend("topright", c(paste("MSE:", round(MSE2, 0), sep = "")))
# plot(SAN$BWK, SAN$CASES.i, type='l') lines(SAN$BWK, out.opt3$I,
# type='l', col='red', lwd=2)
plot(SAN$BWK, out.opt3$I, type = "l", col = "red", lwd = 2)
lines(SAN$BWK, SAN$CASES.i)
legend("topright", c(paste("MSE:", round(MSE3, 0), sep = "")))
param
## TEMP SCHOOL CONSTANT
## beta_0 0.0007651 0.0007395 0.0007352
## beta_1 -0.3632455 0.1961207 NA