Ex. 1 - Background questionnaire generation"

library(knitr)
options(width = 90, tidy = TRUE, warning = FALSE, message = FALSE)
opts_chunk$set(comment = "", warning = FALSE, message = FALSE,
               echo = TRUE, tidy = TRUE)
library(lsasim)
packageVersion("lsasim")

questionnaire_gen(n_obs, cat_prop = NULL, n_vars = NULL, n_X = NULL, n_W = NULL,
                  cor_matrix = NULL, cov_matrix = NULL,
                  c_mean = NULL, c_sd = NULL,
                  theta = FALSE, family = NULL,
                  full_output = FALSE, verbose = TRUE)

The function questionnaire_gen generates correlated continuous and ordinal data which resembles background questionnaire data. The required argument is n_obs and the optional arguments include

The arguments c_mean and c_sd are scaling parameters for continuous variables. If the logical argument theta is TRUE then the latent trait will be generated as the first continuous variable and labeled 'theta'. If family is gaussian then the data will be generated from a multivariate normal distribution, or the data will be generated from the polychoric correlation matrix.

If the logical argument full_output is TRUE, output will be a list containing the questionnaire data as well as several objects that might be of interest for further analysis of the data. The output of full_outputwill be addressed in future tutorials.


We only specify n_obs = 100 and use a multivariate normal distribution. It turned out the generated data involves one continuous variable and four ordinal covariates, which are 2-category, 3-category, 4-category, and 5-category, respectively.

set.seed(4388)
bg <- questionnaire_gen(n_obs = 100, family = "gaussian")
str(bg)

In addition to n_obs = 100, we specify the logical argument theta = TRUE. An additional continuous variable is generated and labeled theta. The latent trait is always placed first in the generated data.

set.seed(4388)
bg <- questionnaire_gen(n_obs = 100, theta = TRUE, family = "gaussian")
str(bg)

We specify n_vars = 4 regardless the item type. Four different item types are generated, one 1-category item (continuous), one 2-category item, one 4-category item, and one 5-category item.

set.seed(4388)
bg <- questionnaire_gen(n_obs = 100, n_vars = 4, family = "gaussian")
str(bg)

In addition to n_vars = 4, we specify the logical argument theta = TRUE. Three different item types are generated, two 1-category item (latent trait and continuous), one 2-category item, and one 5-category item. It is noted that when theta = TRUE, the first continuous variable generated is alwasy labeled theta.

set.seed(4388)
bg <- questionnaire_gen(n_obs = 100, n_vars = 4, theta = TRUE, family = "gaussian")
str(bg)

We generate one latent trait and three continuous variables by specifying theta = TRUE and n_X = 3. We also add n_W = 0, or random number of ordinal variables will be generated.

set.seed(4388)
bg <- questionnaire_gen(n_obs = 100, n_X = 3, n_W = 0, theta = TRUE, family = "gaussian")
str(bg)

set.seed(4388)
bg <- questionnaire_gen(n_obs = 100, n_X = 3, theta = TRUE, family = "gaussian")
str(bg)

We can also specify cat_prop = list(1, 1, 1, 1) to generate one latent trait and three continuous covariates. The length of cat_prop corresponds to the number of generated variables (including latent trait and continuous variables in this case).

set.seed(4388)
bg <- questionnaire_gen(n_obs = 100, cat_prop = list(1, 1, 1, 1), theta = TRUE, family = "gaussian")
str(bg)

We generate two ordinal variables regardless the item type. It turned out one 2-category variable and one 5-category variable are generated, respectively.

set.seed(4388)
bg <- questionnaire_gen(n_obs = 100, n_X = 0, n_W = 2, family = "gaussian")
str(bg)

We generate one binary variable and 3 four-category variables.

set.seed(4388)
bg <- questionnaire_gen(n_obs = 100, n_X = 0, n_W = list(2, 4, 4, 4), family = "gaussian")
str(bg)

We generate five variables including one latent trait, two continuous, and two binary covariates. The latent trait is scaled on a mean set at 500, with a standard deviation of 100.

set.seed(4388)
bg <- questionnaire_gen(n_obs = 100, n_X = 2, n_W = list(2, 2), theta = TRUE,
                        c_mean = c(500, 0, 0), c_sd = c(100, 1, 1), family = "gaussian")
str(bg)

We generate one continuous and two ordinal covariates. We specify the covariance matrix between the numeric and ordinal variables. The continuous covariate is scaled and the average is 2 by specifying c_mean = 2. When cov_matrix is provided, c_sd is ignored .

set.seed(4388)
props <- list(1, c(.25, 1), c(.2, .8, 1))
yw_cov <- matrix(c(1, .5, .5, .5, 1, .8, .5, .8, 1), nrow = 3)
bg <- questionnaire_gen(n_obs = 100, cat_prop = props, cov_matrix = yw_cov,
                        c_mean = 2,
                        family = "gaussian")
str(bg)


Try the lsasim package in your browser

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

lsasim documentation built on Aug. 22, 2023, 5:09 p.m.