QuantReg.jl Types

The QuantReg.jl package introduces four types: QuantRegFit, QuantRegInf, QuantRegModel, and QuantRegModels. For most users, a deep understanding of these types is unnecessary, but the structure of these types will be helpful for advanced users, students investigating the properties of quantile regression, or developers wishing to extend the package.

These four types work together as follows: a QuantRegModel is an immutable object and contains all of the specification necessary to fit and compute inference for a model plus a QuantRegFit andQuantRegInf objects to store the results of fitting the model and computing inference, respectively. A QuantRegModels object stores multiple QuantRegModel objects, indexed by their τ values; note that a QuantRegModels object cannot contain multple QuantRegModel objects with the same τ value.

QuantRegFit

QuantRegFit objects store the results of fitting a quantile regression model and are mutable. Each instance contains the following fields:

  • computed: denotes whether or not the parent model has been fit
  • coef: stores the coefficients for a fitted model or nothing if model hasn't been fit
  • resid: stores the residuals for each observation or nothing if the model hasn't been fit
  • dual: stores the solution to the dual problem if the fitting method produces the dual solution or nothing if the model hasn't been fit or the fitting method does not produce the dual solution
  • yhat: stores the fitted values for the dataset used to build the model or nothing if the model hasn't been fit

QuantRegFit has one construtor, the default constructor in which all field values are specified directly. QuantRegFit objects are primarily constructed in the creation of QuantRegModel objects.

QuantReg.QuantRegFitType
QuantReg.QuantRegFit(computed::Bool, coef::Union{Vector{<:Number}, Nothing},
                     resid::Union{Vector{<:Number}, Nothing}, 
                     dual::Union{Vector{<:Number}, Nothing},
                     yhat::Union{Vector{<:Number}, Nothing})

Stores results of fitting a quantile regression model.

coef, resid, dual, and yhat should be set to nothing until a model is fit.

source

Additionally, there is a method to create deep copies of QuantRegFit objects.

QuantRegInf

Similarly, QuantRegInf objects store the results of computing inference for a quantile regression model and are mutable. Each instance contains the following fields:

  • computed: denotes whether or not inference has been computed for the parent model
  • lowerci: lower bounds for the specified confidence intervals or nothing if inference hasn't been computed for the model; a (k x 1) matrix unless inference is computed via rank test inversion and interpolate is set to false, in which case a (k x 2) matrix
  • upperci: upper bounds for the specified confidence intervals or nothing if inference hasn't been computed for the model; a (k x 1) matrix unless inference is computed via rank test inversion and interpolate is set to false, in which case a (k x 2) matrix
  • σ: standard errors for the coefficients or nothing if inference hasn't been computed for the model or is calculated via a rank test inversion
  • teststat: t or z statistics for the coefficients or nothing if inference hasn't been computed for the model or is calculated via a rank test inversion
  • p: p values for the coefficients or nothing if inference hasn't been computed for the model or is calculated via a rank test inversion

QuantRegInf also has one construtor, the default constructor in which all field values are specified directly. QuantRegFit objects are primarily constructed in the creation of QuantRegModel objects.

QuantReg.QuantRegInfType
QuantReg.QuantRegInf(computed::Bool,
                     lowerci::Union{Nothing, Vector{<:Number}, Matrix{<:Number}}
                     upperci::Union{Nothing, Vector{<:Number}, Matrix{<:Number}}
                     σ::Union{Nothing, Vector{<:Number}}
                     teststat::Union{Nothing, Vector{<:Number}}
                     p::Union{Nothing, Vector{<:Number}})

Stores results of computing inference for a quantile regression model.

lowerci, upperci, σ, teststat, and p should be set to nothing until inference is computed.

source

Additionally, there is a method to create deep copies of QuantRegInf objects.

QuantRegModel

QuantRegModel is the type with which end users are most likely to interact. QuantRegModel objects are immutable and contain the following fields:

  • formula: the formula to fit the data
  • data: DataFrame object containing data to fit the model with
  • mf: ModelFrame object created by applying formula to data
  • mm: ModelMatrix object created from mf
  • τ: quantile to fit the model at
  • fitmethod: the algorithm/method to fit the model; available options are:
    • "br": fit using Barrodale-Roberts simplex (default method)
    • "fn": Fit using Frisch-Newton algorithm
    • "gurobi": Fit using Gurobi (must have Gurobi installed)
  • invers: if true, compute confidence intervals by inverting a rank test (only recommended for datasets with <= 1000 observations); otherwise, use an asymptotic esimtate of the covariance matrix
  • α: size of the test for computing inference
  • hs: if true, use Hall Sheather bandwidth when computing sparsity estimates (not applicable if inference is calculated via rank test inversion with iid regression errors)
  • iid: if true, assume regression errors are iid; otherwise, assume that the conditional quantile function is locally (in tau) linear (in x)
  • interpolate: if true, interpolate the confidence intervals produced by rank test inversion inference; otherwise report values just above and just below each cutoff (only applicable if inference is calculated via rank test inversion)
  • tcrit: if true, use a Student's t distribution for calculating critical points; otherwise use a normal distribution
  • fit: QuantRegFit object storing the results of fitting the model
  • inf: QuantRegInf object storing the results of computing inference for the model

