Skip to content

Commit

Permalink
Support ggeffects (#820)
Browse files Browse the repository at this point in the history
* Support ggeffects

* more methods

* fix

* fix

* version

* update suggests
  • Loading branch information
strengejacke authored Oct 14, 2023
1 parent 0d8b587 commit 6f53383
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 4 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: insight
Title: Easy Access to Model Information for Various Model Objects
Version: 0.19.6
Version: 0.19.6.1
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down Expand Up @@ -123,6 +123,7 @@ Suggests:
gee,
geepack,
geoR,
ggeffects,
GLMMadaptive,
glmmTMB,
gmnl,
Expand Down Expand Up @@ -206,3 +207,4 @@ Config/Needs/website:
rstudio/bslib,
r-lib/pkgdown,
easystats/easystatstemplate
Remotes: strengejacke/ggeffects
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ S3method(get_parameters,gam)
S3method(get_parameters,gamlss)
S3method(get_parameters,gamm)
S3method(get_parameters,gbm)
S3method(get_parameters,ggcomparisons)
S3method(get_parameters,glht)
S3method(get_parameters,glimML)
S3method(get_parameters,glmm)
Expand Down Expand Up @@ -816,6 +817,7 @@ S3method(get_statistic,gamm)
S3method(get_statistic,garch)
S3method(get_statistic,gee)
S3method(get_statistic,geeglm)
S3method(get_statistic,ggcomparisons)
S3method(get_statistic,glht)
S3method(get_statistic,glimML)
S3method(get_statistic,glmm)
Expand Down Expand Up @@ -1314,6 +1316,7 @@ S3method(model_info,gamlss)
S3method(model_info,gamm)
S3method(model_info,garch)
S3method(model_info,gbm)
S3method(model_info,ggcomparisons)
S3method(model_info,glht)
S3method(model_info,glimML)
S3method(model_info,glmm)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# insight 0.19.7

## General

* Support for objects of class `ggcomparisons` from `ggeffects::hypothesis_test()`.

# insight 0.19.6

## General
Expand Down
4 changes: 2 additions & 2 deletions R/find_statistic.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ find_statistic <- function(x, ...) {
"deltaMethod", "DirichletRegModel",
"ergm",
"feglm", "flexsurvreg",
"gee", "glimML", "glmm", "glmmadmb", "glmmFit", "glmmLasso", "glmmTMB",
"glmx", "gmnl",
"gee", "ggcomparisons", "glimML", "glmm", "glmmadmb", "glmmFit", "glmmLasso",
"glmmTMB", "glmx", "gmnl",
"hurdle",
"lavaan", "loggammacenslmrob", "logitmfx", "logitor", "logitr", "LORgee", "lrm",
"margins", "marginaleffects", "marginaleffects.summary", "metaplus", "mixor",
Expand Down
28 changes: 28 additions & 0 deletions R/get_parameters_others.R
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,31 @@ get_parameters.deltaMethod <- function(x, ...) {
row.names = NULL
)
}


#' @export
get_parameters.ggcomparisons <- function(x, merge_parameters = FALSE, ...) {
estimate_pos <- which(colnames(x) == attr(x, "estimate_name"))
params <- x[, seq_len(estimate_pos - 1), drop = FALSE]

if (isTRUE(merge_parameters) && ncol(params) > 1L) {
r <- apply(params, 1, function(i) paste0(colnames(params), " [", i, "]"))
out <- data.frame(
Parameter = unname(vapply(as.data.frame(r), toString, character(1))),
Estimate = x[[estimate_pos]],
stringsAsFactors = FALSE,
row.names = NULL
)
} else {
out <- data.frame(
params,
Estimate = x[[estimate_pos]],
stringsAsFactors = FALSE,
row.names = NULL
)
if (isTRUE(merge_parameters)) {
colnames(out)[1] <- "Parameter"
}
}
text_remove_backticks(out)
}
27 changes: 27 additions & 0 deletions R/get_statistic.R
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,33 @@ get_statistic.emm_list <- function(x, ci = 0.95, adjust = "none", ...) {
}


#' @export
get_statistic.ggcomparisons <- function(x, merge_parameters = FALSE, ...) {
estimate_pos <- which(colnames(x) == attr(x, "estimate_name"))
if (isTRUE(merge_parameters)) {
params <- get_parameters(x, merge_parameters = TRUE)["Parameter"]
} else {
params <- x[, seq_len(estimate_pos - 1), drop = FALSE]
}

stat <- .safe(x[[estimate_pos]] / attributes(x)$standard_error)
if (is.null(stat)) {
return(NULL)
}

out <- data.frame(
params,
Statistic = as.vector(stat),
stringsAsFactors = FALSE,
row.names = NULL
)

out <- text_remove_backticks(out)
attr(out, "statistic") <- find_statistic(x)
out
}


#' @export
get_statistic.robmixglm <- function(x, ...) {
cs <- stats::coef(summary(x))
Expand Down
2 changes: 1 addition & 1 deletion R/is_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ is_regression_model <- function(x) {
"glmaag", "glmbb", "glmboostLSS", "glmc", "glmdm", "glmdisc",
"glmerMod", "glmlep", "glmm", "glmmadmb", "glmmEP", "glmmFit",
"glmmfields", "glmmLasso", "glmmPQL", "glmmTMB", "glmnet", "glmrob",
"glmRob", "glmx", "gls", "gmnl", "gmm", "gnls", "gsm",
"glmRob", "glmx", "gls", "gmnl", "gmm", "gnls", "gsm", "ggcomparisons",

# h --------------------
"heavyLme", "HLfit", "htest", "hurdle", "hglm",
Expand Down
5 changes: 5 additions & 0 deletions R/model_info.R
Original file line number Diff line number Diff line change
Expand Up @@ -1366,3 +1366,8 @@ model_info.earth <- function(x, ...) {
model_info.deltaMethod <- function(x, ...) {
NULL
}

#' @export
model_info.ggcomparisons <- function(x, ...) {
NULL
}
118 changes: 118 additions & 0 deletions tests/testthat/test-ggeffects.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
skip_if_not_installed("ggeffects")

test_that("get_parameters, hypothesis_test", {
data(iris)
mgg <- lm(Sepal.Width ~ Sepal.Length * Species, data = iris)

out <- ggeffects::hypothesis_test(
ggeffects::ggpredict(mgg, c("Sepal.Length", "Species")),
test = NULL
)

param <- get_parameters(out)
expect_named(param, c("Sepal.Length", "Species", "Estimate"))
expect_identical(
param$Sepal.Length,
c("slope", "slope", "slope")
)
expect_identical(
param$Species,
structure(1:3, levels = c("setosa", "versicolor", "virginica"), class = "factor")
)
expect_equal(
param$Estimate,
c(0.79853, 0.31972, 0.23189),
tolerance = 1e-3
)

param <- get_parameters(out, merge_parameters = TRUE)
expect_named(param, c("Parameter", "Estimate"))
expect_identical(
param$Parameter,
c(
"Sepal.Length [slope], Species [setosa]", "Sepal.Length [slope], Species [versicolor]",
"Sepal.Length [slope], Species [virginica]"
)
)
expect_equal(
param$Estimate,
c(0.79853, 0.31972, 0.23189),
tolerance = 1e-3
)

out <- ggeffects::hypothesis_test(ggeffects::ggpredict(mgg, c("Sepal.Length", "Species")))

param <- get_parameters(out)
expect_named(param, c("Sepal.Length", "Species", "Estimate"))
expect_identical(
param$Sepal.Length,
c("slope", "slope", "slope")
)
expect_equal(
param$Estimate,
c(0.47881, 0.56664, 0.08783),
tolerance = 1e-3
)

param <- get_parameters(out, merge_parameters = TRUE)
expect_named(param, c("Parameter", "Estimate"))
})


test_that("get_statistic, hypothesis_test", {
data(iris)
mgg <- lm(Sepal.Width ~ Sepal.Length * Species, data = iris)

out <- ggeffects::hypothesis_test(
ggeffects::ggpredict(mgg, c("Sepal.Length", "Species")),
test = NULL
)

param <- get_statistic(out)
expect_named(param, c("Sepal.Length", "Species", "Statistic"))
expect_identical(
param$Sepal.Length,
c("slope", "slope", "slope")
)
expect_identical(
param$Species,
structure(1:3, levels = c("setosa", "versicolor", "virginica"), class = "factor")
)
expect_equal(
param$Statistic,
c(7.23551, 4.24211, 3.79015),
tolerance = 1e-3
)

param <- get_statistic(out, merge_parameters = TRUE)
expect_named(param, c("Parameter", "Statistic"))
expect_identical(
param$Parameter,
c(
"Sepal.Length [slope], Species [setosa]", "Sepal.Length [slope], Species [versicolor]",
"Sepal.Length [slope], Species [virginica]"
)
)
expect_equal(
param$Statistic,
c(7.23551, 4.24211, 3.79015),
tolerance = 1e-3
)

out <- ggeffects::hypothesis_test(ggeffects::ggpredict(mgg, c("Sepal.Length", "Species")))

param <- get_statistic(out)
expect_named(param, c("Sepal.Length", "Species", "Statistic"))
expect_identical(
param$Sepal.Length,
c("slope", "slope", "slope")
)
expect_equal(
param$Statistic,
c(3.58262, 4.48915, 0.90475),
tolerance = 1e-3
)

param <- get_statistic(out, merge_parameters = TRUE)
expect_named(param, c("Parameter", "Statistic"))
})

0 comments on commit 6f53383

Please sign in to comment.