Skip to contents

Extracts the R-squared value from a fitted ftmglm or ftmlm object. This function provides a measure of how well the model explains the variability of the response/outcome and updates based on the available variables.

Usage

# S4 method for class 'ftmglm'
rsq(object, select = NULL, s = NULL)

# S4 method for class 'ftmlm'
rsq(object, select = NULL, s = NULL)

Arguments

object

An object of class ftmglm or ftmlm.

select

(optional) A character string of variables or indices to use. If omitted, all variables are used to compute R-squared.

s

(optional) Ridge penalty to apply during reweighting. Default is 0, indicating no penalty.

Value

A numeric value representing the R-squared (for ftmlm) or pseudo-R-squared (for ftmglm) of the model. Values range from 0 to 1, with higher values indicating better model fit.

Details

When select is specified, only the selected variables are used to calculate R-squared.

For linear models (ftmlm objects), R-squared is calculated using: $$TSS = yty - n \times y\_mean^2$$ $$RSS = yty - 2 \times \beta^T \times Xty + \beta^T \times XtX \times \beta$$ $$R^2 = 1 - \frac{RSS}{TSS}$$

Where:

  • TSS = Total Sum of Squares

  • RSS = Residual Sum of Squares

  • yty = Sum of squared outcome values

  • n = Number of observations

  • y_mean = Mean of the outcome variable

  • B = Model coefficients

When s is specified, a ridge penalty is applied while calculating R-squared. The estimate of R-squared is no longer exact under this condition, but it still provides a useful measure of model fit.

Note

The use of a ridge penalty is not applicable to ftmglm models and will be ignored if specified.

See also

predict,ftmglm-method and predict,ftmlm-method for methods to make predictions using model objects.

Examples

if (FALSE) { # \dontrun{
# Assuming ftmglmModel and ftmlmModel are pre-fitted model objects
# Calculate R-squared for a ftmglm model using all variables
r_squared_glm <- rsq(ftmglmModel)

# Calculate R-squared using specific variables from a ftmglm model
r_squared_glm_select <- rsq(ftmglmModel, select = c("var1", "var2"))

# Calculate R-squared for a ftmglm model using all variables
r_squared_lm <- rsq(ftmlmModel)

# Calculate R-squared using specific variables from a ftmlm model
r_squared_lm_select <- rsq(ftmlmModel, select = c("var1", "var2"))
} # }