# Cronbach's alpha StatsBase

**URL:** <https://discourse.julialang.org/t/cronbachs-alpha-statsbase/103292>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 28, 2023, 2:36pm UTC](https://discourse.julialang.org/t/cronbachs-alpha-statsbase/103292 "2023-08-28T14:36:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![victorshiramizu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/victorshiramizu/32/52571_2.png) [@victorshiramizu](https://discourse.julialang.org/u/victorshiramizu)\
**Post date:** [August 28, 2023, 2:36pm UTC](https://discourse.julialang.org/t/cronbachs-alpha-statsbase/103292/1 "2023-08-28T14:36:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jondea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jondea/32/39086_2.png) [@jondea](https://discourse.julialang.org/u/jondea)\
**Post date:** [August 28, 2023, 3:07pm UTC](https://discourse.julialang.org/t/cronbachs-alpha-statsbase/103292/2 "2023-08-28T15:07:12Z")

</div>

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](https://docs.julialang.org/en/v1/manual/missing/)

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [August 28, 2023, 3:12pm UTC](https://discourse.julialang.org/t/cronbachs-alpha-statsbase/103292/3 "2023-08-28T15:12:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![victorshiramizu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/victorshiramizu/32/52571_2.png) [@victorshiramizu](https://discourse.julialang.org/u/victorshiramizu)\
**Post date:** [August 28, 2023, 6:25pm UTC](https://discourse.julialang.org/t/cronbachs-alpha-statsbase/103292/4 "2023-08-28T18:25:41Z")

</div>

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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [August 28, 2023, 7:20pm UTC](https://discourse.julialang.org/t/cronbachs-alpha-statsbase/103292/5 "2023-08-28T19:20:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [August 29, 2023, 3:19am UTC](https://discourse.julialang.org/t/cronbachs-alpha-statsbase/103292/6 "2023-08-29T03:19:04Z")

</div>

For what it’s worth, `MethodError`s 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 😉

---

<div class="post-metadata">

**Author:** ![victorshiramizu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/victorshiramizu/32/52571_2.png) [@victorshiramizu](https://discourse.julialang.org/u/victorshiramizu)\
**Post date:** [August 29, 2023, 10:56am UTC](https://discourse.julialang.org/t/cronbachs-alpha-statsbase/103292/7 "2023-08-29T10:56:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![victorshiramizu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/victorshiramizu/32/52571_2.png) [@victorshiramizu](https://discourse.julialang.org/u/victorshiramizu)\
**Post date:** [August 29, 2023, 10:59am UTC](https://discourse.julialang.org/t/cronbachs-alpha-statsbase/103292/8 "2023-08-29T10:59:13Z")

</div>

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. 😃

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [August 29, 2023, 11:26am UTC](https://discourse.julialang.org/t/cronbachs-alpha-statsbase/103292/9 "2023-08-29T11:26:05Z")

</div>

Come for the speed, stay for the community 💯
