Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Raking #258

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Survey

[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://xKDR.github.io/Survey.jl/stable)
[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://xKDR.github.io/Survey.jl/dev)
![Build Status](https://github.com/xKDR/Survey.jl/actions/workflows/ci.yml/badge.svg)
![Build Status](https://github.com/xKDR/Survey.jl/actions/workflows/documentation.yml/badge.svg)
Expand Down Expand Up @@ -171,4 +172,4 @@ We gratefully acknowledge the JuliaLab at MIT for financial support for this pro

## References

[^1]: [Lumley, Thomas. Complex surveys: a guide to analysis using R. John Wiley & Sons, 2011.](https://books.google.co.in/books?hl=en&lr=&id=L96ludyhFBsC&oi=fnd&pg=PP12&dq=complex+surveys+lumley&ots=ie0y1lnzv1&sig=c4UHI3arjspMJ6OYzlX32E9rNRI#v=onepage&q=complex%20surveys%20lumley&f=false) Page 44
[^1]: [Lumley, Thomas. Complex surveys: a guide to analysis using R. John Wiley & Sons, 2011.](https://books.google.co.in/books?hl=en&lr=&id=L96ludyhFBsC&oi=fnd&pg=PP12&dq=complex+surveys+lumley&ots=ie0y1lnzv1&sig=c4UHI3arjspMJ6OYzlX32E9rNRI#v=onepage&q=complex%20surveys%20lumley&f=false) Page 44
3 changes: 2 additions & 1 deletion src/Survey.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ include("boxplot.jl")
include("show.jl")
include("ratio.jl")
include("by.jl")
include("raking.jl")

export load_data
export AbstractSurveyDesign, SurveyDesign, ReplicateDesign
Expand All @@ -35,5 +36,5 @@ export hist, sturges, freedman_diaconis
export boxplot
export bootweights
export ratio

export raking
end
29 changes: 29 additions & 0 deletions src/raking.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function raking(design::AbstractSurveyDesign, sample_margins_row::Symbol,
sample_margins_col::Symbol, population_margins_row::Vector, population_margins_col::Vector, control=Dict("maxit" => 10, "epsilon" => 1))
count = 0
epsilon = 10000
popsize = sum(population_margins_row)
sampsize = length(design.data[!, sample_margins_row]) * 0.5
gdf1 = combine(groupby(design.data, sample_margins_row),nrow)
@show gdf1
gdf2 = combine(groupby(design.data, sample_margins_col),nrow)
@show gdf2
b = gdf1[!,:nrow] .* (popsize ./ sampsize)
a = gdf2.nrow .* (popsize / sampsize)
coln = nrow(gdf1)
rown = nrow(gdf2)
colsum = zeros(rown)
df = DataFrame()
while count <= control["maxit"] || epsilon >= control["epsilon"]
for i in 1:coln-1
for j in 1:rown
colsum[j] = 0
df.i= design.data[!, sample_margins_col][i] / rown
colsum[j] = colsum + design.data.weights[j, i]
end
end
design.weights[j, ncol] = design.data[!, sample_margins_row] - colsum[j]
count = count + 1
end
return DataFrame(design.data.sample_weights)
end
11 changes: 11 additions & 0 deletions test/raking.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@testset "ratio.jl" begin
apiclus1 = load_data("apiclus1") # Load API dataset
apiclus1[!, :pw] = fill(757/15,(size(apiclus1,1),)) # Correct api mistake for pw column
apiclus1.schwide = apiclus1[!,"sch.wide"]
dclus1 = SurveyDesign(apiclus1; clusters = :dnum, weights = :pw)
poptypes = [4421,755,1018]
popschwide = [1072,5122]

raking(dclus1,:stype, :schwide,poptypes,popschwide, [100, 2] )
#@show dclus1.data[!,dclus1.weights]
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ include("hist.jl")
include("boxplot.jl")
include("ratio.jl")
include("show.jl")
include("raking.jl")