# [R-sig-ME] Simulating mixed-effects longitudinal logistic data in R

Simon Harmel @|m@h@rme| @end|ng |rom gm@||@com
Sun Jul 12 22:44:17 CEST 2020

```Good afternoon,

I know to simulate a linear mixed-effects longitudinal data in R, I can use
the procedure below.

But I wonder how to generate a similar design, but for a logistic model in
R?

I appreciate your suggestions,
Simon

#------------- linear mixed-effects longitudinal data in R-------

library(MASS)

growth.sim <- function(J, n.time, gammas, G, sigma = 1, seed = NULL) {

X <- cbind(1, seq_len(n.time) - 1) # time indicators for each individual
X <- X[rep(seq_len(n.time), J), ]  # Repeat each row n.time times

st.id <- seq_len(J)                 # individual id
st.id <- rep(st.id, each = n.time)  # repeat each ID n.time times

set.seed(seed) ## what should go below, possibly correlated Beta?
uj <- MASS::mvrnorm(J, mu = rep(0, 2), Sigma = G) # Generate u0 and u1
random effects for intercepts and slopes

## I think no error term is needed for logistic version?
eij <- rnorm(J * n.time, sd = sigma)              # Generate error
term for observations

betaj <- matrix(gammas, nrow = J, ncol = 2, byrow = TRUE) + uj #
Compute beta_j's

y <- rowSums(X * betaj[st.id, ]) + eij          # Compute outcome
based on Yij = sum(Xij*Bj) + eij:

dat <- data.frame(st.id, time = X[ , 2], y) # Output a data frame
return(dat)                                 # Return data}
*##==== Example of use: =====*
growth.sim(10, 4, gammas = c(300, 25),
G = matrix(c(0.1, 0,
0, 0.01), nrow = 2))

[[alternative HTML version deleted]]

```