From d5ad16b03b5be7317cff5e895e38f968b407900b Mon Sep 17 00:00:00 2001 From: Rob Carnell Date: Sun, 11 Dec 2022 21:15:03 -0500 Subject: [PATCH] continuing to debug examples on rhub platforms --- R/cdfe.R | 4 +++- R/mle-utils.R | 12 +++++++++--- docs/pkgdown.yml | 2 +- docs/reference/mle-utils.html | 6 +----- docs/reference/triangle_cdfe.html | 4 +++- man/mle-utils.Rd | 2 +- man/triangle_cdfe.Rd | 4 +++- tests/testthat/test-mle-utils.R | 32 +++++++++++++++++++------------ 8 files changed, 41 insertions(+), 25 deletions(-) diff --git a/R/cdfe.R b/R/cdfe.R index ad40ced..2400522 100644 --- a/R/cdfe.R +++ b/R/cdfe.R @@ -16,7 +16,9 @@ #' print(cdfe) #' summary(cdfe) #' coef(cdfe) -#' if (isNamespaceLoaded("MASS")) confint(cdfe) +#' \dontrun{ +#' MASS::confint(cdfe) +#' } triangle_cdfe <- function(x) { # x <- rtriangle(100, 0, 1, .3) diff --git a/R/mle-utils.R b/R/mle-utils.R index c6cf4bd..eae2592 100644 --- a/R/mle-utils.R +++ b/R/mle-utils.R @@ -29,11 +29,14 @@ #' \dontrun{ #' prof <- profile(mle1) #' stats4::plot(prof) +#' confint(mle1, 1:3, level = 0.95) #' } -#' confint(mle1, 1:3, level = 0.95) summary.triangle_mle <- function(object, ...) { - cmat <- cbind(Estimate = object$coef, `Std. Error` = sqrt(diag(object$vcov))) + vars <- diag(object$vcov) + # it is possible to have negative numbers in the vcov because of numerical precision + vars[which(vars < 0)] <- NA + cmat <- cbind(Estimate = object$coef, `Std. Error` = sqrt(vars)) m2logL <- 2 * object$min methods::new("summary.mle", call = object$call, coef = cmat, m2logL = m2logL) } @@ -46,10 +49,13 @@ summary.triangle_mle <- function(object, ...) #' @importFrom stats4 coef vcov print.triangle_mle <- function(x, ...) { + vars <- diag(stats4::vcov(x)) + # it is possible to have negative numbers in the vcov because of numerical precision + vars[which(vars < 0)] <- NA cat("Triangle Maximum Likelihood Estimates") cat("\n\nCall: ", deparse(x$call), "\n") cat("\nEstimates:\n") - cmat <- cbind(stats4::coef(x), sqrt(diag(stats4::vcov(x)))) + cmat <- cbind(stats4::coef(x), sqrt(vars)) colnames(cmat) <- c("Estimate", "Std.Err") stats::printCoefmat(cmat, ...) cat("\nConvergence Code: ", ifelse(all(is.na(x$details)), NA, x$details$convergence)) diff --git a/docs/pkgdown.yml b/docs/pkgdown.yml index bec7dac..d8e585f 100644 --- a/docs/pkgdown.yml +++ b/docs/pkgdown.yml @@ -3,5 +3,5 @@ pkgdown: 2.0.6 pkgdown_sha: ~ articles: triangledistributionmath: triangledistributionmath.html -last_built: 2022-12-12T01:03Z +last_built: 2022-12-12T02:13Z diff --git a/docs/reference/mle-utils.html b/docs/reference/mle-utils.html index 335c108..2fe9a98 100644 --- a/docs/reference/mle-utils.html +++ b/docs/reference/mle-utils.html @@ -197,12 +197,8 @@

Examples

if (FALSE) { prof <- profile(mle1) stats4::plot(prof) + confint(mle1, 1:3, level = 0.95) } -confint(mle1, 1:3, level = 0.95) -#> 2.5 % 97.5 % -#> a -0.04358564 0.06047349 -#> b 0.94005344 1.03393455 -#> c 0.30209405 0.46660603 diff --git a/docs/reference/triangle_cdfe.html b/docs/reference/triangle_cdfe.html index 12665e6..dd79d56 100644 --- a/docs/reference/triangle_cdfe.html +++ b/docs/reference/triangle_cdfe.html @@ -111,7 +111,9 @@

Examples

