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
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-13 - R 언어에서 데이터프레임 조건부 업데이트 시 2차원 인덱싱 병목 최적화
**Learning:** R에서 조건에 맞는 특정 열을 업데이트할 때 `df[idx, 'col'] <- val` 방식의 2차원 인덱싱은 팩터(factor) 레벨 검증, 차원 확인, 전체 데이터프레임 깊은 복사(deep copy) 등의 S3 메소드 디스패치(`[<-.data.frame`) 오버헤드를 유발하여 O(N) 수준의 매우 비효율적인 성능 저하를 초래합니다.
**Action:** 메소드 디스패치를 우회하고 O(1) 리스트 접근(list access)과 C-레벨 벡터 수정으로 직접 처리하기 위해 벡터 직접 서브셋팅(direct vector subsetting) 방식인 `df$col[idx] <- val`를 사용하여 성능을 대폭 향상시켜야 합니다.
59 changes: 31 additions & 28 deletions R/aFIPC.R
Original file line number Diff line number Diff line change
Expand Up @@ -598,25 +598,28 @@ autoFIPC <-
# Preserve mirt's structural estimability flags. Forcing every row TRUE
# frees boundary parameters such as 2PL g/u and makes the Hessian unstable.

NewScaleParms[NewScaleParms$item == 'GROUP', "est"] <- FALSE
OldScaleParms[OldScaleParms$item == 'GROUP', "est"] <- FALSE
# ⚡ Bolt: Use direct vector subsetting (e.g. df$col[idx] <- val) instead of
# two-dimensional subsetting to bypass method dispatch overhead
NewScaleParms$est[NewScaleParms$item == 'GROUP'] <- FALSE
OldScaleParms$est[OldScaleParms$item == 'GROUP'] <- FALSE

NewScaleParms[NewScaleParms$name == "COV_11", "est"] <- TRUE
OldScaleParms[OldScaleParms$name == "COV_11", "est"] <- TRUE
NewScaleParms$est[NewScaleParms$name == "COV_11"] <- TRUE
OldScaleParms$est[OldScaleParms$name == "COV_11"] <- TRUE

if (itemtype == 'Rasch') {
NewScaleParms[NewScaleParms$name == "a1", "est"] <- FALSE
OldScaleParms[OldScaleParms$name == "a1", "est"] <- FALSE
NewScaleParms$est[NewScaleParms$name == "a1"] <- FALSE
OldScaleParms$est[OldScaleParms$name == "a1"] <- FALSE
}

