Skip to content

Commit

Permalink
handle no_fit and alternate cq versions
Browse files Browse the repository at this point in the history
  • Loading branch information
markdly committed May 10, 2018
1 parent 1cf777c commit 389b735
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
5 changes: 3 additions & 2 deletions R/cq_itanal.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ cq_read_itanal <- function(fname) {
txt <- txt %>%
tidyr::extract(
.data$case_disc, c("case", "disc"),
"Cases for this item +([0-9]+) +Discrimination +(-?[0-9]+\\.[0-9]+)$")
"Cases for this item +([0-9]+) +(?:Discrimination|Item-Rest Cor.) +(-?[0-9]+\\.[0-9]+)$")

txt <- txt %>%
tidyr::extract(
.data$threshold, c("thrsh", "mnsq"),
"Item Threshold\\(s\\)\\: (.+) Weighted MNSQ (.+)$")
"Item Threshold\\(s\\)\\:\\s+(\\S+)(.+)$") %>%
dplyr::mutate(mnsq = stringr::str_remove(.data$mnsq, "Weighted MNSQ"))

txt <- txt %>%
tidyr::extract(
Expand Down
9 changes: 7 additions & 2 deletions R/cq_show.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
#' @param fname A file path to an existing ConQuest show file (e.g.
#' 'myfile.shw'). Can take other input as for \code{readr::read_file}
#'
#' @param fit A logical value indicating if the show file has fit statistics
#' included or not. This only affects warnings generated. It is convenient
#' when no fit statistics have been requested as the default value \code{TRUE}
#' generates warnings that data is missing.
#'
#' @examples
#' fname <- cq_example(display = FALSE, example_name = "ex1.shw")
#' df <- cq_show(fname)
#'
#' @export
cq_show <- function(fname) {
cq_show <- function(fname, fit = c(TRUE, FALSE)) {

sections <- paste(
"SUMMARY OF THE ESTIMATION",
Expand Down Expand Up @@ -53,7 +58,7 @@ cq_show <- function(fname) {
# one column for each stat
txt <- txt %>%
mutate(stats = trimws(stats)) %>%
tidyr::separate(stats, param_stats_cols, "[\\s\\(\\),\\*]+", convert = TRUE)
tidyr::separate(stats, param_stats_cols, "[\\s\\(\\),\\*]+", convert = TRUE, fill = ifelse(fit, "warn", "right"))

txt
}
7 changes: 6 additions & 1 deletion man/cq_show.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 21 additions & 3 deletions tests/testthat/test-cq_itanal.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ test_that("import one item", {
})

test_that("import zero item", {
zero_item <- cq_itanal(system.file("extdata", "edge_case", "zero_item.itn", package = "conquestr"))
expect_equal(nrow(zero_item), 1)
expect_equal(trimws(zero_item$id), "item:106 (I_am_a_zero_item)")
x <- suppressWarnings(cq_itanal(system.file("extdata", "edge_case", "zero_item.itn", package = "conquestr")))
expect_warning(cq_itanal(system.file("extdata", "edge_case", "zero_item.itn", package = "conquestr")))
expect_equal(nrow(x), 1)
expect_equal(trimws(x$id), "item:106 (I_am_a_zero_item)")
})

test_that("item with one response code", {
Expand All @@ -35,3 +36,20 @@ test_that("very difficult item with extreme logit", {
expect_equal(x$thrsh, -32.00)
expect_equal(x$mnsq, NA_real_)
})

test_that("item rest cor version imports correctly", {
x <- cq_itanal(system.file("extdata", "edge_case", "item_rest_corr_version.itn", package = "conquestr"))
expect_equal(nrow(x), 1)
expect_equal(trimws(x$id), "item:120 (120)")
expect_equal(x$delta, 2.74)
expect_equal(x$thrsh, 2.74)
expect_equal(x$mnsq, NA_real_)
expect_equal(x$disc, 0.25)
})

test_that("thresholds still read correctly even if mnsq is not requested (fit=no)", {
x <- cq_itanal(system.file("extdata", "no_fit.itn", package = "conquestr"))
expect_equal(nrow(x), 9)
expect_equal(x$thrsh[1], 0.39)
expect_equal(x$mnsq[1], NA_real_)
})
15 changes: 15 additions & 0 deletions tests/testthat/test-cq_show.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
context("Show files")

df1 <- cq_show(cq_example(display = FALSE, example_name = "ex1.shw"))
df2 <- cq_itanal(cq_example(display = FALSE)) # using the itanal for comparison

test_that("Values imported correctly", {
expect_equal(round(df1$est, 2), df2$delta)
})

test_that("imports even if mnsq is not requested (fit=no)", {
x <- cq_show(system.file("extdata", "no_fit.shw", package = "conquestr"), fit = FALSE)
expect_warning(cq_show(system.file("extdata", "no_fit.shw", package = "conquestr")))
expect_equal(df1$est, x$est)
expect_equal(df1$err, x$err)
})

0 comments on commit 389b735

Please sign in to comment.