Skip to content
This repository has been archived by the owner on May 4, 2019. It is now read-only.

Weighted mean broken for matrices #287

Open
meggart opened this issue Sep 25, 2017 · 1 comment
Open

Weighted mean broken for matrices #287

meggart opened this issue Sep 25, 2017 · 1 comment

Comments

@meggart
Copy link

meggart commented Sep 25, 2017

I am on julia 0.6. Currently the following results in a stack overflow:

x = @data [1.0 2.0 3.0]
w = Weights(rand(3))
mean(x,w,skipna=true)

because this code

DataArrays.jl/src/reduce.jl

Lines 178 to 185 in 8b9e896

function Base.mean(a::DataArray, w::Weights; skipna::Bool=false)
if skipna
v = a .* w.values
sum(v; skipna=true) / sum(DataArray(w.values, v.na); skipna=true)
else
any(isna, a) ? NA : mean(a.data, w)
end
end

seems to assume that both the DataArray and the weight vector are of the same shape. However, it does not seem possible to create a Weights Vector with some shape information.

I would be happy to provide a PR with a fix, but wanted to ask before if this behavior is intended in some way...

@nalimilan
Copy link
Member

Thanks for the report. It seems logical to expect that the array and its weights have the same shape; else the correspondence between an element and its weight wouldn't be clear. The error message could be improved, though, and weights should probably accept any kind of array, not just vectors (they have recently been renamed from WeightsVec to Weights). Can you file an issue/PR against StatsBase?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants