Skip to content

Commit

Permalink
Merge pull request #159 from ayushpatnaikgit/main
Browse files Browse the repository at this point in the history
Add doctrings in total.
  • Loading branch information
smishr authored Dec 30, 2022
2 parents 6940cb8 + ff0aa72 commit 75710ca
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/SurveyDesign.jl
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ julia> apiclus1[!, :pw] = fill(757/15,(size(apiclus1,1),)); # Correct api mistak
julia> dclus1 = OneStageClusterSample(apiclus1, :dnum; weights=:pw)
OneStageClusterSample:
data: 183x45 DataFrame
data: 183x46 DataFrame
cluster: dnum
design.data[!,design.cluster]: 637, 637, 637, ..., 448
popsize: popsize
Expand Down Expand Up @@ -400,7 +400,7 @@ struct OneStageClusterSample <: AbstractSurveyDesign
data_groupedby_cluster = groupby(data, cluster)
data[!, sampsize_labels] = fill(size(data_groupedby_cluster, 1),(nrow(data),))
weights = :weights
data[!, weights] = data[!, popsize] ./ data[!, sampsize_labels]
data[!, :weights] = data[!, popsize] ./ data[!, sampsize_labels]
data[!, :probs] = 1 ./ data[!, weights] # Many formulae are easily defined in terms of sampling probabilties
data[!, :allprobs] = data[!, :probs] # In one-stage cluster sample, allprobs is just probs, no multiplication needed
data[!, :strata] = ones(nrow(data))
Expand All @@ -414,21 +414,17 @@ struct OneStageClusterSample <: AbstractSurveyDesign
if !(typeof(data[!, weights]) <: Vector{<:Real})
error(string("given weights column ", weights , " is not of numeric type"))
end
if !all(w -> w == first(data[!, weights]), data[!, weights])
error("weights must be same for all observations for OneStageClusterSample")
end
# For one-stage sample only one sampsize vector
sampsize_labels = :sampsize
data_groupedby_cluster = groupby(data, cluster)
data[!, sampsize_labels] = fill(size(data_groupedby_cluster, 1),(nrow(data),))
popsize = :popsize
data[!, popsize] = data[!, weights] .* data[!, sampsize_labels]
data[!, :probs] = 1 ./ data[!, weights] # Many formulae are easily defined in terms of sampling probabilties
data[!, :weights] = data[!, weights]
data[!, :allprobs] = data[!, :probs] # In one-stage cluster sample, allprobs is just probs, no multiplication needed
data[!, :strata] = ones(nrow(data))
pps = false
has_strata = false
new(data, cluster, popsize, sampsize_labels, weights, pps, has_strata)
end
end

end
16 changes: 16 additions & 0 deletions src/total.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ function total(x::Vector{Symbol}, design::AbstractSurveyDesign)
return df
end

"""
```jldoctest
julia> using Survey
julia> apiclus1 = load_data("apiclus1");
julia> dclus1 = OneStageClusterSample(apiclus1, :dnum, :fpc);
julia> total(:api00, dclus1)
1×2 DataFrame
Row │ total SE
│ Float64 Float64
─────┼──────────────────────
1 │ 5.94916e6 1.33948e6
```
"""
function total(x::Symbol, design::OneStageClusterSample)
gdf = groupby(design.data, design.cluster)
ŷₜ = combine(gdf, x => sum => :sum).sum
Expand Down

0 comments on commit 75710ca

Please sign in to comment.