Skip to content

Commit

Permalink
- Switch computable from TRUE to FALSE.
Browse files Browse the repository at this point in the history
  • Loading branch information
joethorley committed Sep 23, 2024
1 parent e8ac421 commit 763fa04
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 45 deletions.
2 changes: 1 addition & 1 deletion R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ ssd_fit_dists <- function(
nrow = 6L,
rescale = FALSE,
reweight = FALSE,
computable = TRUE,
computable = FALSE,
at_boundary_ok = TRUE,
all_dists = FALSE,
min_pmix = ssd_min_pmix(nrow(data)),
Expand Down
2 changes: 1 addition & 1 deletion man/ssd_fit_dists.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 41 additions & 43 deletions tests/testthat/test-fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ test_that("ssd_fit_dists not happy with left as left by default", {

test_that("ssd_fit_dists returns object class fitdists", {
fit <- ssd_fit_dists(ssddata::ccme_boron,
dists = c("lnorm", "llogis"),
rescale = FALSE
dists = c("lnorm", "llogis"),
rescale = FALSE
)
expect_s3_class(fit, "fitdists")
})
Expand Down Expand Up @@ -217,33 +217,33 @@ test_that("ssd_fit_dists warns to rescale data", {
test_that("ssd_fit_dists doesn't warns to rescale data if already rescaled", {
data <- data.frame(Conc = rep(2, 6))
expect_error(expect_warning(ssd_fit_dists(data, rescale = TRUE, dist = "lnorm"),
regexp = "^Distribution 'lnorm' failed to fit:"
regexp = "^Distribution 'lnorm' failed to fit:"
))
})

test_that("ssd_fit_dists warns of optimizer convergence code error", {
data <- ssddata::ccme_boron
expect_error(
expect_warning(ssd_fit_dists(data, control = list(maxit = 1), dist = "lnorm"),
regexp = "^Distribution 'lnorm' failed to converge \\(try rescaling data\\): Iteration limit maxit reach \\(try increasing the maximum number of iterations in control\\)\\.$"
regexp = "^Distribution 'lnorm' failed to converge \\(try rescaling data\\): Iteration limit maxit reach \\(try increasing the maximum number of iterations in control\\)\\.$"
)
)
})

test_that("ssd_fit_dists estimates for ssddata::ccme_boron on bcanz dists", {
fits <- ssd_fit_dists(ssddata::ccme_boron, rescale = TRUE)

tidy <- tidy(fits)
expect_s3_class(tidy, "tbl")
expect_snapshot_data(tidy, "tidy_stable_rescale")
})

test_that("ssd_fit_dists not reorder", {
fit <- ssd_fit_dists(ssddata::ccme_boron,
dists = c("lnorm", "llogis"),
rescale = FALSE
dists = c("lnorm", "llogis"),
rescale = FALSE
)

expect_identical(npars(fit), c(lnorm = 2L, llogis = 2L))
expect_equal(logLik(fit), c(lnorm = -117.514216489547, llogis = -118.507435324581))
})
Expand All @@ -253,85 +253,82 @@ test_that("ssd_fit_dists equal weights no effect", {
data <- ssddata::ccme_boron
data$weight <- rep(2, nrow(data))
fits_weight <- ssd_fit_dists(data)

expect_equal(estimates(fits_weight), estimates(fits))
})

test_that("ssd_fit_dists computable = TRUE allows for fits without standard errors", {
data <- ssddata::ccme_boron
data$Other <- data$Conc
data$Conc <- data$Conc / max(data$Conc)

expect_warning(
expect_warning(
ssd_fit_dists(data, right = "Other", rescale = FALSE, at_boundary_ok = FALSE),
"^Distribution 'lnorm_lnorm' failed to fit \\(try rescaling data\\)"
),
"^Distribution 'lgumbel' failed to compute standard errors \\(try rescaling data\\)\\.$"
ssd_fit_dists(data, right = "Other", rescale = FALSE, at_boundary_ok = FALSE),
"^Distribution 'lnorm_lnorm' failed to fit \\(try rescaling data\\)"
)

set.seed(102)
fits <- ssd_fit_dists(data, right = "Other", dists = c("lgumbel", "llogis", "lnorm", "lnorm_lnorm"), rescale = FALSE, computable = FALSE, at_boundary_ok = TRUE)

fits <- ssd_fit_dists(data, right = "Other", dists = c("lgumbel", "llogis", "lnorm", "lnorm_lnorm"), rescale = FALSE, at_boundary_ok = TRUE)
tidy <- tidy(fits)
expect_s3_class(tidy, "tbl")
expect_snapshot_data(tidy, "tidy_stable_computable", digits = 3)
})

test_that("ssd_fit_dists works with slightly censored data", {
data <- ssddata::ccme_boron

data$right <- data$Conc * 2
data$Conc <- data$Conc * 0.5

fits <- ssd_fit_dists(data, dists = "lnorm", right = "right", rescale = FALSE)

tidy <- tidy(fits)

expect_equal(tidy$est, c(2.56052524750529, 1.17234562953404), tolerance = 1e-06)
expect_equal(tidy$se, c(0.234063281091344, 0.175423555900586), tolerance = 1e-05)
})

test_that("ssd_fit_dists accepts 0 for left censored data", {
data <- ssddata::ccme_boron

data$right <- data$Conc
data$Conc[1] <- 0

fits <- ssd_fit_dists(data, dists = "lnorm", right = "right", rescale = FALSE)

tidy <- tidy(fits)

expect_equal(tidy$est, c(2.54093502870563, 1.27968456496323), tolerance = 1e-06)
expect_equal(tidy$se, c(0.242558677928804, 0.175719927258761), tolerance = 1e-06)
})

test_that("ssd_fit_dists gives same values with zero and missing left values", {
data <- ssddata::ccme_boron

data$right <- data$Conc
data$Conc[1] <- 0

fits0 <- ssd_fit_dists(data, dists = "lnorm", right = "right")

data$Conc[1] <- NA

fitsna <- ssd_fit_dists(data, dists = "lnorm", right = "right")

expect_equal(tidy(fits0), tidy(fitsna))
})

test_that("ssd_fit_dists works with right censored data", {
data <- ssddata::ccme_boron

data$right <- data$Conc
data$right[1] <- Inf

expect_error(
fits <- ssd_fit_dists(data, dists = "lnorm", right = "right"),
"^Distributions cannot currently be fitted to right censored data\\.$"
)

#
# tidy <- tidy(fits)
#
Expand All @@ -341,22 +338,22 @@ test_that("ssd_fit_dists works with right censored data", {

test_that("ssd_fit_dists gives same answer for missing versus Inf right", {
data <- ssddata::ccme_boron

data$right <- data$Conc
data$right[1] <- Inf

expect_error(
fits <- ssd_fit_dists(data, dists = "lnorm", right = "right"),
"^Distributions cannot currently be fitted to right censored data\\.$"
)

data$right[1] <- NA

expect_error(
fits <- ssd_fit_dists(data, dists = "lnorm", right = "right"),
"^Distributions cannot currently be fitted to right censored data\\.$"
)

# fits0 <- ssd_fit_dists(data, dists = "lnorm", right = "right")
#
# data$right[1] <- NA
Expand Down Expand Up @@ -396,8 +393,9 @@ test_that("ssd_fit_dists at_boundary_ok message", {
)
expect_warning(
ssd_fit_dists(ssddata::ccme_boron,
dists = c("lnorm", "burrIII3"),
at_boundary_ok = TRUE
dists = c("lnorm", "burrIII3"),
at_boundary_ok = TRUE,
computable = TRUE
),
"failed to compute standard errors \\(try rescaling data\\)\\.$"
)
Expand All @@ -418,8 +416,8 @@ test_that("ssd_fit_dists unstable with anon_e", {

test_that("ssd_fit_dists works min_pmix = 0.5 and at_boundary_ok = TRUE and computable = FALSE", {
fit <- ssd_fit_dists(ssddata::ccme_boron,
dists = c("lnorm", "lnorm_lnorm"), min_pmix = 0.5,
at_boundary_ok = TRUE, computable = FALSE
dists = c("lnorm", "lnorm_lnorm"), min_pmix = 0.5,
at_boundary_ok = TRUE, computable = FALSE
)
tidy <- tidy(fit)
expect_snapshot_data(tidy, "min_pmix_05")
Expand Down

0 comments on commit 763fa04

Please sign in to comment.