#IPD
if (checkIPD == T) {
# config
# ⚡ Bolt: Explicitly define factor levels to bypass automatic level inference overhead
IPDgroup <-
as.factor(c(
rep('oldForm', nrow(oldformYDataK)),
rep('newForm', nrow(newformXDataK))
))
factor(
rep(c('oldForm', 'newForm'), c(nrow(oldformYDataK), nrow(newformXDataK))),
levels = c('newForm', 'oldForm')
)
Comment on lines +619 to +622
IPDItemCount <- 0

# IPD target item checking
Expand Down Expand Up @@ -786,14 +789,14 @@ autoFIPC <-
oldIdx <- oldScaleParmsItemIdxCache[[oldFormItemStr]]

# ⚡ Bolt: Remove unnecessary paste0() array string generation overhead
message(' Newform Parms: ', paste(NewScaleParms[newIdx, "value"], collapse = ' '))
message(' Oldform Parms: ', paste(OldScaleParms[oldIdx, "value"], collapse = ' '))
message(' Newform Parms: ', paste(NewScaleParms$value[newIdx], collapse = ' '))
message(' Oldform Parms: ', paste(OldScaleParms$value[oldIdx], collapse = ' '))

NewScaleParms[newIdx, "value"] <-
OldScaleParms[oldIdx, "value"]
message(' Linkedform Parms: ', paste(NewScaleParms[newIdx, "value"], collapse = ' '), '\n')
NewScaleParms$value[newIdx] <-
OldScaleParms$value[oldIdx]
message(' Linkedform Parms: ', paste(NewScaleParms$value[newIdx], collapse = ' '), '\n')

NewScaleParms[newIdx, "est"] <-
NewScaleParms$est[newIdx] <-
FALSE
} else {
message(
Expand All @@ -813,17 +816,17 @@ autoFIPC <-
newBetaIdx <- NewScaleParms$item == 'BETA'
oldBetaIdx <- OldScaleParms$item == 'BETA'

NewScaleParms[newBetaIdx, "value"] <-
OldScaleParms[oldBetaIdx, "value"]
NewScaleParms[newBetaIdx, "est"] <-
NewScaleParms$value[newBetaIdx] <-
OldScaleParms$value[oldBetaIdx]
NewScaleParms$est[newBetaIdx] <-
FALSE

message('applying BETA parameter as linking')

message(
' Linkedform Parms: ',
paste0(
NewScaleParms[newBetaIdx, "value"],
NewScaleParms$value[newBetaIdx],
' '
),
'\n'
Expand Down Expand Up @@ -858,13 +861,13 @@ autoFIPC <-
new_mean11_idx <- NewScaleParms$name == "MEAN_11"
old_mean11_idx <- OldScaleParms$name == "MEAN_11"

NewScaleParms[new_cov11_idx, "est"] <- FALSE
OldScaleParms[old_cov11_idx, "est"] <- FALSE
NewScaleParms[new_mean11_idx, "est"] <- FALSE
OldScaleParms[old_mean11_idx, "est"] <- FALSE
NewScaleParms$est[new_cov11_idx] <- FALSE
OldScaleParms$est[old_cov11_idx] <- FALSE
NewScaleParms$est[new_mean11_idx] <- FALSE
OldScaleParms$est[old_mean11_idx] <- FALSE

NewScaleParms[new_cov11_idx, "value"] <- 1
OldScaleParms[old_mean11_idx, "value"] <- 0
NewScaleParms$value[new_cov11_idx] <- 1
OldScaleParms$value[old_mean11_idx] <- 0
}
if (freeMEAN == T) {
LinkedModelSyntax <-
Expand All @@ -875,8 +878,8 @@ autoFIPC <-
'MEAN = F1'
))

NewScaleParms[NewScaleParms$name == "MEAN_1", "est"] <- TRUE
OldScaleParms[OldScaleParms$name == "MEAN_1", "est"] <- TRUE
NewScaleParms$est[NewScaleParms$name == "MEAN_1"] <- TRUE
OldScaleParms$est[OldScaleParms$name == "MEAN_1"] <- TRUE
} else {
LinkedModelSyntax <-
mirt::mirt.model(paste0(
Expand Down
45 changes: 45 additions & 0 deletions tests/testthat/test-autoFIPC-estimation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
test_that("autoFIPC initial estimation failure", {
skip_if_not_installed("mirt")
skip_if_not_installed("mockery")
Comment on lines +2 to +3

# Stub mirt::mirt so it just returns a string, making `isRealMirtModel` and `exists` checks work or fail in specific ways
m_fail <- function(...) stop("forced failure")
mockery::stub(aFIPC::autoFIPC, 'mirt::mirt', m_fail)
Comment on lines +5 to +7

expect_error(
aFIPC::autoFIPC(
newformXData = data.frame(A=c(1,1,1), B=c(0,0,0)),
oldformYData = data.frame(A=c(1,1,1), B=c(0,0,0)),
newformCommonItemNames = c('A'),
oldformCommonItemNames = c('A'),
itemtype = '2PL',
confirmCommonItems = TRUE
),
"Security Error: Initial estimation of oldFormModel completely failed"
)
})

test_that("autoFIPC newform initial estimation failure", {
skip_if_not_installed("mirt")
skip_if_not_installed("mockery")

set.seed(1)
dat_old <- mirt::simdata(a=matrix(c(1,1,1,1)), d=matrix(c(0,0,0,0)), N=100, itemtype="2PL")
colnames(dat_old) <- c("A", "B", "C", "D")
old_mod <- mirt::mirt(dat_old, 1, itemtype="2PL", SE=FALSE, verbose=FALSE)

m_fail <- function(...) stop("forced failure")
mockery::stub(aFIPC::autoFIPC, 'mirt::mirt', m_fail)

expect_error(
aFIPC::autoFIPC(
newformXData = dat_old,
oldformYData = old_mod,
newformCommonItemNames = c('A'),
oldformCommonItemNames = c('A'),
itemtype = '2PL',
confirmCommonItems = TRUE
),
"Security Error: Initial estimation of newFormModel completely failed"
)
})
109 changes: 109 additions & 0 deletions tests/testthat/test-autoFIPC-extras.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
test_that("autoFIPC specific error paths", {
skip_if_not_installed("mirt")

# oldformBILOGprior must be logical
expect_error(
aFIPC::autoFIPC(
newformXData = data.frame(A=1),
oldformYData = data.frame(A=2),
newformCommonItemNames = c('A'),
oldformCommonItemNames = c('A'),
oldformBILOGprior = "TRUE"
),
"Security Error: oldformBILOGprior must be a single non-NA logical value or NULL"
)

# tryFitwholeOldItems must be logical
expect_error(
aFIPC::autoFIPC(
newformXData = data.frame(A=1),
oldformYData = data.frame(A=2),
newformCommonItemNames = c('A'),
oldformCommonItemNames = c('A'),
tryFitwholeOldItems = "TRUE"
),
"Security Error: tryFitwholeOldItems must be a single non-NA logical value"
)

# checkIPD must be logical
expect_error(
aFIPC::autoFIPC(
newformXData = data.frame(A=1),
oldformYData = data.frame(A=2),
newformCommonItemNames = c('A'),
oldformCommonItemNames = c('A'),
checkIPD = "TRUE"
),
"Security Error: checkIPD must be a single non-NA logical value"
)

# freeMEAN must be logical
expect_error(
aFIPC::autoFIPC(
newformXData = data.frame(A=1),
oldformYData = data.frame(A=2),
newformCommonItemNames = c('A'),
oldformCommonItemNames = c('A'),
freeMEAN = "TRUE"
),
"Security Error: freeMEAN must be a single non-NA logical value"
)

# forceNormalZeroOne must be logical
expect_error(
aFIPC::autoFIPC(
newformXData = data.frame(A=1),
oldformYData = data.frame(A=2),
newformCommonItemNames = c('A'),
oldformCommonItemNames = c('A'),
forceNormalZeroOne = "TRUE"
),
"Security Error: forceNormalZeroOne must be a single non-NA logical value"
)

# parameterOverwrite must be logical
expect_error(
aFIPC::autoFIPC(
newformXData = data.frame(A=1),
oldformYData = data.frame(A=2),
newformCommonItemNames = c('A'),
oldformCommonItemNames = c('A'),
parameterOverwrite = "TRUE"
),
"Security Error: parameterOverwrite must be a single non-NA logical value"
)

# empiricalhist must be logical
expect_error(
aFIPC::autoFIPC(
newformXData = data.frame(A=1),
oldformYData = data.frame(A=2),
newformCommonItemNames = c('A'),
oldformCommonItemNames = c('A'),
empiricalhist = "TRUE"
),
"Security Error: empiricalhist must be a single non-NA logical value"
)

# Unequal common items
expect_error(
aFIPC::autoFIPC(
newformXData = data.frame(A=1),
oldformYData = data.frame(A=2),
newformCommonItemNames = c('A', 'B'),
oldformCommonItemNames = c('A')
),
"Common Items are not equal"
)

# 0 length common items
expect_error(
aFIPC::autoFIPC(
newformXData = data.frame(A=1),
oldformYData = data.frame(A=2),
newformCommonItemNames = character(0),
oldformCommonItemNames = character(0)
),
"Please provide common item names"
)
})
19 changes: 19 additions & 0 deletions tests/testthat/test-surveyFA-extras.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
test_that("surveyFA specific branches", {
skip_if_not_installed("mirt")

# 1. Invalid itemtype
expect_error(aFIPC::surveyFA(data.frame(a=c(0,1), b=c(1,0)), itemtype=c("2PL", "3PL")), "surveyFA requires itemtype to be a single non-NA character value.")
expect_error(aFIPC::surveyFA(data.frame(a=c(0,1), b=c(1,0)), itemtype=123), "surveyFA requires itemtype to be a single non-NA character value.")

# 2. Invalid maxItemRemovals
expect_error(aFIPC::surveyFA(data.frame(a=c(0,1), b=c(1,0)), maxItemRemovals="3"), "surveyFA requires maxItemRemovals to be a non-negative numeric scalar.")
expect_error(aFIPC::surveyFA(data.frame(a=c(0,1), b=c(1,0)), maxItemRemovals=-1), "surveyFA requires maxItemRemovals to be a non-negative numeric scalar.")

# 3. Invalid pThreshold
expect_error(aFIPC::surveyFA(data.frame(a=c(0,1), b=c(1,0)), pThreshold="0.05"), "surveyFA requires pThreshold to be in \\(0, 1\\]\\.")
expect_error(aFIPC::surveyFA(data.frame(a=c(0,1), b=c(1,0)), pThreshold=-0.1), "surveyFA requires pThreshold to be in \\(0, 1\\]\\.")
expect_error(aFIPC::surveyFA(data.frame(a=c(0,1), b=c(1,0)), pThreshold=1.5), "surveyFA requires pThreshold to be in \\(0, 1\\]\\.")

# 4. forceUIRT = FALSE (triggers stop)
expect_error(aFIPC::surveyFA(data.frame(a=c(0,1), b=c(1,0)), forceUIRT = FALSE), "surveyFA requires forceUIRT=TRUE")
})
Loading