-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd868b4
commit d2c6f0d
Showing
7 changed files
with
101 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#' @title Ferrari's and Cribari-Neto's R2 | ||
#' @name r2_ferrari | ||
#' | ||
#' @description Calculates Ferrari's and Cribari-Neto's pseudo R2 (for | ||
#' beta-regression models). | ||
#' | ||
#' @param model Generalized linear, in particular beta-regression model. | ||
#' @param ... Currently not used. | ||
#' | ||
#' @return A list with the pseudo R2 value. | ||
#' | ||
#' @references | ||
#' - Ferrari, S., and Cribari-Neto, F. (2004). Beta Regression for Modelling Rates | ||
#' and Proportions. Journal of Applied Statistics, 31(7), 799–815. | ||
#' \doi{10.1080/0266476042000214501} | ||
#' | ||
#' @examplesIf require("betareg") | ||
#' data("GasolineYield", package = "betareg") | ||
#' model <- betareg::betareg(yield ~ batch + temp, data = GasolineYield) | ||
#' r2_ferrari(model) | ||
#' @export | ||
r2_ferrari <- function(model, ...) { | ||
UseMethod("r2_ferrari") | ||
} | ||
|
||
#' @export | ||
r2_ferrari.default <- function(model, ...) { | ||
# coefficients, but remove phi parameter | ||
x <- stats::coef(model) | ||
x <- x[names(x) != "(phi)"] | ||
|
||
# model matrix, check dimensions / length | ||
mm <- insight::get_modelmatrix(model) | ||
|
||
if (length(x) != ncol(mm)) { | ||
insight::format_warning("Model matrix and coefficients do not match.") | ||
return(NULL) | ||
} | ||
|
||
# linear predictor for the mean | ||
eta <- as.vector(x %*% t(mm)) | ||
y <- insight::get_response(m) | ||
|
||
ferrari <- stats::cor(eta, insight::link_function(model)(y))^2 | ||
out <- list(R2 = c(`Ferrari's R2` = ferrari)) | ||
|
||
attr(out, "model_type") <- "Generalized Linear" | ||
structure(class = "r2_generic", out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
test_that("r2_ferarri", { | ||
skip_if_not_installed("betareg") | ||
data("GasolineYield", package = "betareg") | ||
model <- betareg::betareg(yield ~ batch + temp, data = GasolineYield) | ||
out <- r2_ferrari(model) | ||
expect_equal(out$R2, summary(model)$pseudo.r.squared, tolerance = 1e-3, ignore_attr = TRUE) | ||
}) |