Seattle Measles: 1920-1948

alpha = 0.85


SEA <- read.csv("C:/Users/Lisa/Desktop/677/SEA.csv", header = TRUE, stringsAsFactors = FALSE)

SEA <- SEA[seq(1, length(SEA$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(SEA$CASES))[1:length(SEA$CASES)]

SEA$SCH <- SCH

## to analyze a subset of the data
SEA <- SEA[c(1:length(SEA$CASES)), ]

SEA$CASES <- as.numeric(SEA$CASES)
SEA$CASES.i <- as.numeric(SEA$CASES.i)
SEA$TMAX <- as.numeric(SEA$TMAX)
SEA$TMIN <- as.numeric(SEA$TMIN)
SEA$WEEK <- as.numeric(SEA$WEEK)
SEA$BWK <- as.numeric(SEA$BWK)
SEA$SCH <- as.numeric(SEA$SCH)
SEA$B <- as.numeric(SEA$B)
SEA$B.5 <- as.numeric(SEA$B.5)

## to change back and forth from B.5 vs. B SEA$B.5 <- SEA$B

## this is what the data looks like head(SEA)

plot(cumsum(SEA$B.5), cumsum(SEA$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(SEA$B.5), cumsum(SEA$CASES.i), df = 2.5)

lm <- lm(cumsum(SEA$CASES.i) ~ cumsum(SEA$B.5))
# abline(lm, col='gray')

## predict points using the smooth spline and calculate residuals, D
D <- predict(cum.reg)$y - cumsum(SEA$CASES.i)

B <- SEA$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 = " = "))

plot of chunk unnamed-chunk-1



## Ic are actual cases - reported cases divided by u
Ic = SEA$CASES.i/u

# plot(Ic, type='l')

lIt = log(Ic[2:(length(SEA$CASES) + 1)])
lItm1 = log(Ic[1:length(SEA$CASES)])
Dtm1 = D[1:length(SEA$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(SEA$POP.5)
seas = rep(1:26, length(SEA$CASES))[1:length(SEA$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] 18754

## 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 = " = "))

plot of chunk unnamed-chunk-1


## The Smean we want to use is the one that minimizes the log likelihood


plot(D + sbar, type = "l", xlab = "Biweek", ylab = "S")

plot of chunk unnamed-chunk-1


sbar.def <- sbar
D.def <- D
B.def <- SEA$B.5
alpha.def <- 0.85

## 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 - 
        Ic[1] * rho, I = Ic[1] * rho, R = SEA$POP[1] - sbar.def - Ic[1] * rho, 
        CI = Ic[1] * rho)) {

    ## create empty vectors to store S, I, R, B, Beta estimates
    S <- rep(NA, length(SEA$CASES))
    I <- rep(NA, length(SEA$CASES))
    R <- rep(NA, length(SEA$CASES))
    CI <- rep(NA, length(SEA$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 <- (SEA$TMAX - 156)/95
    Beta.TMP <- guess["x1"] * (1 + (guess["x2"] * (tmax)))
    Beta.SCH <- guess["x1"] * (1 + (guess["x2"] * (SEA$SCH)))
    Beta.CST <- rep(guess["x1"], length(SEA$CASES))

    if (Beta == "TMP") {
        for (t in 2:length(SEA$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(SEA$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(SEA$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 - SEA$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.0001089 -0.0659723

## show MSE
MSE1 <- LS1(p$par)
MSE1
## [1] 124627441

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 18965 386.7 178897  386.7 0.0001191 0.0001161 0.0001089
## 2 18596 355.0 179284  741.7 0.0001183 0.0001161 0.0001089
## 3 18337 322.1 179639 1063.9 0.0001177 0.0001017 0.0001089
## 4 18155 297.2 179961 1361.0 0.0001196 0.0001017 0.0001089
## 5 18033 265.8 180258 1626.8 0.0001157 0.0001017 0.0001089
## 6 17924 238.0 180524 1864.8 0.0001147 0.0001017 0.0001089

######################## LS2 is school #######

LS2 <- function(x) {
    sum((runTSIR(guess = x, Beta = "SCH")$I - SEA$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(SEA$BWK, out.opt1$S, type = "l", xlab = "Biweek", ylab = "Number of people", 
    main = "S")
plot(SEA$BWK, out.opt1$I, type = "l", xlab = "Biweek", ylab = "", main = "I")
plot(SEA$BWK, out.opt1$R, type = "l", xlab = "Biweek", ylab = "", main = "R")

plot of chunk unnamed-chunk-1

par(mfrow = c(1, 1))

## show optimal values
k$par
##        x1        x2 
## 0.0001082 0.1381649

## show MSE
MSE2 <- LS2(k$par)
MSE2
## [1] 122769764

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 18965 386.7 178897  386.7 8.694e-05 9.323e-05 0.0001082
## 2 18596 279.8 179284  666.5 8.858e-05 9.323e-05 0.0001082
## 3 18337 275.1 179564  941.7 8.975e-05 1.231e-04 0.0001082
## 4 18155 267.5 179839 1209.1 8.582e-05 1.231e-04 0.0001082
## 5 18033 258.6 180106 1467.7 9.393e-05 1.231e-04 0.0001082
## 6 17924 249.5 180365 1717.2 9.600e-05 1.231e-04 0.0001082

########################## LS3 is constant #######

LS3 <- function(x) {
    sum((runTSIR(guess = x, Beta = "CST")$I - SEA$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.0001098 0.8138221

## show MSE
MSE3 <- LS3(q$par)
MSE3
## [1] 125877022

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 18965 386.7 178897  386.7 -1.719e-05 2.045e-05 0.0001098
## 2 18596 329.6 179284  716.3 -7.375e-06 2.045e-05 0.0001098
## 3 18337 282.1 179614  998.5 -3.855e-07 1.992e-04 0.0001098
## 4 18155 243.8 179896 1242.3 -2.391e-05 1.992e-04 0.0001098
## 5 18033 213.2 180139 1455.4  2.462e-05 1.992e-04 0.0001098
## 6 17924 188.9 180353 1644.3  3.698e-05 1.992e-04 0.0001098


## 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), max(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)

plot of chunk unnamed-chunk-1


## 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(SEA$BWK,out.opt1$I, type='l', col='purple',
# ylim=c(0,4000),xlim=c(0,26*10)) lines(SEA$BWK, out.opt2$I, type='l',
# col='orange')


## Fourier analysis - plot spectrum and ID dominant frequencies
freq <- spectrum(SEA$CASES.i)$freq

plot of chunk unnamed-chunk-1

spec <- spectrum(SEA$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
## 29 0.038667 1106184  25.86     0.9947
## 28 0.037333 1029698  26.79     1.0302
## 36 0.048000  995401  20.83     0.8013
## 7  0.009333  925320 107.14     4.1209
## 8  0.010667  655138  93.75     3.6058
## 14 0.018667  642210  53.57     2.0604

par(mar = c(4, 4, 2, 2))
spectrum(SEA$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[4, 1], data[4, 2], paste(round(data[4, 3], 1), "bwks", sep = "\n"), 
    pos = 3, cex = 0.7)

plot of chunk unnamed-chunk-1


## 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(SEA$BWK, out.opt1$I, type = "l", col = "red", lwd = 2, ylim = c(0, max(SEA$CASES.i)))
lines(SEA$BWK, SEA$CASES.i)
legend("topright", c(paste("MSE:", round(MSE1, 0), sep = "")))

plot of chunk unnamed-chunk-1


plot(SEA$BWK, out.opt2$I, type = "l", col = "red", lwd = 2, ylim = c(0, max(SEA$CASES.i)))
lines(SEA$BWK, SEA$CASES.i)
legend("topright", c(paste("MSE:", round(MSE2, 0), sep = "")))

plot of chunk unnamed-chunk-1


plot(SEA$BWK, out.opt3$I, type = "l", col = "red", lwd = 2, ylim = c(0, max(SEA$CASES.i)))
lines(SEA$BWK, SEA$CASES.i)
legend("topright", c(paste("MSE:", round(MSE3, 0), sep = "")))

plot of chunk unnamed-chunk-1


param
##              TEMP    SCHOOL  CONSTANT
## beta_0  0.0001089 0.0001082 0.0001098
## beta_1 -0.0659723 0.1381649        NA