diff --git a/src/Statistics.jl b/src/Statistics.jl index 588d03c8..0d247608 100644 --- a/src/Statistics.jl +++ b/src/Statistics.jl @@ -665,7 +665,8 @@ end # corzm (non-exported, with centered data) -corzm(x::AbstractVector{T}) where {T} = one(float(T)) +corzm(x::AbstractVector{T}) where {T} = + T === Missing ? missing : one(float(nonmissingtype(T))) function corzm(x::AbstractMatrix, vardim::Int=1) c = unscaled_covzm(x, vardim) return cov2cor!(c, collect(sqrt(c[i,i]) for i in 1:min(size(c)...))) @@ -679,7 +680,8 @@ corzm(x::AbstractMatrix, y::AbstractMatrix, vardim::Int=1) = # corm -corm(x::AbstractVector{T}, xmean) where {T} = one(float(T)) +corm(x::AbstractVector{T}, xmean) where {T} = + T === Missing ? missing : one(float(nonmissingtype(T))) corm(x::AbstractMatrix, xmean, vardim::Int=1) = corzm(x .- xmean, vardim) function corm(x::AbstractVector, mx, y::AbstractVector, my) require_one_based_indexing(x, y) @@ -713,7 +715,8 @@ corm(x::AbstractVecOrMat, xmean, y::AbstractVecOrMat, ymean, vardim::Int=1) = Return the number one. """ -cor(x::AbstractVector) = one(float(eltype(x))) +cor(x::AbstractVector{T}) where {T} = + T === Missing ? missing : one(float(nonmissingtype(T))) """ cor(X::AbstractMatrix; dims::Int=1) diff --git a/test/runtests.jl b/test/runtests.jl index df0c0385..4ac7c52d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -689,7 +689,7 @@ end @test varm(z, 0.0) ≈ invoke(varm, Tuple{Any,Float64}, z, 0.0) ≈ sum(abs2, z)/9 @test isa(varm(z, 0.0), Float64) @test isa(invoke(varm, Tuple{Any,Float64}, z, 0.0), Float64) - @test cor(z) === 1.0 + @test cor(z) === 1.0+0.0im v = varm([1.0+2.0im], 0; corrected = false) @test v ≈ 5 @test isa(v, Float64)