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

InexactError when dealing with means of integers with missings

Open eirikbrandsaas opened this issue 3 years ago • 3 comments

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.

eirikbrandsaas avatar Aug 24 '22 00:08 eirikbrandsaas

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 avatar Aug 24 '22 03:08 pdeffebach

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.

pdeffebach avatar Aug 24 '22 03:08 pdeffebach

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

nalimilan avatar Aug 24 '22 07:08 nalimilan