Skip to content

Commit

Permalink
Using dof approach (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nosferican authored and andreasnoack committed Nov 18, 2018
1 parent d19c2f8 commit 1203e76
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/linpred.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 2 additions & 3 deletions src/lm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.]
Expand Down
13 changes: 9 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1203e76

Please sign in to comment.