tests/testthat/helper-setup.R

# ============================================================
# helper-setup.R
# Common test fixtures for ggExametrika
# Computed once before all tests
# ============================================================

has_exametrika <- requireNamespace("exametrika", quietly = TRUE)

#' 整形済みデータの先頭 n 行を取る
#'
#' このパッケージの主張は「ggplot が返るか」「構造が正しいか」であって,
#' 受験者数には依存しない。一方 plotRMP_gg()/plotCMP_gg() は受験者ごとに
#' 1 枚の ggplot を作るので,3,810 人のデータを渡すと 3,810 枚を描く。
#' それが CRAN の Windows 検査を 686 秒(上限 600)に押し上げていた
#' (2026-08-20 実測。tests が 484 秒で全体の 71%)。行を削るだけなので
#' クラス・メタデータ(response.type, categories, CA)は保たれる。
#'
#' **行を削りすぎると壊れる。**メタデータの `categories` は元データのまま
#' 残るので,部分集合で未観測になったカテゴリがあると推定側と列数が食い違って
#' フィットが落ちる(J15S3810 の順序データは 120 行で発生・150 行で解消)。
#' 余裕をみた行数を使い,下の test-fixtures.R で全フィクスチャが組めたことを
#' 検査する。
head_rows_dat <- function(x, n) {
  n <- min(n, length(x$ID))
  x$ID <- x$ID[seq_len(n)]
  for (f in c("Q", "U", "Z")) {
    if (!is.null(x[[f]])) x[[f]] <- x[[f]][seq_len(n), , drop = FALSE]
  }
  return(x)
}

