Nothing
# Tests for id_estimate function
options(cmdstanr_warn_inits = FALSE)
test_that("id_estimate validates use_method parameter", {
skip_on_cran()
# Create minimal test data
sim_data <- id_sim_gen(
num_person = 10,
num_items = 5,
model_type = "binary"
)
# Invalid use_method should error
expect_error(
id_estimate(sim_data, model_type = "binary", use_method = "invalid_method"),
"use_method"
)
})
test_that("id_estimate validates idealdata input", {
skip_on_cran()
# Passing NULL should work but return NULL if cmdstanr not available
# Passing wrong type should error
expect_error(
id_estimate(idealdata = "not_an_idealdata", model_type = "binary"),
class = "error"
)
expect_error(
id_estimate(idealdata = data.frame(x = 1), model_type = "binary"),
class = "error"
)
})
test_that("id_estimate validates model_type parameter", {
skip_on_cran()
skip_if_not_installed("cmdstanr")
sim_data <- id_sim_gen(
num_person = 10,
num_items = 5,
model_type = "binary"
)
# model_type should be numeric 1-16
expect_error(
id_estimate(sim_data, model_type = "binary"),
class = "error"
)
})
test_that("id_estimate validates vary_ideal_pts parameter", {
skip_on_cran()
sim_data <- id_sim_gen(
num_person = 10,
num_items = 5,
model_type = "binary"
)
# vary_ideal_pts should be one of specific values
# This test checks that valid values are accepted (no error in setup)
expect_s4_class(sim_data, "idealdata")
})
test_that("id_estimate validates fixtype parameter", {
skip_on_cran()
sim_data <- id_sim_gen(
num_person = 10,
num_items = 5,
model_type = "binary"
)
# fixtype should be one of: 'prefix', 'postfix', 'vb_full', 'vb_partial'
valid_fixtypes <- c("prefix", "postfix", "vb_full", "vb_partial")
# Just verify simulation works for setup
expect_s4_class(sim_data, "idealdata")
})
test_that("id_estimate validates const_type parameter", {
skip_on_cran()
sim_data <- id_sim_gen(
num_person = 10,
num_items = 5,
model_type = "binary"
)
# const_type should be "persons" or "items"
valid_const_types <- c("persons", "items")
expect_s4_class(sim_data, "idealdata")
})
test_that("id_estimate validates numeric parameters", {
skip_on_cran()
sim_data <- id_sim_gen(
num_person = 10,
num_items = 5,
model_type = "binary"
)
# nchains, niters, warmup, ncores should be positive integers
# These would be validated during estimation
expect_s4_class(sim_data, "idealdata")
})
test_that("id_estimate validates restrict_ind parameters", {
skip_on_cran()
sim_data <- id_sim_gen(
num_person = 10,
num_items = 5,
model_type = "binary"
)
# restrict_ind_high and restrict_ind_low should be valid indices
expect_s4_class(sim_data, "idealdata")
})
test_that("id_estimate rejects a person or item pinned both high and low", {
skip_on_cran()
set.seed(1)
sim_data <- id_sim_gen(num_person = 10, num_items = 5, model_type = "binary")
expect_error(
id_estimate(sim_data, model_type = 1, use_method = "pathfinder", fixtype = "prefix",
restrict_ind_high = c(3, 4), restrict_ind_low = c(4, 7)),
"can't be in both restrict_ind_high and restrict_ind_low: 4"
)
# items given as character IDs are compared after they are matched to the data
expect_error(
id_estimate(sim_data, model_type = 1, use_method = "pathfinder", fixtype = "prefix",
const_type = "items", restrict_ind_high = c("1", "2"),
restrict_ind_low = c("2", "5")),
"same item"
)
})
test_that("id_estimate validates prior parameters", {
skip_on_cran()
sim_data <- id_sim_gen(
num_person = 10,
num_items = 5,
model_type = "binary"
)
# Prior parameters should be positive where required
# discrim_reg_scale, discrim_reg_shape, person_sd, etc.
expect_s4_class(sim_data, "idealdata")
})
test_that("id_estimate handles subset parameters", {
skip_on_cran()
sim_data <- id_sim_gen(
num_person = 20,
num_items = 10,
model_type = "binary"
)
# use_subset, subset_group, subset_person, sample_size
expect_s4_class(sim_data, "idealdata")
})
test_that("pinned persons get the values they were given", {
skip_on_cran()
skip_if_not_installed("cmdstanr")
set.seed(1)
sim_data <- id_sim_gen(num_person = 20, num_items = 10)
# pins out of index order and with values that aren't whole numbers
fit <- suppressMessages(suppressWarnings(
id_estimate(sim_data, model_type = 1, use_method = "pathfinder", ncores = 1,
fixtype = "prefix", restrict_ind_high = c(7, 3), restrict_ind_low = 5,
fix_high = c(0.7, 2.6), fix_low = -1.5, seed = 1)
))
# the data passed to Stan
stan_data <- fit@this_data
pins <- setNames(stan_data$fix_high, stan_data$restrict_high)
expect_equal(unname(pins[c("7", "3")]), c(0.7, 2.6))
expect_equal(stan_data$restrict_low, 5)
expect_equal(stan_data$fix_low, -1.5)
})
# Long-form ordinal votes (0/1/2) of 30 persons on 15 items, with 40 missing
# values, and the person who is highest and lowest for pinning
ordinal_votes <- function(ordered_id = TRUE) {
set.seed(2)
votes <- expand.grid(person = paste0("p", 1:30), item = paste0("i", 1:15))
ideal <- setNames(rnorm(30), paste0("p", 1:30))
votes$outcome <- as.integer(cut(ideal[as.character(votes$person)] + rnorm(nrow(votes)),
c(-Inf, -0.5, 0.5, Inf))) - 1L
votes$outcome[sample(nrow(votes), 40)] <- NA
if(ordered_id) votes$ordered_id <- 3
attr(votes, "pins") <- c(high = names(which.max(ideal)), low = names(which.min(ideal)))
votes
}
fit_ordinal <- function(votes, model_type) {
ord_data <- suppressMessages(id_make(votes, outcome_disc = "outcome", person_id = "person",
item_id = "item"))
suppressMessages(suppressWarnings(
id_estimate(ord_data, model_type = model_type, use_method = "pathfinder", ncores = 1,
fixtype = "prefix",
restrict_ind_high = attr(votes, "pins")[["high"]],
restrict_ind_low = attr(votes, "pins")[["low"]],
seed = 1)
))
}
test_that("item summaries of an inflated rating-scale model find the cutpoints", {
skip_on_cran()
skip_if_not_installed("cmdstanr")
# made with id_make, so the outcome has a level for the missing values as well
# as the three categories
fit <- fit_ordinal(ordinal_votes(), model_type = 4)
items <- summary(fit, pars = "items")
i1 <- items[items$Parameter == "i1", ]
expect_true(all(c("Non-Inflated Discrimination", "Inflated Discrimination") %in% i1$`Item Type`))
# one midpoint per cutpoint: two for three categories
expect_equal(sum(i1$`Item Type` == "Non-Inflated Item Midpoint"), 2)
# the inflated difficulty is the missing-data intercept
expect_equal(unname(i1$`Posterior Median`[i1$`Item Type` == "Inflated Difficulty"]),
unname(median(fit@stan_samples$draws("A_int_free[1]"))))
draws <- summary(fit, pars = "items", aggregated = FALSE)
expect_equal(sort(unique(draws$`Predicted Outcome`[draws$Parameter == "i1" &
draws$`Item Type` == "Non-Inflated Item Midpoint"])),
c("1", "2"))
})
test_that("item summaries of a graded response model have a midpoint per cutpoint", {
skip_on_cran()
skip_if_not_installed("cmdstanr")
fit <- fit_ordinal(ordinal_votes(), model_type = 6)
items <- summary(fit, pars = "items")
i1 <- items[items$Parameter == "i1", ]
midpoints <- i1$`Posterior Median`[i1$`Item Type` == "Non-Inflated Item Midpoint"]
expect_length(midpoints, 2)
expect_false(isTRUE(all.equal(midpoints[1], midpoints[2])))
expect_equal(unname(i1$`Posterior Median`[i1$`Item Type` == "Inflated Difficulty"]),
unname(median(fit@stan_samples$draws("A_int_free[1]"))))
})
test_that("ordinal models don't need ordered_id when every item is ordinal", {
skip_on_cran()
skip_if_not_installed("cmdstanr")
fit <- fit_ordinal(ordinal_votes(ordered_id = FALSE), model_type = 4)
# the three observed categories, not counting the missing values
expect_equal(unique(fit@score_data@score_matrix$ordered_id), 3)
expect_equal(sum(summary(fit, pars = "items")$`Item Type` == "Non-Inflated Item Midpoint"), 30)
})
test_that("a model that mixes ordinal and other items needs ordered_id", {
# id_estimate compiles the model before it checks the data
skip_on_cran()
skip_if_not_installed("cmdstanr")
votes <- ordinal_votes(ordered_id = FALSE)
# make the first five items binary
binary <- votes$item %in% paste0("i", 1:5)
votes$outcome[binary] <- as.integer(votes$outcome[binary] > 0)
votes$model_id <- ifelse(binary, 1, 3)
mixed <- suppressMessages(id_make(votes, outcome_disc = "outcome", person_id = "person",
item_id = "item", model_id = "model_id"))
expect_error(
id_estimate(mixed, use_method = "pathfinder", ncores = 1, fixtype = "prefix",
restrict_ind_high = attr(votes, "pins")[["high"]],
restrict_ind_low = attr(votes, "pins")[["low"]]),
"needs the number of categories of each ordinal item")
})
test_that("id_plot_persons shows item midpoints for a model without groups", {
skip_on_cran()
skip_if_not_installed("cmdstanr")
fit <- fit_ordinal(ordinal_votes(), model_type = 3)
expect_s3_class(suppressWarnings(id_plot_persons(fit, item_plot = "i1")), "ggplot")
})
test_that("groups are pinned, summarised and plotted by their labels", {
skip_on_cran()
skip_if_not_installed("cmdstanr")
set.seed(3)
sim <- id_sim_gen(num_person = 30, num_items = 20)
# parties from the true ideal points
truth <- sim@simul_data$true_person[, 1]
party <- cut(truth, quantile(truth, c(0, 1/3, 2/3, 1)), include.lowest = TRUE,
labels = c("Left", "Center", "Right"))
votes <- data.frame(person = sim@score_matrix$person_id,
item = sim@score_matrix$item_id,
party = as.character(party[as.numeric(sim@score_matrix$person_id)]),
outcome = as.integer(sim@score_matrix$outcome_disc) - 1L)
data <- suppressMessages(id_make(votes, outcome_disc = "outcome", person_id = "person",
item_id = "item", group_id = "party"))
fit <- suppressMessages(suppressWarnings(
id_estimate(data, use_groups = TRUE, use_method = "pathfinder", ncores = 1,
fixtype = "prefix", restrict_ind_high = "Right", restrict_ind_low = "Left",
seed = 1)
))
groups <- suppressMessages(summary(fit)) |>
dplyr::distinct(Group, `Posterior Median`)
expect_setequal(groups$Group, c("Left", "Center", "Right"))
median_of <- function(g) groups$`Posterior Median`[groups$Group == g]
expect_gt(median_of("Right"), median_of("Center"))
expect_gt(median_of("Center"), median_of("Left"))
expect_error(
suppressMessages(id_estimate(data, use_groups = TRUE, use_method = "pathfinder",
fixtype = "prefix", restrict_ind_high = "Up",
restrict_ind_low = "Left")),
"not in the data")
# plots of some groups only
g <- suppressMessages(id_plot_persons(fit, include = "Center"))
expect_equal(unique(as.character(g$data$group_id)), "Center")
expect_error(suppressMessages(id_plot_persons(fit, include = "Up")), "not in the data")
# objects made with idealstan 1.0 stored the groups as integers
old <- fit
old@score_data@score_matrix$group_id <- as.integer(old@score_data@score_matrix$group_id)
expect_equal(suppressMessages(summary(old))$Group, suppressMessages(summary(fit))$Group)
# persons, with the parties as labels
fit <- suppressMessages(suppressWarnings(
id_estimate(data, use_method = "pathfinder", ncores = 1, fixtype = "prefix",
restrict_ind_high = as.character(which.max(truth)),
restrict_ind_low = as.character(which.min(truth)), seed = 1)
))
expect_setequal(suppressMessages(summary(fit))$Group, c("Left", "Center", "Right"))
g <- suppressMessages(id_plot_persons(fit, include = c("1", "2")))
expect_setequal(as.character(g$data$person_id), c("1", "2"))
g <- suppressMessages(id_plot_persons(fit, sample_persons = 0.5))
expect_equal(nrow(g$data), 15)
# numeric item IDs
expect_s3_class(suppressMessages(suppressWarnings(id_plot_persons(fit, item_plot = 3))), "ggplot")
})
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.