Skip to contents padding-top: 70px;

Calculates several key statistics from the residuals of of a fit: the residual sum of squares (RSS), the mean squared error (MSE), the root mean squared error (RMSE), and the residual standard error (RSE). This function is used internally by several fitting functions in the PhotoGEA package, such as fit_ball_berry and fit_c3_aci.

Usage

residual_stats(fit_residuals, units, nparam)

Arguments

fit_residuals

A numeric vector representing the residuals from a fit, i.e., the differences between the measured and fitted values.

units

A string expressing the units of the residuals.

nparam

The number of free parameters that were varied when performing the fit.

Details

This function calculates several model-independent measures of the quality of a fit. The basis for these statistics are the residuals (also known as the errors). If the measured values of a quantity y are given by y_measured and the fitted values are y_fitted, then the residuals are defined to be residual = y_measured - y_fitted. The key statistics that can be calculated from the residuals are as follows:

  • The residual sum of squares (RSS) is also known as the sum of squared errors (SSE). As its name implies, it is simply the sum of all the squared residuals: RSS = sum(residuals^2).

  • The mean squared error (MSE) is the mean value of the squared residuals: MSE = sum(residuals^2) / n = RSS / n, where n is the number of residuals.

  • The root mean squared error (RMSE) is the square root of the mean squared error: RMSE = sqrt(MSE) = sqrt(RSS / n).

  • The residual standard error RSE is given by RSE = sqrt(RSS / dof), where dof = n - nparam is the number of degrees of freedom involved in the fit.

For a given model, the RMSE is usually a good way to compare the quality of different fits. When trying to decide which model best fits the measured data, the RSE may be a more appropriate metric since it controls for the number of parameters in the model.

Value

An exdf object with one row and the following columns: npts (the number of residual values), nparam, dof, RSS, MSE, RMSE, and RSE.

Examples

# Generate some random residuals
residuals <- runif(10, -1, 1)

# Calculate residual stats as if these values had units of `kg` and were related
# to a model with 3 free parameters
residual_stats(residuals, 'kg', 3)
#>   npts [residual_stats] (NA) nparam [residual_stats] (NA)
#> 1                         10                            3
#>   dof [residual_stats] (NA) RSS [residual_stats] ((kg)^2)
#> 1                         7                      3.632105
#>   MSE [residual_stats] ((kg)^2) RMSE [residual_stats] (kg)
#> 1                     0.3632105                  0.6026695
#>   RSE [residual_stats] (kg)
#> 1                 0.7203278