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

Give preference to weights #315

Merged
merged 1 commit into from
Aug 17, 2023
Merged
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
9 changes: 5 additions & 4 deletions src/SurveyDesign.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,8 @@ struct SurveyDesign <: AbstractSurveyDesign
else
data[!, sampsize_labels] = fill(length(unique(data[!, cluster])), (nrow(data),))
end
if isa(popsize, Symbol)
weights_labels = :_weights
data[!, weights_labels] = data[!, popsize] ./ data[!, sampsize_labels]
elseif isa(weights, Symbol)

if isa(weights, Symbol)
if !(typeof(data[!, weights]) <: Vector{<:Real})
throw(
ArgumentError(
Expand All @@ -100,6 +98,9 @@ struct SurveyDesign <: AbstractSurveyDesign
popsize = :_popsize
data[!, popsize] = data[!, sampsize_labels] .* data[!, weights_labels]
end
elseif isa(popsize, Symbol)
weights_labels = :_weights
data[!, weights_labels] = data[!, popsize] ./ data[!, sampsize_labels]
else
# neither popsize nor weights given
weights_labels = :_weights
Expand Down
12 changes: 12 additions & 0 deletions test/SurveyDesign.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@
### Weights as non-numeric error
apisrs = copy(apisrs_original)
@test_throws ArgumentError SurveyDesign(apisrs, weights = :stype)

### popsize and weights as symbols

apisrs = copy(apisrs_original)
srs_pop_weights = SurveyDesign(apisrs, weights =:pw, popsize = :fpc)
@test srs_pop_weights.data[!, srs_pop_weights.weights][1] ≈ 30.97 atol = 1e-4
@test srs_pop_weights.data[!, srs_pop_weights.weights] == 1 ./ srs_pop_weights.data[!, srs_pop_weights.allprobs]
@test srs_pop_weights.data[!, srs_pop_weights.allprobs] ≈ srs_pop_weights.data[!, :derived_probs] atol = 1e-4
@test srs_pop_weights.data[!, srs_pop_weights.sampsize] ≈ srs_pop_weights.data[!, :derived_sampsize] atol = 1e-4
### Both ways should achieve same weights and allprobs!
@test srs_pop_weights.data[!, srs_pop_weights.weights] == srs_weights.data[!, srs_weights.weights]

end

@testset "SurveyDesign_strat" begin
Expand Down