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

Abstract types for summary statistics function for code reuse #275

Open
smishr opened this issue Mar 1, 2023 · 4 comments
Open

Abstract types for summary statistics function for code reuse #275

smishr opened this issue Mar 1, 2023 · 4 comments
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed high priority High priority tasks, eg. relating to release

Comments

@smishr
Copy link
Contributor

smishr commented Mar 1, 2023

Right now we have four summary stats functions (mean,total,quantile,ratio).

Adding a feature, say confidence intervals ( #274 #184 ) to summary stats currently requires multiple dispatch on each of the four functions.

If they shared a common abstract type, then less code will need to be written.

eg. confint(x,design,::SummaryStat) and confint(Vector{x},design,::SummaryStat) could work for all four methods?

@ayushpatnaikgit @codetalker7

@smishr smishr added enhancement New feature or request help wanted Extra attention is needed high priority High priority tasks, eg. relating to release labels Mar 1, 2023
@asinghvi17
Copy link
Member

Just FYI you can also dispatch on functions, so if you don't mind repeating the mean calculation, you could simply dispatch confint as confint(x, design, ::typeof(mean)) etc.

@ayushpatnaikgit
Copy link
Member

Based on #277

We need types for estimates.

Currently, mean(x::Symbol, design::ReplicateDesign) returns a DataFrame. Similarly, total(x::Symbol, design::ReplicateDesign) also returns a DataFrame. If we want a function, such as CI that returns the confidence interval, our framework forces us to all CI separately for both function.

If we have

abstract type AbstractEstimate end
struct Estimate{statistic}
   statistic_type::statistic
   value::Number
   SE::Number
end
Base.@kwdef struct Mean 
    name = "mean"
end

Base.@kwdef struct Total 
    name = "mean"
end

Base.@kwdef struct Quantile 
    name = "Quantile"
    p = 0.5
end

This allows us to define

    function CI(x::Estimate)  
    ...
end

function Base.show(IO, x::Estimate) 
    df = DataFrame(x.statistic_type.name = x.value, SE = x.SE) 
    print(df)
end

And there can be functions specific to the estimators, like

function some_function(x::Estimate{Quantile})
    return x.statistic_type.p
end

@ayushpatnaikgit
Copy link
Member

I will implement this, and later we can decide on something better.

@ayushpatnaikgit
Copy link
Member

Implementing the following:

abstract type AbstractEstimate end

struct Estimate{statistic} <: AbstractEstimate
    statistic_type::AbstractStatistic
    estimate::Number
end

struct EstimateStdErr{statistic} <: AbstractEstimate
    statistic_type::AbstractStatistic
    estimate::Estimate
    stderr::Number
end

struct EstimateStdErrCI{statistic} <: AbstractEstimate
    statistic_type::AbstractStatistic
    estimate_stderr::EstimateStdErr
    CI::Tuple
end

struct Estimates{statistic} <: AbstractEstimate
    statistic_type::AbstractStatistic
    estimates::Vector{Estimate}
end 

struct EstimatesStdErrs{statistic} <: AbstractEstimate
    statistic_type::AbstractStatistic
    estimates_stderrs::Vector{EstimateStdErr}
end

struct EstimatesStdErrsCIs{statistic} <: AbstractEstimate
    statistic_type::AbstractStatistic
    estimates_stderrs_cis::Vector{EstimateStdErrCI}
end

abstract type AbstractStatistic end

struct Mean <: AbstractStatistic
    name = "Mean"
end

struct Total <: AbstractStatistic
    name = "Total"
end

struct Quantile <: AbstractStatistic
    name = "Quantile"
    p = 0.5
end

struct Coefficient <: AbstractStatistic
    name = "Coefficient"
end

@smishr @nadiaenh please give suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed high priority High priority tasks, eg. relating to release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants