Learn R Programming

rcompanion (version 2.5.4)

efronRSquared: Efron's pseudo r-squared

Description

Produces Efron's pseudo r-squared from certain models, or vectors of residuals, predicted values, and actual values. Alternately produces minimum maximum accuracy, mean absolute percent error, root mean square error, or coefficient of variation. Alternately produces a fit statistic based on absolute deviations.

Usage

efronRSquared(
  model = NULL,
  actual = NULL,
  predicted = NULL,
  residual = NULL,
  statistic = "EfronRSquared",
  null.absolute.deviation = "mean",
  rescale.adr = TRUE,
  plotit = FALSE,
  digits = 3,
  ...
)

Value

A single statistic

Arguments

model

A model of the class lm, glm, nls, betareg, gls, lme, lmerMod, lmerModLmerTest, glmmTMB, rq, loess, gam, negbin, glmRob, rlm, rfit, or mblm.

actual

A vector of actual y values

predicted

A vector of predicted values

residual

A vector of residuals

statistic

The statistic to produce. One of "EfronRSquared", "AbsoluteDeviationRSquared", "MinMaxAccuracy", "MAE", "MAPE", "MSE", "RMSE", "NRMSE.Mean", "CV".

null.absolute.deviation

For the AbsoluteDeviationRSquared statistic whether the null model is based on the absolute deviations of observed values from their mean or median.

rescale.adr

For the AbsoluteDeviationRSquared statistic, if TRUE, the default, an empirical function is used to re-scale the statistic to be similar in magnitude to an r-squared value.

plotit

If TRUE, produces plots of the predicted values vs. the actual values.

digits

The number of significant digits in the output.

...

Other arguments passed to plot.

Author

Salvatore Mangiafico, mangiafico@njaes.rutgers.edu

Details

Efron's pseudo r-squared is calculated as 1 minus the residual sum of squares divided by the total sum of squares. For (lm model objects), Efron's pseudo r-squared will be equal to r-squared.

Absolute deviation pseudo r-squared is calculated as 1 minus the absolute deviations of predicted and observed values divided by the absolute deviation of observed values from their mean or median. By default, this value is re-scaled to be similar in magnitude to an r-squared value.

This function produces the same statistics as does the accuracy function. While the accuracy function extracts values from a model object, this function allows for the manual entry of residual, predicted, or actual values.

It is recommended that the user consults the accuracy function for further details on these statistics, such as if the reported value is presented as a percentage or fraction.

If modelis not supplied, two of the following need to be passed to the function: actual, predicted, residual.

Note that, for some model objects, to extract residuals and predicted values on the original scale, a type="response" option needs to be added to the call, e.g. residuals(model.object, type="response").

References

https://rcompanion.org/handbook/F_16.html

See Also

accuracy, nagelkerke

Examples

Run this code
data(BrendonSmall)
BrendonSmall$Calories = as.numeric(BrendonSmall$Calories)
BrendonSmall$Calories2 = BrendonSmall$Calories ^ 2
model.1 = lm(Sodium ~ Calories + Calories2, data = BrendonSmall)

efronRSquared(model.1)

efronRSquared(model.1, statistic="MAPE")

efronRSquared(model.1, statistic="AbsoluteDeviationRSquared")

efronRSquared(actual=BrendonSmall$Sodium, residual=model.1$residuals)
efronRSquared(residual=model.1$residuals, predicted=model.1$fitted.values)
efronRSquared(actual=BrendonSmall$Sodium, predicted=model.1$fitted.values)

summary(model.1)$r.squared

Run the code above in your browser using DataLab