Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Sep 11, 2023
1 parent 713a370 commit bc08095
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 24 deletions.
4 changes: 2 additions & 2 deletions R/marginal_means.R
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ marginal_means <- function(model,

# if type is NULL, we backtransform if relevant
type_string <- sanitize_type(model = model, type = type, calling_function = "predictions")
if (type_string == "linkinv(link)") {
if (type_string == "invlink(link)") {
type_call <- "link"
} else {
type_call <- type_string
Expand Down Expand Up @@ -387,7 +387,7 @@ marginal_means <- function(model,
out <- equivalence(out, equivalence = equivalence, df = df, ...)

# after assign draws
if (identical(type_string, "linkinv(link)")) {
if (identical(type_string, "invlink(link)")) {
linv <- tryCatch(insight::link_inverse(model), error = function(e) identity)
out <- backtransform(out, transform = linv)
}
Expand Down
4 changes: 2 additions & 2 deletions R/predictions.R
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ predictions <- function(model,

# if type is NULL, we backtransform if relevant
type_string <- sanitize_type(model = model, type = type, calling_function = "predictions")
if (type_string == "linkinv(link)") {
if (type_string == "invlink(link)") {
type_call <- "link"
} else {
type_call <- type_string
Expand Down Expand Up @@ -526,7 +526,7 @@ predictions <- function(model,
out <- equivalence(out, equivalence = equivalence, df = df, ...)

# after rename to estimate / after assign draws
if (identical(type_string, "linkinv(link)")) {
if (identical(type_string, "invlink(link)")) {
linv <- tryCatch(insight::link_inverse(model), error = function(e) identity)
out <- backtransform(out, transform = linv)
}
Expand Down
4 changes: 4 additions & 0 deletions R/print.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ print.marginaleffects <- function(x,
nrows = getOption("marginaleffects_print_nrows", default = 30),
ncols = getOption("marginaleffects_print_ncols", default = 30),
style = getOption("marginaleffects_print_style", default = "summary"),
type = getOption("marginaleffects_print_type", default = TRUE),
...) {


Expand Down Expand Up @@ -243,6 +244,9 @@ print.marginaleffects <- function(x,
if (ncol(x) <= ncols) {
cat("Columns:", paste(colnames(x), collapse = ", "), "\n")
}
if (isTRUE(type) && !is.null(attr(x, "type"))) {
cat("Type: ", attr(x, "type"), "\n")
}
cat("\n")

print_tail <- attr(x, "print_tail")
Expand Down
6 changes: 3 additions & 3 deletions R/sanitize_type.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ sanitize_type <- function(model, type, calling_function = NULL) {
}
dict <- type_dictionary
if (calling_function %in% c("slopes", "comparisons")) {
dict <- dict[dict$type != "linkinv(link)", , drop = FALSE]
dict <- dict[dict$type != "invlink(link)", , drop = FALSE]
}

# fixest: linkinv(link) only supported for glm model
# fixest: invlink(link) only supported for glm model
if (inherits(model, "fixest")) {
if (!isTRUE(hush(model[["method_type"]]) %in% c("feglm"))) {
dict <- dict[dict$type != "linkinv(link)", , drop = FALSE]
dict <- dict[dict$type != "invlink(link)", , drop = FALSE]
}
}

Expand Down
8 changes: 4 additions & 4 deletions R/type_dictionary.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ hxlr,scale
hxlr,density
ivpml,pr
ivpml,xb
fixest,linkinv(link)
fixest,invlink(link)
fixest,response
fixest,link
hurdle,response
Expand All @@ -60,14 +60,14 @@ iv_robust,response
lm,response
gam,response
gam,link
Gam,linkinv(link)
Gam,invlink(link)
Gam,response
Gam,link
geeglm,response
geeglm,link
glimML,response
glimML,link
glm,linkinv(link)
glm,invlink(link)
glm,response
glm,link
glmerMod,response
Expand Down Expand Up @@ -104,7 +104,7 @@ mhurdle,E
mhurdle,Ep
mhurdle,p
mlogit,response
negbin,linkinv(link)
negbin,invlink(link)
negbin,response
negbin,link
ols,lp
Expand Down
4 changes: 2 additions & 2 deletions book/articles/predictions.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ pred <- predictions(modlin)
mean(pred$estimate)
```

Note that in GLM models with a non-linear link function, the default `type` is `linkinv(link)`. This means that predictions are first made on the link scale, averaged, and then back transformed. Thus, the average prediction may not be exactly identical to the average of predictions:
Note that in GLM models with a non-linear link function, the default `type` is `invlink(link)`. This means that predictions are first made on the link scale, averaged, and then back transformed. Thus, the average prediction may not be exactly identical to the average of predictions:

```{r}
mod <- glm(vs ~ hp + am, data = mtcars, family = binomial)
Expand Down Expand Up @@ -224,7 +224,7 @@ predictions(
summarize(AAP = mean(estimate))
```

Note that the two results are exactly identical when we specify `type="response"` explicitly. However, they will differ slightly when we leave `type` unspecified, because `marginaleffects` will then automatically make predictions and average on the link scale, before backtransforming (`"linkinv(link)"`):
Note that the two results are exactly identical when we specify `type="response"` explicitly. However, they will differ slightly when we leave `type` unspecified, because `marginaleffects` will then automatically make predictions and average on the link scale, before backtransforming (`"invlink(link)"`):

```{r}
predictions(
Expand Down
2 changes: 1 addition & 1 deletion man-roxygen/type.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' The `type` argument determines the scale of the predictions used to compute quantities of interest with functions from the `marginaleffects` package. Admissible values for `type` depend on the model object. When users specify an incorrect value for `type`, `marginaleffects` will raise an informative error with a list of valid `type` values for the specific model object. The first entry in the list in that error message is the default type.
#'
#' The `linkinv(link)` is a special type defined by `marginaleffects`. It is available for some (but not all) models and functions. With this link type, we first compute predictions on the link scale, then we use the inverse link function to backtransform the predictions to the response scale. This is useful for models with non-linear link functions as it can ensure that confidence intervals stay within desirable bounds, ex: [0,1] for a logit model. Note that an average of estimates with `type="linkinv(link)"` will not always be equivalent to the average of estimates with `type="response"`.
#' The `invlink(link)` is a special type defined by `marginaleffects`. It is available for some (but not all) models and functions. With this link type, we first compute predictions on the link scale, then we use the inverse link function to backtransform the predictions to the response scale. This is useful for models with non-linear link functions as it can ensure that confidence intervals stay within desirable bounds, ex: [0,1] for a logit model. Note that an average of estimates with `type="invlink(link)"` will not always be equivalent to the average of estimates with `type="response"`.
#'
#' Some of the most common `type` values are:
#'
Expand Down
4 changes: 2 additions & 2 deletions man/comparisons.Rd

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

4 changes: 2 additions & 2 deletions man/marginal_means.Rd

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

4 changes: 2 additions & 2 deletions man/marginalmeans.Rd

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

4 changes: 2 additions & 2 deletions man/predictions.Rd

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

4 changes: 2 additions & 2 deletions man/slopes.Rd

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

0 comments on commit bc08095

Please sign in to comment.