LinearCountRegressor
LinearCountRegressorA model type for constructing a linear count regressor, based on GLM.jl, and implementing the MLJ model interface.
From MLJ, the type can be imported using
LinearCountRegressor = @load LinearCountRegressor pkg=GLMDo model = LinearCountRegressor() to construct an instance with default hyper-parameters. Provide keyword arguments to override hyper-parameter defaults, as in LinearCountRegressor(fit_intercept=...).
LinearCountRegressor is a generalized linear model, specialised to the case of a Count target variable (non-negative, unbounded integer) with user-specified link function. Options exist to specify an intercept or offset feature.
Training data
In MLJ or MLJBase, bind an instance model to data with one of:
mach = machine(model, X, y)
mach = machine(model, X, y, w)Here
X: is any table of input features (eg, aDataFrame) whose columns are of scitypeContinuous; check the scitype withschema(X)y: is the target, which can be anyAbstractVectorwhose element scitype isCount; check the scitype withschema(y)w: is a vector ofRealper-observation weights
Train the machine using fit!(mach, rows=...).
Hyper-parameters
fit_intercept=true: Whether to calculate the intercept for this model. If set to false, no intercept will be calculated (e.g. the data is expected to be centered)distribution=Distributions.Poisson(): The distribution which the residuals/errors of the model should fit.link=GLM.LogLink(): The function which links the linear prediction function to the probability of a particular outcome or class. This should be one of the following:GLM.IdentityLink(),GLM.InverseLink(),GLM.InverseSquareLink(),GLM.LogLink(),GLM.SqrtLink().offsetcol=nothing: Name of the column to be used as an offset, if any. An offset is a variable which is known to have a coefficient of 1.maxiter::Integer=30: The maximum number of iterations allowed to achieve convergence.atol::Real=1e-6: Absolute threshold for convergence. Convergence is achieved when the relative change in deviance is less than `max(rtol*dev, atol). This term exists to avoid failure when deviance is unchanged except for rounding errors.rtol::Real=1e-6: Relative threshold for convergence. Convergence is achieved when the relative change in deviance is less than `max(rtol*dev, atol). This term exists to avoid failure when deviance is unchanged except for rounding errors.minstepfac::Real=0.001: Minimum step fraction. Must be between 0 and 1. Lower bound for the factor used to update the linear fit.report_keys:Vectorof keys for the report. Possible keys are::deviance,:dof_residual,:stderror,:vcov,:coef_tableand:glm_model. By default only:glm_modelis excluded.
Operations
predict(mach, Xnew): return predictions of the target given new featuresXnewhaving the same Scitype asXabove. Predictions are probabilistic.predict_mean(mach, Xnew): instead return the mean of each prediction abovepredict_median(mach, Xnew): instead return the median of each prediction above.
Fitted parameters
The fields of fitted_params(mach) are:
features: The names of the features encountered during model fitting.coef: The linear coefficients determined by the model.intercept: The intercept determined by the model.
Report
The fields of report(mach) are:
deviance: Measure of deviance of fitted model with respect to a perfectly fitted model. For a linear model, this is the weighted residual sum of squaresdof_residual: The degrees of freedom for residuals, when meaningful.stderror: The standard errors of the coefficients.vcov: The estimated variance-covariance matrix of the coefficient estimates.coef_table: Table which displays coefficients and summarizes their significance and confidence intervals.glm_model: The raw fitted model returned byGLM.lm. Note this points to training data. Refer to the GLM.jl documentation for usage.
Examples
using MLJ
import MLJ.Distributions.Poisson
## Generate some data whose target y looks Poisson when conditioned on
## X:
N = 10_000
w = [1.0, -2.0, 3.0]
mu(x) = exp(w'x) ## mean for a log link function
Xmat = rand(N, 3)
X = MLJ.table(Xmat)
y = map(1:N) do i
x = Xmat[i, :]
rand(Poisson(mu(x)))
end;
CountRegressor = @load LinearCountRegressor pkg=GLM
model = CountRegressor(fit_intercept=false)
mach = machine(model, X, y)
fit!(mach)
Xnew = MLJ.table(rand(3, 3))
yhat = predict(mach, Xnew)
yhat_point = predict_mean(mach, Xnew)
## get coefficients approximating `w`:
julia> fitted_params(mach).coef
3-element Vector{Float64}:
0.9969008753103842
-2.0255901752504775
3.014407534033522
report(mach)See also LinearRegressor, LinearBinaryClassifier