Skip to content

Commit

Permalink
increasing coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
markolalovic committed Jun 21, 2024
1 parent 46632b4 commit 3d98066
Show file tree
Hide file tree
Showing 5 changed files with 317 additions and 115 deletions.
6 changes: 6 additions & 0 deletions dev/dev.R
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# Development
devtools::load_all("/Users/markolalovic/dev/latent2likert/")

covr::codecov(
quiet = FALSE,
clean = FALSE,
token = Sys.getenv("CODECOV_TOKEN")
)
29 changes: 14 additions & 15 deletions tests/testthat/test_discretization.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
context("Testing discretization")

testthat::test_that("discretization of N(0,1) using 4 levels gives expected results", {
#print("TODO: test coverage")
res = discretize_density(density_fn = dnorm, n_levels = 4)
expected = list(
prob = c(0.16, 0.34, 0.34, 0.16),
endp = c(-Inf, -0.98, 0, 0.98, Inf),
repr = c(-1.51, -0.45, 0.45, 1.51),
dist = 0.12)
testthat::expect_equal(res, expected, tolerance = 0.05)
res = discretize_density(density_fn = dnorm, n_levels = 4)
expected = list(
prob = c(0.16, 0.34, 0.34, 0.16),
endp = c(-Inf, -0.98, 0, 0.98, Inf),
repr = c(-1.51, -0.45, 0.45, 1.51),
dist = 0.12)
testthat::expect_equal(res, expected, tolerance = 0.05)
})

testthat::test_that("discretization of N(0,1) using 5 levels gives expected results", {
res = discretize_density(density_fn = dnorm, n_levels = 5)
expected = list(
prob = c(0.11, 0.24, 0.30, .24, 0.11),
endp = c(-Inf, -1.25, -0.38, 0.38, 1.25, Inf),
repr = c(-1.73, -0.77, 0, 0.77, 1.73),
dist = 0.08)
testthat::expect_equal(res, expected, tolerance = 0.05)
res = discretize_density(density_fn = dnorm, n_levels = 5)
expected = list(
prob = c(0.11, 0.24, 0.30, .24, 0.11),
endp = c(-Inf, -1.25, -0.38, 0.38, 1.25, Inf),
repr = c(-1.73, -0.77, 0, 0.77, 1.73),
dist = 0.08)
testthat::expect_equal(res, expected, tolerance = 0.05)
})
88 changes: 86 additions & 2 deletions tests/testthat/test_estimation.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
context("Testing estimation of parameters")

testthat::test_that("`mean` and `sd` are well estimated given `pk` for normal case", {
testthat::test_that("`mean` and `sd` are well estimated given `prob`
for normal case", {
actual <- c(-1, 0.5) # actual mean and sd
prob <- c("1" = 0.313, "2" = 0.579, "3" = 0.105, "4" = 0.003)
estimates <- estimate_mean_and_sd(prob, 5)
testthat::expect_equal(estimates, actual, tolerance = 0.1)
})

testthat::test_that("`mean` and `sd` are well estimated given `pk` for skew case", {
testthat::test_that("`mean` and `sd` are well estimated given `prob`
for skew case", {
actual <- c(0.146858, 1.084341) # actual mean and sd
skew <- -0.4565873 # skewness
prob <- c(
Expand All @@ -17,3 +19,85 @@ testthat::test_that("`mean` and `sd` are well estimated given `pk` for skew case
estimates <- estimate_mean_and_sd(prob, 7, skew)
testthat::expect_equal(estimates, actual, tolerance = 0.05)
})

testthat::test_that("estimate_params returns accurate estimates for
latent parameters when data is a vector", {
set.seed(12345) # for reproducibility

# Generate test data for a single item (vector data)
data_vector <- rlikert(size = 1000,
n_items = 1,
n_levels = 5,
mean = 0.5,
sd = 1.2,
skew = 0)

# Estimate parameters
estimates <- estimate_params(data_vector, n_levels = 5, skew = 0)

# Extract estimated means and standard deviations
estimated_mean <- as.numeric(estimates["mean"])
estimated_sd <- as.numeric(estimates["sd"])

# Actual means and standard deviations
actual_mean <- 0.5
actual_sd <- 1.2

# Check if estimated means are close to actual means
testthat::expect_equal(estimated_mean, actual_mean, tolerance = 0.1)

# Check if estimated standard deviations are close to actual standard deviations
testthat::expect_equal(estimated_sd, actual_sd, tolerance = 0.1)
})

testthat::test_that("estimate_params returns accurate estimates for
latent parameters", {
set.seed(12345) # for reproducibility

# Generate test data
corr <- matrix(c(1.00, -0.63, -0.39,
-0.63, 1.00, 0.41,
-0.39, 0.41, 1.00), nrow = 3)
data <- rlikert(size = 1000,
n_items = 3,
n_levels = c(4, 5, 6),
mean = c(0, -1, 0),
sd = c(0.8, 1, 1),
corr = corr)

# Estimate parameters
estimates <- estimate_params(data, n_levels = c(4, 5, 6), skew = 0)

# Extract estimated means and standard deviations
estimated_means <- as.numeric(estimates["mean", ])
estimated_sds <- as.numeric(estimates["sd", ])

# Actual means and standard deviations
actual_means <- c(0, -1, 0)
actual_sds <- c(0.8, 1, 1)

# Check if estimated means are close to actual means
testthat::expect_equal(estimated_means, actual_means, tolerance = 0.1)

# Check if estimated sds are close to actual sds
testthat::expect_equal(estimated_sds, actual_sds, tolerance = 0.1)
})

testthat::test_that("plot_contour executes without errors", {
# Define a simple objective function for testing
test_fn <- function(x, endp, prob, cdf_X) {
u <- x[1]
v <- x[2]
y <- cdf_X(v * endp - u * v)
return(matrix(utils::tail(y, -1) - utils::head(y, -1) - prob))
}

# Example parameters for the test
endp <- c(-Inf, -1, 0, 1, Inf)
prob <- c(0.1, 0.2, 0.4, 0.3)
cdf_X <- stats::pnorm
trace <- matrix(c(rep(0, 100), rep(1, 100)), nrow = 2)

# Run the plotting function
testthat::expect_silent(plot_contour(test_fn, endp, prob, cdf_X, trace))
})
Loading

0 comments on commit 3d98066

Please sign in to comment.