coef(cdfe) #> a b c #> 0.7848163 5.3555055 2.1058729 -if (isNamespaceLoaded("MASS")) confint(cdfe) +if (FALSE) { + MASS::confint(cdfe) +} diff --git a/man/mle-utils.Rd b/man/mle-utils.Rd index e0a665e..4007fd7 100644 --- a/man/mle-utils.Rd +++ b/man/mle-utils.Rd @@ -81,6 +81,6 @@ vcov(mle1) \dontrun{ prof <- profile(mle1) stats4::plot(prof) + confint(mle1, 1:3, level = 0.95) } -confint(mle1, 1:3, level = 0.95) } diff --git a/man/triangle_cdfe.Rd b/man/triangle_cdfe.Rd index 899701e..8ec86a1 100644 --- a/man/triangle_cdfe.Rd +++ b/man/triangle_cdfe.Rd @@ -22,5 +22,7 @@ cdfe <- triangle_cdfe(xtest) print(cdfe) summary(cdfe) coef(cdfe) -if (isNamespaceLoaded("MASS")) confint(cdfe) +\dontrun{ + MASS::confint(cdfe) +} } diff --git a/tests/testthat/test-mle-utils.R b/tests/testthat/test-mle-utils.R index a3d53ae..beee7b1 100644 --- a/tests/testthat/test-mle-utils.R +++ b/tests/testthat/test-mle-utils.R @@ -1,5 +1,6 @@ test_that("summary works", { - xtest <- c(0.1, 0.25, 0.3, 0.4, 0.45, 0.6, 0.75, 0.8) + set.seed(39854) + xtest <- rtriangle(200, 0, 1, 0.5) mle1 <- triangle_mle(xtest) summ <- summary(mle1) @@ -12,14 +13,16 @@ test_that("summary works", { }) test_that("print works", { - xtest <- c(0.1, 0.25, 0.3, 0.4, 0.45, 0.6, 0.75, 0.8) + set.seed(39854) + xtest <- rtriangle(200, 0, 1, 0.5) mle1 <- triangle_mle(xtest) expect_output(print(mle1)) }) test_that("coef works", { - xtest <- c(0.1, 0.25, 0.3, 0.4, 0.45, 0.6, 0.75, 0.8) + set.seed(39854) + xtest <- rtriangle(200, 0, 1, 0.5) mle1 <- triangle_mle(xtest) cf <- coef(mle1) @@ -28,7 +31,8 @@ test_that("coef works", { }) test_that("logLik works", { - xtest <- c(0.1, 0.25, 0.3, 0.4, 0.45, 0.6, 0.75, 0.8) + set.seed(39854) + xtest <- rtriangle(200, 0, 1, 0.5) mle1 <- triangle_mle(xtest) ll <- logLik(mle1) @@ -40,7 +44,8 @@ test_that("logLik works", { }) test_that("AIC works", { - xtest <- c(0.1, 0.25, 0.3, 0.4, 0.45, 0.6, 0.75, 0.8) + set.seed(39854) + xtest <- rtriangle(200, 0, 1, 0.5) mle1 <- triangle_mle(xtest) aic <- AIC(mle1) @@ -50,7 +55,8 @@ test_that("AIC works", { }) test_that("BIC works", { - xtest <- c(0.1, 0.25, 0.3, 0.4, 0.45, 0.6, 0.75, 0.8) + set.seed(39854) + xtest <- rtriangle(200, 0, 1, 0.5) mle1 <- triangle_mle(xtest) bic <- BIC(mle1) @@ -60,7 +66,8 @@ test_that("BIC works", { }) test_that("vcov works", { - xtest <- c(0.1, 0.25, 0.3, 0.4, 0.45, 0.6, 0.75, 0.8) + set.seed(39854) + xtest <- rtriangle(200, 0, 1, 0.5) mle1 <- triangle_mle(xtest) v <- vcov(mle1) @@ -69,7 +76,8 @@ test_that("vcov works", { }) test_that("profile works", { - xtest <- c(0.1, 0.25, 0.3, 0.4, 0.45, 0.6, 0.75, 0.8) + set.seed(39854) + xtest <- rtriangle(200, 0, 1, 0.5) mle1 <- triangle_mle(xtest) prof <- profile(mle1) @@ -87,20 +95,20 @@ test_that("profile works", { }) test_that("confint works", { - xtest <- c(0.1, 0.25, 0.3, 0.4, 0.45, 0.6, 0.75, 0.8) + set.seed(39854) + xtest <- rtriangle(200, 0, 1, 0.5) mle1 <- triangle_mle(xtest) cfi <- confint(mle1, level = 0.95) expect_equal(c(3,2), dim(cfi)) - expect_true(all(is.na(cfi[3,]))) expect_true(cfi[1,1] < 0 & 0 < cfi[1,2]) expect_true(cfi[2,1] < 1 & 1 < cfi[2,2]) - cfi <- confint(mle1, level = 0.6) + cfi <- confint(mle1, level = 0.9) expect_true(cfi[1,1] < 0 & 0 < cfi[1,2]) expect_true(cfi[2,1] < 1 & 1 < cfi[2,2]) - expect_true(cfi[3,1] < 0.3 & 0.3 < cfi[3,2]) + expect_true(cfi[3,1] < 0.5 & 0.5 < cfi[3,2]) })