Skip to content

Commit

Permalink
Give preference to weights
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushpatnaikgit committed Aug 17, 2023
1 parent 735613e commit b5b9b09
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
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

0 comments on commit b5b9b09

Please sign in to comment.