Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for package WeightIt #920

Merged
merged 7 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 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.20.3.3
Version: 0.20.3.4
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
11 changes: 11 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ S3method(find_parameters,mle2)
S3method(find_parameters,mlm)
S3method(find_parameters,model_fit)
S3method(find_parameters,multinom)
S3method(find_parameters,multinom_weightit)
S3method(find_parameters,mvord)
S3method(find_parameters,negbinirr)
S3method(find_parameters,negbinmfx)
Expand Down Expand Up @@ -689,11 +690,13 @@ S3method(get_parameters,mle2)
S3method(get_parameters,mlm)
S3method(get_parameters,model_fit)
S3method(get_parameters,multinom)
S3method(get_parameters,multinom_weightit)
S3method(get_parameters,mvord)
S3method(get_parameters,negbinirr)
S3method(get_parameters,negbinmfx)
S3method(get_parameters,nestedLogit)
S3method(get_parameters,nlmerMod)
S3method(get_parameters,ordinal_weightit)
S3method(get_parameters,orm)
S3method(get_parameters,pgmm)
S3method(get_parameters,poissonirr)
Expand Down Expand Up @@ -901,13 +904,15 @@ S3method(get_statistic,mmrm_fit)
S3method(get_statistic,mmrm_tmb)
S3method(get_statistic,model_fit)
S3method(get_statistic,multinom)
S3method(get_statistic,multinom_weightit)
S3method(get_statistic,mvord)
S3method(get_statistic,negbin)
S3method(get_statistic,negbinirr)
S3method(get_statistic,negbinmfx)
S3method(get_statistic,nestedLogit)
S3method(get_statistic,nlrq)
S3method(get_statistic,ols)
S3method(get_statistic,ordinal_weightit)
S3method(get_statistic,orm)
S3method(get_statistic,pgmm)
S3method(get_statistic,plm)
Expand Down Expand Up @@ -1142,10 +1147,12 @@ S3method(link_function,mmrm_fit)
S3method(link_function,mmrm_tmb)
S3method(link_function,model_fit)
S3method(link_function,multinom)
S3method(link_function,multinom_weightit)
S3method(link_function,mvord)
S3method(link_function,negbinirr)
S3method(link_function,negbinmfx)
S3method(link_function,nestedLogit)
S3method(link_function,ordinal_weightit)
S3method(link_function,orm)
S3method(link_function,phylolm)
S3method(link_function,plm)
Expand Down Expand Up @@ -1268,10 +1275,12 @@ S3method(link_inverse,mmrm_fit)
S3method(link_inverse,mmrm_tmb)
S3method(link_inverse,model_fit)
S3method(link_inverse,multinom)
S3method(link_inverse,multinom_weightit)
S3method(link_inverse,mvord)
S3method(link_inverse,negbinirr)
S3method(link_inverse,negbinmfx)
S3method(link_inverse,nestedLogit)
S3method(link_inverse,ordinal_weightit)
S3method(link_inverse,orm)
S3method(link_inverse,phyloglm)
S3method(link_inverse,phylolm)
Expand Down Expand Up @@ -1418,12 +1427,14 @@ S3method(model_info,mmrm_fit)
S3method(model_info,mmrm_tmb)
S3method(model_info,model_fit)
S3method(model_info,multinom)
S3method(model_info,multinom_weightit)
S3method(model_info,mvord)
S3method(model_info,negbinirr)
S3method(model_info,negbinmfx)
S3method(model_info,nestedLogit)
S3method(model_info,nlrq)
S3method(model_info,nls)
S3method(model_info,ordinal_weightit)
S3method(model_info,orm)
S3method(model_info,phyloglm)
S3method(model_info,phylolm)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# insight 0.20.4

## New supported models

* Support for models of classes `glm_weighit`, `multinom_weightit` and
`ordinal_weightit` (package *WeightIt*).

## Changes

* `null_model()` and `formula_ok()` now warn when indexed data frames, such as
Expand Down
13 changes: 13 additions & 0 deletions R/find_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@
#' @export
find_parameters.brmultinom <- find_parameters.multinom

