Survey.jl icon indicating copy to clipboard operation
Survey.jl copied to clipboard

Abstract types for summary statistics function for code reuse

Open smishr opened this issue 1 year ago • 4 comments

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 avatar Mar 01 '23 05:03 smishr

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.

asinghvi17 avatar Mar 25 '23 13:03 asinghvi17

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 avatar May 08 '23 11:05 ayushpatnaikgit

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

ayushpatnaikgit avatar Jun 25 '23 04:06 ayushpatnaikgit

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.

ayushpatnaikgit avatar Jun 27 '23 10:06 ayushpatnaikgit