alpha = 0.90
TPA <- read.csv("C:/Users/Lisa/Desktop/677/TPA.csv", header = TRUE, stringsAsFactors = FALSE)
TPA <- TPA[seq(1, length(TPA$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(TPA$CASES))[1:length(TPA$CASES)]
TPA$SCH <- SCH
## to analyze a subset of the data
TPA <- TPA[c(1:length(TPA$CASES)), ]
TPA$CASES <- as.numeric(TPA$CASES)
TPA$CASES.i <- as.numeric(TPA$CASES.i)
TPA$TMAX <- as.numeric(TPA$TMAX)
TPA$TMIN <- as.numeric(TPA$TMIN)
TPA$WEEK <- as.numeric(TPA$WEEK)
TPA$BWK <- as.numeric(TPA$BWK)
TPA$SCH <- as.numeric(TPA$SCH)
TPA$B <- as.numeric(TPA$B)
TPA$B.5 <- as.numeric(TPA$B.5)
## to change back and forth from B.5 vs. B TPA$B.5 <- TPA$B
## this is what the data looks like head(TPA)
plot(cumsum(TPA$B.5), cumsum(TPA$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(TPA$B.5), cumsum(TPA$CASES.i), df = 2.5)
lm <- lm(cumsum(TPA$CASES.i) ~ cumsum(TPA$B.5))
# abline(lm, col='gray')
## predict points using the smooth spline and calculate residuals, D
D <- predict(cum.reg)$y - cumsum(TPA$CASES.i)
B <- TPA$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 = TPA$CASES.i/u
# plot(Ic, type='l')
lIt = log(Ic[2:(length(TPA$CASES) + 1)])
lItm1 = log(Ic[1:length(TPA$CASES)])
Dtm1 = D[1:length(TPA$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(TPA$POP.5)
seas = rep(1:26, length(TPA$CASES))[1:length(TPA$CASES)]
seas <- as.factor(seas)
## test Smeans from 1% to whole population
Smean = seq(0.01, 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
}
sbar <- Smean[which(llik == min(llik))]
sbar
## [1] 7851
## plot likelihood curve
plot(Smean, llik, type = "l", xlim = c(0, (0.5 * 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 <- TPA$B.5
alpha.def <- 0.9
## 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 = TPA$POP[1] - sbar.def - 1, CI = 1)) {
## create empty vectors to store S, I, R, B, Beta estimates
S <- rep(NA, length(TPA$CASES))
I <- rep(NA, length(TPA$CASES))
R <- rep(NA, length(TPA$CASES))
CI <- rep(NA, length(TPA$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 <- (TPA$TMAX - 270)/62
Beta.TMP <- guess["x1"] * (1 + (guess["x2"] * (tmax)))
Beta.SCH <- guess["x1"] * (1 + (guess["x2"] * (TPA$SCH)))
Beta.CST <- rep(guess["x1"], length(TPA$CASES))
if (Beta == "TMP") {
for (t in 2:length(TPA$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(TPA$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(TPA$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 - TPA$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.0001846 -0.3584873
## show MSE
MSE1 <- LS1(p$par)
MSE1
## [1] 10745784
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 7967 1.000 45909 1.000 0.0002469 0.0002508 0.0001846
## 2 7980 1.905 45910 2.905 0.0002391 0.0002508 0.0001846
## 3 7994 3.236 45912 6.141 0.0002270 0.0001184 0.0001846
## 4 8007 5.741 45915 11.882 0.0002496 0.0001184 0.0001846
## 5 8018 9.991 45921 21.872 0.0002588 0.0001184 0.0001846
## 6 8026 13.909 45931 35.781 0.0002186 0.0001184 0.0001846
######################## LS2 is school #######
LS2 <- function(x) {
sum((runTSIR(guess = x, Beta = "SCH")$I - TPA$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(TPA$BWK, out.opt1$S, type = "l", xlab = "Biweek", ylab = "Number of people",
main = "S")
plot(TPA$BWK, out.opt1$I, type = "l", xlab = "Biweek", ylab = "", main = "I")
plot(TPA$BWK, out.opt1$R, type = "l", xlab = "Biweek", ylab = "", main = "R")
par(mfrow = c(1, 1))
## show optimal values
k$par
## x1 x2
## 0.0001791 0.3072100
## show MSE
MSE2 <- LS2(k$par)
MSE2
## [1] 11499750
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 7967 1.0000 45909 1.000 0.0001272 0.0001241 0.0001791
## 2 7980 0.9884 45910 1.988 0.0001338 0.0001241 0.0001791
## 3 7994 1.8486 45911 3.837 0.0001438 0.0002341 0.0001791
## 4 8007 3.2531 45913 7.090 0.0001250 0.0002341 0.0001791
## 5 8018 5.4192 45916 12.509 0.0001173 0.0002341 0.0001791
## 6 8026 8.5897 45921 21.099 0.0001508 0.0002341 0.0001791
########################## LS3 is constant #######
LS3 <- function(x) {
sum((runTSIR(guess = x, Beta = "CST")$I - TPA$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.0001812 0.8040319
## show MSE
MSE3 <- LS3(q$par)
MSE3
## [1] 12702800
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 7967 1.000 45909 1.000 4.389e-05 0.0000355 0.0001812
## 2 7980 1.443 45910 2.443 6.118e-05 0.0000355 0.0001812
## 3 7994 2.011 45911 4.455 8.769e-05 0.0003268 0.0001812
## 4 8007 2.716 45913 7.171 3.802e-05 0.0003268 0.0001812
## 5 8018 3.566 45916 10.737 1.771e-05 0.0003268 0.0001812
## 6 8026 4.561 45920 15.298 1.063e-04 0.0003268 0.0001812
## 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(TPA$BWK,out.opt1$I, type='l', col='purple',
# ylim=c(0,4000),xlim=c(0,26*10)) lines(TPA$BWK, out.opt2$I, type='l',
# col='orange')
## Fourier analysis - plot spectrum and ID dominant frequencies
freq <- spectrum(TPA$CASES.i)$freq
spec <- spectrum(TPA$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.038519 138784 25.96 0.9985
## 7 0.010370 48014 96.43 3.7088
## 6 0.008889 45072 112.50 4.3269
## 32 0.047407 41280 21.09 0.8113
## 52 0.077037 38994 12.98 0.4993
## 33 0.048889 36311 20.45 0.7867
par(mar = c(4, 4, 2, 2))
spectrum(TPA$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(TPA$BWK, out.opt1$I, type = "l", col = "red", lwd = 2, ylim = c(0, max(TPA$CASES.i)))
lines(TPA$BWK, TPA$CASES.i)
legend("topright", c(paste("MSE:", round(MSE1, 0), sep = "")))
plot(TPA$BWK, out.opt2$I, type = "l", col = "red", lwd = 2, ylim = c(0, max(TPA$CASES.i)))
lines(TPA$BWK, TPA$CASES.i)
legend("topright", c(paste("MSE:", round(MSE2, 0), sep = "")))
plot(TPA$BWK, out.opt3$I, type = "l", col = "red", lwd = 2, ylim = c(0, max(TPA$CASES.i)))
lines(TPA$BWK, TPA$CASES.i)
legend("topright", c(paste("MSE:", round(MSE3, 0), sep = "")))
param
## TEMP SCHOOL CONSTANT
## beta_0 0.0001846 0.0001791 0.0001812
## beta_1 -0.3584873 0.3072100 NA