Skip to content

Commit

Permalink
Support for "serp" (#850)
Browse files Browse the repository at this point in the history
* Support for "serp"

* suggest

* Update model_info.R

* Update NAMESPACE

* ...

* lintr

* add tests

* readme

* typo

* lintr

* styler
  • Loading branch information
strengejacke authored Feb 23, 2024
1 parent 43ccafd commit 0d051b0
Show file tree
Hide file tree
Showing 16 changed files with 277 additions and 107 deletions.
3 changes: 2 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.8.3
Version: 0.19.8.4
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down Expand Up @@ -186,6 +186,7 @@ Suggests:
rstantools (>= 2.1.0),
rstudioapi,
sandwich,
serp,
speedglm,
splines,
statmod,
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ S3method(get_modelmatrix,lm_robust)
S3method(get_modelmatrix,lme)
S3method(get_modelmatrix,merMod)
S3method(get_modelmatrix,rlm)
S3method(get_modelmatrix,serp)
S3method(get_modelmatrix,svyglm)
S3method(get_parameters,BBmm)
S3method(get_parameters,BBreg)
Expand Down Expand Up @@ -1124,6 +1125,7 @@ S3method(link_function,robmixglm)
S3method(link_function,rq)
S3method(link_function,rqs)
S3method(link_function,rqss)
S3method(link_function,serp)
S3method(link_function,speedglm)
S3method(link_function,speedlm)
S3method(link_function,stanmvreg)
Expand Down Expand Up @@ -1248,6 +1250,7 @@ S3method(link_inverse,robmixglm)
S3method(link_inverse,rq)
S3method(link_inverse,rqs)
S3method(link_inverse,rqss)
S3method(link_inverse,serp)
S3method(link_inverse,speedglm)
S3method(link_inverse,speedlm)
S3method(link_inverse,stanmvreg)
Expand Down Expand Up @@ -1397,6 +1400,7 @@ S3method(model_info,robmixglm)
S3method(model_info,rq)
S3method(model_info,rqs)
S3method(model_info,rqss)
S3method(model_info,serp)
S3method(model_info,speedglm)
S3method(model_info,speedlm)
S3method(model_info,stanmvreg)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# insight 0.19.9

* Support for models of class `serp` (package *serp*).

# insight 0.19.8

## General
Expand Down
20 changes: 9 additions & 11 deletions R/find_statistic.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ find_statistic <- function(x, ...) {
t = "t-statistic",
Z = "z-statistic",
`Quade F` = ,
`F` = "F-statistic",
`F` = "F-statistic", # nolint
`Bartlett's K-squared` = ,
`Fligner-Killeen:med chi-squared` = ,
`Friedman chi-squared` = ,
Expand Down Expand Up @@ -132,7 +132,7 @@ find_statistic <- function(x, ...) {
"poissonmfx", "poissonirr", "psm", "probitmfx", "pgmm", "phyloglm",
"qr", "QRNLMM", "QRLMM",
"Rchoice", "riskRegression", "robmixglm", "rma", "rma.mv", "rma.uni", "rrvglm",
"Sarlm", "sem", "SemiParBIV", "slm", "slopes", "survreg", "svy_vglm",
"Sarlm", "sem", "SemiParBIV", "serp", "slm", "slopes", "survreg", "svy_vglm",
"test_mediation", "tobit",
"vglm",
"wbgee",
Expand Down Expand Up @@ -229,13 +229,11 @@ find_statistic <- function(x, ...) {
# family, so this exception needs to be caught before checking for g.mods

tryCatch(
{
suppressWarnings(
if (!is_multivariate(x) && .is_tweedie(x, m_info)) {
return("t-statistic")
}
)
},
suppressWarnings(
if (!is_multivariate(x) && .is_tweedie(x, m_info)) {
return("t-statistic")
}
),
error = function(e) {}
)

Expand Down Expand Up @@ -267,8 +265,8 @@ find_statistic <- function(x, ...) {
if (model_class %in% c("emmGrid", "emm_list")) {
stat <- tryCatch(
{
df <- get_df(x)
if (all(is.na(df)) || all(is.infinite(df))) {
dof <- get_df(x)
if (all(is.na(dof)) || all(is.infinite(dof))) {
"z-statistic"
} else {
"t-statistic"
Expand Down
9 changes: 4 additions & 5 deletions R/get_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,15 @@ get_df.default <- function(x, type = "residual", verbose = TRUE, ...) {
}


# Wald normal approximation - always Inf -----
if (type == "normal") {
# Wald normal approximation - always Inf -----
return(Inf)

# residual/analytical df, falls back to Inf if we have no residual df method -----
} else if (type == "residual") {
# residual/analytical df, falls back to Inf if we have no residual df method -----
dof <- .get_residual_df(x, verbose)

# Wald df - always Inf for z-statistic, 1 for Chi2-statistic, else residual df -----
} else if (type == "wald") {
# Wald df - always Inf for z-statistic, 1 for Chi2-statistic, else residual df -----

# z-statistic always Inf, *unless* we have residual df (which we have for some models)
if (identical(statistic, "z-statistic")) {
return(Inf)
Expand Down
21 changes: 12 additions & 9 deletions R/get_df_residual.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,10 @@
.degrees_of_freedom_residual.lqmm <- function(x, verbose = TRUE, ...) {
cs <- summary(x)
tryCatch(
{
if (!is.null(cs$rdf)) {
cs$rdf
} else {
attr(cs$B, "R") - 1
}
if (is.null(cs$rdf)) {
attr(cs$B, "R") - 1
} else {
cs$rdf
},
error = function(e) {
NULL
Expand Down Expand Up @@ -136,6 +134,11 @@
x$df
}

#' @keywords internal
.degrees_of_freedom_residual.serp <- function(x, verbose = TRUE, ...) {
x$rdf
}

#' @export
.degrees_of_freedom_residual.BBmm <- .degrees_of_freedom_residual.glht

Expand Down Expand Up @@ -207,7 +210,7 @@

#' @keywords internal
.degrees_of_freedom_residual.systemfit <- function(x, verbose = TRUE, ...) {
df <- NULL
dof <- NULL
s <- summary(x)$eq
params <- find_parameters(x)
f <- find_formula(x)
Expand All @@ -216,8 +219,8 @@
for (i in seq_along(system_names)) {
dfs <- rep(s[[i]]$df[2], length(params[[i]]))
df_names <- rep(names(params[i]), length(params[[i]]))
df <- c(df, stats::setNames(dfs, df_names))
dof <- c(dof, stats::setNames(dfs, df_names))
}

df
dof
}
24 changes: 18 additions & 6 deletions R/get_modelmatrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ get_modelmatrix.bracl <- function(x, ...) {
mm
}

#' @export
get_modelmatrix.serp <- function(x, ...) {
dots <- list(...)
if ("data" %in% names(dots)) {
mm <- stats::model.matrix(object = x$Terms, data = dots$data, ...)
} else {
mm <- stats::model.matrix(object = x$Terms, data = get_data(x), ...)
}

mm
}

#' @export
get_modelmatrix.iv_robust <- function(x, ...) {
dots <- list(...)
Expand Down Expand Up @@ -118,7 +130,7 @@ get_modelmatrix.clmm <- function(x, ...) {
get_modelmatrix.svyglm <- function(x, ...) {
dots <- list(...)
if ("data" %in% names(dots)) {
data <- tryCatch(
model_data <- tryCatch(
{
d <- as.data.frame(dots$data)
response_name <- find_response(x)
Expand All @@ -135,7 +147,7 @@ get_modelmatrix.svyglm <- function(x, ...) {
}
)
model_terms <- stats::terms(x)
mm <- stats::model.matrix(model_terms, data = data)
mm <- stats::model.matrix(model_terms, data = model_data)
} else {
mm <- stats::model.matrix(object = x, ...)
}
Expand Down Expand Up @@ -171,7 +183,7 @@ get_modelmatrix.rlm <- function(x, ...) {
data = mf,
contrasts.arg = x$contrasts
)
return(mm)
mm
}


Expand All @@ -191,7 +203,7 @@ get_modelmatrix.betareg <- function(x, ...) {
))
mm <- stats::model.matrix(stats::delete.response(x$terms$mean), mf)
}
return(mm)
mm
}


Expand Down Expand Up @@ -220,7 +232,7 @@ get_modelmatrix.BFBayesFactor <- function(x, ...) {


.data_in_dots <- function(..., object = NULL, default_data = NULL) {
dot.arguments <- lapply(match.call(expand.dots = FALSE)$`...`, function(x) x)
dot.arguments <- lapply(match.call(expand.dots = FALSE)[["..."]], function(x) x)
data_arg <- if ("data" %in% names(dot.arguments)) {
eval(dot.arguments[["data"]])
} else {
Expand Down Expand Up @@ -259,7 +271,7 @@ get_modelmatrix.BFBayesFactor <- function(x, ...) {
out <- rbind(pad, data)
row.names(out) <- NULL
attr(out, "pad") <- nrow(pad)
return(out)
out
}


Expand Down
2 changes: 1 addition & 1 deletion R/is_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ is_regression_model <- function(x) {
"rma.uni", "rms", "robmixglm", "robtab", "rq", "rqs", "rqss",

# s --------------------
"Sarlm", "scam", "selection", "sem", "SemiParBIV", "slm", "speedlm",
"Sarlm", "scam", "selection", "sem", "SemiParBIV", "serp", "slm", "speedlm",
"speedglm", "splmm", "spml", "stanmvreg", "stanreg", "summary.lm",
"survfit", "survreg", "survPresmooth", "svychisq", "svyglm", "svy_vglm",
"svyolr", "svytable", "systemfit",
Expand Down
4 changes: 2 additions & 2 deletions R/is_model_supported.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ supported_models <- function() {
"robtab", "RM", "rma", "rma.uni", "robmixglm", "rq", "rqs", "rqss", "rvar",

# s ----------------------------
"Sarlm", "scam", "selection", "sem", "semLm", "semLme", "SemiParBIV", "slm",
"speedlm", "speedglm", "stanfit", "stanmvreg", "stanreg", "summary.lm",
"Sarlm", "scam", "selection", "sem", "semLm", "semLme", "SemiParBIV", "serp",
"slm", "speedlm", "speedglm", "stanfit", "stanmvreg", "stanreg", "summary.lm",
"survfit", "survreg", "svy_vglm", "svychisq", "svyglm", "svyolr",

# t ----------------------------
Expand Down
18 changes: 9 additions & 9 deletions R/link_function.R
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ link_function.clm2 <- link_function.clm
#' @export
link_function.clmm <- link_function.clm

#' @export
link_function.serp <- link_function.clm

#' @export
link_function.mixor <- link_function.clm

Expand Down Expand Up @@ -760,16 +763,13 @@ link_function.brmsfit <- function(x, ...) {
# do we have custom families?
if (!is.null(fam$family) && (is.character(fam$family) && fam$family == "custom")) {
il <- stats::make.link(fam$link)$linkfun
} else if ("linkfun" %in% names(fam)) {
il <- fam$linkfun
} else if ("link" %in% names(fam) && is.character(fam$link)) {
il <- stats::make.link(fam$link)$linkfun
} else {
if ("linkfun" %in% names(fam)) {
il <- fam$linkfun
} else if ("link" %in% names(fam) && is.character(fam$link)) {
il <- stats::make.link(fam$link)$linkfun
} else {
ff <- get(fam$family, asNamespace("stats"))
il <- ff(fam$link)$linkfun
}
ff <- get(fam$family, asNamespace("stats"))
il <- ff(fam$link)$linkfun
}

il
}
18 changes: 9 additions & 9 deletions R/link_inverse.R
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ link_inverse.clmm <- link_inverse.clm
#' @export
link_inverse.clm2 <- link_inverse.clm

#' @export
link_inverse.serp <- link_inverse.clm

#' @export
link_inverse.mixor <- link_inverse.clm

Expand Down Expand Up @@ -726,17 +729,14 @@ link_inverse.mira <- function(x, ...) {
# do we have custom families?
if (!is.null(fam$family) && (is.character(fam$family) && fam$family == "custom")) {
il <- stats::make.link(fam$link)$linkinv
} else if ("linkinv" %in% names(fam)) {
il <- fam$linkinv
} else if ("link" %in% names(fam) && is.character(fam$link)) {
il <- stats::make.link(fam$link)$linkinv
} else {
if ("linkinv" %in% names(fam)) {
il <- fam$linkinv
} else if ("link" %in% names(fam) && is.character(fam$link)) {
il <- stats::make.link(fam$link)$linkinv
} else {
ff <- get(fam$family, asNamespace("stats"))
il <- ff(fam$link)$linkinv
}
ff <- get(fam$family, asNamespace("stats"))
il <- ff(fam$link)$linkinv
}

il
}

Expand Down
3 changes: 3 additions & 0 deletions R/model_info.R
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,9 @@ model_info.clm2 <- model_info.clm
#' @export
model_info.clmm <- model_info.clm

#' @export
model_info.serp <- model_info.clm

#' @export
model_info.mixor <- model_info.clm

Expand Down
2 changes: 1 addition & 1 deletion R/utils_model_info.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
# ordinal family --------

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


Expand Down
Loading

0 comments on commit 0d051b0

Please sign in to comment.