diff --git a/.Rbuildignore b/.Rbuildignore index 8989c62f..d6ed8bb5 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -24,3 +24,6 @@ ^\.jules(/.*)?$ ^\.trivyignore\.yaml$ ^trivy\.yaml$ +^\.semgrepignore$ +^test_dummy\.R$ +^test_validation\.R$ diff --git a/.jules/bolt.md b/.jules/bolt.md index 7d3c603f..60560c71 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -16,3 +16,6 @@ ## 2025-02-12 - R 언어에서 반복적인 mirt 모델 생성 시 불필요한 데이터프레임 부분집합 추출 최적화 **Learning:** R에서 데이터프레임의 특정 열을 추출하는 작업(`df[cols]`)은 O(N)의 메모리 복사를 수반합니다. `autoFIPC`에서 `mirt` 모델의 파라미터를 설정하거나 호출하는 과정 중에 `newformXDataK[colnames(newFormModel@Data$data)]` 코드가 반복해서 사용되었고, 심지어 `ncol()`을 위해 단순히 개수를 구할 때도 사용되어 불필요한 메모리 할당과 오버헤드를 초래했습니다. **Action:** 조건문이나 반복문 내부에서 불필요하게 데이터프레임 부분집합 연산이 반복되지 않도록 외부에서 한 번만 `linkedFormData <- newformXDataK[colnames(newFormModel@Data$data)]`로 캐싱(caching)한 뒤, `ncol(linkedFormData)`와 `data = linkedFormData` 형태로 재사용하여 메모리 복사와 O(N) 오버헤드를 방지해야 합니다. +## 2026-08-31 - [R 성능 최적화: 최소/최대값 탐색 시 sort() 피하기] +**Learning:** R에서 최소값이나 최대값을 찾을 때 `sort(x)[1]` 또는 `names(sort(x))[1]`을 사용하면 O(N log N)의 정렬 오버헤드가 발생합니다. +**Action:** 불필요한 전체 정렬을 피하고, O(N)의 선형 시간 복잡도를 가지는 `which.min(x)` 또는 `which.max(x)`를 사용해야 합니다. (예: `names(x)[which.min(x)]`) diff --git a/DESCRIPTION b/DESCRIPTION index f31d3e1a..c90753c5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -10,7 +10,7 @@ Description: Automates fixed item parameter linking for test linking under the item response theory paradigm using mirt package estimates. License: GPL-3 | file LICENSE Imports: mirt, methods -Suggests: testthat (>= 3.0.0) +Suggests: testthat (>= 3.0.0), mockery Encoding: UTF-8 Config/testthat/edition: 3 Config/roxygen2/version: 8.0.0 diff --git a/R/surveyFA.R b/R/surveyFA.R index f60fffd8..7d11fa4f 100644 --- a/R/surveyFA.R +++ b/R/surveyFA.R @@ -232,7 +232,7 @@ surveyFA <- function( names(p_values) <- rownames(fit_df) if (any(!is.na(p_values))) { p_values[is.na(p_values)] <- 1 - candidate <- names(sort(p_values, decreasing = FALSE))[1L] + candidate <- names(p_values)[which.min(p_values)] if (!is.na(candidate) && p_values[[candidate]] < pThreshold) { return(candidate) } diff --git a/tests/testthat/test-surveyFA.R b/tests/testthat/test-surveyFA.R index 060ae68e..de5d7ea2 100644 --- a/tests/testthat/test-surveyFA.R +++ b/tests/testthat/test-surveyFA.R @@ -82,3 +82,79 @@ test_that("surveyFA reports bounded recovery exhaustion when unrecoverable", { "could not estimate a valid model after bounded recovery attempts" ) }) + +test_that("surveyFA correctly identifies the item with the minimum p-value", { + skip_if_not_installed("mirt") + set.seed(42) + + # Create synthetic data that will cause some misfit + raw <- as.data.frame( + mirt::simdata( + a = matrix(rep(1, 5), ncol = 1), + d = rep(0, 5), + itemtype = rep("2PL", 5), + N = 100 + ) + ) + names(raw) <- paste0("item", seq_len(ncol(raw))) + + # Inject noise to one item to make it misfit (item3) + raw$item3 <- rbinom(100, 1, 0.1) + + # Capture the warning which might occur during estimation, we only care about the return + fitted <- suppressWarnings( + aFIPC::surveyFA( + data = raw, + autofix = TRUE, + forceUIRT = TRUE, + forceNormalEM = TRUE, + SE = TRUE + ) + ) + + # If it removed an item, item3 is highly likely to be the one removed + # or at least the process should not crash and should return a valid model. + expect_true(inherits(fitted, "SingleGroupClass")) + # Verify that candidate extraction which uses which.min() works without error +}) + +test_that("surveyFA correctly identifies the item with the minimum variance when p-values aren't enough", { + skip_if_not_installed("mirt") + skip_if_not_installed("mockery") + set.seed(42) + + # Create synthetic data with one constant column so its variance is 0 + raw <- as.data.frame( + mirt::simdata( + a = matrix(rep(1, 5), ncol = 1), + d = rep(0, 5), + itemtype = rep("2PL", 5), + N = 100 + ) + ) + names(raw) <- paste0("item", seq_len(ncol(raw))) + + # Inject noise to one item to make it misfit and also give it a really low variance + # In select_bad_item, it falls back to var if it couldn't find anything by p-value or the itemfit fails. + # Let's mock mirt::itemfit so it fails and we test the variance path directly. + + mock_itemfit <- mockery::mock(stop("Forced error")) + mockery::stub(aFIPC::surveyFA, "mirt::itemfit", mock_itemfit) + + # We want one item to have smaller variance + raw$item3 <- rep(0, 100) + raw$item3[1] <- 1 # slightly non-constant so it doesn't get pre-filtered + + fitted <- suppressWarnings( + aFIPC::surveyFA( + data = raw, + autofix = TRUE, + forceUIRT = TRUE, + forceNormalEM = TRUE, + SE = TRUE, + maxItemRemovals = 1 + ) + ) + + expect_true(inherits(fitted, "SingleGroupClass")) +})