Learn R Programming

kvr2 (version 0.2.0)

comp_model: Contrast R-squared Definitions: Intercept vs. No-Intercept

Description

A specialized tool for educational and diagnostic purposes. This function automatically generates a comparison between a model with an intercept and its forced no-intercept counterpart (or vice versa), revealing how mathematical definitions of R-squared diverge under different constraints.

Usage

comp_model(model, type = c("auto", "linear", "power"), adjusted = FALSE)

Value

A data frame of class comp_model containing nine R-squared definitions and three fit metrics (RMSE, MAE, MSE) for both intercept and no-intercept versions.

The original model objects are stored as attributes with_int and without_int

for use by the plot method.

Arguments

model

A linear model or power regression model of the lm.

type

Character string. Selects the model type: "linear", "power", or "auto" (default). In "auto", the function detects if the dependent variable is log-transformed.

adjusted

Logical. If TRUE, calculates the adjusted coefficient of determination for each formula.

Details

This function reconstructs the alternative model using QR decomposition rather than update() to ensure robustness against environment/scoping issues.

It is particularly useful for observing how definitions like \(R^2_2\) can exceed 1.0 or \(R^2_1\) can become negative when an intercept is removed, illustrating the "pitfalls" discussed in Kvalseth (1985).

References

Kvalseth, T. O. (1985) Cautionary Note about R2. The American Statistician, 39(4), 279-285.

Examples

Run this code
df1 <- data.frame(x = 1:6, y = c(15, 37, 52, 59, 83, 92))
model <- lm(y ~ x, data = df1)

# Compare R-squared sensitivity
comp_model(model)

# Compare adjusted R-squared
comp_model(model, adjusted = TRUE)

Run the code above in your browser using DataLab