Constructors

There are multiple ways to construct a QuantRegModel object. The most relevant one for end users accepts a formula and dataset as positional arguments and model specifications as keyword arguments. Depending on the passed arguments, the constructor intelligently choses defaults for unspecified fields as described below and constructs empty QuantRegFit and QuantRegInf objects.

QuantReg.QuantRegModelMethod
QuantRegModel(formula::FormulaTerm, data::DataFrame; τ::Number=0.5,
              fitmethod::String="br",invers::Union{Nothing, Bool}=nothing,
              α::Number=0.05, hs::Bool=true, iid::Union{Nothing, Bool}=nothing,
              interpolate::Bool=true, tcrit::Bool=true)

Construct a quantile regression model at the τth quantile fitting data according to formula.

In any call, formula and data must be set. The logic for setting values of unspecified parameters is as follows:

  • τ: default is 0.5, i.e. median regression
  • fitmethod: default method is the Barrodale-Roberts simplex
  • invers: default setting is true for datasets with 1000 or fewer observations and false for larger datasets
  • α: default setting is 0.05
  • hs: default is true; always set to true when computing inference via rank test inversion
  • iid: default is true if computing inference via rank test inversion and false otherwise
  • interpolate: default is true
  • tcrit: default is true
source

Additionally, one can construct a new QuantRegModel object from an existing QuantRegModel object by specifying parameters to change.

QuantReg.QuantRegModelMethod
QuantRegModel(model::QuantRegModel; τ::Union{Nothing, Number}=nothing,
              fitmethod::Union{Nothing, String}=nothing,
              invers::Union{Nothing, Bool}=nothing, α::Union{Nothing, Number}=nothing,
              hs::Union{Nothing, Bool}=nothing, iid::Union{Nothing, Bool}=nothing,
              interpolate::Union{Nothing, Bool}=nothing,
              tcrit::Union{Nothing, Bool}=nothing)

Construct a new quantile regression model by changing one or more parameters in an existing model.

If τ or method are unchanged, then the model fit is copied from the passed model but the model inf is not. Otherwise, neither is copied.

source

One particularly convient use caase for this constructor would be changing the test size or inference type for an existing model. For example, consider generating the following model:

julia> model = rq(@formula(Y ~ X1 + X2 + X3), df; τ=0.80, fitmethod="fn", α=0.20, iid=false)

Suppose after fitting this model, the user wants to calculate 90% confidence intervals without recomputing the model fit. Such a change is nontrivial because the Hall-Sheather bandwidth depends on the the confidence interval size. To compute this change, the user could employ the above constructor as follows:

julia> newmodel = QuantRegModel(model; α=0.10) # Generate model with new test size
julia> compute_inf!(newmodel) # Compute inference for the modified model in-place

Finally, the QuantRegModel object also has a default constructor. Use of the default constructor is strongly discouraged.

QuantReg.QuantRegModelType
QuantRegModel(formula::FormulaTerm, data::DataFrame, mf::ModelFrame, mm::ModelMatrix,
              τ::Number, fitmethod::String, invers::Bool, α::Number, hs::Bool,
              iid::Bool, interpolate::Bool, tcrit::Bool, fit::QuantRegFit,
              inf::QuantRegInfe)

Contains a quantile regression model at the τth quantile fitting data according to formula.

Use of this default constructor is not recommended. If rank test inversion is used to compute inference, then the Hall-Sheather bandwidth flag will always be set to true, overriding user input.

source

Methods

There is a method to create a deep copy of a QuantRegModel object.

Base.deepcopyMethod
deepcopy(model::QuantRegModel)

Create a deep copy of model (excluding data and formula).

source

There are also methods for the following StatsBase abstractions:

StatsBase.coefFunction
coef(obj::StatisticalModel)

Return the coefficients of the model.

StatsBase.coefnamesFunction
coefnames(obj::StatisticalModel)

Return the names of the coefficients.

StatsBase.coeftableFunction
coeftable(model::QuantRegModel)

Return a table of class CoefTable with coefficients and related statistics.

source
coeftable(obj::StatisticalModel; level::Real=0.95)

Return a table of class CoefTable with coefficients and related statistics. level determines the level for confidence intervals (by default, 95%).

StatsBase.confintMethod
confint(obj::StatisticalModel; level::Real=0.95)

Compute confidence intervals for coefficients, with confidence level level (by default 95%).

StatsBase.dofFunction
dof(obj::StatisticalModel)

Return the number of degrees of freedom consumed in the model, including when applicable the intercept and the distribution's dispersion parameter.

StatsBase.dof_residualFunction
dof_residual(obj::RegressionModel)

Return the residual degrees of freedom of the model.

StatsBase.fittedFunction
fitted(obj::RegressionModel)

Return the fitted values of the model.

StatsBase.isfittedFunction
isfitted(obj::StatisticalModel)

Indicate whether the model has been fitted.

StatsBase.islinearFunction
islinear(obj::StatisticalModel)

Indicate whether the model is linear.

StatsBase.nobsFunction
nobs(obj::StatisticalModel)

Return the number of independent observations on which the model was fitted. Be careful when using this information, as the definition of an independent observation may vary depending on the model, on the format used to pass the data, on the sampling plan (if specified), etc.

StatsBase.modelmatrixFunction
modelmatrix(obj::RegressionModel)

Return the model matrix (a.k.a. the design matrix).

StatsBase.responseFunction
response(obj::RegressionModel)

Return the model response (a.k.a. the dependent variable).

StatsBase.responsenameFunction
responsename(obj::RegressionModel)

Return the name of the model response (a.k.a. the dependent variable).

StatsBase.residualsFunction
residuals(obj::RegressionModel)

Return the residuals of the model.

As well as a method to nicely print the results of a model:

Base.showMethod
show(io::IO, model::QuantRegModel)

Display a quantreg model.

source

QuantRegModels

QuantReg.jl also contains a QuantRegModels type that enables users to store multiple models at different τ values. This type is a hybrid between a dictionary and a list so that models are always indexed by the τ value for which they are fit. A single QuantRegModels object cannot contain multiple models with the same τ value.

Note

The primary intent of these objects is to store quantile regression models with the exact same specifications aside from τ, but this pattern is not strictly enforced. The internal implementation may change in the future, however, so other uses are strongly discouraged.

Currently, a QuantRegModels object has a single field, models, which stores QuantRegModel objects in a dictionary indexed by their τ values. This dictionary should not be modified directly.

Constructor

This type has one constructor, which always creates an empty QuantRegModels object.

QuantReg.QuantRegModelsType
QuantRegModels()

Wrapper containing multiple QuantRegModel at different quantiles indexed by quantile.

This method implements some behaviors of a dictionary (for easy indexing) and some behaviors of an array (for easy, consistent appends). This type is not intended to be directly created by most end users.

If models::QuantRegModels contained models with τ=0.25, 0.5 and 0.75, these models could be accessed as models[0.25], models[0.5], and models[0.75] respectively.

source

Methods

As a hybrid of an array and a dictionary, some methods intended for arrays and some intended for dictionaries are implemented for QuantRegModels.

For example, to add a new model to a QuantRegModels object, one should use append!. This method ensures that the underlying dictionary exclusively contains QuantRegModel objects, that the models are indexed by their τ values, and that each QuantRegModels object contains no more than one QuantRegModel for a given τ value.

Base.append!Method
append!(X::QuantRegModels, model::QuantRegModel)

Add model to X in-place

Throws an error if X already contains a model with the same τ value as model.

source

A getindex method is implemented for this type so that users can access a particular model via the following syntax:

julia> models[τ]

where models is a QuantRegModels object and τ is a τ value for a QuantRegModel stored in models.

Base.getindexMethod
getindex(X::QuantRegModels, τ::Number)

Returns the model in X at the τth quantile.

source

There are also two methods, taus and hastau, which respectively display all the τ values for which a QuantRegModels object contains a model and checks whther a given QuantRegModels object contains a model for a specific value of τ.

QuantReg.tausFunction
taus(X::QuantRegModels)

Retrive all the τ values for models stored in X.

source
QuantReg.hastauFunction
getindex(X::QuantRegModels, τ::Number)

Check if X contains a model at the τth quantile.

source

Lastly, there is also an implementation of show which nicely displays all the models stored in a QuantRegModels object.

Base.showMethod
show(io::IO, models:QuantRegModels)

Display each model in models.

source