Skip to content

Commit

Permalink
Merge pull request #147 from sayantikaSSG/jackknife2
Browse files Browse the repository at this point in the history
Add Jackknife
  • Loading branch information
smishr authored Dec 16, 2022
2 parents 9ad29fc + ebc3c52 commit 23ebb67
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Survey.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using CategoricalArrays
include("SurveyDesign.jl")
include("mean.jl")
include("quantile.jl")
include("jackknife.jl")
include("total.jl")
include("load_data.jl")
include("hist.jl")
Expand All @@ -30,5 +31,6 @@ export mean, total, quantile
export plot
export hist, sturges, freedman_diaconis
export boxplot
export jkknife

end
16 changes: 16 additions & 0 deletions src/jackknife.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function jkknife(variable:: Symbol, design::OneStageClusterSample ,func:: Function; params =[])
statistic = func(design.data[!,variable],params...)
nh = length(unique(design.data[!,design.cluster]))
newv = []
gdf = groupby(design.data, design.cluster)
replicates = [filter(n -> n != i, 1:nh) for i in 1:nh]
for i in replicates
push!(newv,func(DataFrame(gdf[i])[!,variable]))
end
c = 0
for i in 1:nh
c = c+(newv[i]-statistic)^2
end
var = c*(nh-1)/nh
return DataFrame(Statistic = statistic, SE = sqrt(var))
end
10 changes: 10 additions & 0 deletions test/jackknife.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@testset "jackknife.jl" begin
apiclus1_original = load_data("apiclus1")
apiclus1_original[!, :pw] = fill(757/15,(size(apiclus1_original,1),)) # Correct api mistake for pw column
##############################
# one-stage cluster sample
apiclus1 = copy(apiclus1_original)
dclus1 = OneStageClusterSample(apiclus1, :dnum, :fpc)
@test jkknife(:api00,dclus1, mean).SE[1] 26.5997 atol = 1e-4
@test jkknife(:api00, dclus1, mean).Statistic[1] 644.1693 atol = 1e-4
end

0 comments on commit 23ebb67

Please sign in to comment.