Skip to content

Commit

Permalink
Adding tests for the new ReplicateDesign constructors.
Browse files Browse the repository at this point in the history
  • Loading branch information
codetalker7 committed Feb 28, 2023
1 parent a88c51b commit a7c6f80
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
30 changes: 29 additions & 1 deletion test/SurveyDesign.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ end
dyrbs = SurveyDesign(yrbs; clusters = :psu, strata = :stratum, weights = :weight)
end

@testset "ReplicateDesign_constructor" begin
@testset "ReplicateDesign_direct" begin
for (sample, sample_direct) in [(bsrs, bsrs_direct), (bstrat, bstrat_direct), (dclus1_boot, dclus1_boot_direct)]
@test isequal(sample.data, sample_direct.data)
@test isequal(sample.popsize, sample_direct.popsize)
Expand All @@ -273,3 +273,31 @@ end
@test isequal(sample.replicate_weights, sample_direct.replicate_weights)
end
end

@testset "ReplicateDesign_unitrange" begin
for (sample, sample_unitrange) in [(bsrs, bsrs_unitrange), (bstrat, bstrat_unitrange), (dclus1_boot, dclus1_boot_unitrange)]
@test isequal(sample.data, sample_unitrange.data)
@test isequal(sample.popsize, sample_unitrange.popsize)
@test isequal(sample.sampsize, sample_unitrange.sampsize)
@test isequal(sample.strata, sample_unitrange.strata)
@test isequal(sample.weights, sample_unitrange.weights)
@test isequal(sample.allprobs, sample_unitrange.allprobs)
@test isequal(sample.pps, sample_unitrange.pps)
@test isequal(sample.replicates, sample_unitrange.replicates)
@test isequal(sample.replicate_weights, sample_unitrange.replicate_weights)
end
end

@testset "ReplicateDesign_regex" begin
for (sample, sample_regex) in [(bsrs, bsrs_regex), (bstrat, bstrat_regex), (dclus1_boot, dclus1_boot_regex)]
@test isequal(sample.data, sample_regex.data)
@test isequal(sample.popsize, sample_regex.popsize)
@test isequal(sample.sampsize, sample_regex.sampsize)
@test isequal(sample.strata, sample_regex.strata)
@test isequal(sample.weights, sample_regex.weights)
@test isequal(sample.allprobs, sample_regex.allprobs)
@test isequal(sample.pps, sample_regex.pps)
@test isequal(sample.replicates, sample_regex.replicates)
@test isequal(sample.replicate_weights, sample_regex.replicate_weights)
end
end
19 changes: 15 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,37 @@ using CategoricalArrays

const STAT_TOL = 1e-5
const SE_TOL = 1e-1
const REPLICATE_WEIGHTS = [Symbol("replicate_"*string(i)) for i in 1:4000]
TOTAL_REPLICATES = 4000
REPLICATES_VECTOR = [Symbol("replicate_"*string(i)) for i in 1:TOTAL_REPLICATES]
REPLICATES_REGEX = r"r*_\d"

# Simple random sample
apisrs = load_data("apisrs") # Load API dataset
srs = SurveyDesign(apisrs, weights = :pw)
unitrange = UnitRange((length(names(apisrs)) + 1):(TOTAL_REPLICATES + length(names(apisrs))))
bsrs = srs |> bootweights # Create replicate design
bsrs_direct = ReplicateDesign(bsrs.data, REPLICATE_WEIGHTS, weights = :pw) # using ReplicateDesign constructor
bsrs_direct = ReplicateDesign(bsrs.data, REPLICATES_VECTOR, weights = :pw) # using ReplicateDesign constructor
bsrs_unitrange = ReplicateDesign(bsrs.data, unitrange, weights = :pw) # using ReplicateDesign constructor
bsrs_regex = ReplicateDesign(bsrs.data, REPLICATES_REGEX, weights = :pw) # using ReplicateDesign constructor

# Stratified sample
apistrat = load_data("apistrat") # Load API dataset
dstrat = SurveyDesign(apistrat, strata = :stype, weights = :pw) # Create SurveyDesign
unitrange = UnitRange((length(names(apistrat)) + 1):(TOTAL_REPLICATES + length(names(apistrat))))
bstrat = dstrat |> bootweights # Create replicate design
bstrat_direct = ReplicateDesign(bstrat.data, REPLICATE_WEIGHTS, strata=:stype, weights=:pw) # using ReplicateDesign constructor
bstrat_direct = ReplicateDesign(bstrat.data, REPLICATES_VECTOR, strata=:stype, weights=:pw) # using ReplicateDesign constructor
bstrat_unitrange = ReplicateDesign(bstrat.data, unitrange, strata=:stype, weights=:pw) # using ReplicateDesign constructor
bstrat_regex = ReplicateDesign(bstrat.data, REPLICATES_REGEX, strata=:stype, weights=:pw) # using ReplicateDesign constructor

# One-stage cluster sample
apiclus1 = load_data("apiclus1") # Load API dataset
apiclus1[!, :pw] = fill(757 / 15, (size(apiclus1, 1),)) # Correct api mistake for pw column
dclus1 = SurveyDesign(apiclus1; clusters = :dnum, weights = :pw) # Create SurveyDesign
unitrange = UnitRange((length(names(apiclus1)) + 1):(TOTAL_REPLICATES + length(names(apiclus1))))
dclus1_boot = dclus1 |> bootweights # Create replicate design
dclus1_boot_direct = ReplicateDesign(dclus1_boot.data, REPLICATE_WEIGHTS, clusters=:dnum, weights=:pw) # using ReplicateDesign constructor
dclus1_boot_direct = ReplicateDesign(dclus1_boot.data, REPLICATES_VECTOR, clusters=:dnum, weights=:pw) # using ReplicateDesign constructor
dclus1_boot_unitrange = ReplicateDesign(dclus1_boot.data, unitrange, clusters=:dnum, weights=:pw) # using ReplicateDesign constructor
dclus1_boot_regex = ReplicateDesign(dclus1_boot.data, REPLICATES_REGEX, clusters=:dnum, weights=:pw) # using ReplicateDesign constructor

@testset "Survey.jl" begin
@test size(load_data("apiclus1")) == (183, 40)
Expand Down

0 comments on commit a7c6f80

Please sign in to comment.