# Simulated Logistic Models

The simglm package offers users the ability to simulate from a variety of generalized linear models, both single level and multilevel generalized models. Instead of using the sim_reg function to call these, there is now a sim_glm function to use.

Similar to the sim_reg function, one benefit of this package for simulation is that the intermediate steps are returned as well. This is useful for additional processing of the data, for example to add in your own missing data function.

Here is an example for simulating a single level logistic model:

fixed <- ~ 1 + act + diff
fixed_param <- c(2, 0.5, 0.3)
cov_param <- list(dist_fun = c('rnorm', 'rnorm'),
var_type = c("single", "single"),
opts = list(list(mean = 0, sd = 4),
list(mean = 0, sd = 3)))
n <- 150

temp_single <- sim_glm(fixed = fixed, fixed_param = fixed_param,
cov_param = cov_param,
n = n, data_str = "single", outcome_type = 'logistic')
head(temp_single, n = 5)
X.Intercept. act diff Fbeta logistic sim_data ID
1 0.2414737 -0.0353711 2.110126 0.8918834 1 1
1 -1.7183739 0.8892484 1.407588 0.8033852 1 2
1 2.2632643 -2.8184229 2.286105 0.9077197 1 3
1 -4.6748860 -1.2688634 -0.718102 0.3278111 1 4
1 6.7696881 -1.2425183 5.012088 0.9933870 1 5

As you can see from the code above, the syntax is virtually identical to the syntax for the sim_reg function. The largest difference is the omission of the error_var and rand_gen commands. The returned data frame includes the response variable in the logistic function (Fbeta), the probability found by taking $$\frac{exp(Fbeta)}{1 + exp(Fbeta)}$$ (logistic), and the returned 0/1 variable by using the rbinom function using the probabilities defined above (sim_data).

## Multilevel logistic models

Adding in additional levels is straightforward and again very similar to the sim_reg function. Here is a two level example with students nested in classrooms, the act variable is treated as a classroom variable:

# Longitudinal linear mixed model example
fixed <- ~1 + diff + act
random <- ~1
fixed_param <- c(2, 0.5, 0.3)
random_param <- list(random_var = 7, rand_gen = "rnorm", ther_sim = TRUE)
cov_param <- list(dist_fun = c('rnorm', 'rnorm'),
var_type = c("level1", "level2"),
opts = list(list(mean = 0, sd = 2),
list(mean = 0, sd = 1.4)))
n <- 150
p <- 30
data_str <- "cross"
temp_cross <- sim_glm(fixed, random, random3 = NULL, fixed_param,
random_param, random_param3 = NULL,
cov_param, k = NULL, n, p,
data_str = data_str, outcome_type = 'logistic')
head(temp_cross, n = 5)
X.Intercept. diff act b0 Fbeta randEff logistic prob sim_data withinID clustID
1 0.9237420 -0.0097551 -1.711611 2.458944 -1.711611 0.7473337 0.6785974 1 1 1
1 -0.6308613 -0.0097551 -1.711611 1.681643 -1.711611 -0.0299680 0.4925086 0 2 1
1 -1.9862518 -0.0097551 -1.711611 1.003948 -1.711611 -0.7076633 0.3301154 0 3 1
1 1.3430121 -0.0097551 -1.711611 2.668579 -1.711611 0.9569687 0.7225145 1 4 1
1 3.5447844 -0.0097551 -1.711611 3.769466 -1.711611 2.0578549 0.8867389 1 5 1

## Three level example

Below is sample code for a three level example. Primary differences are the additional terms associated with the third level.

fixed <- ~1 + diff + act + actClust
random <- ~1
random3 <- ~ 1
fixed_param <- c(4, 0.8, 0.15, 1.1)
random_param <- list(random_var = 7, rand_gen = "rnorm")
random_param3 <- list(random_var = 4, rand_gen = "rnorm")
cov_param <- list(dist_fun = c('rnorm', 'rnorm', 'rnorm'),
var_type = c("level1", "level2", "level3"),
opts = list(list(mean = 0, sd = 1.5),
list(mean = 0, sd = 4),
list(mean = 0, sd = 2)))
k <- 10
n <- 150
p <- 30
data_str <- "cross"
temp_three <- sim_glm(fixed, random, random3, fixed_param, random_param,
random_param3, cov_param, k, n, p, data_str = data_str,
outcome_type = 'logistic')
head(temp_three, n = 5)
X.Intercept. diff act actClust b0_2 b0_3 Fbeta randEff randEff3 logistic prob sim_data withinID clustID clust3ID
1 0.5790996 6.229569 -0.5580506 0.8803781 -0.3122799 4.783859 0.8803781 -0.3122799 5.351957 0.9952835 1 1 1 1
1 0.5050491 6.229569 -0.5580506 0.8803781 -0.3122799 4.724619 0.8803781 -0.3122799 5.292717 0.9949971 1 2 1 1
1 0.4404655 6.229569 -0.5580506 0.8803781 -0.3122799 4.672952 0.8803781 -0.3122799 5.241050 0.9947332 1 3 1 1
1 1.4282945 6.229569 -0.5580506 0.8803781 -0.3122799 5.463215 0.8803781 -0.3122799 6.031313 0.9976034 1 4 1 1
1 0.8498346 6.229569 -0.5580506 0.8803781 -0.3122799 5.000447 0.8803781 -0.3122799 5.568545 0.9961985 1 5 1 1

# Count Outcomes

The package also has the ability to simulate count outcomes in addition to the 0/1 dichotomous outcomes. The syntax is the same as above, except the outcome_type argument is modified from logistic to poisson. As some may have realized from above, the dichotomous outcomes are assumed to follow a logistic metric where as the count outcomes follow a poisson model. Belwo is an adapted example from above.

fixed <- ~ 1 + act + diff
fixed_param <- c(-0.5, 0.5, 0.3)
cov_param <- list(dist_fun = c('rnorm', 'rnorm'),
var_type = c("single", "single"),
opts = list(list(mean = 0, sd = 4),
list(mean = 0, sd = 3)))
n <- 150

temp_single <- sim_glm(fixed = fixed, fixed_param = fixed_param,
cov_param = cov_param,
n = n, data_str = "single", outcome_type = 'poisson')
head(temp_single, n = 5)
X.Intercept. act diff Fbeta poisson sim_data ID
1 1.220895 1.696411 0.6193709 1.8577590 2 1
1 -2.545946 2.098709 -1.1433607 0.3187460 0 2
1 -5.276145 -1.171580 -3.4895464 0.0305147 0 3
1 9.467849 -1.890342 3.6668217 39.1273511 37 4
1 2.224318 3.227227 1.5803270 4.8565438 3 5