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

Throw an error in predict with intervals for rank-deficient models #438

Merged
merged 2 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions src/lm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ function predict(mm::LinearModel, newx::AbstractMatrix;
end
if interval === nothing
return retmean
elseif mm.pp.chol isa CholeskyPivoted &&
mm.pp.chol.rank < size(mm.pp.chol, 2)
throw(ArgumentError("prediction intervals are currently not implemented " *
"when some independent variables have been dropped " *
"from model due to collinearity"))
nalimilan marked this conversation as resolved.
Show resolved Hide resolved
end
length(mm.rr.wts) == 0 || error("prediction with confidence intervals not yet implemented for weighted regression")
chol = cholesky!(mm.pp)
Expand Down
17 changes: 17 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,23 @@ end

# Deprecated argument value
@test predict(m1, x, interval=:confint) == p1

# Prediction intervals would give incorrect results when some variables
# have been dropped due to collinearity (#410)
x = [1.0 1.0 2.0
1.0 2.0 3.0
1.0 -1.0 0.0];
nalimilan marked this conversation as resolved.
Show resolved Hide resolved
y = [1.0, 3.0, -2.0]
m1 = lm(x, y)
m2 = lm(x[:, 1:2], y)

@test predict(m1) ≈ predict(m2)
@test_broken predict(m1, interval=:confidence) ≈
predict(m2, interval=:confidence)
@test_broken predict(m1, interval=:prediction) ≈
predict(m2, interval=:prediction)
@test_throws ArgumentError predict(m1, x, interval=:confidence)
@test_throws ArgumentError predict(m1, x, interval=:prediction)
end

@testset "GLM confidence intervals" begin
Expand Down