library(knitr) library(simglm) knit_print.data.frame = function(x, ...) { res = paste(c('', '', kable(x, output = FALSE)), collapse = '\n') asis_output(res) }

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)

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).

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)

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)

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)

**Any scripts or data that you put into this service are public.**

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.