Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@
^\.jules(/.*)?$
^\.trivyignore\.yaml$
^trivy\.yaml$
^\.semgrepignore$
^\.Jules(/.*)?$
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) 오버헤드를 방지해야 합니다.
## 2024-08-01 - Avoid sort() overhead for finding minimum element
**Learning:** In R, using `sort(x)[1]` or `names(sort(x))[1]` to find the minimum (or maximum) element incurs unnecessary O(N log N) overhead, compared to using `which.min()` (or `which.max()`) which runs in O(N) linear time.
**Action:** When searching for the minimum or maximum value or its index/name, prefer using `which.min(x)` or `which.max(x)` instead of sorting the entire vector, especially inside iterative/looping structures like model fitting fallbacks where overhead compounds.
3 changes: 2 additions & 1 deletion R/surveyFA.R
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ 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]
# ⚡ Bolt: Use which.min() for O(N) linear time lookup instead of O(N log N) sort() overhead
candidate <- names(which.min(p_values))
if (!is.na(candidate) && p_values[[candidate]] < pThreshold) {
return(candidate)
}
Expand Down
2 changes: 0 additions & 2 deletions test_dummy.R

This file was deleted.

3 changes: 0 additions & 3 deletions test_validation.R

This file was deleted.

82 changes: 82 additions & 0 deletions tests/testthat/test-surveyFA.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,85 @@ test_that("surveyFA reports bounded recovery exhaustion when unrecoverable", {
"could not estimate a valid model after bounded recovery attempts"
)
})

