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

WIP: Add GLM with design based standard errors. #221

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
GLM = "38e38edf-8417-5370-95a0-9cbb8c7f171a"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Missings = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand Down
4 changes: 4 additions & 0 deletions src/Survey.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ using AlgebraOfGraphics
using CategoricalArrays
using Random
using Missings
using GLM
import GLM: glm

include("SurveyDesign.jl")
include("bootstrap.jl")
Expand All @@ -26,6 +28,7 @@ include("show.jl")
include("ratio.jl")
include("by.jl")
include("jackknife.jl")
include("reg.jl")

export load_data
export AbstractSurveyDesign, SurveyDesign, ReplicateDesign
Expand All @@ -37,5 +40,6 @@ export boxplot
export bootweights
export ratio
export jackknifeweights, jackknife_variance
export @formula, glm

end
15 changes: 15 additions & 0 deletions src/reg.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
```julia
apisrs = load_data("apisrs")
srs = SurveyDesign(apisrs)
bsrs = bootweights(srs, replicates = 2000)
glm(@formula(api00~api99), bsrs, Normal())
```
"""
function glm(formula::FormulaTerm, design::ReplicateDesign, args...; kwargs...)
X = coef(glm(formula, design.data, args...; wts = design.data[!, design.weights], kwargs...))
Xt = [coef(glm(formula, design.data, args...; wts = design.data[!, "replicate_"*string(i)]), kwargs...) for i in 1:design.replicates]
Xt = hcat(Xt...)
SE = [std(Xt[i,:]) for i in 1:size(Xt)[1]]
DataFrame(Coefficients = X, SE = SE)
end
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
GLM = "38e38edf-8417-5370-95a0-9cbb8c7f171a"
6 changes: 6 additions & 0 deletions test/reg.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using GLM
@testset "GLM on simple random sample" begin
repdesign = bootweights(srs; replicates = 500)
@test glm(@formula(api00~api99), repdesign, Normal()).Coefficients ≈ coef(glm(@formula(api00~api99), repdesign.data, Normal()))
@test glm(@formula(api00~api99), repdesign, Normal()).SE ≈ stderror(glm(@formula(api00~api99), repdesign.data, Normal())) rtol = 1
end
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ include("hist.jl")
include("boxplot.jl")
include("ratio.jl")
include("show.jl")
include("jackknife.jl")
include("jackknife.jl")
include("reg.jl")