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

VIF #26

Merged
merged 16 commits into from
Sep 6, 2023
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StatsAPI"
uuid = "82ae8749-77ed-4fe6-ae5f-f523153014b0"
authors = ["Milan Bouchet-Valat <[email protected]"]
version = "1.6.0"
version = "1.7.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
1 change: 0 additions & 1 deletion src/StatsAPI.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module StatsAPI

using LinearAlgebra

include("statisticalmodel.jl")
palday marked this conversation as resolved.
Show resolved Hide resolved
include("regressionmodel.jl")

Expand Down
47 changes: 47 additions & 0 deletions src/regressionmodel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,50 @@ function linearpredictor end
In-place version of [`linearpredictor`](@ref), storing the result in `storage`.
"""
function linearpredictor! end

"""
vif(m::RegressionModel)

Compute the variance inflation factor (VIF).

The [VIF](https://en.wikipedia.org/wiki/Variance_inflation_factor) measures
the increase in the variance of a parameter's estimate in a model with multiple parameters relative to
the variance of a parameter's estimate in a model containing only that parameter.

See also [`gvif`](@ref).

!!! warning
This method will fail if there is (numerically) perfect multicollinearity,
i.e. rank deficiency. In that case though, the VIF
is not particularly informative anyway.
"""
function vif end
# This generic function is owned by StatsModels.jl, which is the sole provider
# of the default definition.

"""
gvif(m::RegressionModel; scale=false)

Compute the generalized variance inflation factor (GVIF).

If `scale=true`, then each GVIF is scaled by the degrees of freedom
for (number of coefficients associated with) the predictor: ``GVIF^(1 / (2*df))``
palday marked this conversation as resolved.
Show resolved Hide resolved

The [generalized variance inflation factor (VIF)](https://doi.org/10.2307/2290467)
palday marked this conversation as resolved.
Show resolved Hide resolved
measures the increase in the variance of a (group of) parameter's estimate in a model
with multiple parameters relative to the variance of a parameter's estimate in a
model containing only that parameter. For continuous, numerical predictors, the GVIF
is the same as the VIF, but for categorical predictors, the GVIF provides a single
number for the entire group of contrast-coded coefficients associated with a categorical
predictor.

See also [`vif`](@ref).

## References

Fox, J., & Monette, G. (1992). Generalized Collinearity Diagnostics.
Journal of the American Statistical Association, 87(417), 178. doi:10.2307/2290467
"""
function gvif end
# This generic function is owned by StatsModels.jl, which is the sole provider
# of the default definition.
18 changes: 16 additions & 2 deletions test/regressionmodel.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
module TestRegressionModel

using Test, LinearAlgebra, StatsAPI
using StatsAPI: RegressionModel, crossmodelmatrix
using StatsAPI: RegressionModel, crossmodelmatrix, vif

struct MyRegressionModel <: RegressionModel
end

StatsAPI.modelmatrix(::MyRegressionModel) = [1 2; 3 4]
StatsAPI.vcov(::MyRegressionModel) = [1 0; 0 1]

struct MyRegressionModel2 <: RegressionModel
end

StatsAPI.modelmatrix(::MyRegressionModel2) = [1 2; 1 2]
StatsAPI.vcov(::MyRegressionModel2) = [1 0; 0 1]

struct MyRegressionModel3 <: RegressionModel
end

StatsAPI.modelmatrix(::MyRegressionModel3) = [1 2 3; 1 2 3]
StatsAPI.vcov(::MyRegressionModel3) = [1 0 0; 0 1 0; 0 0 1]


@testset "TestRegressionModel" begin
m = MyRegressionModel()
Expand All @@ -15,4 +29,4 @@ StatsAPI.modelmatrix(::MyRegressionModel) = [1 2; 3 4]
@test crossmodelmatrix(m) isa Symmetric
end

end # module TestRegressionModel
end # module TestRegressionModel
3 changes: 2 additions & 1 deletion test/statisticalmodel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ StatsAPI.nobs(::MyStatisticalModel) = 100
@test adjr2 === adjr²
end

end # module TestStatisticalModel
end # module TestStatisticalModel

Loading