gof_qr | R Documentation |
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.
gof_qr(obs, pred, quantiles = NULL)
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 |
A matrix with the following rows: R1
and ATWE
.
The number of columns in the output matches the number of columns in pred
.
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.