Skip to content
Open
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
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
^\.jules(/.*)?$
^\.trivyignore\.yaml$
^trivy\.yaml$
^\.semgrepignore$
40 changes: 39 additions & 1 deletion .github/workflows/r.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ jobs:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd

- name: Install build dependencies
run: |
sudo apt-get update
sudo apt-get install -y cmake make

- name: Set up R
uses: r-lib/actions/setup-r@d3c5be51b12e724e68f33216ca3c148b66d5f0b6
with:
r-version: release
r-version: '4.5.3'
use-public-rspm: true

- name: Set up R package dependencies
Expand All @@ -41,6 +46,39 @@ jobs:
extra-packages: any::rcmdcheck
needs: check

- name: Build verified ABI-compatible R dependencies
shell: bash
run: |
set -euo pipefail

install_verified_source() {
local package="$1"
local version="$2"
local url="$3"
local expected_sha256="$4"
local archive
archive="$(mktemp --suffix=.tar.gz)"

curl --fail --show-error --silent --location \
--proto '=https' --tlsv1.2 "$url" --output "$archive"
printf '%s %s\n' "$expected_sha256" "$archive" |
sha256sum --check --strict
R CMD INSTALL "$archive"
rm -f "$archive"

Rscript -e \
"stopifnot(as.character(packageVersion('$package')) == '$version'); cat(normalizePath(find.package('$package')), '\n')"
}

install_verified_source \
RcppParallel 6.2.0 \
https://cran.r-project.org/src/contrib/RcppParallel_6.2.0.tar.gz \
3b6eaf73a696059552186292c79233916267f8e2b5c9a309519391d16c5a8bbe
install_verified_source \
qs2 0.2.2 \
https://cran.r-project.org/src/contrib/qs2_0.2.2.tar.gz \
c59ff879e858aef0afb13de25127239624e65b20179c8631fa1f62edea25f48f

- name: Run R CMD check
uses: r-lib/actions/check-r-package@6f6e5bc62fba3a704f74e7ad7ef7676c5c6a2590
with:
Expand Down
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-07-26 - R 언어에서 최솟값/최댓값 탐색 시 정렬(sort) 오버헤드 최적화
**Learning:** R에서 벡터의 최솟값 또는 최댓값을 찾기 위해 `sort(x)[1]` 또는 `names(sort(x))[1]`과 같이 전체를 정렬하는 방식을 사용하면, O(N log N)의 불필요한 연산 오버헤드가 발생하여 성능이 저하됩니다.
**Action:** `which.min(x)` 또는 `which.max(x)`를 활용하여 `names(x)[which.min(x)]`와 같이 변경함으로써 전체 데이터를 정렬하지 않고 O(N) 선형 탐색으로 성능을 향상시켜야 합니다.
19 changes: 18 additions & 1 deletion R/surveyFA.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
#' Select the first named minimum without sorting the full vector
#'
#' @description Returns the name attached to the first minimum value in a named
#' numeric vector. The helper intentionally uses a single linear scan so the
#' bounded recovery loop does not pay the cost of sorting every candidate.
#' @param values A named numeric vector whose missing-value policy has already
#' been applied by the caller.
#' @return The first minimum value's name, or `NA_character_` for an empty input.
#' @keywords internal
.minimum_named_value <- function(values) {
minimum_index <- which.min(values)
if (length(minimum_index) == 0L) {
return(NA_character_)
}
names(values)[minimum_index]
}

#' @title surveyFA
#' @description Fallback calibration helper used when direct model estimation in
#' `autoFIPC()` fails.
Expand Down Expand Up @@ -232,7 +249,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 <- .minimum_named_value(p_values)
if (!is.na(candidate) && p_values[[candidate]] < pThreshold) {
Comment thread
seonghobae marked this conversation as resolved.
return(candidate)
}
Expand Down
24 changes: 24 additions & 0 deletions tests/testthat/test-surveyFA.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,32 @@ test_that("surveyFA validates boolean control flags before estimator dispatch",
)
})

test_that("minimum named selection preserves prior sort semantics", {
normalized_cases <- list(
c(item_a = 0.40, item_b = 0.10, item_c = 0.30),
c(item_a = 0.10, item_b = 0.10, item_c = 0.20),
c(item_a = -0.50, item_b = 0.00, item_c = 0.50)
)
missing_case <- c(item_a = NA_real_, item_b = 0.20, item_c = 0.30)
missing_case[is.na(missing_case)] <- 1
normalized_cases[[length(normalized_cases) + 1L]] <- missing_case

for (p_values in normalized_cases) {
prior_candidate <- names(sort(p_values, decreasing = FALSE))[1L]
expect_identical(
aFIPC:::.minimum_named_value(p_values),
prior_candidate
)
}
expect_identical(
aFIPC:::.minimum_named_value(setNames(numeric(), character())),
NA_character_
)
})

test_that("surveyFA reports bounded recovery exhaustion when unrecoverable", {
skip_if_not_installed("mirt")
set.seed(20260726)

raw <- as.data.frame(
matrix(
Expand Down
Loading