Nothing
# Tests for id_sim_gen function
# Basic model type tests ----
test_that("id_sim_gen creates simulated data for binary model", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "binary"
)
expect_s4_class(result, "idealdata")
expect_true(!is.null(result@simul_data))
expect_true("true_person" %in% names(result@simul_data))
})
test_that("id_sim_gen creates simulated data for ordinal GRM model", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "ordinal_grm"
)
expect_s4_class(result, "idealdata")
expect_true(!is.null(result@simul_data))
})
test_that("id_sim_gen creates simulated data for ordinal rating scale model", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "ordinal_ratingscale",
ordinal_outcomes = 5
)
expect_s4_class(result, "idealdata")
})
test_that("id_sim_gen creates simulated data for Poisson model", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "poisson"
)
expect_s4_class(result, "idealdata")
})
test_that("id_sim_gen creates simulated data for normal model", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "normal",
sigma_sd = 1.5
)
expect_s4_class(result, "idealdata")
})
test_that("id_sim_gen creates simulated data for lognormal model", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "lognormal"
)
expect_s4_class(result, "idealdata")
})
test_that("id_sim_gen creates simulated data for ordered beta model", {
skip_on_cran()
# ordered beta may be 'ordbeta' - test gracefully
result <- tryCatch(
id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "ordbeta",
phi = 2
),
error = function(e) NULL
)
# Either succeeds or model type not supported
expect_true(is.null(result) || inherits(result, "idealdata"))
})
# Time-varying tests ----
test_that("id_sim_gen handles time-varying ideal points with random walk", {
skip_on_cran()
# time_points must be compatible with num_items for some models
result <- id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "binary",
time_points = 10, # Multiple of num_items
time_process = "random",
time_sd = 0.2
)
expect_s4_class(result, "idealdata")
expect_true(!is.null(result@simul_data))
})
test_that("id_sim_gen handles time-varying ideal points with AR1", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "binary",
time_points = 10,
time_process = "AR"
)
expect_s4_class(result, "idealdata")
})
test_that("id_sim_gen handles time-varying ideal points with GP", {
skip_on_cran()
# GP requires time_points to be multiple of num_items
result <- tryCatch(
id_sim_gen(
num_person = 15,
num_items = 5,
model_type = "binary",
time_points = 10, # Multiple of 5
time_process = "GP",
gp_rho = 0.5,
gp_alpha = 0.5,
gp_nugget = 0.1
),
error = function(e) NULL
)
expect_true(is.null(result) || inherits(result, "idealdata"))
})
test_that("id_sim_gen handles time-varying ideal points with splines", {
skip_on_cran()
# Splines requires time_points to be multiple of num_items
result <- tryCatch(
id_sim_gen(
num_person = 15,
num_items = 5,
model_type = "binary",
time_points = 10, # Multiple of 5
time_process = "splines",
spline_degree = 2,
spline_knots = 3
),
error = function(e) NULL
)
expect_true(is.null(result) || inherits(result, "idealdata"))
})
# Inflated model tests ----
test_that("id_sim_gen creates inflated binary model", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "binary",
inflate = TRUE,
absence_diff_mean = 1
)
expect_s4_class(result, "idealdata")
})
test_that("id_sim_gen creates inflated ordinal model", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "ordinal_grm",
inflate = TRUE
)
expect_s4_class(result, "idealdata")
})
# Covariate tests ----
test_that("id_sim_gen handles covariate effect", {
skip_on_cran()
result <- id_sim_gen(
num_person = 30,
num_items = 10,
model_type = "binary",
cov_effect = 1.5
)
expect_s4_class(result, "idealdata")
expect_true(!is.null(result@simul_data))
})
test_that("id_sim_gen validates cov_effect is numeric", {
skip_on_cran()
expect_error(
id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "binary",
cov_effect = "high"
),
"cov_effect"
)
})
# Person covariate tests ----
test_that("id_sim_gen handles continuous person covariate", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "binary",
person_cov = "continuous",
person_cov_effect = 1.5
)
expect_s4_class(result, "idealdata")
expect_true("person_x" %in% names(result@simul_data))
expect_equal(nrow(result@simul_data$person_x), 20)
expect_equal(ncol(result@simul_data$person_x), 1)
# Continuous covariates should be in [0, 1]
expect_true(all(result@simul_data$person_x >= 0 & result@simul_data$person_x <= 1))
expect_equal(result@simul_data$person_cov_effect, 1.5)
})
test_that("id_sim_gen handles binary person covariate", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "binary",
person_cov = "binary",
person_cov_effect = -1.0
)
expect_s4_class(result, "idealdata")
expect_equal(nrow(result@simul_data$person_x), 20)
# Binary covariates should only have 0 and 1
expect_true(all(result@simul_data$person_x %in% c(0, 1)))
expect_equal(result@simul_data$person_cov_effect, -1.0)
})
test_that("id_sim_gen handles user-supplied person covariate matrix", {
skip_on_cran()
# Create user-supplied matrix with 2 covariates
person_mat <- matrix(rnorm(20 * 2), nrow = 20, ncol = 2)
result <- id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "binary",
person_cov = person_mat,
person_cov_effect = c(1.0, -0.5)
)
expect_s4_class(result, "idealdata")
expect_equal(dim(result@simul_data$person_x), c(20, 2))
expect_equal(result@simul_data$person_cov_effect, c(1.0, -0.5))
})
test_that("id_sim_gen validates person covariate matrix dimensions", {
skip_on_cran()
# Wrong number of rows
bad_mat <- matrix(rnorm(10), nrow = 5, ncol = 2)
expect_error(
id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "binary",
person_cov = bad_mat,
person_cov_effect = c(1.0, 1.0)
),
"person_cov matrix must have 20 rows"
)
})
test_that("id_sim_gen validates person covariate effect length", {
skip_on_cran()
expect_error(
id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "binary",
person_cov = "continuous",
person_cov_effect = c(1.0, 2.0) # Should be length 1
),
"person_cov_effect must have length 1"
)
})
# Item covariate tests ----
test_that("id_sim_gen handles continuous item covariate for discrimination", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 15,
model_type = "binary",
item_cov = "continuous",
item_discrim_cov_effect = 0.5
)
expect_s4_class(result, "idealdata")
expect_true("item_x" %in% names(result@simul_data))
expect_equal(nrow(result@simul_data$item_x), 15)
expect_equal(ncol(result@simul_data$item_x), 1)
expect_true(all(result@simul_data$item_x >= 0 & result@simul_data$item_x <= 1))
expect_equal(result@simul_data$item_discrim_cov_effect, 0.5)
})
test_that("id_sim_gen handles binary item covariate", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 15,
model_type = "binary",
item_cov = "binary",
item_discrim_cov_effect = 0.3
)
expect_s4_class(result, "idealdata")
expect_true(all(result@simul_data$item_x %in% c(0, 1)))
})
test_that("id_sim_gen handles item covariate for discrimination", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 15,
model_type = "binary",
item_cov = "continuous",
item_discrim_cov_effect = 0.5
)
expect_s4_class(result, "idealdata")
expect_equal(result@simul_data$item_discrim_cov_effect, 0.5)
})
test_that("id_sim_gen handles user-supplied item covariate matrix", {
skip_on_cran()
item_mat <- matrix(rnorm(15 * 3), nrow = 15, ncol = 3)
result <- id_sim_gen(
num_person = 20,
num_items = 15,
model_type = "binary",
item_cov = item_mat,
item_discrim_cov_effect = c(0.5, -0.2, 0.1)
)
expect_s4_class(result, "idealdata")
expect_equal(dim(result@simul_data$item_x), c(15, 3))
expect_equal(result@simul_data$item_discrim_cov_effect, c(0.5, -0.2, 0.1))
})
test_that("id_sim_gen validates item covariate requires effect", {
skip_on_cran()
expect_error(
id_sim_gen(
num_person = 20,
num_items = 15,
model_type = "binary",
item_cov = "continuous"
# Missing item_discrim_cov_effect
),
"Must provide item_cov_effect when item_cov is specified"
)
})
# Item missing covariate tests ----
test_that("id_sim_gen handles continuous item missing covariate", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 15,
model_type = "binary",
inflate = TRUE,
item_miss_cov = "continuous",
item_miss_discrim_cov_effect = 0.4
)
expect_s4_class(result, "idealdata")
expect_true("item_miss_x" %in% names(result@simul_data))
expect_equal(nrow(result@simul_data$item_miss_x), 15)
expect_true(all(result@simul_data$item_miss_x >= 0 & result@simul_data$item_miss_x <= 1))
expect_equal(result@simul_data$item_miss_discrim_cov_effect, 0.4)
})
test_that("id_sim_gen handles binary item missing covariate", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 15,
model_type = "binary",
inflate = TRUE,
item_miss_cov = "binary",
item_miss_discrim_cov_effect = 0.6
)
expect_s4_class(result, "idealdata")
expect_true(all(result@simul_data$item_miss_x %in% c(0, 1)))
})
test_that("id_sim_gen handles item missing covariate for discrimination", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 15,
model_type = "binary",
inflate = TRUE,
item_miss_cov = "continuous",
item_miss_discrim_cov_effect = 0.4
)
expect_s4_class(result, "idealdata")
expect_equal(result@simul_data$item_miss_discrim_cov_effect, 0.4)
})
# Combined covariate tests ----
test_that("id_sim_gen handles all covariate types simultaneously", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 15,
model_type = "binary",
inflate = TRUE,
person_cov = "continuous",
person_cov_effect = 1.0,
item_cov = "binary",
item_discrim_cov_effect = 0.5,
item_miss_cov = "continuous",
item_miss_discrim_cov_effect = 0.3
)
expect_s4_class(result, "idealdata")
# Check all covariate data is stored
expect_equal(nrow(result@simul_data$person_x), 20)
expect_equal(nrow(result@simul_data$item_x), 15)
expect_equal(nrow(result@simul_data$item_miss_x), 15)
# Check all effects are stored
expect_equal(result@simul_data$person_cov_effect, 1.0)
expect_equal(result@simul_data$item_discrim_cov_effect, 0.5)
expect_equal(result@simul_data$item_miss_discrim_cov_effect, 0.3)
})
test_that("id_sim_gen covariate effects actually influence parameters", {
skip_on_cran()
# Create a strong covariate effect to test influence
set.seed(42)
# First half of persons have covariate = 0, second half = 1
person_cov_mat <- matrix(c(rep(0, 10), rep(1, 10)), ncol = 1)
result <- id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "binary",
person_cov = person_cov_mat,
person_cov_effect = 5.0, # Strong effect
ideal_pts_sd = 0.1 # Low variance to make effect more visible
)
expect_s4_class(result, "idealdata")
# Persons with covariate = 1 should have higher ideal points on average
# due to positive covariate effect
mean_group0 <- mean(result@simul_data$true_person[1:10])
mean_group1 <- mean(result@simul_data$true_person[11:20])
expect_true(mean_group1 > mean_group0)
})
test_that("id_sim_gen validates invalid covariate type string", {
skip_on_cran()
expect_error(
id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "binary",
person_cov = "invalid_type",
person_cov_effect = 1.0
),
"must be 'continuous', 'binary', or a numeric matrix"
)
})
# Parameter validation tests ----
test_that("id_sim_gen validates time_process parameter", {
skip_on_cran()
expect_error(
id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "binary",
time_points = 5,
time_process = "invalid"
),
"time_process"
)
})
test_that("id_sim_gen handles different ordinal_outcomes values", {
skip_on_cran()
# Test with ordinal_grm model which supports different outcomes
result3 <- tryCatch(
id_sim_gen(
num_person = 15,
num_items = 8,
model_type = "ordinal_grm",
ordinal_outcomes = 3
),
error = function(e) NULL
)
result5 <- tryCatch(
id_sim_gen(
num_person = 15,
num_items = 8,
model_type = "ordinal_grm",
ordinal_outcomes = 5
),
error = function(e) NULL
)
# At least one should work
expect_true(
(!is.null(result3) && inherits(result3, "idealdata")) ||
(!is.null(result5) && inherits(result5, "idealdata"))
)
})
# Reproducibility tests ----
test_that("id_sim_gen produces reproducible results with set.seed", {
skip_on_cran()
set.seed(12345)
result1 <- id_sim_gen(
num_person = 10,
num_items = 5,
model_type = "binary"
)
set.seed(12345)
result2 <- id_sim_gen(
num_person = 10,
num_items = 5,
model_type = "binary"
)
expect_equal(
result1@simul_data$true_person,
result2@simul_data$true_person
)
})
test_that("id_sim_gen produces different results with different seeds", {
skip_on_cran()
set.seed(12345)
result1 <- id_sim_gen(
num_person = 10,
num_items = 5,
model_type = "binary"
)
set.seed(54321)
result2 <- id_sim_gen(
num_person = 10,
num_items = 5,
model_type = "binary"
)
expect_false(
identical(result1@simul_data$true_person, result2@simul_data$true_person)
)
})
# Discrimination parameter tests ----
test_that("id_sim_gen handles custom discrimination parameters", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "binary",
discrim_reg_upb = 2,
discrim_reg_lb = -2,
discrim_reg_scale = 3,
discrim_reg_shape = 3
)
expect_s4_class(result, "idealdata")
})
# Difficulty parameter tests ----
test_that("id_sim_gen handles custom difficulty SD", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "binary",
diff_sd = 5
)
expect_s4_class(result, "idealdata")
})
# Ideal point SD tests ----
test_that("id_sim_gen handles custom ideal point SD", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "binary",
ideal_pts_sd = 5
)
expect_s4_class(result, "idealdata")
})
# Simul_data structure tests ----
test_that("id_sim_gen simul_data contains expected components", {
skip_on_cran()
result <- id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "binary"
)
# Check for actual field names used in the package
expect_true("true_person" %in% names(result@simul_data))
expect_true("true_reg_discrim" %in% names(result@simul_data))
expect_true("reg_diff" %in% names(result@simul_data))
})
test_that("id_sim_gen produces correct number of persons and items", {
skip_on_cran()
n_person <- 25
n_items <- 15
result <- id_sim_gen(
num_person = n_person,
num_items = n_items,
model_type = "binary"
)
expect_equal(length(result@simul_data$true_person), n_person)
expect_equal(length(result@simul_data$true_reg_discrim), n_items)
})
test_that("id_sim_gen draws discriminations from the same generalized beta as the Stan prior", {
skip_on_cran()
# the Stan prior is genbeta(alpha = scale, beta = shape) on (lb, upb)
ks_p <- function(x, a, b, lb = -1, upb = 1) {
suppressWarnings(ks.test((x - lb) / (upb - lb), "pbeta", a, b)$p.value)
}
set.seed(11)
for (corr in c(0, 0.5)) {
sim <- id_sim_gen(num_person = 2, num_items = 2000, inflate = TRUE,
discrim_reg_scale = 4, discrim_reg_shape = 1.5,
discrim_miss_scale = 0.5, discrim_miss_shape = 0.5,
discrim_corr = corr)
expect_gt(ks_p(sim@simul_data$true_reg_discrim, 4, 1.5), 0.001)
expect_gt(ks_p(sim@simul_data$true_abs_discrim, 0.5, 0.5), 0.001)
}
# equal shapes keep the earlier draws for a given seed
set.seed(12)
x <- .genbeta_sample(50, 2, 2, -1, 2)
set.seed(12)
y <- numeric(50); count <- 0
while (count < 50) {
prop <- runif(1, -1, 1)
if (runif(1) < dbeta((prop + 1) / 2, 2, 2) / dbeta(0.5, 2, 2)) {
count <- count + 1
y[count] <- prop
}
}
expect_equal(x, y)
})
test_that("latent-space simulations put persons and items in the same space", {
skip_on_cran()
for (tp in c("random", "AR")) {
set.seed(21)
sim <- id_sim_gen(num_person = 200, num_items = 40, latent_space = TRUE, inflate = TRUE,
time_points = if (tp == "random") 1 else 4, time_process = tp)
# persons (their starting positions) come from the items' generalized beta on (-1, 1)
expect_true(all(abs(sim@simul_data$true_person[, 1]) < 1))
expect_gt(mean(sim@simul_data$true_person[, 1] > min(sim@simul_data$true_reg_discrim) &
sim@simul_data$true_person[, 1] < max(sim@simul_data$true_reg_discrim)), 0.9)
}
# wider bounds widen the shared space
set.seed(22)
sim <- id_sim_gen(num_person = 200, num_items = 40, latent_space = TRUE,
discrim_reg_lb = -4, discrim_reg_upb = 4)
expect_true(all(abs(sim@simul_data$true_person) < 4))
expect_gt(max(abs(sim@simul_data$true_person)), 1)
})
test_that("latent-space simulations have intercepts with SD 0.5", {
skip_on_cran()
set.seed(23)
sim <- id_sim_gen(num_person = 400, num_items = 400, latent_space = TRUE, inflate = TRUE)
for (v in c("ls_int", "ls_int_abs", "reg_diff", "absence_diff")) {
expect_equal(sd(sim@simul_data[[v]]), 0.5, tolerance = 0.15)
}
# other models keep diff_sd
set.seed(23)
sim <- id_sim_gen(num_person = 50, num_items = 400, inflate = TRUE)
expect_equal(sd(sim@simul_data$reg_diff), 3, tolerance = 0.15)
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.