-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
get_family.R
67 lines (60 loc) · 1.52 KB
/
get_family.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#' A robust alternative to stats::family
#'
#' A robust and resilient alternative to `stats::family`. To avoid issues
#' with models like `gamm4`.
#'
#' @param x A statistical model.
#' @param ... Further arguments passed to methods.
#'
#' @examplesIf require("mgcv")
#' data(mtcars)
#' x <- glm(vs ~ wt, data = mtcars, family = "binomial")
#' get_family(x)
#'
#' x <- mgcv::gamm(
#' vs ~ am + s(wt),
#' random = list(cyl = ~1),
#' data = mtcars,
#' family = "binomial"
#' )
#' get_family(x)
#' @export
get_family <- function(x, ...) {
UseMethod("get_family")
}
#' @export
get_family.default <- function(x, ...) {
fam <- .safe(stats::family(x, ...))
if (is.null(fam)) {
fam <- .safe(.get_family(x, ...))
}
fam
}
#' @export
get_family.list <- function(x, ...) {
if ("gam" %in% names(x)) {
.get_family(x)
} else {
format_error("Could not retrieve family from this list. Check the input.")
}
}
#' @export
get_family.model_fit <- function(x, ...) {
get_family(x$fit, ...)
}
.get_family <- function(x, ...) {
info <- model_info(x, verbose = FALSE)
if (info$family == "binomial") {
fam <- stats::binomial(link = info$link_function)
} else if (info$is_linear) {
fam <- stats::gaussian(link = "identity")
} else if (info$is_poisson) {
fam <- stats::poisson(link = info$link_function)
} else {
format_error(
paste0("Could not retrieve family from this object of class `", class(x)[1], "`."),
"Open an issue at {.url https://github.com/easystats/insight/issues}"
)
}
fam
}