Calculates goodness-of-fit metrics based on Kvalseth (1985), including Root Mean Squared Error (RMSE), Mean Absolute Error (MAE), and Mean Squared Error (MSE). This function provides a unified output for comparing different model specifications.
Value
For
comp_fit(): An object of classcomp_kvr2, which is a list containing the calculated RMSE, MAE, and MSE values.For individual functions (
RMSE(),MAE(),MSE()): A named numeric value of the specific metric.
Details
The metrics are calculated according to the formulas in Kvalseth (1985):
RMSE: Root Mean Squared Residual or Error $$RMSE = \sqrt{\frac{\sum (y - \hat{y})^2}{n}}$$
MAE: Mean Absolute Residual or Error $$MAE = \frac{\sum |y - \hat{y}|}{n}$$
MSE: Mean Squared Residual or Error (Adjusted for degrees of freedom) $$MSE = \frac{\sum (y - \hat{y})^2}{n - p}$$
where \(n\) is the sample size and \(p\) is the number of model parameters (including the intercept).
Note on MSE: In many modern contexts, "MSE" refers to the mean squared error without degree-of-freedom adjustment (denominator \(n\)). However, this function follows Kvalseth's definition, which uses \(n - p\) as the denominator.
Note
The power regression model must be based on a logarithmic transformation.
When type = "auto", the choice between linear and power regression
is determined by analyzing the model formula. It identifies a power
regression if the dependent variable is a function call to log()
(e.g., lm(log(y) ~ x)).
Note that simple variable names containing the string "log" (e.g.,
lm(log_value ~ x)) are correctly treated as linear regression.
To override this automatic detection, manually specify type = "linear"
or type = "power".
References
Tarald O. Kvalseth (1985) Cautionary Note about R 2 , The American Statistician, 39:4, 279-285, doi: 10.1080/00031305.1985.10479448
Examples
# example data set 1. Kvålseth (1985).
df1 <- data.frame(x = c(1:6),
y = c(15,37,52,59,83,92))
model_intercept <- lm(y ~ x, df1)
model_without <- lm(y ~ x - 1, df1)
model_power <- lm(log(y) ~ log(x), df1)
comp_fit(model_intercept)
#> RMSE : 3.6165
#> MAE : 3.5238
#> MSE : 19.6190
#> ---------------------------------
#> (Type: linear, with intercept, n: 6, k: 2)
comp_fit(model_without)
#> RMSE : 3.9008
#> MAE : 3.6520
#> MSE : 18.2593
#> ---------------------------------
#> (Type: linear, without intercept, n: 6, k: 1)
comp_fit(model_power)
#> RMSE : 3.8982
#> MAE : 3.6334
#> MSE : 22.7938
#> ---------------------------------
#> (Type: power, with intercept, n: 6, k: 2)