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

Speed-up in bootweights by using a function barrier #290

Merged
merged 2 commits into from
Mar 26, 2023
Merged
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
42 changes: 26 additions & 16 deletions src/bootstrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,18 @@ replicates: 1000
function bootweights(design::SurveyDesign; replicates = 4000, rng = MersenneTwister(1234))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kind of unrelated, but why is the function named bootweights? Could it be refactored to ReplicateDesign(design::SurveyDesign, ...)? This would make the #292 approach a lot easier to implement. If it's an R function you want to wrap, there is always const bootweights = ReplicateDesign or similar.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are several ways of making replicate weights. This is the first one that we have implemented.

We may choose your approach, we are thinking about the design in #292 and a couple of other issues.

stratified = groupby(design.data, design.strata)
H = length(keys(stratified))
substrata_dfs = DataFrame[]
substrata_dfs = Vector{DataFrame}(undef, H)
for h = 1:H
substrata = DataFrame(stratified[h])
cluster_sorted = sort(substrata, design.cluster)
psus = unique(cluster_sorted[!, design.cluster])
npsus = [(count(==(i), cluster_sorted[!, design.cluster])) for i in psus]
nh = length(psus)
cluster_sorted_designcluster = cluster_sorted[!, design.cluster]
cluster_weights = cluster_sorted[!, design.weights]
for replicate = 1:replicates
randinds = rand(rng, 1:(nh), (nh - 1))
cluster_sorted[!, "replicate_"*string(replicate)] =
vcat(
[
fill((count(==(i), randinds)) * (nh / (nh - 1)), npsus[i]) for
i = 1:nh
]...,
) .* cluster_weights
end
push!(substrata_dfs, cluster_sorted)
# Perform the inner loop in a type-stable function to improve runtime.
_bootweights_cluster_sorted!(cluster_sorted, cluster_weights,
jishnub marked this conversation as resolved.
Show resolved Hide resolved
cluster_sorted_designcluster, replicates, rng)
substrata_dfs[h] = cluster_sorted
end
df = vcat(substrata_dfs...)
df = reduce(vcat, substrata_dfs)
return ReplicateDesign(
df,
design.cluster,
Expand All @@ -57,3 +48,22 @@ function bootweights(design::SurveyDesign; replicates = 4000, rng = MersenneTwis
replicates,
)
end

function _bootweights_cluster_sorted!(cluster_sorted,
cluster_weights, cluster_sorted_designcluster, replicates, rng)

psus = unique(cluster_sorted_designcluster)
npsus = [count(==(i), cluster_sorted_designcluster) for i in psus]
nh = length(psus)
for replicate = 1:replicates
randinds = rand(rng, 1:(nh), (nh - 1))
cluster_sorted[!, "replicate_"*string(replicate)] =
reduce(vcat,
[
fill((count(==(i), randinds)) * (nh / (nh - 1)), npsus[i]) for
i = 1:nh
]
) .* cluster_weights
end
cluster_sorted
end