Philadelphia Measles: 1920-1948

alpha = 0.89


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

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

PHL$SCH <- SCH

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

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

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

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

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

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

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

B <- PHL$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 = PHL$CASES.i/u

# plot(Ic, type='l')

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

## 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 <- PHL$B.5
alpha.def <- 0.89

## 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 = PHL$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(PHL$CASES))
    I <- rep(NA, length(PHL$CASES))
    R <- rep(NA, length(PHL$CASES))
    CI <- rep(NA, length(PHL$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 <- (PHL$TMIN - 74.4)/125
    Beta.TMP <- guess["x1"] * (1 + (guess["x2"] * (tmax)))
    Beta.SCH <- guess["x1"] * (1 + (guess["x2"] * (PHL$SCH)))
    Beta.CST <- rep(guess["x1"], length(PHL$CASES))

    if (Beta == "TMP") {
        for (t in 2:length(PHL$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(PHL$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(PHL$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 - PHL$CASES.i/u)^2)
}

g <- c(x1 = 2.5e-05, x2 = -0.4)
p <- optim(g, LS1)

## show optimal values
p$par
##         x1         x2 
##  4.401e-05 -2.751e-01

## show MSE
MSE1 <- LS1(p$par)
MSE1
## [1] 3.087e+09

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 53010  660.8 1387078  660.8 5.695e-05 5.612e-05 4.401e-05
## 2 52216 1019.5 1387739 1680.3 5.945e-05 5.612e-05 4.401e-05
## 3 51659 1404.6 1388759 3084.9 5.653e-05 3.190e-05 4.401e-05
## 4 50808 1816.9 1390163 4901.8 5.557e-05 3.190e-05 4.401e-05
## 5 49874 2325.9 1391980 7227.7 5.752e-05 3.190e-05 4.401e-05
## 6 48929 2455.0 1394306 9682.7 4.965e-05 3.190e-05 4.401e-05

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

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

## show MSE
MSE2 <- LS2(k$par)
MSE2
## [1] 3.197e+09

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 53010  660.8 1387078  660.8 3.351e-05 3.416e-05 4.372e-05
## 2 52216  585.8 1387739 1246.6 3.153e-05 3.416e-05 4.372e-05
## 3 51659  808.3 1388325 2054.9 3.383e-05 5.327e-05 4.372e-05
## 4 50808 1065.1 1389133 3120.1 3.459e-05 5.327e-05 4.372e-05
## 5 49874 1339.1 1390198 4459.2 3.305e-05 5.327e-05 4.372e-05
## 6 48929 1611.6 1391538 6070.8 3.927e-05 5.327e-05 4.372e-05

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

LS3 <- function(x) {
    sum((runTSIR(guess = x, Beta = "CST")$I - PHL$CASES.i/u)^2)
}

g <- c(x1 = 2.5e-05, x2 = 0.8)
q <- optim(g, LS3)

## show optimal values
q$par
##        x1        x2 
## 4.393e-05 8.002e-01

## show MSE
MSE3 <- LS3(q$par)
MSE3
## [1] 3.685e+09

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 53010 660.8 1387078  660.8  6.377e-06 8.780e-06 4.393e-05
## 2 52216 753.4 1387739 1414.2 -8.742e-07 8.780e-06 4.393e-05
## 3 51659 834.0 1388493 2248.2  7.583e-06 7.909e-05 4.393e-05
## 4 50808 903.2 1389327 3151.4  1.037e-05 7.909e-05 4.393e-05
## 5 49874 953.6 1390230 4104.9  4.710e-06 7.909e-05 4.393e-05
## 6 48929 982.4 1391183 5087.4  2.757e-05 7.909e-05 4.393e-05


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


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

plot of chunk unnamed-chunk-1

spec <- spectrum(PHL$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.03867 19081801  25.86     0.9947
## 11 0.01467 11215693  68.18     2.6224
## 17 0.02267  9812818  44.12     1.6968
## 10 0.01333  7434567  75.00     2.8846
## 12 0.01600  6149355  62.50     2.4038
## 41 0.05467  6026824  18.29     0.7036

par(mar = c(4, 4, 2, 2))
spectrum(PHL$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)

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

plot of chunk unnamed-chunk-1


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

plot of chunk unnamed-chunk-1


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

plot of chunk unnamed-chunk-1


param
##              TEMP    SCHOOL  CONSTANT
## beta_0  4.401e-05 4.372e-05 4.393e-05
## beta_1 -2.751e-01 2.186e-01        NA