GLM

This page demonstrates an example implementation of the JModels.jl interface for GLM.jl and applies GLM.jl via the interface.

using GLM
using GLM: AbstractGLM

Implementation of JModels

This is an example implementation which could live inside GLM.jl.

import JModels
JModels.ismodel(AbstractGLM) = true
function JModels.fit(
        mt::Type{GeneralizedLinearModel},
        data;
        settings = (;
            formula=FormulaTerm(term(:y), term(:x)),
        	family=Binomial(),
            link=LogitLink()
        )
    )
    return glm(settings.formula, data, settings.family, settings.link)
end
function JModels.predict(
        model::Union{AbstractGLM,LinearModel,RegressionModel},
        data;
        settings=NamedTuple()
    )
    return GLM.predict(model, data; settings...)
end

Example usage

data = (; x = 1:3, y = rand(3));
model = JModels.fit(GeneralizedLinearModel, data)
StatsModels.TableRegressionModel{GeneralizedLinearModel{GLM.GlmResp{Vector{Float64}, Binomial{Float64}, LogitLink}, GLM.DensePredChol{Float64, LinearAlgebra.Cholesky{Float64, Matrix{Float64}}}}, Matrix{Float64}}

y ~ 1 + x

Coefficients:
────────────────────────────────────────────────────────────────────────
                Coef.  Std. Error      z  Pr(>|z|)  Lower 95%  Upper 95%
────────────────────────────────────────────────────────────────────────
(Intercept)  -5.24316     6.04551  -0.87    0.3858  -17.0921     6.60583
x             1.95887     2.38844   0.82    0.4121   -2.72239    6.64013
────────────────────────────────────────────────────────────────────────
JModels.predict(model, data)
3-element Vector{Union{Missing, Float64}}:
 0.03611418211352498
 0.20991848014497355
 0.6532721127267466