if (has_exametrika) {
  # ---- GridSearch fixtures (fast: small grids on J15S500) ----
  fixture_GS_LCA <- tryCatch(
    suppressMessages(exametrika::GridSearch(exametrika::J15S500,
      max_ncls = 4, fun = "LCA", index = "BIC", verbose = FALSE
    )),
    error = function(e) NULL
  )
  fixture_GS_Bicl <- tryCatch(
    suppressMessages(exametrika::GridSearch(exametrika::J15S500,
      max_ncls = 3, max_nfld = 3, fun = "Biclustering", index = "BIC", verbose = FALSE
    )),
    error = function(e) NULL
  )

  # ---- IRT fixtures (fast) ----
  fixture_IRT_2PL <- tryCatch(
    exametrika::IRT(exametrika::J15S500, model = 2),
    error = function(e) NULL
  )

  fixture_IRT_3PL <- tryCatch(
    exametrika::IRT(exametrika::J15S500, model = 3),
    error = function(e) NULL
  )

  # ---- GRM fixture (fast) ----
  fixture_GRM <- tryCatch(
    exametrika::GRM(exametrika::J5S1000),
    error = function(e) NULL
  )

  # ---- LCA fixture ----
  fixture_LCA <- tryCatch(
    exametrika::LCA(exametrika::J15S500, ncls = 3),
    error = function(e) NULL
  )

  # ---- LRA fixture ----
  fixture_LRA <- tryCatch(
    exametrika::LRA(exametrika::J15S500, nrank = 3),
    error = function(e) NULL
  )

  # ---- Binary Biclustering fixture ----
  fixture_Biclust <- tryCatch(
    exametrika::Biclustering(exametrika::J35S515, nfld = 3, ncls = 4),
    error = function(e) NULL
  )

  # ---- Ranklustering fixture (same data, method = "R": latent ranks) ----
  fixture_Ranklust <- tryCatch(
    exametrika::Biclustering(exametrika::J35S515, nfld = 3, ncls = 4, method = "R"),
    error = function(e) NULL
  )

  # ---- Ordinal Biclustering fixture (synthetic small data) ----
  set.seed(42)
  .synth_ordinal <- matrix(sample(0:3, 40 * 12, replace = TRUE),
    nrow = 40, ncol = 12
  )
  colnames(.synth_ordinal) <- paste0("Item", 1:12)
  # (the response type is read from the data; `dataType =` was never an
  # argument of Biclustering()/LRA(), so until 2026-11-06 the "nominal" and
  # "rated" fixtures below were in fact ordinal fits)
  fixture_ordBiclust <- tryCatch(
    exametrika::Biclustering(.synth_ordinal, nfld = 2, ncls = 3),
    error = function(e) NULL
  )

  # ---- Nominal Biclustering fixture (synthetic small data) ----
  # Use 4 categories (0:3) to ensure FRP 3rd dimension >= 3 for FCRP tests
  set.seed(43)
  .synth_nominal <- matrix(sample(0:3, 40 * 12, replace = TRUE),
    nrow = 40, ncol = 12
  )
  colnames(.synth_nominal) <- paste0("Item", 1:12)
  fixture_nomBiclust <- tryCatch(
    exametrika::Biclustering(
      exametrika::dataFormat(.synth_nominal, response.type = "nominal"),
      nfld = 2, ncls = 3
    ),
    error = function(e) NULL
  )

  # ---- LRAordinal fixture ----
  fixture_LRAord <- tryCatch(
    exametrika::LRA(head_rows_dat(exametrika::J15S3810, 200), nrank = 3),
    error = function(e) NULL
  )

  # ---- LRArated fixture ----
  # J21S300 is the rated dataset (with correct answers)
  fixture_LRArated <- tryCatch(
    exametrika::LRA(exametrika::J21S300, nrank = 3),
    error = function(e) NULL
  )

  # ---- LDLRA fixture ----
  # LDLRA requires a DAG list (one igraph per rank)
  fixture_LDLRA <- tryCatch(
    {
      ldlra_dag <- data.frame(
        from = c("Item01", "Item02", "Item03"),
        to   = c("Item02", "Item03", "Item04")
      )
      ldlra_g <- igraph::graph_from_data_frame(ldlra_dag, directed = TRUE)
      exametrika::LDLRA(head_rows_dat(exametrika::J12S5000, 300),
        ncls = 3,
        g = list(ldlra_g, ldlra_g, ldlra_g)
      )
    },
    error = function(e) NULL
  )

  # ---- LDB fixture ----
  # LDB requires field assignments (conf) and a DAG list (g_list).
  fixture_LDB <- tryCatch(
    {
      ldb_conf <- rep(1:3, length.out = 35)
      ldb_g <- igraph::graph_from_data_frame(
        data.frame(
          from = c("Field01", "Field02"),
          to = c("Field02", "Field03")
        ),
        directed = TRUE
      )
      exametrika::LDB(exametrika::J35S515,
        ncls = 3, conf = ldb_conf,
        g_list = list(ldb_g, ldb_g, ldb_g)
      )
    },
    error = function(e) NULL
  )

  # ---- BNM fixture (fast) ----
  # BNM requires a DAG (directed acyclic graph) as input
  fixture_BNM <- tryCatch(
    {
      # Create a simple DAG for J5S10 (5 items)
      bnm_dag <- igraph::make_empty_graph(n = 5, directed = TRUE)
      igraph::V(bnm_dag)$name <- exametrika::J5S10$ItemLabel
      # Add edges: Item01->Item03, Item02->Item04, Item03->Item05
      bnm_dag <- igraph::add_edges(bnm_dag, c(1, 3, 2, 4, 3, 5))
      exametrika::BNM(exametrika::J5S10, g = bnm_dag)
    },
    error = function(e) NULL
  )

  # ---- BINET fixture ----
  # BINET requires field assignments (conf) and DAG edge file (adj_file).
  # Note: BINET's adj_list input path has a known bug (g_csv undefined),
  # so we use adj_file with a temporary CSV instead.
  fixture_BINET <- tryCatch(
    {
      binet_conf <- rep(1:3, length.out = 35)
      # Create temporary edge CSV: From, To, Field
      binet_edges <- data.frame(
        From  = c(1, 2, 1, 2, 1, 2),
        To    = c(2, 3, 2, 3, 2, 3),
        Field = c(1, 1, 2, 2, 3, 3)
      )
      binet_edge_file <- tempfile(fileext = ".csv")
      write.csv(binet_edges, binet_edge_file, row.names = FALSE)
      result <- exametrika::BINET(exametrika::J35S515,
        ncls = 3, nfld = 3,
        conf = binet_conf, adj_file = binet_edge_file,
        verbose = FALSE
      )
      unlink(binet_edge_file)
      result
    },
    error = function(e) NULL
  )

  # ---- Rated Biclustering fixture (synthetic small data) ----
  # Rated data: items scored 0-4 (e.g. 5-point Likert scale)
  # method = "B": the rated Biclustering defaults to method = "R" (ranks),
  # and the tests treat this fixture as a class model
  fixture_ratedBiclust <- tryCatch(
    exametrika::Biclustering(exametrika::J21S300, nfld = 2, ncls = 3, method = "B"),
    error = function(e) NULL
  )

  # ---- DistractorAnalysis fixtures (from LRA.rated) ----
  fixture_DA_lra <- tryCatch(
    {
      lra_rated <- exametrika::LRA(exametrika::J21S300, nrank = 5, mic = TRUE)
      exametrika::DistractorAnalysis(lra_rated)
    },
    error = function(e) NULL
  )

  # ---- DistractorAnalysis fixtures (from ratedBiclustering) ----
  fixture_DA_biclust <- tryCatch(
    {
      bic_rated <- exametrika::Biclustering(exametrika::J21S300,
        ncls = 5, nfld = 3, method = "R", maxiter = 100
      )
      exametrika::DistractorAnalysis(bic_rated)
    },
    error = function(e) NULL
  )

  # Clean up temporary variables
  rm(.synth_ordinal, .synth_nominal)
}

Try the ggExametrika package in your browser

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

ggExametrika documentation built on Nov. 25, 2026, 9:07 a.m.