Skip to content

Commit

Permalink
test coverage reached 100% for main est functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
SeewooLi committed Jul 11, 2023
1 parent 5af60dc commit b9f02ec
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 18 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: test-coverage

jobs:
test-coverage:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::covr
needs: coverage

- name: Test coverage
run: |
covr::codecov(
quiet = FALSE,
clean = FALSE,
install_path = file.path(Sys.getenv("RUNNER_TEMP"), "package")
)
shell: Rscript {0}

- name: Show testthat output
if: always()
run: |
## --------------------------------------------------------------------
find ${{ runner.temp }}/package -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash

- name: Upload test results
if: failure()
uses: actions/upload-artifact@v3
with:
name: coverage-test-failures
path: ${{ runner.temp }}/package
4 changes: 2 additions & 2 deletions R/IRTest_Dich.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ IRTest_Dich <- function(initialitem, data, range = c(-6,6), q = 121, model,
}

# Two-component normal mixture distribution
if(latent_dist=="Mixture"){
if(latent_dist %in% c("Mixture", "2NM")){
while(iter < max_iter & diff > threshold){
iter <- iter +1

Expand Down Expand Up @@ -247,7 +247,7 @@ IRTest_Dich <- function(initialitem, data, range = c(-6,6), q = 121, model,
}

# Davidian curve method
if(latent_dist=="DC"){
if(latent_dist%in% c("DC", "Davidian")){
phipar <- nlminb(start = rep(1,h),
objective = optim_phi,
gradient = optim_phi_grad,
Expand Down
6 changes: 3 additions & 3 deletions R/IRTest_Mix.R
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ IRTest_Mix <- function(initialitem_D, initialitem_P, data_D, data_P, range = c(-
model_D=model_D, model_P=model_P,
latent_dist=latent_dist, max_iter=max_iter, threshold=threshold)
if(nrow(data_D)!=nrow(data_P)){
warning("data_D and data_P have different number of rows.")
stop("data_D and data_P have different number of rows.")
}else{
I_D <- initialitem_D
I_P <- initialitem_P
Expand Down Expand Up @@ -253,7 +253,7 @@ if(nrow(data_D)!=nrow(data_P)){
}

# Two-component normal mixture distribution
if(latent_dist=="Mixture"){
if(latent_dist %in% c("Mixture", "2NM")){
while(iter < max_iter & diff > threshold){
iter <- iter +1

Expand Down Expand Up @@ -303,7 +303,7 @@ if(nrow(data_D)!=nrow(data_P)){
}

# Davidian curve method
if(latent_dist=="DC"){
if(latent_dist %in% c("DC", "Davidian")){
phipar <- nlminb(start = rep(1,h),
objective = optim_phi,
gradient = optim_phi_grad,
Expand Down
4 changes: 2 additions & 2 deletions R/IRTest_Poly.R
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ IRTest_Poly <- function(initialitem, data, range = c(-6,6), q = 121, model,
}

# Two-component normal mixture distribution
if(latent_dist=="Mixture"){
if(latent_dist %in% c("Mixture", "2NM")){
while(iter < max_iter & diff > threshold){
iter <- iter +1

Expand Down Expand Up @@ -243,7 +243,7 @@ IRTest_Poly <- function(initialitem, data, range = c(-6,6), q = 121, model,
}

# Davidian curve method
if(latent_dist=="DC"){
if(latent_dist %in% c("DC", "Davidian")){
phipar <- nlminb(start = rep(1,h),
objective = optim_phi,
gradient = optim_phi_grad,
Expand Down
4 changes: 2 additions & 2 deletions R/non_exporting_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ Mstep_Poly <- function(E, item, model="GPCM", max_iter=3, threshold=1e-7, EMiter
} else warning("model is incorrect or unspecified.")

}
return(list(item_estimated, se, Grad, IM))
return(list(item_estimated, se))
}

PDs <- function(probab, param, pmat, pcummat, a_supp, par, tcum){
Expand Down Expand Up @@ -490,7 +490,7 @@ latent_dist_est <- function(method, Xk, posterior, range,
to=range[2])
lin <- lin_inex(Xk, SJPI$y/sum(SJPI$y), range = range)
}
if(method=='DC'){
if(method %in% c('DC', 'Davidian')){
phipar <- nlminb(start = phipar,
objective = DC.LL,
gradient = DC.grad,
Expand Down
99 changes: 93 additions & 6 deletions tests/testthat/test-IRTest_Dich.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
test_that("testing basic operations for IRTest_Dich", {
test_that(
"testing basic operations for IRTest_Dich",
{
Alldata <- DataGeneration(seed = 123456789,
model_D = rep(1, 5),
model_D = rep(1, 10),
N=500,
nitem_D = 5,
nitem_D = 10,
nitem_P = 0,
d = 1.664,
sd_ratio = 2,
Expand All @@ -12,12 +14,97 @@ test_that("testing basic operations for IRTest_Dich", {
initialitem <- Alldata$initialitem_D
theta <- Alldata$theta

# Normal distribution
# EAP
Mod1 <- IRTest_Dich(initialitem = initialitem,
data = data,
model = rep(1, 5),
model = rep(1, 10),
latent_dist = "N",
max_iter = 2,
threshold = .0001)
expect_equal(dim(Mod1$par_est), dim(item))
expect_equal(length(Mod1$theta), length(theta))

# MLE
Mod1 <- IRTest_Dich(initialitem = initialitem,
data = data,
model = rep(1, 10),
latent_dist = "N",
max_iter = 2,
threshold = .0001,
ability_method = "MLE"
)
expect_equal(dim(Mod1$par_est), dim(item))
expect_equal(length(Mod1$theta), length(theta))

# 2PL
Mod1 <- IRTest_Dich(initialitem = initialitem,
data = data,
model = rep(2, 10),
latent_dist = "N",
max_iter = 2,
threshold = .0001)
expect_equal(dim(Mod1$par_est), dim(item))
expect_equal(length(Mod1$theta), length(theta))

# 3PL
Mod1 <- IRTest_Dich(initialitem = initialitem,
data = data,
model = rep(3, 10),
latent_dist = "N",
max_iter = 2,
threshold = .0001)
expect_equal(dim(Mod1$par_est), dim(item))
expect_equal(length(Mod1$theta), length(theta))

# EHM
Mod1 <- IRTest_Dich(initialitem = initialitem,
data = data,
model = rep(1, 10),
latent_dist = "EHM",
max_iter = 2,
threshold = .0001)
expect_equal(dim(Mod1$par_est), dim(item))
expect_equal(dim(Mod1$theta), dim(theta))
})
expect_equal(length(Mod1$theta), length(theta))

# Two-component normal distribution
Mod1 <- IRTest_Dich(initialitem = initialitem,
data = data,
model = rep(1, 10),
latent_dist = "2NM",
max_iter = 2,
threshold = .0001)
expect_equal(dim(Mod1$par_est), dim(item))
expect_equal(length(Mod1$theta), length(theta))

# KDE
Mod1 <- IRTest_Dich(initialitem = initialitem,
data = data,
model = rep(1, 10),
latent_dist = "KDE",
max_iter = 2,
threshold = .0001)
expect_equal(dim(Mod1$par_est), dim(item))
expect_equal(length(Mod1$theta), length(theta))

# Davidian curve
Mod1 <- IRTest_Dich(initialitem = initialitem,
data = data,
model = rep(1, 10),
latent_dist = "DC",
max_iter = 2,
threshold = .0001,
h=2)
expect_equal(dim(Mod1$par_est), dim(item))
expect_equal(length(Mod1$theta), length(theta))

expect_warning(
IRTest_Dich(initialitem = initialitem,
data = data,
model = NA,
latent_dist = "2NM",
max_iter = 2,
threshold = .0001)
)
}
)
77 changes: 75 additions & 2 deletions tests/testthat/test-IRTest_Mix.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,49 @@ test_that("testing basic operations for IRTest_Mix", {
initialitemP <- Alldata$initialitem_P
theta <- Alldata$theta

# Normal Distribution
Mod1 <- IRTest_Mix(initialitem_D = initialitemD,
initialitem_P = initialitemP,
data_D = DataD,
data_P = DataP,
model_D = rep(1,5),
model_P = "GPCM",
latent_dist = "N",
max_iter = 2,
threshold = .001)
expect_equal(dim(Mod1$par_est$Dichotomous), dim(itemD))
expect_equal(dim(Mod1$par_est$Polytomous), dim(itemP))
expect_equal(length(Mod1$theta), length(theta))

# EHM
Mod1 <- IRTest_Mix(initialitem_D = initialitemD,
initialitem_P = initialitemP,
data_D = DataD,
data_P = DataP,
model_D = rep(1,5),
model_P = "GPCM",
latent_dist = "EHM",
max_iter = 2,
threshold = .001)
expect_equal(dim(Mod1$par_est$Dichotomous), dim(itemD))
expect_equal(dim(Mod1$par_est$Polytomous), dim(itemP))
expect_equal(length(Mod1$theta), length(theta))

# Two-component normal distribution
Mod1 <- IRTest_Mix(initialitem_D = initialitemD,
initialitem_P = initialitemP,
data_D = DataD,
data_P = DataP,
model_D = rep(1,5),
model_P = "GPCM",
latent_dist = "2NM",
max_iter = 2,
threshold = .001)
expect_equal(dim(Mod1$par_est$Dichotomous), dim(itemD))
expect_equal(dim(Mod1$par_est$Polytomous), dim(itemP))
expect_equal(length(Mod1$theta), length(theta))

# KDE
Mod1 <- IRTest_Mix(initialitem_D = initialitemD,
initialitem_P = initialitemP,
data_D = DataD,
Expand All @@ -30,5 +73,35 @@ test_that("testing basic operations for IRTest_Mix", {
threshold = .001)
expect_equal(dim(Mod1$par_est$Dichotomous), dim(itemD))
expect_equal(dim(Mod1$par_est$Polytomous), dim(itemP))
expect_equal(dim(Mod1$theta), dim(theta))
})
expect_equal(length(Mod1$theta), length(theta))

# Davidian curve
Mod1 <- IRTest_Mix(initialitem_D = initialitemD,
initialitem_P = initialitemP,
data_D = DataD,
data_P = DataP,
model_D = rep(1,5),
model_P = "GPCM",
latent_dist = "DC",
max_iter = 2,
threshold = .001,
h=2)
expect_equal(dim(Mod1$par_est$Dichotomous), dim(itemD))
expect_equal(dim(Mod1$par_est$Polytomous), dim(itemP))
expect_equal(length(Mod1$theta), length(theta))


expect_error(
IRTest_Mix(initialitem_D = initialitemD,
initialitem_P = initialitemP,
data_D = DataD[1:900,],
data_P = DataP,
model_D = rep(1,5),
model_P = "GPCM",
latent_dist = "DC",
max_iter = 2,
threshold = .001,
h=2)
)
}
)
53 changes: 52 additions & 1 deletion tests/testthat/test-IRTest_Poly.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,47 @@ test_that("testing basic operations for IRTest_Poly", {
initialitem <- Alldata$initialitem_P
theta <- Alldata$theta

# Normal distribution
Mod1 <- IRTest_Poly(initialitem = initialitem,
data = data,
model = "GPCM",
latent_dist = "N",
max_iter = 2,
threshold = .001)
expect_equal(dim(Mod1$par_est), dim(item))
expect_equal(length(Mod1$theta), length(theta))

# EHM
Mod1 <- IRTest_Poly(initialitem = initialitem,
data = data,
model = "GPCM",
latent_dist = "EHM",
max_iter = 2,
threshold = .001)
expect_equal(dim(Mod1$par_est), dim(item))
expect_equal(length(Mod1$theta), length(theta))

# Two-component normal distribution
Mod1 <- IRTest_Poly(initialitem = initialitem,
data = data,
model = "GPCM",
latent_dist = "2NM",
max_iter = 2,
threshold = .001)
expect_equal(dim(Mod1$par_est), dim(item))
expect_equal(length(Mod1$theta), length(theta))

# KDE
Mod1 <- IRTest_Poly(initialitem = initialitem,
data = data,
model = "GPCM",
latent_dist = "KDE",
max_iter = 2,
threshold = .001)
expect_equal(dim(Mod1$par_est), dim(item))
expect_equal(length(Mod1$theta), length(theta))

# DC
Mod1 <- IRTest_Poly(initialitem = initialitem,
data = data,
model = "GPCM",
Expand All @@ -22,5 +63,15 @@ test_that("testing basic operations for IRTest_Poly", {
threshold = .001,
h=10)
expect_equal(dim(Mod1$par_est), dim(item))
expect_equal(dim(Mod1$theta), dim(theta))
expect_equal(length(Mod1$theta), length(theta))

expect_warning(
IRTest_Poly(initialitem = initialitem,
data = data,
model = NA,
latent_dist = "DC",
max_iter = 2,
threshold = .001,
h=10)
)
})

0 comments on commit b9f02ec

Please sign in to comment.