Skip to content

Commit

Permalink
continuing to debug examples on rhub platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
bertcarnell committed Dec 12, 2022
1 parent 1af7ded commit d5ad16b
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 25 deletions.
4 changes: 3 additions & 1 deletion R/cdfe.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions R/mle-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

6 changes: 1 addition & 5 deletions docs/reference/mle-utils.html

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

4 changes: 3 additions & 1 deletion docs/reference/triangle_cdfe.html

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

2 changes: 1 addition & 1 deletion man/mle-utils.Rd

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

4 changes: 3 additions & 1 deletion man/triangle_cdfe.Rd

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

32 changes: 20 additions & 12 deletions tests/testthat/test-mle-utils.R
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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])
})

0 comments on commit d5ad16b

Please sign in to comment.