-
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 pull request #63 from iuliadmtru/minor_corrections
Minor corrections
- Loading branch information
Showing
17 changed files
with
177 additions
and
514 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,80 +1,17 @@ | ||
function HorwitzThompson_HartleyRaoApproximation(y::AbstractVector, design, HT_total) | ||
# TODO: add docstrings | ||
function HorwitzThompson_HartleyRaoApproximation(y::AbstractVector, design::AbstractSurveyDesign, HT_total) | ||
# TODO: change function name | ||
# TODO: change variable name (gets mixed up with the constant pi) | ||
pi = design.data.probs | ||
hartley_rao_variance_formula = sum( (1 .- ((design.sampsize .- 1) ./ design.sampsize ) .* pi ) .* ( (y .* design.data.weights) .- (HT_total./design.sampsize) ).^2 ) | ||
return sqrt(hartley_rao_variance_formula) | ||
hartley_rao_var = sum((1 .- ((design.sampsize .- 1) ./ design.sampsize) .* pi) .* | ||
((y .* design.data.weights) .- (HT_total ./ design.sampsize)) .^ 2) | ||
return sqrt(hartley_rao_var) | ||
end | ||
|
||
function ht_calc(x::Symbol,design) | ||
total = wsum(Float32.( design.data[!,x] ), design.data.weights ) | ||
# se = hte_se( design.data[!,x], design ) # works | ||
se = HorwitzThompson_HartleyRaoApproximation( design.data[!,x], design , total) # Also works | ||
return DataFrame(total = total, se = se ) | ||
# TODO: add docstrings | ||
function ht_calc(x::Symbol, design) | ||
# TODO: change function name | ||
total = wsum(Float32.(design.data[!, x]), design.data.weights) | ||
se = HorwitzThompson_HartleyRaoApproximation(design.data[!, x], design, total) | ||
return DataFrame(total = total, se = se) | ||
end | ||
|
||
# function HansenHurwitzEstimator() | ||
|
||
# end | ||
|
||
# function HorwitzThompson_HartleyRaoApproximation(y::Vector{Int}, design, HT_total) | ||
# pi = design.data.probs | ||
# hartley_rao_variance_formula = sum( (1 .- ((design.sampsize .- 1) ./ design.sampsize ) .* pi ) .* ( (y./pi) .- (total./design.sampsize) ).^2 ) | ||
# return sqrt(hartley_rao_variance_formula) | ||
# end | ||
|
||
# # prob = 1 .- ((1 .- pi ) .^ (1/n)) | ||
# # For SRS only, in other design, we need to make a function | ||
# # pi_i = design.sampsize / design.popsize | ||
# # pi_j = design.sampsize / design.popsize | ||
|
||
# # Use Hartley Rao approximation to calc joint probability of selecting two units, for any sample | ||
# pi_ij = hartley_rao_approx() # design.sampsize .* (design.sampsize .- 1 ) ./ ( design.popsize .* (design.popsize .- 1) ) | ||
|
||
# first_sum = sum((1 .- pi) ./ (pi .^ 2) .* (y .^ 2)) | ||
# @show pi, pi_ij, first_sum | ||
# second_sum = 0 # sum( (pi_ij - ( pi_i * pi_j) ) / (pi_i * pi_j * pi_ij) .* y[i] .* y[j] ) | ||
|
||
# # pimat = zeros(length(pi),length(pi)) | ||
# # coefmat=zeros(length(pi),length(pi)) | ||
|
||
# for i in 1:length(pi) | ||
# for j in 1:length(pi) | ||
# if i != j | ||
# # pimat[i,j] = pi[i] + pi[j] - (1 - ((1 - prob[i] - prob[j] ) ^ n)) | ||
# coefmat = ((pi_ij - ( pi_i * pi_j) ) / (pi_i * pi_j * pi_ij)) | ||
# second_sum += coefmat * y[i] * y[j] | ||
# end | ||
# end | ||
# end | ||
|
||
# return sqrt(first_sum + second_sum) | ||
# # catch | ||
# # return -1 | ||
# # end | ||
# end | ||
|
||
# function hte_se(y::AbstractVector, design::SimpleRandomSample) | ||
# pi = design.data.probs | ||
|
||
# # For SRS only, in other design, we need to make a function | ||
# pi_i = design.sampsize / design.popsize | ||
# pi_j = design.sampsize / design.popsize | ||
# pi_ij = design.sampsize .* (design.sampsize .- 1 ) ./ ( design.popsize .* (design.popsize .- 1) ) | ||
|
||
# first_sum = sum((1 .- pi_i) ./ (pi_i .^ 2) .* (y .^ 2)) | ||
# @show pi_i , pi_j, pi_ij, first_sum | ||
|
||
# second_sum = 0 | ||
# coefmat = ((pi_ij - ( pi_i * pi_j) ) / (pi_i * pi_j * pi_ij)) | ||
# for i in 1:length(pi) | ||
# for j in 1:length(pi) | ||
# if i != j | ||
# second_sum += coefmat * y[i] * y[j] | ||
# end | ||
# end | ||
# end | ||
# @show second_sum | ||
# return sqrt(first_sum + second_sum) | ||
# 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"Helper function for nice printing" | ||
function makeshort(x) | ||
# write floats in short form | ||
if isa(x[1], Float64) | ||
x = round.(x, sigdigits=3) | ||
end | ||
# print short vectors or single values as they are, compress otherwise | ||
x = length(x) < 3 ? join(x, ", ") : join(x[1:3], ", ") * ", ..., " * string(last(x)) | ||
end | ||
|
||
"Prints title: content" | ||
function printinfo(io::IO, name::String, content::String; newline::Bool=true) | ||
printstyled(io, name, ": "; bold=true) | ||
newline ? println(io, content) : print(io, content) | ||
end | ||
|
||
"`show` method for printing information about a survey design" | ||
function Base.show(io::IO, ::MIME"text/plain", design::AbstractSurveyDesign) | ||
type = typeof(design) | ||
printstyled(io, "$type:\n"; bold=true) | ||
printstyled(io, "data: "; bold=true) | ||
println(io, size(design.data, 1), "x", size(design.data, 2), " DataFrame") | ||
printinfo(io, "weights", makeshort(design.data.weights)) | ||
printinfo(io, "probs", makeshort(design.data.probs)) | ||
printinfo(io, "fpc", makeshort(design.data.fpc)) | ||
printinfo(io, "popsize", makeshort(design.popsize)) | ||
printinfo(io, "sampsize", makeshort(design.sampsize)) | ||
printinfo(io, "sampfraction", makeshort(design.sampfraction)) | ||
printinfo(io, "ignorefpc", string(design.ignorefpc); newline=false) | ||
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
Oops, something went wrong.