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 fitcoef: stores the coefficients for a fitted model or nothing if model hasn't been fitresid: stores the residuals for each observation or nothing if the model hasn't been fitdual: 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 solutionyhat: 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.QuantRegFit — TypeQuantReg.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.
Additionally, there is a method to create deep copies of QuantRegFit objects.
Base.deepcopy — Methoddeepcopy(fit::QuantRegFit)Create a deep copy of fit.
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 modellowerci: 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) matrixupperci: 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 inversionteststat: 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 inversionp: 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.QuantRegInf — TypeQuantReg.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.
Additionally, there is a method to create deep copies of QuantRegInf objects.
Base.deepcopy — Methoddeepcopy(inf::QuantRegInf)Create a deep copy of inf.
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 datadata: DataFrame object containing data to fit the model withmf: ModelFrame object created by applyingformulatodatamm: ModelMatrix object created frommfτ: quantile to fit the model atfitmethod: 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 inferencehs: 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 distributionfit:QuantRegFitobject storing the results of fitting the modelinf:QuantRegInfobject 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.QuantRegModel — MethodQuantRegModel(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 regressionfitmethod: default method is the Barrodale-Roberts simplexinvers: default setting is true for datasets with 1000 or fewer observations and false for larger datasetsα: default setting is 0.05hs: default is true; always set to true when computing inference via rank test inversioniid: default is true if computing inference via rank test inversion and false otherwiseinterpolate: default is truetcrit: default is true
Additionally, one can construct a new QuantRegModel object from an existing QuantRegModel object by specifying parameters to change.
QuantReg.QuantRegModel — MethodQuantRegModel(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.
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-placeFinally, the QuantRegModel object also has a default constructor. Use of the default constructor is strongly discouraged.
QuantReg.QuantRegModel — TypeQuantRegModel(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.
Methods
There is a method to create a deep copy of a QuantRegModel object.
Base.deepcopy — Methoddeepcopy(model::QuantRegModel)Create a deep copy of model (excluding data and formula).
There are also methods for the following StatsBase abstractions:
StatsBase.coef — Functioncoef(obj::StatisticalModel)Return the coefficients of the model.
StatsBase.coefnames — Functioncoefnames(obj::StatisticalModel)Return the names of the coefficients.
StatsBase.coeftable — Functioncoeftable(model::QuantRegModel)Return a table of class CoefTable with coefficients and related statistics.
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.confint — Methodconfint(obj::StatisticalModel; level::Real=0.95)Compute confidence intervals for coefficients, with confidence level level (by default 95%).
StatsBase.dof — Functiondof(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_residual — Functiondof_residual(obj::RegressionModel)Return the residual degrees of freedom of the model.
StatsBase.fitted — Functionfitted(obj::RegressionModel)Return the fitted values of the model.
StatsBase.isfitted — Functionisfitted(obj::StatisticalModel)Indicate whether the model has been fitted.
StatsBase.islinear — Functionislinear(obj::StatisticalModel)Indicate whether the model is linear.
StatsBase.nobs — Functionnobs(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.modelmatrix — Functionmodelmatrix(obj::RegressionModel)Return the model matrix (a.k.a. the design matrix).
StatsBase.response — Functionresponse(obj::RegressionModel)Return the model response (a.k.a. the dependent variable).
StatsBase.responsename — Functionresponsename(obj::RegressionModel)Return the name of the model response (a.k.a. the dependent variable).
StatsBase.residuals — Functionresiduals(obj::RegressionModel)Return the residuals of the model.
As well as a method to nicely print the results of a model:
Base.show — Methodshow(io::IO, model::QuantRegModel)Display a quantreg model.
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.
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.QuantRegModels — TypeQuantRegModels()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.
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! — Methodappend!(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.
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.getindex — Methodgetindex(X::QuantRegModels, τ::Number)Returns the model in X at the τth quantile.
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.taus — Functiontaus(X::QuantRegModels)Retrive all the τ values for models stored in X.
QuantReg.hastau — Functiongetindex(X::QuantRegModels, τ::Number)Check if X contains a model at the τth quantile.
Lastly, there is also an implementation of show which nicely displays all the models stored in a QuantRegModels object.
Base.show — Methodshow(io::IO, models:QuantRegModels)Display each model in models.