From 50299b81aefb77bb359352a56021ef4d5fcff817 Mon Sep 17 00:00:00 2001 From: PharmCat <13901158+PharmCat@users.noreply.github.com> Date: Sun, 12 Feb 2023 19:13:01 +0300 Subject: [PATCH] update --- src/descriptive.jl | 262 +++++++++++++++++++++++---------------------- 1 file changed, 133 insertions(+), 129 deletions(-) diff --git a/src/descriptive.jl b/src/descriptive.jl index 19e0972..a5144a1 100644 --- a/src/descriptive.jl +++ b/src/descriptive.jl @@ -213,147 +213,151 @@ function descriptives(data::DataSet{T}; kwargs...) where T <: ObsData cicalk = false q = NaN end - + # ds = Vector{Descriptives}(undef, length(data)) i = 1 for d in data - # skipmissing - if kwargs[:skipmissing] - vec = skipnanormissing(d.obs) - else - vec = d.obs - end - n_ = length(vec) - if cicalk - if n_ > 1 q = quantile(TDist(n_ - 1), 1 - (1-kwargs[:level])/2) end - end - # skipnonpositive - logstats = makelogvec #calk logstats - if makelogvec - if kwargs[:skipnonpositive] - logvec = log.(skipnonpositive(d.obs)) + ds[i] = Descriptives(d, descriptives_(d.obs, kwargs, makelogvec, cicalk)) + i += 1 + end + DataSet(identity.(ds)) + +end + +function descriptives_(obsvec, kwargs, makelogvec, cicalk) + # skipmissing + if kwargs[:skipmissing] + vec = skipnanormissing(obsvec) else - if kwargs[:skipmissing] - itr = skipnanormissing(d.obs) - if any(!ispositive, itr) - logvec = Float64[] - logstats = false - else - logvec = log.(itr) - end + vec = obsvec + end + n_ = length(vec) + if cicalk + if n_ > 1 q = quantile(TDist(n_ - 1), 1 - (1-kwargs[:level])/2) end + end + # skipnonpositive + logstats = makelogvec #calk logstats + if makelogvec + if kwargs[:skipnonpositive] + logvec = log.(skipnonpositive(obsvec)) else - if any(!ispositive, d.obs) - logvec = Float64[] - logstats = false + if kwargs[:skipmissing] + itr = skipnanormissing(obsvec) + if any(!ispositive, itr) + logvec = Float64[] + logstats = false + else + logvec = log.(itr) + end else - logvec = log.(d.obs) + if any(!ispositive, obsvec) + logvec = Float64[] + logstats = false + else + logvec = log.(obsvec) + end end end end - end - logn_ = length(skipnonpositive(d.obs)) - - result = Dict{Symbol, Float64}() - - for s in kwargs[:stats] - - if s == :n - result[s] = n_ - elseif s == :posn - result[s] = logn_ - elseif !(n_ > 0) - result[s] = NaN - continue - elseif s == :mean - result[s] = sum(vec) / n_ - elseif s == :sd - haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end - result[s] = std(vec; corrected = kwargs[:corrected], mean = result[:mean]) - elseif s == :var - haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end - result[s] = var(vec; corrected = kwargs[:corrected], mean = result[:mean]) - elseif s == :bvar - haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end - result[s] = var(vec; corrected = false, mean = result[:mean]) - elseif s == :se - haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end - haskey(result, :sd) || begin result[:sd] = std(vec; corrected = kwargs[:corrected], mean = result[:mean]) end - result[s] = result[:sd] / sqrt(n_) - elseif s == :cv - haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end - haskey(result, :sd) || begin result[:sd] = std(vec; corrected = kwargs[:corrected], mean = result[:mean]) end - result[s] = abs(result[:sd] / result[:mean] * 100) - elseif s == :uci - haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end - haskey(result, :sd) || begin result[:sd] = std(vec; corrected = kwargs[:corrected], mean = result[:mean]) end - result[s] = result[:mean] + q*result[:sd] - elseif s == :lci - haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end - haskey(result, :sd) || begin result[:sd] = std(vec; corrected = kwargs[:corrected], mean = result[:mean]) end - result[s] = result[:mean] - q*result[:sd] - elseif s == :umeanci - haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end - haskey(result, :sd) || begin result[:sd] = std(vec; corrected = kwargs[:corrected], mean = result[:mean]) end - haskey(result, :se) || begin result[:se] = result[:sd] / sqrt(n_) end - result[s] = result[:mean] + q*result[:se] - elseif s == :lmeanci - haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end - haskey(result, :sd) || begin result[:sd] = std(vec; corrected = kwargs[:corrected], mean = result[:mean]) end - haskey(result, :se) || begin result[:se] = result[:sd] / sqrt(n_) end - result[s] = result[:mean] - q*result[:se] - elseif s == :median - result[s] = median(vec) - elseif s == :min - result[s] = minimum(vec) - elseif s == :max - result[s] = maximum(vec) - elseif s == :q1 - result[s] = quantile(vec, 0.25) - elseif s == :q3 - result[s] = quantile(vec, 0.75) - elseif s == :iqr - result[s] = abs(quantile(vec, 0.75) - quantile(vec, 0.25)) - elseif s == :range - result[s] = abs(maximum(vec) - minimum(vec)) - elseif s == :kurt - haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end - result[s] = kurtosis_(vec, result[:mean]) - elseif s == :skew - haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end - result[s] = skewness_(vec, result[:mean]) - elseif s == :harmmean - result[s] = harmmean(vec) - elseif s == :ses - result[s] = sesvec(vec) - elseif s == :sek - result[s] = sekvec(vec) - elseif s == :sum - result[s] = sum(vec) - elseif !logstats - result[s] = NaN - continue - elseif s == :logmean - result[s] = sum(logvec) / logn_ - elseif s == :geom || s == :geomean - haskey(result, :logmean) || begin result[:logmean] = sum(logvec) / logn_ end - result[s] = exp(result[:logmean]) - elseif s == :logvar - haskey(result, :logmean) || begin result[:logmean] = sum(logvec) / logn_ end - result[s] = var(logvec; corrected = kwargs[:corrected], mean = result[:logmean]) - elseif s == :geocv - haskey(result, :logmean) || begin result[:logmean] = sum(logvec) / logn_ end - haskey(result, :logvar) || begin result[:logvar] = var(logvec; corrected = kwargs[:corrected], mean = result[:logmean]) end - result[s] = sqrt(exp(result[:logvar])-1)*100 + logn_ = length(skipnonpositive(obsvec)) + + result = Dict{Symbol, Float64}() + + for s in kwargs[:stats] + + if s == :n + result[s] = n_ + elseif s == :posn + result[s] = logn_ + elseif !(n_ > 0) + result[s] = NaN + continue + elseif s == :mean + result[s] = sum(vec) / n_ + elseif s == :sd + haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end + result[s] = std(vec; corrected = kwargs[:corrected], mean = result[:mean]) + elseif s == :var + haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end + result[s] = var(vec; corrected = kwargs[:corrected], mean = result[:mean]) + elseif s == :bvar + haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end + result[s] = var(vec; corrected = false, mean = result[:mean]) + elseif s == :se + haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end + haskey(result, :sd) || begin result[:sd] = std(vec; corrected = kwargs[:corrected], mean = result[:mean]) end + result[s] = result[:sd] / sqrt(n_) + elseif s == :cv + haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end + haskey(result, :sd) || begin result[:sd] = std(vec; corrected = kwargs[:corrected], mean = result[:mean]) end + result[s] = abs(result[:sd] / result[:mean] * 100) + elseif s == :uci + haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end + haskey(result, :sd) || begin result[:sd] = std(vec; corrected = kwargs[:corrected], mean = result[:mean]) end + result[s] = result[:mean] + q*result[:sd] + elseif s == :lci + haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end + haskey(result, :sd) || begin result[:sd] = std(vec; corrected = kwargs[:corrected], mean = result[:mean]) end + result[s] = result[:mean] - q*result[:sd] + elseif s == :umeanci + haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end + haskey(result, :sd) || begin result[:sd] = std(vec; corrected = kwargs[:corrected], mean = result[:mean]) end + haskey(result, :se) || begin result[:se] = result[:sd] / sqrt(n_) end + result[s] = result[:mean] + q*result[:se] + elseif s == :lmeanci + haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end + haskey(result, :sd) || begin result[:sd] = std(vec; corrected = kwargs[:corrected], mean = result[:mean]) end + haskey(result, :se) || begin result[:se] = result[:sd] / sqrt(n_) end + result[s] = result[:mean] - q*result[:se] + elseif s == :median + result[s] = median(vec) + elseif s == :min + result[s] = minimum(vec) + elseif s == :max + result[s] = maximum(vec) + elseif s == :q1 + result[s] = quantile(vec, 0.25) + elseif s == :q3 + result[s] = quantile(vec, 0.75) + elseif s == :iqr + result[s] = abs(quantile(vec, 0.75) - quantile(vec, 0.25)) + elseif s == :range + result[s] = abs(maximum(vec) - minimum(vec)) + elseif s == :kurt + haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end + result[s] = kurtosis_(vec, result[:mean]) + elseif s == :skew + haskey(result, :mean) || begin result[:mean] = sum(vec) / n_ end + result[s] = skewness_(vec, result[:mean]) + elseif s == :harmmean + result[s] = harmmean(vec) + elseif s == :ses + result[s] = sesvec(vec) + elseif s == :sek + result[s] = sekvec(vec) + elseif s == :sum + result[s] = sum(vec) + elseif !logstats + result[s] = NaN + continue + elseif s == :logmean + result[s] = sum(logvec) / logn_ + elseif s == :geom || s == :geomean + haskey(result, :logmean) || begin result[:logmean] = sum(logvec) / logn_ end + result[s] = exp(result[:logmean]) + elseif s == :logvar + haskey(result, :logmean) || begin result[:logmean] = sum(logvec) / logn_ end + result[s] = var(logvec; corrected = kwargs[:corrected], mean = result[:logmean]) + elseif s == :geocv + haskey(result, :logmean) || begin result[:logmean] = sum(logvec) / logn_ end + haskey(result, :logvar) || begin result[:logvar] = var(logvec; corrected = kwargs[:corrected], mean = result[:logmean]) end + result[s] = sqrt(exp(result[:logvar]) - 1)*100 + end end - end - filter!(x -> x.first in kwargs[:stats], result) - ds[i] = Descriptives(d, result) - i += 1 - end - DataSet(identity.(ds)) + filter!(x -> x.first in kwargs[:stats], result) + result end - function sesvec(vec) ses(length(vec)) end