Skip to content

Commit

Permalink
Support for panelr::asym() (#969)
Browse files Browse the repository at this point in the history
* Support for `panelr::asym()`
Fixes #607

* fix formula

* fixes

* update readme

* Update get_varcov.R
  • Loading branch information
strengejacke authored Nov 18, 2024
1 parent 305a5e0 commit d1e6e85
Show file tree
Hide file tree
Showing 16 changed files with 363 additions and 121 deletions.
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.99.0.14
Version: 0.99.0.15
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ S3method(find_formula,SemiParBIV)
S3method(find_formula,afex_aov)
S3method(find_formula,anova)
S3method(find_formula,aovlist)
S3method(find_formula,asym)
S3method(find_formula,averaging)
S3method(find_formula,bamlss)
S3method(find_formula,betamfx)
Expand Down Expand Up @@ -199,6 +200,7 @@ S3method(find_parameters,aareg)
S3method(find_parameters,afex_aov)
S3method(find_parameters,anova.rms)
S3method(find_parameters,aovlist)
S3method(find_parameters,asym)
S3method(find_parameters,averaging)
S3method(find_parameters,bamlss)
S3method(find_parameters,bayesQR)
Expand Down Expand Up @@ -620,6 +622,7 @@ S3method(get_parameters,aareg)
S3method(get_parameters,afex_aov)
S3method(get_parameters,aov)
S3method(get_parameters,aovlist)
S3method(get_parameters,asym)
S3method(get_parameters,averaging)
S3method(get_parameters,bamlss)
S3method(get_parameters,bayesQR)
Expand Down Expand Up @@ -824,6 +827,7 @@ S3method(get_statistic,SemiParBIV)
S3method(get_statistic,aareg)
S3method(get_statistic,afex_aov)
S3method(get_statistic,anova.rms)
S3method(get_statistic,asym)
S3method(get_statistic,averaging)
S3method(get_statistic,bayesx)
S3method(get_statistic,betamfx)
Expand Down Expand Up @@ -972,6 +976,7 @@ S3method(get_varcov,MixMod)
S3method(get_varcov,Rchoice)
S3method(get_varcov,afex_aov)
S3method(get_varcov,aov)
S3method(get_varcov,asym)
S3method(get_varcov,averaging)
S3method(get_varcov,betamfx)
S3method(get_varcov,betaor)
Expand Down Expand Up @@ -1081,6 +1086,7 @@ S3method(link_function,RM)
S3method(link_function,Rchoice)
S3method(link_function,afex_aov)
S3method(link_function,aovlist)
S3method(link_function,asym)
S3method(link_function,averaging)
S3method(link_function,bamlss)
S3method(link_function,bayesx)
Expand Down Expand Up @@ -1208,6 +1214,7 @@ S3method(link_inverse,RM)
S3method(link_inverse,Rchoice)
S3method(link_inverse,afex_aov)
S3method(link_inverse,aovlist)
S3method(link_inverse,asym)
S3method(link_inverse,averaging)
S3method(link_inverse,bamlss)
S3method(link_inverse,bayesx)
Expand Down Expand Up @@ -1342,6 +1349,7 @@ S3method(model_info,aareg)
S3method(model_info,afex_aov)
S3method(model_info,anova)
S3method(model_info,aovlist)
S3method(model_info,asym)
S3method(model_info,averaging)
S3method(model_info,bamlss)
S3method(model_info,bayesQR)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@

* Added support for `coxph.panel` models.

* Added support for models of class `asym` (package *panelr*).

* Overhaul of documentation for the package-functions.

## Bug fix
Expand Down
27 changes: 27 additions & 0 deletions R/find_formula.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,33 @@ find_formula.default <- function(x, verbose = TRUE, ...) {
}


#' @export
find_formula.asym <- function(x, verbose = TRUE, ...) {
modified_f <- safe_deparse(stats::formula(x))
# limitation: we can't preserve "*" and ":"
modified_f <- gsub("*", "+", modified_f, fixed = TRUE)
modified_f <- gsub(":", "+", modified_f, fixed = TRUE)
# explanation:
# - gsub("\\+\\s*minus__[^\\+]+", "", input_string):
# This regular expression matches and removes any term that starts with
# + minus__ followed by any characters that are not a +.
# - gsub("\\s*\\+\\s*$", "", output_string):
# This removes any trailing plus sign and whitespace that might be left
# at the end of the string.
output_string <- gsub("\\+\\s*minus__[^\\+]+", "", modified_f)
output_string <- gsub("\\s*(\\+|\\*)\\s*$", "", output_string) # Remove trailing plus sign if any
# explanation:
# - gsub("lag_([a-zA-Z]+)_", "lag(\\1)", input_string):
# This regular expression matches the pattern "lag_", followed by one or
# more letters (captured in a group), followed by "_". It replaces this
# pattern with "lag(", the captured group, and ")".
output_string <- gsub("lag_([a-zA-Z]+)_", "lag(\\1)", output_string)
output_string <- gsub("plus__", "", output_string, fixed = TRUE)
f <- .safe(list(conditional = stats::as.formula(output_string)))
.find_formula_return(f, verbose = verbose)
}


#' @export
find_formula.list <- function(x, verbose = TRUE, ...) {
if (object_has_names(x, "gam")) {
Expand Down
18 changes: 18 additions & 0 deletions R/find_parameters_other.R
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,21 @@ find_parameters.coxph <- function(x, flatten = FALSE, ...) {
out
}
}


#' @export
find_parameters.asym <- function(x, flatten = FALSE, ...) {
cf <- stats::coef(x)

params <- names(cf)
params <- gsub("^plus__", "+", params)
params <- gsub("^minus__", "-", params)

out <- list(conditional = params)

if (flatten) {
unique(unlist(out, use.names = FALSE))
} else {
out
}
}
1 change: 1 addition & 0 deletions R/find_statistic.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ find_statistic.default <- function(x, ...) {
# t-value objects ----------------------------------------------------------

t.mods <- c(
"asym",
"bayesx", "BBreg", "BBmm", "bcplm", "biglm", "bfsl", "blmerMod",
"cch", "censReg", "complmrob", "cpglm", "cpglmm", "crq", "crqs",
"drc",
Expand Down
17 changes: 17 additions & 0 deletions R/get_parameters_others.R
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,20 @@ get_parameters.coxph <- function(x, verbose = TRUE, ...) {

text_remove_backticks(params)
}


#' @export
get_parameters.asym <- function(x, verbose = TRUE, ...) {
cf <- stats::coef(x)
params <- data.frame(
Parameter = names(cf),
Estimate = unname(cf),
stringsAsFactors = FALSE,
row.names = NULL
)

params$Parameter <- gsub("^plus__", "+", params$Parameter)
params$Parameter <- gsub("^minus__", "-", params$Parameter)

text_remove_backticks(params)
}
16 changes: 16 additions & 0 deletions R/get_statistic.R
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,22 @@ get_statistic.merModList <- function(x, ...) {
}


#' @export
get_statistic.asym <- function(x, ...) {
cftable <- summary(x)$coef_table
out <- data.frame(
Parameter = rownames(x$cftable),
Statistic = cftable[, "t val."],
stringsAsFactors = FALSE,
row.names = NULL
)

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


#' @export
get_statistic.afex_aov <- function(x, ...) {
out <- data.frame(
Expand Down
10 changes: 10 additions & 0 deletions R/get_varcov.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ get_varcov.fixest <- function(x,
}


#' @export
get_varcov.asym <- function(x, ...) {
out <- get_varcov.default(x, ...)
colnames(out) <- gsub("^plus__", "+", colnames(out))
rownames(out) <- gsub("^plus__", "+", rownames(out))
colnames(out) <- gsub("^minus__", "-", colnames(out))
rownames(out) <- gsub("^minus__", "-", rownames(out))
out
}


# mlm ---------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions R/is_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ is_regression_model <- function(x) {

# a --------------------
"aareg", "afex_aov", "AKP", "ancova", "anova", "Anova.mlm",
"anova.rms", "aov", "aovlist", "Arima", "averaging",
"anova.rms", "aov", "aovlist", "Arima", "averaging", "asym",

# b --------------------
"bamlss", "bamlss.frame", "bayesGAM", "bayesmeta", "bayesx",
Expand All @@ -81,7 +81,7 @@ is_regression_model <- function(x) {
"eglm", "elm", "emmGrid", "emm_list", "epi.2by2", "ergm",

# f --------------------
"feglm", "feis", "felm", "fitdistr", "fixest", "flexmix",
"fdm", "feglm", "feis", "felm", "fitdistr", "fixest", "flexmix",
"flexsurvreg", "flac", "flic",

# g --------------------
Expand Down
7 changes: 4 additions & 3 deletions R/is_model_supported.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ supported_models <- function() {
.supported_models_list <- function() {
c(
# a ----------------------------
"aareg", "afex_aov", "aov", "aovlist", "AKP", "Anova.mlm", "anova.rms",
"Arima", "averaging",
"asym", "aareg", "afex_aov", "aov", "aovlist", "AKP", "Anova.mlm",
"anova.rms", "Arima", "averaging",

# b ----------------------------
"bamlss", "bamlss.frame", "bayesx", "bayesQR", "BBmm", "BBreg", "bcplm",
Expand All @@ -66,7 +66,8 @@ supported_models <- function() {
"eglm", "elm", "epi.2by2", "ergm", "emmGrid",

# f ----------------------------
"feis", "felm", "feglm", "fitdistr", "fixest", "flexsurvreg", "flac", "flic",
"fdm", "feis", "felm", "feglm", "fitdistr", "fixest", "flexsurvreg",
"flac", "flic",

# g ----------------------------
"gam", "Gam", "gamlss", "gamm", "gamm4", "garch", "gbm", "gee", "geeglm",
Expand Down
3 changes: 3 additions & 0 deletions R/link_function.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ link_function.lm <- function(x, ...) {
.extract_generic_linkfun(x, "identity")
}

#' @export
link_function.asym <- link_function.lm

#' @export
link_function.phylolm <- link_function.lm

Expand Down
3 changes: 3 additions & 0 deletions R/link_inverse.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ link_inverse.lm <- function(x, ...) {
.extract_generic_linkinv(x, "identity")
}

#' @export
link_inverse.asym <- link_inverse.lm

#' @export
link_inverse.phylolm <- link_inverse.lm

Expand Down
6 changes: 6 additions & 0 deletions R/model_info.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ model_info.anova <- function(x, verbose = TRUE, ...) {
}


#' @export
model_info.asym <- function(x, verbose = TRUE, ...) {
.make_family(x, verbose = verbose, ...)
}


#' @export
model_info.mclogit <- function(x, verbose = TRUE, ...) {
.make_family(
Expand Down
Loading

0 comments on commit d1e6e85

Please sign in to comment.