dat <- read.table("../RubinRecall.txt", header = TRUE)
aggregate(y ~ lag + cond, dat, mean) |>
lattice::xyplot(y/18 ~ factor(lag), data = _, type = c("g", "o"),
groups = factor(cond,
labels = c("matched", "white", "random")),
ylim = c(-.03, 1.03), auto.key = list(space = "right"),
xlab = "Lag", ylab = "Proportion recalled")## First 8 subjects, exclude lag 0, order by avg performance
dat <- subset(dat, subj %in% 1:8 & lag > 0)
dat <- dat[order(ave(dat$y, dat$subj), dat$subj), ]
dat$subj <-
factor(dat$subj, levels = unique(dat$subj), labels = 1:8) |>
as.numeric()
names(dat)[names(dat) == "lag"] <- "t"
rownames(dat) <- NULL
## Pooled data
dat2 <- aggregate(y ~ t, dat, sum)|> print()## t y
## 1 1 94
## 2 2 59
## 3 4 46
## 4 7 51
## 5 12 53
## 6 21 33
## 7 35 32
## 8 59 18
## 9 99 13
\[\begin{align} p(t) &= \alpha e^{-\beta t} + \gamma \\ p(t) &= \alpha e^{-\beta t} \\ p(t) &= \alpha t^{-\beta} = \alpha (e^{\log t})^{-\beta} = \alpha e^{-\beta \log t} \end{align}\] where \(t > 0\) and \(\alpha\), \(\beta\), \(\gamma\) represent offset, decay, baseline, respectively.
## Prediction function and negative log-likelihood function
predfun <- function(theta, t) {
alpha <- theta[1]
beta <- theta[2]
gamma <- theta[3]
alpha * exp(-beta * t) + gamma
}
nll <- function(theta, y, t, n) {
prob <- predfun(theta, t)
-sum(dbinom(y, n, prob, log = TRUE))
}
## MLE
m1 <- suppressWarnings(
optim(par = c(alpha = .5, beta = .5, gamma = .5), # starting values
fn = nll, # function to be minimized
y = dat2$y, # arguments passed to fn
t = dat2$t,
n = 18 * 8,
method = "BFGS", # algorithm
hessian = TRUE) # return neg. hessian matrix
)
print(m1)## $par
## alpha beta gamma
## 0.40863992 0.04742432 0.09942333
##
## $value
## [1] 38.14987
##
## $counts
## function gradient
## 125 34
##
## $convergence
## [1] 0
##
## $message
## NULL
##
## $hessian
## alpha beta gamma
## alpha 2046.389 -4741.585 2924.575
## beta -4741.585 25716.627 -12484.304
## gamma 2924.575 -12484.304 7598.019
## Plot observed vs. predicted
plot.default(factor(dat2$t), predfun(m1$par, dat2$t), type = "l",
axes = FALSE, ylim = 0:1, col = "darkblue",
xlab = "Lag", ylab = "Proportion recalled")
points(y/(18 * 8) ~ factor(t), dat2)
axis(1, 1:9, dat2$t)
axis(2)
box(bty = "L")optim_nopool <- function(df, n = 18, method = "BFGS", hessian = TRUE) {
suppressWarnings(
optim(par = c(alpha = .5, beta = .5, gamma = .5),
fn = nll, y = df$y, t = df$t, n = n,
method = method, hessian = hessian)
)
}
m2 <- by(dat, dat$subj, optim_nopool, method = "Nelder-Mead", hessian = FALSE)
sapply(m2, "[[", "par") |>
t() |>
round(3)## alpha beta gamma
## 1 1.274 1.164 0.101
## 2 0.751 0.794 0.151
## 3 174.999 6.293 0.175
## 4 0.472 0.030 -0.025
## 5 0.644 0.428 0.220
## 6 0.589 0.024 -0.054
## 7 0.805 0.367 0.331
## 8 0.592 0.065 0.221
## Plot observed vs. predicted
par(mfrow = c(3, 3), mai = c(.5, .5, .1, .1), mgp = c(2, .7, 0))
for(s in 1:8) {
with(dat[dat$subj == s, ], {
plot.default(factor(t), predfun(m2[[s]]$par, t), type = "l",
axes = FALSE, ylim = 0:1, col = "darkblue",
xlab = "Lag", ylab = "Proportion recalled")
points(y/18 ~ factor(t))
})
axis(1, 1:9, dat2$t)
axis(2)
box(bty = "L")
}library(lme4)
nform <- ~ alpha * exp(-beta * t) + gamma
nfun <- deriv(nform, namevec = c("alpha", "beta", "gamma"),
function.arg = c("t", "alpha", "beta", "gamma"))
m <- nlmer(
y/18 ~ nfun(t, alpha, beta, gamma) ~ alpha + gamma | subj, dat,
start = c(alpha = 0.5, beta = 0.5, gamma = 0.5)
) |> print()## Nonlinear mixed model fit by maximum likelihood ['nlmerMod']
## Formula: y/18 ~ nfun(t, alpha, beta, gamma) ~ alpha + gamma | subj
## Data: dat
## AIC BIC logLik -2*log(L) df.resid
## -60.5841 -44.6475 37.2921 -74.5841 65
## Random effects:
## Groups Name Std.Dev. Corr
## subj alpha 0.11996
## gamma 0.04846 1.00
## Residual 0.12840
## Number of obs: 72, groups: subj, 8
## Fixed Effects:
## alpha beta gamma
## 0.40503 0.05736 0.11552
## alpha beta gamma
## 1 0.265 0.057 0.059
## 2 0.309 0.057 0.077
## 3 0.293 0.057 0.070
## 4 0.367 0.057 0.100
## 5 0.400 0.057 0.114
## 6 0.467 0.057 0.141
## 7 0.557 0.057 0.177
## 8 0.581 0.057 0.187
par(mfrow = c(3, 3), mai = c(.5, .5, .1, .1), mgp = c(2, .7, 0))
for(s in 1:8) {
plot.default(factor(dat[dat$subj == s, "t"]), predict(m)[dat$subj == s],
type = "l", axes = FALSE, ylim = 0:1, col = "darkblue",
xlab = "Lag", ylab = "Proportion recalled")
points(y/18 ~ factor(t), dat[dat$subj == s, ])
axis(1, 1:9, dat2$t)
axis(2)
box(bty = "L")
}