From 1203e76b9fc5c515557789e881392525d92ee86f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Bayo=C3=A1n=20Santiago=20Calder=C3=B3n=20=28?= =?UTF-8?q?=E5=8F=B2=E5=BF=97=E9=BC=8E=29?= Date: Sun, 18 Nov 2018 15:33:01 -0400 Subject: [PATCH] Using dof approach (#265) --- src/linpred.jl | 2 +- src/lm.jl | 5 ++--- test/runtests.jl | 13 +++++++++---- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/linpred.jl b/src/linpred.jl index e91909c2..d8d0b79b 100644 --- a/src/linpred.jl +++ b/src/linpred.jl @@ -253,4 +253,4 @@ end coef(x::LinPred) = x.beta0 coef(obj::LinPredModel) = coef(obj.pp) -dof_residual(obj::LinPredModel) = nobs(obj) - length(coef(obj)) +dof_residual(obj::LinPredModel) = nobs(obj) - dof(obj) + 1 diff --git a/src/lm.jl b/src/lm.jl index 78309098..8925520b 100644 --- a/src/lm.jl +++ b/src/lm.jl @@ -147,6 +147,8 @@ lm(X, y, allowrankdeficient::Bool=false) = fit(LinearModel, X, y, allowrankdefic dof(x::LinearModel) = length(coef(x)) + 1 +dof(obj::LinearModel{<:LmResp,<:DensePredChol{<:Real,<:CholeskyPivoted}}) = obj.pp.chol.rank + 1 + """ deviance(obj::LinearModel) @@ -219,9 +221,6 @@ function predict(mm::LinearModel, newx::AbstractMatrix; hcat(retmean, retmean .+ interval, retmean .- interval) end -dof_residual(obj::LinearModel{<:Any,<:DensePredChol{<:Any,<:CholeskyPivoted}}) = - nobs(obj) - obj.pp.chol.rank - function confint(obj::LinearModel, level::Real) hcat(coef(obj),coef(obj)) + stderror(obj) * quantile(TDist(dof_residual(obj)), (1. - level)/2.) * [1. -1.] diff --git a/test/runtests.jl b/test/runtests.jl index 22e5d948..211f4f7d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -574,9 +574,14 @@ end end @testset "Issue #263" begin - data = DataFrame(y = rand(100), x = rand(100)) - data.x2 = data.x - model1 = lm(@formula(y ~ x), data) - model2 = lm(@formula(y ~ x + x2), data, true) + data = dataset("datasets", "iris") + data.SepalWidth2 = data.SepalWidth + model1 = lm(@formula(SepalLength ~ SepalWidth), data) + model2 = lm(@formula(SepalLength ~ SepalWidth + SepalWidth2), data, true) + model3 = lm(@formula(SepalLength ~ 0 + SepalWidth), data) + model4 = lm(@formula(SepalLength ~ 0 + SepalWidth + SepalWidth2), data, true) + @test dof(model1) == dof(model2) + @test dof(model3) == dof(model4) @test dof_residual(model1) == dof_residual(model2) + @test dof_residual(model3) == dof_residual(model4) end