test_that("surveyFA properly triggers fallback when autofix is disabled and covers item removal", {
skip_if_not_installed("mirt")
set.seed(20260702)

# Intentionally messy data to force failure on standard methods
raw <- as.data.frame(
matrix(
c(rep(1, 20), rep(0, 20), rbinom(160, 1, 0.5)),
ncol = 5
)
)
names(raw) <- paste0("item", 1:5)
raw$item6 <- 0 # Constant column

# Force failure without autofix
expect_error(
suppressWarnings(
aFIPC::surveyFA(
data = raw,
autofix = FALSE,
forceUIRT = TRUE,
forceNormalEM = FALSE,
forceMHRM = TRUE, # Cover forceMHRM branch
unstable = FALSE,
SE = TRUE,
itemtype = "2PL",
maxItemRemovals = 2
)
),
"could not estimate a valid model after bounded recovery attempts"
)

# Trigger unstable branch and force NormalEM=FALSE
expect_error(
suppressWarnings(
aFIPC::surveyFA(
data = raw,
autofix = TRUE,
forceUIRT = TRUE,
forceNormalEM = FALSE,
forceMHRM = FALSE,
unstable = FALSE,
SE = TRUE,
itemtype = "2PL",
maxItemRemovals = 2
)
),
"could not estimate a valid model after bounded recovery attempts"
)
Comment on lines +90 to +134

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- tracked files ---'
git ls-files | rg '(^|/)(surveyFA\\.R|test-surveyFA\\.R)$'
printf '%s\n' '--- test fixture and expectations ---'
test_file=$(git ls-files | rg '(^|/)tests/testthat/test-surveyFA\\.R$' | head -n1)
survey_file=$(git ls-files | rg '(^|/)R/surveyFA\\.R$' | head -n1)
printf 'test_file=%s\nsurvey_file=%s\n' "$test_file" "$survey_file"
sed -n '70,180p' "$test_file"
printf '%s\n' '--- surveyFA implementation ---'
sed -n '1,240p' "$survey_file"

Repository: ContextualWisdomLab/aFIPC

Length of output: 185


🏁 Script executed:

#!/bin/bash
set -e

test_file=$(git ls-files | rg '(^|/)tests/testthat/test-surveyFA\.R$' | head -n1)
survey_file=$(git ls-files | rg '(^|/)R/surveyFA\.R$' | head -n1)

printf '%s\n' '--- test section ---'
sed -n '70,180p' "$test_file"

printf '%s\n' '--- surveyFA implementation ---'
sed -n '1,240p' "$survey_file"

Repository: ContextualWisdomLab/aFIPC

Length of output: 9828


🏁 Script executed:

#!/bin/bash
set -e
survey_file=$(git ls-files | rg '(^|/)R/surveyFA\.R$' | head -n1)
printf '%s\n' '--- recovery and branch logic ---'
sed -n '220,430p' "$survey_file"
printf '%s\n' '--- test setup and skip scope ---'
test_file=$(git ls-files | rg '(^|/)tests/testthat/test-surveyFA\.R$' | head -n1)
sed -n '1,155p' "$test_file"
printf '%s\n' '--- deterministic matrix-shape probe ---'
python3 - <<'PY'
import random
values = [1] * 20 + [0] * 20 + [random.randint(0, 1) for _ in range(160)]
nrow, ncol = 40, 5
columns = [values[i*nrow:(i+1)*nrow] for i in range(ncol)]
for i, col in enumerate(columns, 1):
    print(f"item{i}: unique={sorted(set(col))}, counts={{0:{col.count(0)},1:{col.count(1)}}}")
PY

Repository: ContextualWisdomLab/aFIPC

Length of output: 6638


🏁 Script executed:

#!/bin/bash
set -e

survey_file=$(git ls-files | rg '(^|/)R/surveyFA\.R$' | head -n1)
test_file=$(git ls-files | rg '(^|/)tests/testthat/test-surveyFA\.R$' | head -n1)

sed -n '220,430p' "$survey_file"
sed -n '1,155p' "$test_file"

python3 - <<'PY'
import random
values = [1] * 20 + [0] * 20 + [random.randint(0, 1) for _ in range(160)]
for i in range(5):
    col = values[i * 40:(i + 1) * 40]
    print(i + 1, sorted(set(col)), col.count(0), col.count(1))
PY

Repository: ContextualWisdomLab/aFIPC

Length of output: 6399


복구 실패를 결정적으로 재현하는 fixture를 사용하세요. raw$item6은 적합 전에 제거됩니다. matrix(...)item1item5에 유효한 응답을 남깁니다. 따라서 forceMHRM = TRUEautofix = TRUEtry_fit() 실패를 보장하지 않으며, 현재 테스트는 autofix 항목 제거도 검증하지 못합니다. 실패를 보장하는 입력 또는 mirt::mirt stub을 사용하세요. unstable 분기를 검증하는 두 번째 호출은 unstable = TRUE로 설정하세요.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/testthat/test-surveyFA.R` around lines 90 - 134, Update the test
fixture and calls around surveyFA so the recovery failure is deterministic: use
input data that still causes try_fit() to fail after the pre-fit removal of
raw$item6, or stub mirt::mirt to force that failure. Ensure the autofix = TRUE
case exercises item removal, and set unstable = TRUE in the second surveyFA call
to actually cover the unstable branch.


# Trigger legacy forceUIRT warning
expect_error(
aFIPC::surveyFA(data=raw, forceUIRT = FALSE),
"surveyFA requires forceUIRT=TRUE"
)

# Check invalid itemtype
expect_error(
aFIPC::surveyFA(data=raw, itemtype = c("2PL", "3PL")),
"surveyFA requires itemtype to be a single non-NA character value"
)

# Check invalid maxItemRemovals
expect_error(
aFIPC::surveyFA(data=raw, maxItemRemovals = -1),
"surveyFA requires maxItemRemovals to be a non-negative numeric scalar"
)

# Check invalid pThreshold
expect_error(
aFIPC::surveyFA(data=raw, pThreshold = 1.5),
"surveyFA requires pThreshold to be in \\(0, 1\\]"
)

# Check insufficient non-constant columns
bad_raw <- data.frame(item1 = rep(1, 10), item2 = rep(2, 10))
expect_error(
aFIPC::surveyFA(bad_raw, forceUIRT=TRUE),
"surveyFA needs at least two non-constant response columns"
)
})
Loading