if (FALSE) {
library(dplyr)
data(DemoData)
# Create dataset of counts
counts.all <- NULL
for(i in 1:length(DemoData)){
counts <- getCounts(DemoData[[i]][, c("clustid", "time", "age", "died",
"region", "strata")],
variables = 'died', by = c("age", "clustid", "region",
"time", "strata"))
counts <- counts %>% mutate(cluster = clustid, years = time, Y=died)
counts$strata <- gsub(".*\\.","",counts$strata)
counts$survey <- names(DemoData)[i]
counts.all <- rbind(counts.all, counts)
}
# fit cluster-level model on the periods
periods <- levels(DemoData[[1]]$time)
fit <- smoothCluster(data = counts.all,
Amat = DemoMap$Amat,
time.model = "rw2",
st.time.model = "rw1",
strata.time.effect = TRUE,
survey.effect = TRUE,
family = "betabinomial",
year_label = c(periods, "15-19"))
summary(fit)
est <- getSmoothed(fit, nsim = 1000)
plot(est$stratified, plot.CI=TRUE) + ggplot2::facet_wrap(~strata)
# fit cluster-level space-time model with covariate
# notice without projected covariates, we use periods up to 10-14 only
# construct a random covariate matrix for illustration
periods <- levels(DemoData[[1]]$time)
X <- expand.grid(years = periods,
region = unique(counts.all$region))
X$X1 <- rnorm(dim(X)[1])
X$X2 <- rnorm(dim(X)[1])
fit.covariate <- smoothCluster(data = counts.all,
X = X,
Amat = DemoMap$Amat,
time.model = "rw2",
st.time.model = "rw1",
strata.time.effect = TRUE,
survey.effect = TRUE,
family = "betabinomial",
year_label = c(periods))
est <- getSmoothed(fit.covariate, nsim = 1000)
# fit cluster-level model for one time point only
# i.e., space-only model
fit.sp <- smoothCluster(data = subset(counts.all, time == "10-14"),
Amat = DemoMap$Amat,
time.model = NULL,
survey.effect = TRUE,
family = "betabinomial")
summary(fit.sp)
est <- getSmoothed(fit.sp, nsim = 1000)
plot(est$stratified, plot.CI = TRUE) + ggplot2::facet_wrap(~strata)
# fit cluster-level model for one time point and covariate
# construct a random covariate matrix for illustration
X <- data.frame(region = unique(counts.all$region),
X1 = c(1, 2, 2, 1),
X2 = c(1, 1, 1, 2))
fit.sp.covariate <- smoothCluster(data = subset(counts.all, time == "10-14"),
X = X,
Amat = DemoMap$Amat,
time.model = NULL,
survey.effect = TRUE,
family = "betabinomial")
summary(fit.sp.covariate)
est <- getSmoothed(fit.sp.covariate, nsim = 1000)
}
Run the code above in your browser using DataLab