-
Notifications
You must be signed in to change notification settings - Fork 19
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: Abstract type for summary statistics (Mean) #277
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
""" | ||
SummaryStats | ||
|
||
Supertype for every type of summary statistic. | ||
|
||
""" | ||
|
||
abstract type SummaryStats end | ||
|
||
""" | ||
Calculate confidence intervals for given estimates. | ||
|
||
""" | ||
|
||
function confint(estimate::Float64, std_dev::Float64; type::String="normal", alpha::Float64=0.05) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. EDIT: After reviewing I think you can just copy the entire of
|
||
# Parse type of CI & calculate critical value | ||
if type == "normal" | ||
critical_value = quantile(Normal(), 1 - alpha / 2) | ||
end | ||
# Calculate upper and lower estimates | ||
lower = estimate .- critical_value .* std_dev | ||
upper = estimate .+ critical_value .* std_dev | ||
return (lower=lower, upper=upper) | ||
end | ||
|
||
""" | ||
|
||
Mean <: SummaryStats | ||
|
||
Population mean estimate for a column of a SurveyDesign object. | ||
|
||
# Arguments: | ||
- `x::Symbol`: the column to compute population mean statistics for. | ||
- `design::SurveyDesign`: a SurveyDesign object. | ||
|
||
```jldoctest | ||
julia> using Survey; | ||
julia> apiclus1 = load_data("apiclus1"); | ||
julia> dclus1 = SurveyDesign(apiclus1; clusters=:dnum, strata=:stype, weights=:pw); | ||
|
||
julia> api00_mean = Mean(:api00, dclus1) | ||
(x = :api00, estimate = 644.1693989071047, std_dev = 105.74886663549471) | ||
|
||
julia> api00_mean.estimate | ||
644.1693989071047 | ||
|
||
julia> api00_mean.std_dev | ||
105.74886663549471 | ||
|
||
julia> api00_mean.CI | ||
(lower = 436.90542889560527, upper = 851.4333689186041) | ||
|
||
``` | ||
|
||
# TODO add : | ||
#std_err = stderror(design, x) # standard error of the estimate of the mean | ||
|
||
""" | ||
|
||
struct Mean <: SummaryStats | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went with the idea that it's easier to have all summary statistics as a struct in a single file ( The reason for having Also I agree with having Thoughts ? |
||
x::Symbol | ||
design::SurveyDesign | ||
end | ||
|
||
function Mean(x::Symbol, design::SurveyDesign) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
column = design.data[!, x] | ||
estimate = mean(column, weights(design.data[!, design.weights])) | ||
std_dev = std(column) | ||
CI = confint(estimate, std_dev) | ||
return (x=column, estimate=estimate, std_dev=std_dev, CI=CI) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes this abstract type looks good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Am wondering if
SummaryStats
should be anAbstractFunction
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't know about this, will look into it !