#' @export
find_parameters.multinom_weightit <- function(x, flatten = FALSE, ...) {
params <- stats::coef(x)
resp <- gsub("(.*)~(.*)", "\\1", names(params))
pars <- gsub("(.*)~(.*)", "\\2", names(params))[resp == resp[1]]

if (flatten) {
unique(unlist(pars, use.names = FALSE))
} else {
pars
}
}


# SEM models ------------------------------------------------------

Expand Down Expand Up @@ -815,7 +828,7 @@

# helper ----------------------------

.filter_parameters <- function(l, effects, component = "all", flatten, recursive = TRUE) {

Check warning on line 831 in R/find_parameters.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/find_parameters.R,line=831,col=63,[function_argument_linter] Arguments without defaults should come before arguments with defaults.
if (isTRUE(recursive)) {
# recursively remove back-ticks from all list-elements parameters
l <- rapply(l, text_remove_backticks, how = "list")
Expand Down
4 changes: 2 additions & 2 deletions R/find_statistic.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#' m <- lm(mpg ~ wt + cyl + vs, data = mtcars)
#' find_statistic(m)
#' @export
find_statistic <- function(x, ...) {

Check warning on line 21 in R/find_statistic.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/find_statistic.R,line=21,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 53 to at most 40.
# model object check --------------------------------------------------------

# check if the object is a model object; if not, quit early
Expand Down Expand Up @@ -127,9 +127,9 @@
"lavaan", "loggammacenslmrob", "logitmfx", "logitor", "logitr", "LORgee", "lrm",
"margins", "marginaleffects", "marginaleffects.summary", "metaplus", "mixor",
"MixMod", "mjoint", "mle", "mle2", "mlogit", "mblogit", "mclogit", "mmclogit",
"multinom", "mvmeta", "mvord",
"multinom", "mvmeta", "mvord", "multinom_weightit",
"negbin", "negbinmfx", "negbinirr", "nlreg", "nestedLogit",
"objectiveML", "orm",
"objectiveML", "orm", "ordinal_weightit",
"poissonmfx", "poissonirr", "psm", "probitmfx", "pgmm", "phyloglm",
"qr", "QRNLMM", "QRLMM",
"Rchoice", "riskRegression", "robmixglm", "rma", "rma.mv", "rma.uni", "rrvglm",
Expand Down
28 changes: 26 additions & 2 deletions R/get_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ get_parameters.default <- function(x, verbose = TRUE, ...) {
sprintf("Parameters can't be retrieved for objects of class `%s`.", class(x)[1])
)
}
return(NULL)
NULL
}
)
}
Expand Down Expand Up @@ -128,6 +128,14 @@ get_parameters.model_fit <- function(x, ...) {
}


#' @export
get_parameters.ordinal_weightit <- function(x, ...) {
out <- get_parameters.default(x, ...)
out$Component <- "conditional"
out
}


