Skip to content

Commit

Permalink
Support glmtoolbox::glmgee() (#1207)
Browse files Browse the repository at this point in the history
* support glmtoolbox::glmgee()

* fixup test

* test fixup

* supported models
  • Loading branch information
vincentarelbundock authored Aug 31, 2024
1 parent 25b3b4f commit eb9a3c4
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 18 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: marginaleffects
Title: Predictions, Comparisons, Slopes, Marginal Means, and Hypothesis Tests
Version: 0.21.0.21
Version: 0.21.0.22
Authors@R:
c(person(given = "Vincent",
family = "Arel-Bundock",
Expand Down Expand Up @@ -92,6 +92,7 @@ Suggests:
ggplot2,
ggrepel,
glmmTMB,
glmtoolbox,
glmx,
haven,
here,
Expand Down Expand Up @@ -237,6 +238,7 @@ Collate:
'methods_flexsurv.R'
'methods_gamlss.R'
'methods_glmmTMB.R'
'methods_glmtoolbox.R'
'methods_glmx.R'
'methods_lme4.R'
'methods_mclogit.R'
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ S3method(get_coef,data.frame)
S3method(get_coef,default)
S3method(get_coef,gam)
S3method(get_coef,gamlss)
S3method(get_coef,glmgee)
S3method(get_coef,glmmTMB)
S3method(get_coef,lmerMod)
S3method(get_coef,lmerModLmerTest)
Expand Down Expand Up @@ -62,6 +63,7 @@ S3method(get_predict,flexsurvreg)
S3method(get_predict,gamlss)
S3method(get_predict,glimML)
S3method(get_predict,glm)
S3method(get_predict,glmgee)
S3method(get_predict,glmmPQL)
S3method(get_predict,glmmTMB)
S3method(get_predict,gsm)
Expand Down Expand Up @@ -143,6 +145,7 @@ S3method(set_coef,flexsurvreg)
S3method(set_coef,gamlss)
S3method(set_coef,glimML)
S3method(set_coef,glm)
S3method(set_coef,glmgee)
S3method(set_coef,glmmPQL)
S3method(set_coef,glmmTMB)
S3method(set_coef,glmx)
Expand Down
12 changes: 8 additions & 4 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ Breaking changes:
* `type="invlink(link)"` is no longer default in `avg_predictions()` or when calling `predictions()` with the `by` argument. It is still default in `predictions()` without the `by` argument. The backtransform strategy is still available with by setting `type="invlink(link)"` explicitly.
* The `type` argument in `plot_comparisons()` now defaults to `NULL`, which is now consistent with `comparisons()` and `avg_comparisons()`. Before, the default was `type="response"`. Thanks to @giakhang1906 for report #1202.

New:
New models supported:

* `stpm2`, `pstpm2`, `gsm`, and `aft` models from `rstpm2`. Thanks to @aghaynes and @mclements.
* `glm_weightit`, `coxph_weightit`, `multinom_weightit`, and `ordinal_weightit` models from `Weightit`. Thanks to @ngreifer.
* `glmmgee` from the `glmtoolbox` package. Thanks to @adrianolszewski for the request and @lhvanegasp for help with implementation.

New feautres:

* `hypotheses(joint=TRUE)`: do not call `stats::nobs()` unless necessary.
* Added support for `stpm2`, `pstpm2`, `gsm`, and `aft` models from `rstpm2`. Thanks to @aghaynes and @mclements.
* Added support for `glm_weightit`, `coxph_weightit`, `multinom_weightit`, and `ordinal_weightit` models from `Weightit`. Thanks to @ngreifer.
* Parallel computation with `future` is more efficient by chunking tasks to avoid passing large objects to every worker for every future. Issue #1158.
* All columns of `newdata` are passed to the `hypothesis` function when `newdata` is supplied explicitly. Thanks to @gravesti for report #1175.
* `hypotheses(joint=TRUE)`: do not call `stats::nobs()` unless necessary.
* `hypotheses()` supports formulas in the `hypothesis` argument: `hypotheses(model, hypothesis = ratio ~ reference)`
* Global option: `options("marginaleffects_print_omit" = "s.value")`
* Round significant digits for labels in `plot_predictions(mod, condition = list(x = "fivenum"))`
Expand Down
29 changes: 29 additions & 0 deletions R/methods_glmtoolbox.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#' @include get_coef.R
#' @rdname get_coef
#' @export
get_coef.glmgee <- function(model, ...) {
b <- model$coefficients
b <- setNames(as.vector(b), row.names(b))
return(b)
}

#' @include set_coef.R
#' @rdname set_coef
#' @export
set_coef.glmgee <- function(model, coefs, ...) {
out <- model
idx <- match(row.names(out$coefficients), names(coefs))
out$coefficients[, 1] <- coefs[idx]
return(out)
}

#' @include get_predict.R
#' @rdname get_predict
#' @export
get_predict.glmgee <- function(model, newdata, ...) {
Yhat <- predict(model, newdata = newdata, type = "response")
out <- data.frame(
rowid = seq_len(nrow(Yhat)),
estimate = as.vector(Yhat))
return(out)
}
1 change: 1 addition & 0 deletions R/sanity_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ sanity_model_supported_class <- function(model) {
"glmerMod",
"glmrob",
"glmmTMB",
"glmgee",
c("glmmPQL", "lme"),
"glimML",
"glmx",
Expand Down
1 change: 1 addition & 0 deletions R/type_dictionary.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ glm,response
glm,link
glmerMod,response
glmerMod,link
glmgee,response
glmrob,response
glmrob,link
glmmTMB,response
Expand Down
1 change: 1 addition & 0 deletions data-raw/supported_models.csv
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ gam,gam,TRUE,TRUE,,,U,U,TRUE,TRUE
gamlss,gamlss,TRUE,TRUE,,,U,U,TRUE,TRUE
geepack,geeglm,TRUE,TRUE,,,U,U,TRUE,TRUE
glmmTMB,glmmTMB,TRUE,FALSE,,,U,U,TRUE,TRUE
glmtoolbox,glmgee,TRUE,TRUE,,, ,,,
glmx,glmx,TRUE,TRUE,,,TRUE,U,U,U
ivreg,ivreg,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,U,U
lme4,glmer,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE
Expand Down
14 changes: 14 additions & 0 deletions inst/tinytest/test-pkg-glmtoolbox.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
source("helpers.R")
using("marginaleffects")
requiet("glmtoolbox")

data(spruces)
mod <- size ~ poly(days,4) + treat
fit <- glmgee(mod, id=tree, family=Gamma(log), corstr="AR-M-dependent(1)", data=spruces)

s <- avg_slopes(fit)
expect_inherits(s, "slopes")
p <- avg_predictions(fit, by = "treat")
expect_inherits(p, "predictions")
k <- avg_comparisons(fit)
expect_inherits(k, "comparisons")
10 changes: 7 additions & 3 deletions man/get_coef.Rd

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

15 changes: 9 additions & 6 deletions man/get_predict.Rd

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

12 changes: 8 additions & 4 deletions man/set_coef.Rd

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

0 comments on commit eb9a3c4

Please sign in to comment.