-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'AnalyticSolution' into two_stage
- Loading branch information
Showing
13 changed files
with
147 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
ratio(numerator, denominator, design) | ||
Estimate the ratio of the columns specified in numerator and denominator | ||
```jldoctest | ||
julia> using Survey; | ||
julia> apiclus1 = load_data("apiclus1"); | ||
julia> apiclus1[!, :pw] = fill(757/15,(size(apiclus1,1),)); # Correct api mistake for pw column | ||
julia> dclus1 = OneStageClusterSample(apiclus1, :dnum, :fpc); | ||
julia> ratio(:api00, :enroll, dclus1) | ||
1×2 DataFrame | ||
Row │ Statistic SE | ||
│ Float64 Float64 | ||
─────┼───────────────────── | ||
1 │ 1.17182 0.151242 | ||
``` | ||
""" | ||
function ratio(variable_num:: Symbol, variable_den:: Symbol, design::OneStageClusterSample) | ||
statistic = wsum(design.data[!,variable_num],design.data.weights)/wsum(design.data[!,variable_den],design.data.weights) | ||
nh = length(unique(design.data[!,design.cluster])) | ||
newv = [] | ||
gdf = groupby(design.data, design.cluster) | ||
replicates = [filter(n -> n != i, 1:nh) for i in 1:nh] | ||
for i in replicates | ||
df = DataFrame(gdf[i]) | ||
push!(newv, wsum(df[!,variable_num],df[!,:weights])/wsum(df[!,variable_den],df[!,:weights])) | ||
end | ||
c = 0 | ||
for i in 1:nh | ||
c = c+(newv[i]-statistic)^2 | ||
end | ||
var = c*(nh-1)/nh | ||
return DataFrame(Statistic = statistic, SE = sqrt(var)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
@testset "ratio.jl" begin | ||
apiclus1_original = load_data("apiclus1") | ||
apiclus1_original[!, :pw] = fill(757/15,(size(apiclus1_original,1),)) # Correct api mistake for pw column | ||
############################## | ||
# one-stage cluster sample | ||
apiclus1 = copy(apiclus1_original) | ||
dclus1 = OneStageClusterSample(apiclus1, :dnum, :fpc) | ||
@test ratio(:api00, :enroll, dclus1).SE[1] ≈ 0.151242 atol = 1e-4 | ||
@test ratio(:api00, :enroll, dclus1).Statistic[1] ≈ 1.17182 atol = 1e-4 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters