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

Cooks Distance tests #415

Merged
merged 30 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
c4ba123
Cooks Distance tests
ericqu Mar 26, 2021
f66d1d7
updated to couple with StatsModels
ericqu Mar 26, 2021
b680e2a
using StatsModels etc.
ericqu Mar 26, 2021
deb4f38
no weights
ericqu Mar 26, 2021
6d13db3
no weights
ericqu Mar 26, 2021
1ec1d71
added normalised weights support
ericqu Mar 29, 2021
fec1415
added reference
ericqu Mar 29, 2021
1582c18
Merge branch 'master' of https://github.com/ericqu/GLM.jl
ericqu Mar 29, 2021
0c3c50f
add docstring to wrapper
ericqu Mar 29, 2021
463aeb1
Update src/lm.jl
ericqu Mar 29, 2021
253c503
base, no intercept and collinear test amended
ericqu Apr 3, 2021
66d1b1e
Delete .gitignore
ericqu Apr 3, 2021
d5b8c0d
collinear v2
ericqu Apr 4, 2021
a5795d9
Merge branch 'master' of github.com:ericqu/GLM.jl
ericqu Apr 4, 2021
e7af456
no cook's distance when there are some weights.
ericqu Apr 4, 2021
c8dac94
Delete settings.json
ericqu Apr 5, 2021
e283b0e
cooksdistance definition
ericqu Apr 5, 2021
2364662
Merge branch 'master' of github.com:ericqu/GLM.jl
ericqu Apr 5, 2021
406f976
Update src/lm.jl
ericqu Apr 23, 2021
6a57a34
Create .gitignore
ericqu Apr 26, 2021
58f9f38
Update src/lm.jl
ericqu Apr 26, 2021
d503aa4
Update Project.toml
ericqu Apr 26, 2021
6d0fa18
Update Project.toml
ericqu Apr 26, 2021
b1ab4d3
Update lm.jl
ericqu Apr 26, 2021
2c3ac21
Update test/runtests.jl
ericqu Apr 27, 2021
e49f88f
Update src/lm.jl
ericqu Apr 27, 2021
b0fb18b
Update test/runtests.jl
ericqu Apr 27, 2021
0a4581f
add test values provenance.
ericqu Apr 27, 2021
86b6a35
Update src/lm.jl
ericqu Apr 27, 2021
fc70f79
Apply suggestions from code review
nalimilan Apr 28, 2021
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
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ Distributions = "0.16, 0.17, 0.18, 0.19, 0.20, 0.21, 0.22, 0.23, 0.24"
RDatasets = "0.5, 0.6, 0.7"
Reexport = "0.1, 0.2, 1.0"
SpecialFunctions = "0.6, 0.7, 0.8, 0.9, 0.10, 1"
StatsBase = "0.30, 0.31, 0.32, 0.33"
StatsBase = "0.33.5"
StatsFuns = "0.6, 0.7, 0.8, 0.9"
StatsModels = "0.6"
StatsModels = "0.6.22"
julia = "1"

[extras]
Expand Down
3 changes: 2 additions & 1 deletion src/GLM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module GLM
import SpecialFunctions: erfc, erfcinv, digamma, trigamma
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²
fitted, fit, fit!, model_response, response, modelmatrix, r2, r², adjr2, adjr²,
cooksdistance
ericqu marked this conversation as resolved.
Show resolved Hide resolved

export
# types
Expand Down
26 changes: 26 additions & 0 deletions src/lm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,29 @@ function confint(obj::LinearModel; level::Real=0.95)
hcat(coef(obj),coef(obj)) + stderror(obj) *
quantile(TDist(dof_residual(obj)), (1. - level)/2.) * [1. -1.]
end

"""
cooksdistance(obj::LinearModel)

Compute [Cook's distance](https://en.wikipedia.org/wiki/Cook%27s_distance)
for each observation in linear model `obj`, giving an estimate of the influence
of each data point.
Currently only implemented for LinearModel models without weights.
nalimilan marked this conversation as resolved.
Show resolved Hide resolved
"""
function StatsBase.cooksdistance(obj::LinearModel)
u = residuals(obj)
mse = dispersion(obj,true)
k = dof(obj)-1
d_res = dof_residual(obj)
X = modelmatrix(obj)
ericqu marked this conversation as resolved.
Show resolved Hide resolved
XtX = crossmodelmatrix(obj)
k == size(X,2) || throw(ArgumentError("Models with collinear terms are not currently supported."))
wts = obj.rr.wts
if isempty(wts)
hii = diag(X * inv(XtX) * X')
else
throw(ArgumentError("Weighted models are not currently supported."))
end
D = @. u^2 * (hii / (1 - hii)^2) / (k*mse)
return D
end
32 changes: 32 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,38 @@ linreg(x::AbstractVecOrMat, y::AbstractVector) = qr!(simplemm(x)) \ y
@test coef(lm3) == coef(lm4) ≈ [11, 1, 2, 3, 4]
end

@testset "Linear Model Cooks Distance - refers to PR #368 and issue #414" begin
nalimilan marked this conversation as resolved.
Show resolved Hide resolved
st_df = DataFrame(
Y=[6.4, 7.4, 10.4, 15.1, 12.3 , 11.4],
XA=[1.5, 6.5, 11.5, 19.9, 17.0, 15.5],
XB=[1.8, 7.8, 11.8, 20.5, 17.3, 15.8],
XC=[3., 13., 23., 39.8, 34., 31.],
# values from SAS proc reg
CooksD_base=[1.4068501943, 0.176809102, 0.0026655177, 1.0704009915, 0.0875726457, 0.1331183932],
CooksD_noint=[0.0076891801, 0.0302993877, 0.0410262965, 0.0294348488, 0.0691589296, 0.0273045538],
CooksD_multi=[1.7122291956, 18.983407026, 0.000118078, 0.8470797843, 0.0715921999, 0.1105843157 ],
ericqu marked this conversation as resolved.
Show resolved Hide resolved
nalimilan marked this conversation as resolved.
Show resolved Hide resolved
)

# linear regression
t_lm_base = lm(@formula(Y ~ XA), st_df)
@test isapprox(st_df.CooksD_base, cooksdistance(t_lm_base))

# linear regression, no intercept
t_lm_noint = lm(@formula(Y ~ XA +0), st_df)
@test isapprox(st_df.CooksD_noint, cooksdistance(t_lm_noint))

# linear regression, two collinear variables (Variance inflation factor ≊ 250)
t_lm_multi = lm(@formula(Y ~ XA + XB), st_df)
@test isapprox(st_df.CooksD_multi, cooksdistance(t_lm_multi))

# linear regression, two full collinear variables (XC = 2 XA) hence should get the same results as the original
# after pivoting
t_lm_colli = lm(@formula(Y ~ XA + XC), st_df, dropcollinear=true)
## Currently the test fails as the collinear variable is not droped from a ```modelmatrix(obj)``` call.
nalimilan marked this conversation as resolved.
Show resolved Hide resolved
@test_throws ArgumentError isapprox(st_df.CooksD_base, cooksdistance(t_lm_colli))

end
ericqu marked this conversation as resolved.
Show resolved Hide resolved

@testset "linear model with weights" begin
df = dataset("quantreg", "engel")
N = nrow(df)
Expand Down