gof_qr: Goodness-of-fit Measures for Quantile Regression

View source: R/gof_qr.R

gof_qrR Documentation

Goodness-of-fit Measures for Quantile Regression

Description

Calculate goodness-of-fit measures for quantile regression as given in \insertCiteHaupt_etal_2011;textualMML. Specifically, for a given quantile v, let p_v(u) = (v - I_{u<0})u, where I is an indicator function with outputs \{0,1\}; and let y_i be the observed values, \hat{y}_i(v) fitted values for the sample of size n (i = 1,\dots,n), and y_v be the observed v-quantile. Then, R^1 – an analogue of R^2 – is defined as

R^1(v) = 1 - \frac{\sum_{i=1}^n p_v(y_i - \hat{y}_i(v))}{\sum_{i=1}^n p_v(y_i - y_v)}

and average v-weighted absolute error (ATWE) is

ATWE(v) = n^{-1}\sum_{i=1}^n p_v(y_i - \hat{y}_i(v)).

Higher R^1 and lower ATWE are preferred.

Usage

gof_qr(obs, pred, quantiles = NULL)

Arguments

obs

a numeric vector or a column matrix of observed values.

pred

a numeric vector or a matrix of predicted quantiles (columns represent different quantiles).

quantiles

an optional parameter specifying the quantiles for which the predictions were made. If pred is a vector, length(quantiles) must be 1; if pred is a matrix, length(quantiles) must be ncol(pred). If not specified, the quantiles are guessed from the colnames(pred) assuming pred are predictions from ranger or rq (see Example 1.1 below).

Value

A matrix with the following rows: R1 and ATWE. The number of columns in the output matches the number of columns in pred.

References

\insertAllCited

Examples

## Not run: 
# Example 1: Swiss
# select 30% of data for testing
n <- nrow(swiss)
testindex <- sample(1:n, 0.3*n, replace = FALSE)
# desired quantiles
qs <- c(0.025, 0.5, 0.975)

# 1.1) quantile random forest
# fit a model on the training set
qrf <- ranger::ranger(Examination ~ ., data = swiss[-testindex,], quantreg = TRUE)
# predict on the testing set
pred_qrf <- predict(qrf, swiss[testindex,], type = "quantiles", quantiles = qs)$predictions
# get a summary on the testing set
gof_qr(swiss[testindex, "Examination"], pred_qrf)

# 1.2) quantile regression
# fit a model on the training set
qrm <- quantreg::rq(Examination ~ ., data = swiss[-testindex,], tau = qs)
# predict on the testing set
pred_qrm <- predict(qrm, newdata = swiss[testindex,])
# get a summary on the testing set
gof_qr(swiss[testindex, "Examination"], pred_qrm)

## End(Not run)


vlyubchich/MML documentation built on April 16, 2023, 2:02 a.m.