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 1 4.308783 -0.7412115 3.9320281 0.98077305 1 1
## 2 1 4.867741 -0.6129266 4.2499927 0.98593627 1 2
## 3 1 -5.777483 -1.7201841 -1.4047969 0.19705602 0 3
## 4 1 -8.989782 -0.5180032 -2.6502921 0.06597101 0 4
## 5 1 -5.606983 0.8515632 -0.5480227 0.36632329 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).
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
## 1 1 -2.4782517 0.510712 0.3755224 0.9140877 0.3755224 1.2896102
## 2 1 -3.0621637 0.510712 0.3755224 0.6221317 0.3755224 0.9976542
## 3 1 -0.6295923 0.510712 0.3755224 1.8384175 0.3755224 2.2139399
## 4 1 1.0613815 0.510712 0.3755224 2.6839043 0.3755224 3.0594267
## 5 1 0.8294658 0.510712 0.3755224 2.5679465 0.3755224 2.9434689
## prob sim_data withinID clustID
## 1 0.7840812 1 1 1
## 2 0.7305971 1 2 1
## 3 0.9014944 1 3 1
## 4 0.9551878 1 4 1
## 5 0.9499539 1 5 1
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
## 1 1 3.9888690 6.141684 -2.084903 0.1779015 0.6353578 5.818955
## 2 1 2.7370162 6.141684 -2.084903 0.1779015 0.6353578 4.817473
## 3 1 -0.7785872 6.141684 -2.084903 0.1779015 0.6353578 2.004990
## 4 1 -0.7672332 6.141684 -2.084903 0.1779015 0.6353578 2.014073
## 5 1 0.6907792 6.141684 -2.084903 0.1779015 0.6353578 3.180483
## randEff randEff3 logistic prob sim_data withinID clustID
## 1 0.1779015 0.6353578 6.632214 0.9986845 1 1 1
## 2 0.1779015 0.6353578 5.630732 0.9964269 1 2 1
## 3 0.1779015 0.6353578 2.818249 0.9436540 1 3 1
## 4 0.1779015 0.6353578 2.827332 0.9441351 1 4 1
## 5 0.1779015 0.6353578 3.993742 0.9819029 1 5 1
## clust3ID
## 1 1
## 2 1
## 3 1
## 4 1
## 5 1
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 -4.2809923 1.4287430 -2.211873 0.10949534 0 1
## 2 1 -4.2778138 -1.6077157 -3.121222 0.04410326 0 2
## 3 1 -4.4481936 1.7835822 -2.189022 0.11202624 0 3
## 4 1 0.3934006 1.8470022 0.250801 1.28505427 3 4
## 5 1 3.7834379 -0.6690836 1.190994 3.29034986 1 5