#' @export
get_parameters.bfsl <- function(x, ...) {
cf <- stats::coef(x)
Expand Down Expand Up @@ -426,6 +434,22 @@ get_parameters.multinom <- function(x, ...) {
}


#' @export
get_parameters.multinom_weightit <- function(x, ...) {
params <- stats::coef(x)

out <- data.frame(
Parameter = gsub("(.*)~(.*)", "\\2", names(params)),
Estimate = unname(params),
Response = gsub("(.*)~(.*)", "\\1", names(params)),
stringsAsFactors = FALSE,
row.names = NULL
)

text_remove_backticks(out)
}


#' @export
get_parameters.brmultinom <- get_parameters.multinom

Expand Down Expand Up @@ -749,7 +773,7 @@ get_parameters.manova <- function(x, ...) {
out <- out[c("Parameter", "Estimate", "Response")]
rownames(out) <- NULL

pattern <- paste0("(", paste0(paste0(".", unique(out$Response)), collapse = "|"), ")$")
pattern <- paste0("(", paste(paste0(".", unique(out$Response)), collapse = "|"), ")$")
out$Parameter <- gsub(pattern, "", out$Parameter)

text_remove_backticks(out)
Expand Down
28 changes: 28 additions & 0 deletions R/get_statistic.R
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,14 @@ get_statistic.clm2 <- function(x, component = "all", ...) {
get_statistic.clmm2 <- get_statistic.clm2


#' @export
get_statistic.ordinal_weightit <- function(x, ...) {
out <- get_statistic.default(x, ...)
out$Component <- "conditional"
out
}


#' @export
get_statistic.mvord <- function(x, component = "all", ...) {
component <- match.arg(
Expand Down Expand Up @@ -924,6 +932,26 @@ get_statistic.multinom <- function(x, ...) {
#' @export
get_statistic.brmultinom <- get_statistic.multinom

#' @export
get_statistic.multinom_weightit <- function(x, ...) {
parms <- get_parameters(x)
cs <- suppressWarnings(stats::coef(summary(x)))

out <- data.frame(
Parameter = parms$Parameter,
Statistic = as.vector(cs[, 3]),
stringsAsFactors = FALSE,
row.names = NULL
)

if ("Response" %in% colnames(parms)) {
out$Response <- parms$Response
}

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


#' @export
get_statistic.bracl <- function(x, ...) {
Expand Down
5 changes: 3 additions & 2 deletions R/is_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ is_regression_model <- function(x) {
"glmerMod", "glmlep", "glmm", "glmmadmb", "glmmEP", "glmmFit",
"glmmfields", "glmmLasso", "glmmPQL", "glmmTMB", "glmnet", "glmrob",
"glmRob", "glmx", "gls", "gmnl", "gmm", "gnls", "gsm", "ggcomparisons",
"glm_weighit",

# h --------------------
"heavyLme", "HLfit", "htest", "hurdle", "hglm",
Expand Down Expand Up @@ -118,14 +119,14 @@ is_regression_model <- function(x) {
"mle", "mle2", "mlergm", "mlm", "mlma", "mlogit", "model_fit",
"multinom", "mvmeta", "mvord", "mvr", "marginaleffects",
"marginaleffects.summary", "mblogit", "mclogit", "mmrm", "mmrm_fit",
"mmrm_tmb",
"mmrm_tmb", "multinom_weightit",

# n --------------------
"negbin", "negbinmfx", "negbinirr", "nlreg", "nlrq", "nls",
"nparLD", "nestedLogit",

# o --------------------
"objectiveML", "ols", "osrt", "orcutt",
"objectiveML", "ols", "osrt", "orcutt", "ordinal_weightit",

# p --------------------
"pairwise.htest", "pb1", "pb2", "pgmm", "plm", "plmm", "PMCMR",
Expand Down
6 changes: 3 additions & 3 deletions R/is_model_supported.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ supported_models <- function() {
"gam", "Gam", "gamlss", "gamm", "gamm4", "garch", "gbm", "gee", "geeglm",
"glht", "glimML", "glmm", "glm", "Glm", "glmmadmb", "glmmPQL", "glmmTMB",
"glmrob", "glmRob", "glmx", "gls", "gmnl", "glmgee", "ggcomparisons",
"glmerMod",
"glmerMod", "glm_weighit",

# h ----------------------------
"HLfit", "htest", "hurdle", "hglm",
Expand All @@ -90,13 +90,13 @@ supported_models <- function() {
"metaplus", "merMod", "merModList", "mipo", "mira", "mixed", "mixor", "MixMod",
"mhurdle", "mjoint", "mle", "mle2", "mlm", "mblogit", "mclogit", "mcmc.list",
"meta_bma", "meta_fixed", "meta_random", "mlogit", "mmlogit", "model_fit",
"multinom", "mvord", "mmclogit", "mmrm", "mmrm_fit", "mmrm_tmb",
"multinom", "mvord", "mmclogit", "mmrm", "mmrm_fit", "mmrm_tmb", "multinom_weightit",

# n ----------------------------
"negbinmfx", "negbinirr", "nestedLogit",

# o ----------------------------
"ols", "onesampb", "orm",
"ols", "onesampb", "orm", "ordinal_weighit",

# p ----------------------------
"PMCMR", "poissonmfx", "poissonirr", "pgmm", "plm", "polr", "psm",
Expand Down
8 changes: 8 additions & 0 deletions R/link_function.R
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,14 @@ link_function.riskRegression <- link_function.multinom
#' @export
link_function.comprisk <- link_function.multinom

#' @export
link_function.multinom_weightit <- function(x, ...) {
stats::make.link(link = x$family$link)$linkfun
}

#' @export
link_function.ordinal_weightit <- link_function.multinom_weightit


# Phylo glm ------------------------

Expand Down
8 changes: 8 additions & 0 deletions R/link_inverse.R
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ link_inverse.flic <- link_inverse.gmnl
#' @export
link_inverse.multinom <- link_inverse.gmnl

#' @export
link_inverse.multinom_weightit <- function(x, ...) {
stats::make.link(link = x$family$link)$linkinv
}

#' @export
link_inverse.ordinal_weightit <- link_inverse.multinom_weightit


# Probit link ------------------------

Expand Down
15 changes: 15 additions & 0 deletions R/model_info.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#' * `is_hurdle`: model has zero-inflation component and is a hurdle-model (truncated family distribution)
#' * `is_dispersion`: model has dispersion component (not only dispersion _parameter_)
#' * `is_mixed`: model is a mixed effects model (with random effects)
#' * `is_multivariate`: model is a multivariate response model (currently only works for _brmsfit_ and _vglm/vgam_ objects)

Check warning on line 42 in R/model_info.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/model_info.R,line=42,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 123 characters.
#' * `is_trial`: model response contains additional information about the trials
#' * `is_bayesian`: model is a Bayesian model
#' * `is_gam`: model is a generalized additive model
Expand Down Expand Up @@ -415,6 +415,21 @@
#' @export
model_info.mixor <- model_info.clm

#' @export
model_info.ordinal_weightit <- function(x, verbose = TRUE, ...) {
faminfo <- stats::binomial(link = .get_ordinal_link(x$family))
.make_family(
x = x,
fitfam = faminfo$family,
logit.link = faminfo$link == "logit",
link.fun = faminfo$link,
verbose = verbose,
...
)
}

#' @export
model_info.multinom_weightit <- model_info.ordinal_weightit

#' @export
model_info.mvord <- function(x, verbose = verbose, ...) {
Expand Down
4 changes: 2 additions & 2 deletions R/utils_model_info.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.make_family <- function(x,

Check warning on line 1 in R/utils_model_info.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/utils_model_info.R,line=1,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 255 to at most 40.
fitfam = "gaussian",
zero.inf = FALSE,
hurdle = FALSE,
Expand Down Expand Up @@ -144,14 +144,14 @@
# ordinal family --------

is.ordinal <-
inherits(x, c("svyolr", "polr", "serp", "clm", "clm2", "clmm", "mixor", "LORgee", "mvord")) |
inherits(x, c("svyolr", "polr", "serp", "clm", "clm2", "clmm", "mixor", "LORgee", "mvord", "ordinal_weightit")) |
fitfam %in% c("cumulative", "ordinal")


# multinomial family --------

is.multinomial <-
inherits(x, c("gmnl", "mclogit", "mblogit", "mmclogit", "mlogit", "DirichletRegModel", "multinom", "brmultinom")) |
inherits(x, c("gmnl", "mclogit", "mblogit", "mmclogit", "mlogit", "DirichletRegModel", "multinom", "brmultinom", "multinom_weightit")) |

Check warning on line 154 in R/utils_model_info.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/utils_model_info.R,line=154,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 140 characters.
fitfam %in% c("cratio", "sratio", "acat", "multinom", "multinomial", "multinomial2", "dirichlet")


Expand Down Expand Up @@ -345,7 +345,7 @@
dirichlet_fam || is.ordinal || zero.inf || is.censored || is.survival || is_binomtest ||
is.categorical || hurdle || is.multinomial || is_chi2test || is_proptest || is_xtab) {
linear_model <- FALSE
} else if (!(fitfam %in% c("Student's-t", "t Family", "gaussian", "Gaussian", "lognormal")) && !grepl("(\\st)$", fitfam)) {

Check warning on line 348 in R/utils_model_info.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/utils_model_info.R,line=348,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 125 characters.
linear_model <- FALSE
}
if (!linear_model && is.survival && fitfam == "gaussian") {
Expand Down
Loading
Loading