Cronbach's alpha StatsBase

I’m trying to calculate Cronbach’s alpha via cronbachalpha function from StatsBase. I’m getting the following error: ERROR: MethodError: no method matching cronbachalpha(::Matrix{Union{Missing, Float64}}). However, my matrix has no missing value.
Any thoughts?

Welcome to the Julia discourse @victorshiramizu! It may be the case that this function should have a method for this type, I don’t know enough about it sorry. Either way though, you can do something like new_matrix = coalesce.(matrix, NaN) to turn any missing values into Float64 NaNs (which in many cases behave similarly to missing).

The Union{Missing, Float64} type means that the entries can be missing, but there may actually be no missing entries.

If indeed there are no missings in your matrix (and you expect there never to be), you could work backwards to track down why it has that type. (For example the matrix was read from a CSV which can cause a container to have a missing type.)

Edit: You may want to have a look at the docs for missing to understand what they are and what you should do about them Missing Values · The Julia Language

1 Like

disallowmissing(matrix) will give you a new matrix with type Matrix{Float64} if your matrix indeed does not have any missing entries.


Hi @jondea and @tbeason, thanks for your reply.

I backtracked the code and I’m getting Union{Missing, Float64} when using the unstack function from DataFrames. I solved this employing what you suggested to me (disallowmissing())

When using permutedims, the variables became Float64? type and the cronbachalpha function is now returning the following error:

ERROR: MethodError: no method matching cronbachalpha(::DataFrame) Closest candidates are: cronbachalpha(::AbstractMatrix{<:Real})

Here’s my code:

df2 = @pipe df |> 
filter(row -> row.g == "f") |>
select(_, :id, :tn, :value) |>
unstack(_, :id, :tn, :value) |>
select(_, Not(:id))

df_2_m = disallowmissing(df2)
df2_t = permutedims(df2)
alpha = cronbachalpha(df2_t)

Not sure what’s going on.

You cannot pass in a DataFrame, you must pass in a Matrix. This could be as easy as Matrix(df2_t), unless you need to fiddle with the columns.

1 Like

For what it’s worth, MethodErrors were among the hardest things for me to figure out when I was transitioning to Julia from python. I was not used to thinking or caring at all about types.

But stick with it! Now, method errors are among my favorites, since they’re usually quite straightforwardly solvable :wink:


That worked! Thanks, @tbeason. However, the matrix is not positive definite and the function does not apply an automatic smoothing method as in the psych package for R. Thanks again.

Ha! I’m transitioning from R to Julia given the computational speed is so much better for the models I am fitting. Glad the community here is very supportive. :smiley:


Come for the speed, stay for the community :100:

1 Like