# Simulated SW-CRT example with binary outcome
########################################################################
### Example 1): simulated SW-CRT with smaller cluster-period sizes (5~10)
########################################################################
sampleSWCRT <- sampleSWCRTSmall
#############################################################
### cluster-period id, period, outcome, and design matrix ###
#############################################################
### id, period, outcome
id <- sampleSWCRT$id
period <- sampleSWCRT$period
y <- sampleSWCRT$y_bin
X <- as.matrix(sampleSWCRT[, c("period1", "period2", "period3", "period4", "treatment")])
m <- as.matrix(table(id, period))
n <- dim(m)[1]
t <- dim(m)[2]
clp_mu <- tapply(y, list(id, period), FUN = mean)
y_cp <- c(t(clp_mu))
### design matrix for correlation parameters
trt <- tapply(X[, t + 1], list(id, period), FUN = mean)
trt <- c(t(trt))
time <- tapply(period, list(id, period), FUN = mean)
time <- c(t(time))
X_cp <- matrix(0, n * t, t)
s <- 1
for (i in 1:n) {
for (j in 1:t) {
X_cp[s, time[s]] <- 1
s <- s + 1
}
}
X_cp <- cbind(X_cp, trt)
id_cp <- rep(1:n, each = t)
m_cp <- c(t(m))
#####################################################
### cluster-period matrix-adjusted estimating equations (MAEE)
### with exchangeable, nested exchangeable and exponential decay correlation structures ###
#####################################################
# exchangeable
est_maee_exc <- cpgeeSWD(
y = y_cp, X = X_cp, id = id_cp,
m = m_cp, corstr = "exchangeable",
alpadj = TRUE
)
print(est_maee_exc)
# nested exchangeable
est_maee_nex <- cpgeeSWD(
y = y_cp, X = X_cp, id = id_cp,
m = m_cp, corstr = "nest_exch",
alpadj = TRUE
)
print(est_maee_nex)
# exponential decay
est_maee_ed <- cpgeeSWD(
y = y_cp, X = X_cp, id = id_cp,
m = m_cp, corstr = "exp_decay",
alpadj = TRUE
)
print(est_maee_ed)
#####################################################
### cluster-period GEE
### with exchangeable, nested exchangeable and exponential decay correlation structures ###
#####################################################
# exchangeable
est_uee_exc <- cpgeeSWD(
y = y_cp, X = X_cp, id = id_cp,
m = m_cp, corstr = "exchangeable",
alpadj = FALSE
)
print(est_uee_exc)
# nested exchangeable
est_uee_nex <- cpgeeSWD(
y = y_cp, X = X_cp, id = id_cp,
m = m_cp, corstr = "nest_exch",
alpadj = FALSE
)
print(est_uee_nex)
# exponential decay
est_uee_ed <- cpgeeSWD(
y = y_cp, X = X_cp, id = id_cp,
m = m_cp, corstr = "exp_decay",
alpadj = FALSE
)
print(est_uee_ed)
########################################################################
### Example 2): simulated SW-CRT with larger cluster-period sizes (20~30)
########################################################################
sampleSWCRT <- sampleSWCRTLarge
#############################################################
### cluster-period id, period, outcome, and design matrix ###
#############################################################
### id, period, outcome
id <- sampleSWCRT$id
period <- sampleSWCRT$period
y <- sampleSWCRT$y_bin
X <- as.matrix(sampleSWCRT[, c("period1", "period2", "period3", "period4", "period5", "treatment")])
m <- as.matrix(table(id, period))
n <- dim(m)[1]
t <- dim(m)[2]
clp_mu <- tapply(y, list(id, period), FUN = mean)
y_cp <- c(t(clp_mu))
### design matrix for correlation parameters
trt <- tapply(X[, t + 1], list(id, period), FUN = mean)
trt <- c(t(trt))
time <- tapply(period, list(id, period), FUN = mean)
time <- c(t(time))
X_cp <- matrix(0, n * t, t)
s <- 1
for (i in 1:n) {
for (j in 1:t) {
X_cp[s, time[s]] <- 1
s <- s + 1
}
}
X_cp <- cbind(X_cp, trt)
id_cp <- rep(1:n, each = t)
m_cp <- c(t(m))
#####################################################
### cluster-period matrix-adjusted estimating equations (MAEE)
### with exchangeable, nested exchangeable and exponential decay correlation structures ###
#####################################################
# exchangeable
est_maee_exc <- cpgeeSWD(
y = y_cp, X = X_cp, id = id_cp,
m = m_cp, corstr = "exchangeable",
alpadj = TRUE
)
print(est_maee_exc)
# nested exchangeable
est_maee_nex <- cpgeeSWD(
y = y_cp, X = X_cp, id = id_cp,
m = m_cp, corstr = "nest_exch",
alpadj = TRUE
)
print(est_maee_nex)
# exponential decay
est_maee_ed <- cpgeeSWD(
y = y_cp, X = X_cp, id = id_cp,
m = m_cp, corstr = "exp_decay",
alpadj = TRUE
)
print(est_maee_ed)
#####################################################
### cluster-period GEE
### with exchangeable, nested exchangeable and exponential decay correlation structures ###
#####################################################
# exchangeable
est_uee_exc <- cpgeeSWD(
y = y_cp, X = X_cp, id = id_cp,
m = m_cp, corstr = "exchangeable",
alpadj = FALSE
)
print(est_uee_exc)
# nested exchangeable
est_uee_nex <- cpgeeSWD(
y = y_cp, X = X_cp, id = id_cp,
m = m_cp, corstr = "nest_exch",
alpadj = FALSE
)
print(est_uee_nex)
# exponential decay
est_uee_ed <- cpgeeSWD(
y = y_cp, X = X_cp, id = id_cp,
m = m_cp, corstr = "exp_decay",
alpadj = FALSE
)
print(est_uee_ed)
Run the code above in your browser using DataLab