Skip to content

Commit

Permalink
Multivariate is default for r2()'s 'mlm' method; linter suggestion …
Browse files Browse the repository at this point in the history
…edits; minor documentation edit
  • Loading branch information
jluchman committed Aug 24, 2024
1 parent aa93d88 commit f708a17
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 28 deletions.
24 changes: 15 additions & 9 deletions R/print-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,28 @@ print.r2_mlm <- function(x, digits = 3, ...) {
model_type <- attr(x, "model_type")
is_multivar_r2 <- all(names(x) == c("Symmetric Rxy", "Asymmetric Pxy"))
if (!is.null(model_type) && !is_multivar_r2) {
insight::print_color(sprintf("# R2 for %s Regression\n\n", model_type), "blue")
insight::print_color(
sprintf("# R2 for %s Regression\n\n", model_type),
"blue"
)
} else if (!is.null(model_type) && is_multivar_r2) {
insight::print_color(sprintf("# Multivariate R2 for %s Regression\n", model_type), "blue")
insight::print_color(
sprintf("# Multivariate R2 for %s Regression\n", model_type),
"blue"
)
} else {
insight::print_color("# R2\n\n", "blue")
}

if (!is_multivar_r2) {
if (is_multivar_r2) {
cat(sprintf(" Symmetric Rxy: %.*f", digits, x[["Symmetric Rxy"]]))
cat("\n")
cat(sprintf("Asymmetric Pxy: %.*f", digits, x[["Asymmetric Pxy"]]))
cat("\n\n")
} else {
for (i in names(x)) {
insight::print_color(sprintf("## %s\n", i), "cyan")
out <- paste0(
out <- paste(
c(
sprintf(" R2: %.*f", digits, x[[i]]$R2),
sprintf(" adj. R2: %.*f", digits, x[[i]]$R2_adjusted)
Expand All @@ -90,11 +101,6 @@ print.r2_mlm <- function(x, digits = 3, ...) {
cat(out)
cat("\n\n")
}
} else {
cat(sprintf(" Symmetric Rxy: %.*f", digits, x[["Symmetric Rxy"]]))
cat("\n")
cat(sprintf("Asymmetric Pxy: %.*f", digits, x[["Asymmetric Pxy"]]))
cat("\n\n")
}
invisible(x)
}
Expand Down
14 changes: 7 additions & 7 deletions R/r2.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#' (`TRUE`) or not (`FALSE`)?
#' @param ci Confidence interval level, as scalar. If `NULL` (default), no
#' confidence intervals for R2 are calculated.
#' @param multivariate Logical. Should R2 reported be by separated by
#' response (FALSE) or combined across responses as computed by
#' [`r2_mlm`] (TRUE).
#' @param multivariate Logical. Should multiple R2 values be reported as
#' separated by response (FALSE) or should a single R2 be reported as
#' combined across responses computed by [`r2_mlm`] (TRUE).
#' @param ... Arguments passed down to the related r2-methods.
#' @inheritParams r2_nakagawa
#'
Expand Down Expand Up @@ -250,9 +250,11 @@ r2.aov <- function(model, ci = NULL, ...) {

#' @rdname r2
#' @export
r2.mlm <- function(model, multivariate = FALSE, ...) {
r2.mlm <- function(model, multivariate = TRUE, ...) {

if (!multivariate) {
if (multivariate) {
out <- r2_mlm(model)
} else {
model_summary <- summary(model)

out <- lapply(names(model_summary), function(i) {
Expand All @@ -268,8 +270,6 @@ r2.mlm <- function(model, multivariate = FALSE, ...) {
})

names(out) <- names(model_summary)
} else {
out <- r2_mlm(model)
}

attr(out, "model_type") <- "Multivariate Linear"
Expand Down
9 changes: 4 additions & 5 deletions R/r2_mlm.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
#'
#' @examples
#' model <- lm(cbind(qsec, drat) ~ wt + mpg + cyl, data = mtcars)
#' r2(model)
#' r2_mlm(model)
#'
#' model_swap <- lm(cbind(wt, mpg, cyl) ~ qsec + drat, data = mtcars)
Expand Down Expand Up @@ -77,12 +76,12 @@ r2_mlm.mlm <- function(model, verbose = TRUE, ...) {

resid_cov <- stats::cov(residuals(model))
resp_cov <- stats::cov(insight::get_response(model))
q <- ncol(insight::get_response(model))
V_xy <- q - sum(diag(solve(resp_cov) %*% resid_cov))
P_xy <- V_xy/q
qq <- ncol(insight::get_response(model))
V_xy <- qq - sum(diag(solve(resp_cov) %*% resid_cov))
P_xy <- V_xy / qq

c(
"Symmetric Rxy" = R_xy,
"Asymmetric Pxy" = P_xy
)
}
}
8 changes: 4 additions & 4 deletions man/r2.Rd

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

1 change: 0 additions & 1 deletion man/r2_mlm.Rd

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

4 changes: 2 additions & 2 deletions tests/testthat/test-r2_mlm.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
test_that("r2_mlm_Rxy", {
model <- lm(cbind(qsec, drat) ~ wt + mpg, data = mtcars)
expect_equal(r2_mlm(model)[["Symmetric Rxy"]], c(0.68330688076502), tolerance = 1e-3)
expect_equal(r2_mlm(model)[["Symmetric Rxy"]], 0.68330688076502, tolerance = 1e-3)
})

test_that("r2_mlm_Pxy", {
model <- lm(cbind(qsec, drat) ~ wt + mpg, data = mtcars)
expect_equal(r2_mlm(model)[["Asymmetric Pxy"]], c(0.407215267524997), tolerance = 1e-3)
expect_equal(r2_mlm(model)[["Asymmetric Pxy"]], 0.407215267524997, tolerance = 1e-3)
})

0 comments on commit f708a17

Please sign in to comment.