JuliaStats / Statistics.jl

The Statistics stdlib that ships with Julia.
https://juliastats.org/Statistics.jl/dev/
Other
71 stars 40 forks source link

InexactError when dealing with means of integers with missings #126

Open eirikbrandsaas opened 2 years ago

eirikbrandsaas commented 2 years ago
a = [missing missing; 0 1]
mean(a;dims=2) # InexactError

The answer should be [missing, 0.5].

Note that

a = [missing missing; 1 1]
means(a;dims=2) # [missing 1]

works.

pdeffebach commented 2 years ago

Works with floats. I would guess some call to similar that isnt correct.

julia> b = [missing missing; 0.0 1.0]
2×2 Matrix{Union{Missing, Float64}}:
  missing   missing
 0.0       1.0

julia> mean(b, dims = 2)
2×1 Matrix{Union{Missing, Float64}}:
  missing
 0.5
pdeffebach commented 2 years ago

Error is this line

julia> Statistics._mean_promote(missing, 1)
1

julia> 1/1
1.0

julia> missing / 1
missing

The solution is probably to add more f(x) / 1 checks to get the output type right, since missing / 1 is not informative of what the output should be the same way 1/1 is. We could also add a skipmissing or something, though that is likely quite costly.

nalimilan commented 2 years ago

See also https://github.com/JuliaStats/Statistics.jl/issues/7.