Debian Codegraph @

R/lmodel2.R

raw
1 #' @templateVar class lmodel2
2 #' @template title_desc_tidy
3 #'
4 #' @param x A `lmodel2` object returned by [lmodel2::lmodel2()].
5 #' @template param_unused_dots
6 #'
7 #' @evalRd return_tidy(
8 #' "term",
9 #' "estimate",
10 #' "p.value",
11 #' "conf.low",
12 #' "conf.high",
13 #' method = "Either OLS/MA/SMA/RMA"
14 #' )
15 #'
16 #' @details There are always only two terms in an `lmodel2`: `"Intercept"`
17 #' and `"Slope"`. These are computed by four methods: OLS
18 #' (ordinary least squares), MA (major axis), SMA (standard major
19 #' axis), and RMA (ranged major axis).
20 #'
21 #' The returned p-value is one-tailed and calculated via a permutation test.
22 #' A permutational test is used because distributional assumptions may not
23 #' be valid. More information can be found in
24 #' `vignette("mod2user", package = "lmodel2")`.
25 #'
26 #' @examplesIf rlang::is_installed(c("lmodel2", "ggplot2"))
27 #'
28 #' # load libraries for models and data
29 #' library(lmodel2)
30 #'
31 #' data(mod2ex2)
32 #' Ex2.res <- lmodel2(Prey ~ Predators, data = mod2ex2, "relative", "relative", 99)
33 #' Ex2.res
34 #'
35 #' # summarize model fit with tidiers + visualization
36 #' tidy(Ex2.res)
37 #' glance(Ex2.res)
38 #'
39 #' # this allows coefficient plots with ggplot2
40 #' library(ggplot2)
41 #'
42 #' ggplot(tidy(Ex2.res), aes(estimate, term, color = method)) +
43 #' geom_point() +
44 #' geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
45 #' geom_errorbarh(aes(xmin = conf.low, xmax = conf.high))
46 #'
47 #' @export
48 #' @seealso [tidy()], [lmodel2::lmodel2()]
49 #' @aliases lmodel2_tidiers
50 #' @family lmodel2 tidiers
51 tidy.lmodel2 <- function(x, ...) {
52 ret <- x$regression.results[c(1:3, 5)] |>
53 select(
54 method = Method,
55 Intercept,
56 Slope,
57 p.value = `P-perm (1-tailed)`
58 ) |>
59 pivot_longer(
60 cols = c(dplyr::everything(), -method, -p.value),
61 names_to = "term",
62 values_to = "estimate"
63 ) |>
64 arrange(method, term)
65
66 # add confidence intervals
67 confints <- x$confidence.intervals |>
68 pivot_longer(
69 cols = c(dplyr::everything(), -Method),
70 names_to = "key",
71 values_to = "value"
72 ) |>
73 tidyr::separate(key, c("level", "term"), "-") |>
74 mutate(level = ifelse(level == "2.5%", "conf.low", "conf.high")) |>
75 tidyr::pivot_wider(
76 id_cols = c(Method, term),
77 names_from = level,
78 values_from = value
79 ) |>
80 dplyr::arrange(Method) |>
81 as.data.frame() |>
82 select(method = Method, term, conf.low, conf.high)
83
84 ret |>
85 inner_join(confints, by = c("method", "term")) |>
86 # change column order so `p.value` is at the end
87 select(-p.value, dplyr::everything()) |>
88 as_tibble()
89 }
90
91
92 #' @templateVar class lmodel2
93 #' @template title_desc_glance
94 #'
95 #' @inherit tidy.lmodel2 params examples
96 #'
97 #' @evalRd return_glance(
98 #' "r.squared",
99 #' "p.value",
100 #' theta = "Angle between OLS lines `lm(y ~ x)` and `lm(x ~ y)`",
101 #' H = "H statistic for computing confidence interval of major axis slope",
102 #' "nobs"
103 #' )
104 #'
105 #' @export
106 #' @seealso [glance()], [lmodel2::lmodel2()]
107 #' @family lmodel2 tidiers
108 #'
109 glance.lmodel2 <- function(x, ...) {
110 as_glance_tibble(
111 r.squared = x$rsquare,
112 theta = x$theta,
113 p.value = x$P.param,
114 H = x$H,
115 nobs = stats::nobs(x),
116 na_types = "rrrri"
117 )
118 }