Skip to contents

quantreg_fit() fits linear quantile regressions, as quantreg::rq(): it minimises the weighted check loss sum(w * rho_tau(y - X beta)) by an interior point on the dual (Koenker and Portnoy's Frisch-Newton) and then moves to the exact vertex, which is rq(method = "br")'s solution when it is unique. Its standard errors are Powell's kernel sandwich, summary.rq(se = "ker"), with Hall and Sheather's bandwidth (hs = TRUE) or Bofinger's; vcov(m, se = "boot") gives a paired bootstrap instead. Several tau fit at once, and predict(m, newdata, rearrange = TRUE) sorts each row's quantiles so they do not cross. The objective is the weighted check loss minimised (rq's rho leaves the weights out).

Usage

quantreg_fit(
  formula,
  data,
  tau = 0.5,
  weights = NULL,
  offset = NULL,
  bandwidth = "hall_sheather"
)

quantreg_model(ptr, terms, xlevels, data)

expectreg_fit(
  formula,
  data,
  tau = 0.5,
  weights = NULL,
  offset = NULL,
  lambda = NULL,
  penalty = NULL
)

expectreg_model(ptr, terms, xlevels)

Arguments

formula

A model formula.

data

A data frame.

tau

The quantile or expectile: one value, or several for quantreg_fit().

weights, offset

Optional prior weights and an offset, subtracted from the response.

bandwidth

"hall_sheather" (the default) or "bofinger".

lambda, penalty

The penalty's multiplier and its matrix over the design's columns (zeros for unpenalised ones), or NULL.

Value

A quantreg_model with tau, coefficients (a named vector, or a matrix with one column per tau), std_errors, objective and bandwidth; coef(), vcov(m, se = c("ker", "boot"), n_boot, seed, k), predict(m, newdata, rearrange) and predict_distribution(m, newdata, n_sims, seed, k) (draws of the fitted quantile k from its coefficients' normal approximation). An expectreg_model with tau, coefficients, std_errors, covariance, objective and iterations, and the same methods.

Details

expectreg_fit() fits linear expectile regression (asymmetric least squares, Newey and Powell 1987), as expectreg::expectreg.ls()'s least asymmetrically weighted squares, by iteratively reweighted least squares, with the sandwich covariance A^-1 B A^-1 (lm's HC0 at tau = 0.5). A quadratic penalty lambda * t(beta) %*% penalty %*% beta on a P-spline basis gives expectreg's smooth at a fixed lambda (docs/design/models.md, "Quantile and expectile regression").

Examples

d <- data.frame(x = 1:40, y = 1:40 + rep(c(-2, 0, 1, 5), 10))
m <- quantreg_fit(y ~ x, d, tau = c(0.25, 0.5, 0.75))
m@coefficients
#>              tau=0.25     tau=0.5  tau=0.75
#> (Intercept) -2.054054 -0.05405405 0.6756757
#> x            1.054054  1.02702703 1.1081081
predict(m, data.frame(x = 41), rearrange = TRUE)
#>      tau=0.25  tau=0.5 tau=0.75
#> [1,] 41.16216 42.05405 46.10811
e <- expectreg_fit(y ~ x, d, tau = 0.9)
e@std_errors
#> (Intercept)           x 
#>  0.84346602  0.03195074