R Under development (unstable) (2024-02-14 r85901 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # Tests for extracting coefficients and vcov matrix from regression fits > library(cpr) > require(lme4) Loading required package: lme4 Loading required package: Matrix > > ################################################################################ > # There is one method of interest, and it is non exported. There are several S3 > # methods to check. > stopifnot(!grepl("loglikelihood", ls("package:cpr"))) > cpr_namespace <- ls( getNamespace("cpr"), all.names = TRUE) > stopifnot(sum(cpr_namespace == "loglikelihood") == 1L) > stopifnot(sum(cpr_namespace == "loglikelihood.default") == 1L) > stopifnot(sum(cpr_namespace == "loglikelihood.geeglm") == 1L) > stopifnot(sum(grepl("^loglikelihood", cpr_namespace)) == 3L) > > ################################################################################ > e <- new.env() > with(e, { + fit <- lm(mpg ~ wt, data = mtcars) + stopifnot("recover loglikelihood from lm" = + isTRUE(all.equal(cpr:::loglikelihood(fit), as.numeric(stats::logLik(fit))))) + }) > > ################################################################################ > e <- new.env() > with(e, { + fit <- glm(I(mpg < 20) ~ wt, data = mtcars, family = quasibinomial()) + stopifnot("recover quasiloglikelihood from glm" = + isTRUE(all.equal(cpr:::loglikelihood(fit), as.numeric(stats::logLik(fit))))) + }) > > ################################################################################ > e <- new.env() > with(e, { + require(lme4) + fit <- lmer(mpg ~ wt + (1|cyl), data = mtcars) + stopifnot("recover loglikelihood from lme4" = + isTRUE(all.equal(cpr:::loglikelihood(fit), as.numeric(stats::logLik(fit))))) + }) > > ################################################################################ > e <- new.env() > with(e, { + require(geepack) + fit <- geeglm(mpg ~ wt, id = cyl, data = mtcars, family = gaussian()) + y <- fit$y + mu <- fit$fitted.values + stopifnot("recover loglikelihood from gaussian geeglm" = + isTRUE(all.equal(cpr:::loglikelihood(fit), -sum((y - mu)^2 / 2)))) + }) Loading required package: geepack > > ################################################################################ > e <- new.env() > with(e, { + require(geepack) + fit <- geeglm(I(mpg < 20) ~ wt, id = cyl, data = mtcars, family = binomial()) + y <- fit$y + mu <- fit$fitted.values + stopifnot("recover loglikelihood from binomial geeglm" = + isTRUE(all.equal(cpr:::loglikelihood(fit), + sum(y * log(mu/(1 - mu)) + log(1 - mu))))) + }) > > ################################################################################ > e <- new.env() > with(e, { + require(geepack) + fit <- geeglm(as.integer(mpg) ~ wt, id = cyl, data = mtcars, family = poisson()) + y <- fit$y + mu <- fit$fitted.values + stopifnot("recover loglikelihood from poisson geeglm" = + isTRUE(all.equal(cpr:::loglikelihood(fit), + sum(y * log(mu) - mu)))) + }) > > ################################################################################ > e <- new.env() > with(e, { + require(geepack) + fit <- geeglm(mpg ~ wt, id = cyl, data = mtcars, family = Gamma()) + y <- fit$y + mu <- fit$fitted.values + stopifnot("recover loglikelihood from Gamma geeglm" = + isTRUE(all.equal(cpr:::loglikelihood(fit), -sum(y/mu + log(mu))))) + }) > > ################################################################################ > # End of File # > ################################################################################ > > proc.time() user system elapsed 2.65 0.28 2.93