Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Residuals in GLM #540

Open
wants to merge 7 commits into
base: pa/resid
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/CI-future.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
arch: ['x64']
steps:
- run: git config --global core.autocrlf false
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.version }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/CI-stable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
arch: ['x64']
steps:
- run: git config --global core.autocrlf false
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.version }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/Documenter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
name: Documentation
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: julia-actions/julia-buildpkg@latest
- uses: julia-actions/julia-docdeploy@latest
env:
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ SpecialFunctions = "0.6, 0.7, 0.8, 0.9, 0.10, 1, 2.0"
StatsAPI = "1.4"
StatsBase = "0.33.5, 0.34"
StatsFuns = "0.6, 0.7, 0.8, 0.9, 1.0"
StatsModels = "0.6.23, 0.7"
StatsModels = "0.7.3"
Tables = "1"
julia = "1.6"

Expand Down
3 changes: 2 additions & 1 deletion src/GLM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module GLM
import Base: (\), convert, show, size
import LinearAlgebra: cholesky, cholesky!
import Statistics: cor
using StatsAPI
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be in using part

import StatsBase: coef, coeftable, coefnames, confint, deviance, nulldeviance, dof, dof_residual,
loglikelihood, nullloglikelihood, nobs, stderror, vcov,
residuals, predict, predict!,
Expand All @@ -21,7 +22,7 @@ module GLM
export coef, coeftable, confint, deviance, nulldeviance, dof, dof_residual,
loglikelihood, nullloglikelihood, nobs, stderror, vcov, residuals, predict,
fitted, fit, fit!, model_response, response, modelmatrix, r2, r², adjr2, adjr²,
cooksdistance, hasintercept, dispersion
cooksdistance, hasintercept, dispersion, vif, gvif, termnames

export
# types
Expand Down
3 changes: 2 additions & 1 deletion src/glmfit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,7 @@ end
const _RESIDUAL_TYPES = [:deviance, :pearson, :response, :working]

"""
residuals(model::LinearModel; type=:deviance)
residuals(model::GeneralizedLinearModel; type=:deviance)

Return the residuals of a GLM.
Expand All @@ -852,7 +853,7 @@ Supported values for `type` are:
- `:deviance` (the default): the signed square root of the element-wise
contribution to the deviance
- `:response`: the difference between the observed and fitted values
- `:working`: working residuals (used during the IRLS process)
- `:working`: working residuals (used during the IRLS process). For a linear model, it is same as `:response`.
- `:pearson`: Pearson residuals, i.e., response residuals scaled
by the standard error of the response
"""
Expand Down
16 changes: 14 additions & 2 deletions src/linpred.jl
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,21 @@ response(obj::LinPredModel) = obj.rr.y

fitted(m::LinPredModel) = m.rr.mu
predict(mm::LinPredModel) = fitted(mm)
residuals(obj::LinPredModel) = residuals(obj.rr)

function formula(obj::LinPredModel)
function residuals(model::LinPredModel; type=:deviance)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know the details of the implementation, but is the residuals method properly documented (as it is not documented here). I.e. that it supports type kwarg and what is the behavior depending on the value of the kwarg.

type in _RESIDUAL_TYPES ||
throw(ArgumentError("Unsupported type `$(type)``; supported types are" *
"$(_RESIDUAL_TYPES)"))

resid = response(model) - fitted(model)
if length(model.rr.wts) > 0 && (type === :deviance || type === :pearson)
return resid .* sqrt.(model.rr.wts)
else
return resid
end
end

function StatsModels.formula(obj::LinPredModel)
obj.formula === nothing && throw(ArgumentError("model was fitted without a formula"))
return obj.formula
end
Expand Down
2 changes: 0 additions & 2 deletions src/lm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ function loglikelihood(r::LmResp)
-n/2 * (log(2π * deviance(r)/n) + 1)
end

residuals(r::LmResp) = r.y - r.mu

"""
LinearModel

Expand Down
404 changes: 291 additions & 113 deletions test/runtests.jl

Large diffs are not rendered by default.

Loading