-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,27 +24,30 @@ abstract type AbstractSurveyDesign end | |
SimpleRandomSample <: AbstractSurveyDesign | ||
Survey design sampled by simple random sampling. | ||
The population size is equal to the sample size unless `popsize` is explicitly provided. | ||
""" | ||
struct SimpleRandomSample <: AbstractSurveyDesign | ||
data::AbstractDataFrame | ||
sampsize::UInt | ||
popsize::Union{UInt,Nothing} | ||
sampsize::Union{Nothing,Unsigned} | ||
popsize::Union{Nothing,Unsigned} | ||
sampfraction::Real | ||
fpc::Real | ||
ignorefpc::Bool | ||
function SimpleRandomSample(data::AbstractDataFrame; | ||
popsize = nothing, | ||
sampsize = nrow(data), | ||
weights = ones(nrow(data)), # Check the defaults | ||
probs = nothing, | ||
ignorefpc = true | ||
) | ||
popsize=nothing, | ||
sampsize=nrow(data), | ||
weights=nothing, # Check the defaults | ||
probs=nothing, | ||
ignorefpc=false | ||
) | ||
# Functionality: weights arg can be passed as Symbol instead of vector | ||
if isa(weights, Symbol) | ||
weights = data[!, weights] | ||
end | ||
# set population size if it is not given; `weights` and `sampsize` must be given | ||
# Set population size if it is not given; `weights` and `sampsize` must be given | ||
if ignorefpc # && (isnothing(popsize) || isnothing(weights) || isnothing(probs)) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
smishr
Author
Contributor
|
||
@warn "Assuming equal weights" | ||
weights = ones(nrow(data)) | ||
end | ||
if isnothing(popsize) | ||
if typeof(weights) <: Vector{<:Real} | ||
if !all(y -> y == first(weights), weights) # SRS by definition is equi-weighted | ||
|
@@ -70,11 +73,14 @@ struct SimpleRandomSample <: AbstractSurveyDesign | |
# If all weights are equal then estimate | ||
equal_weight = first(weights) | ||
popsize = round(sampsize .* equal_weight) |> UInt | ||
if sampsize > popsize | ||
error("You have either given wrong or not enough keyword args. sampsize cannot be greate than popsize. Check given inputs. eg if weights given then popsize must be given (for now)") | ||
end | ||
elseif typeof(popsize) <: Vector{<:Real} | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
smishr
Author
Contributor
|
||
if !all(y -> y == first(popsize), popsize) # SRS by definition is equi-weighted | ||
error("Simple Random Sample must be equi-weighted. Different sampling weights detected in vectors") | ||
end | ||
weights = popsize ./ sampsize | ||
weights = popsize ./ sampsize # This line is ratio estimator, we may need to change it when doing compley surveys | ||
popsize = first(popsize) |> UInt | ||
else | ||
error("If popsize not given then either sampling weights or sampling probabilities must be given") | ||
|
@@ -97,6 +103,10 @@ struct SimpleRandomSample <: AbstractSurveyDesign | |
end | ||
new(data, sampsize, popsize, sampfraction, fpc, ignorefpc) | ||
end | ||
function SimpleRandomSample(data::AbstractDataFrame) | ||
This comment has been minimized.
Sorry, something went wrong.
iuliadmtru
Contributor
|
||
ignorefpc = true | ||
return SimpleRandomSample(data; popsize=nothing,sampsize=nrow(data), weights=nothing, probs=nothing, ignorefpc=ignorefpc) | ||
end | ||
end | ||
|
||
# `show` method for printing information about a `SimpleRandomSample` after construction | ||
|
@@ -141,7 +151,7 @@ struct StratifiedSample <: AbstractSurveyDesign | |
data[!, :sampsize] = repeat([nrow(data)], nrow(data)) | ||
data[!, :strata] = strata | ||
|
||
new(data) | ||
new(data,sampsize,popsize,sampfraction,fpc,nofpc) | ||
end | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
@testset "svyboxplot.jl" begin | ||
# StratifiedSample | ||
apisrs = load_data("apisrs") | ||
srs = SimpleRandomSample(apisrs) | ||
srs = SimpleRandomSample(apisrs, popsize = apisrs.fpc) | ||
# srs = SimpleRandomSample(apisrs) | ||
bp = svyboxplot(srs, :stype, :enroll; weights = :pw) | ||
|
||
@test bp.grid[1].entries[1].positional[2] == srs.data[!, :enroll] | ||
@test bp.grid[1].entries[1].named[:weights] == srs.data[!, :pw] | ||
|
||
# StratifiedSample | ||
apistrat = load_data("apistrat") | ||
strat = StratifiedSample(apistrat, apistrat.stype) | ||
bp = svyboxplot(strat, :stype, :enroll; weights = :pw) | ||
# # StratifiedSample | ||
# apistrat = load_data("apistrat") | ||
# strat = StratifiedSample(apistrat, apistrat.stype) | ||
# bp = svyboxplot(strat, :stype, :enroll; weights = :pw) | ||
|
||
@test bp.grid[1].entries[1].positional[2] == strat.data[!, :enroll] | ||
@test bp.grid[1].entries[1].named[:weights] == strat.data[!, :pw] | ||
# @test bp.grid[1].entries[1].positional[2] == strat.data[!, :enroll] | ||
# @test bp.grid[1].entries[1].named[:weights] == strat.data[!, :pw] | ||
end |
Is this check necessary? The weights are set after this check anyway, regardless of
ignorefpc
. The warning is also not necessary since if the weights are not equal the constructor gives an error (in later versions).