Manual
Installation
Pkg.add("GLM")
will install this package and its dependencies, which includes the Distributions package.
The RDatasets package is useful for fitting models on standard R datasets to compare the results with those from R.
Fitting GLM models
To fit a Generalized Linear Model (GLM), use the function, glm(formula, data, family, link)
, where,
formula
: uses column symbols from the DataFrame data, for example, ifnames(data)=[:Y,:X1,:X2]
, then a valid formula is@formula(Y ~ X1 + X2)
data
: a DataFrame which may contain NA values, any rows with NA values are ignoredfamily
: chosen fromBernoulli()
,Binomial()
,Gamma()
,Normal()
,Poisson()
, orNegativeBinomial(θ)
link
: chosen from the list below, for example,LogitLink()
is a valid link for theBinomial()
family
Typical distributions for use with glm
and their canonical link functions are
Bernoulli (LogitLink)
Binomial (LogitLink)
Gamma (InverseLink)
InverseGaussian (InverseSquareLink)
NegativeBinomial (LogLink)
Normal (IdentityLink)
Poisson (LogLink)
Currently the available Link types are
CauchitLink
CloglogLink
IdentityLink
InverseLink
InverseSquareLink
LogitLink
LogLink
NegativeBinomialLink
ProbitLink
SqrtLink
The NegativeBinomial
distribution belongs to the exponential family only if θ (the shape parameter) is fixed, thus θ has to be provided if we use glm
with NegativeBinomial
family. If one would like to also estimate θ, then negbin(formula, data, link)
should be used instead.
An intercept is included in any GLM by default.
Methods applied to fitted models
Many of the methods provided by this package have names similar to those in R.
coef
: extract the estimates of the coefficients in the modeldeviance
: measure of the model fit, weighted residual sum of squares for lm'sdof_residual
: degrees of freedom for residuals, when meaningfulglm
: fit a generalized linear model (an alias forfit(GeneralizedLinearModel, ...)
)lm
: fit a linear model (an alias forfit(LinearModel, ...)
)stderror
: standard errors of the coefficientsvcov
: estimated variance-covariance matrix of the coefficient estimatespredict
: obtain predicted values of the dependent variable from the fitted model
Note that the canonical link for negative binomial regression is NegativeBinomialLink
, but in practice one typically uses LogLink
.
Separation of response object and predictor object
The general approach in this code is to separate functionality related to the response from that related to the linear predictor. This allows for greater generality by mixing and matching different subtypes of the abstract type LinPred
and the abstract type ModResp
.
A LinPred
type incorporates the parameter vector and the model matrix. The parameter vector is a dense numeric vector but the model matrix can be dense or sparse. A LinPred
type must incorporate some form of a decomposition of the weighted model matrix that allows for the solution of a system X'W * X * delta=X'wres
where W
is a diagonal matrix of "X weights", provided as a vector of the square roots of the diagonal elements, and wres
is a weighted residual vector.
Currently there are two dense predictor types, DensePredQR
and DensePredChol
, and the usual caveats apply. The Cholesky version is faster but somewhat less accurate than that QR version. The skeleton of a distributed predictor type is in the code but not yet fully fleshed out. Because Julia by default uses OpenBLAS, which is already multi-threaded on multicore machines, there may not be much advantage in using distributed predictor types.
A ModResp
type must provide methods for the wtres
and sqrtxwts
generics. Their values are the arguments to the updatebeta
methods of the LinPred
types. The Float64
value returned by updatedelta
is the value of the convergence criterion.
Similarly, LinPred
types must provide a method for the linpred
generic. In general linpred
takes an instance of a LinPred
type and a step factor. Methods that take only an instance of a LinPred
type use a default step factor of 1. The value of linpred
is the argument to the updatemu
method for ModResp
types. The updatemu
method returns the updated deviance.