From b5b9b095b51cd668ea13367c72b538e5873dafb5 Mon Sep 17 00:00:00 2001 From: ayushpatnaikgit Date: Thu, 17 Aug 2023 23:09:55 +1000 Subject: [PATCH] Give preference to weights --- src/SurveyDesign.jl | 9 +++++---- test/SurveyDesign.jl | 12 ++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/SurveyDesign.jl b/src/SurveyDesign.jl index 5b7df85a..ac699c05 100644 --- a/src/SurveyDesign.jl +++ b/src/SurveyDesign.jl @@ -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( @@ -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 diff --git a/test/SurveyDesign.jl b/test/SurveyDesign.jl index 5084c1e4..4cb65b79 100644 --- a/test/SurveyDesign.jl +++ b/test/SurveyDesign.jl @@ -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