0
$\begingroup$

I want to simulate data for a multilevel logistic regression with several independent variables. I tried doing this with the steps of >>Arnold, B. F., Hogan, D. R., Colford, J. M., & Hubbard, A. E. (2011). Simulation Methods to Estimate Design Power: An Overview for Applied Research. BMC Medical Research Methodology, 11(94): page 4.<< however, in their example, there is only one independent variable. How do I calculate the log odds if I have more than one independent variable?

nItemsPerRtreatment <- 20
# Observations and Dataframe ####
obs <- 140*40  # 40 observations per person, 140 persons in the dataframe
obsnum <- c(1:obs)
DF <- data.frame(obsnum)

# participantID ####
DF$x <- 0
DF$x[((DF$obsnum-1) %% nitemsperparticipant) == 0] <- 1   
DF$participantID  <- cumsum(DF$x)    
# Item Nr. 
DF$itemNr <- data.table::rowid(DF$participantID)
DF$randomItem <- c(sapply(40, function(i) sample(40, i, replace=FALSE)))

# Treatment 1 
DF$treat1 <- -99
DF$treat1[DF$itemNr < (nItemsPerRtreatment+1)] <- 0   
DF$treat1[DF$itemNr > nItemsPerRtreatment] <- 1

# Treatment 2
DF$treat2 <- -99
DF$treat2[(DF$itemNr %% 2) == 0] <- 0   
DF$treat2[(DF$itemNr %% 2) != 0] <- 1  

# Continuous treatment
DF$treat3 <-  rep(rnorm(obs,0,5)) 

# Random effect for the clusters
DF$raneff <- rep(rnorm(140,0,32),each=40)

# effects in percentages
e_b0 <- 0.6
e_treat1 <- 0.2
e_treat2 <- -0.2
e_treat12 <- 0.1
e_treat3 <- 0.001

# translate effects to odds ratios
log_b0 <-  # Here I should calculate the log of the odds ratios 
log_treat1 <- # Here I should calculate the log of the odds ratios 
log_treat2 <- # Here I should calculate the log of the odds ratios 
log_treat12 <- # Here I should calculate the log of the odds ratios 
log_treat3 <- # Here I should calculate the log of the odds ratios 
  
# Calculate log odds for y
DF$log_odds_y = (log_b0 + log_treat1 *DF$treat1 + log_treat2*DF$treat2
              +log_treat12 *DF$treat1*DF$treat2 + log_treat3 *DF$treat3 + DF$raneff)
DF$prop = stats::plogis(DF$log_odds_y)
DF$y <- rbinom(n= obsnum, size = 1,  DF$prop)


$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.