simsem / semTools

Useful tools for structural equation modeling
75 stars 36 forks source link

Using reliability when fit includes ordered variables II #62

Closed andresimi closed 5 years ago

andresimi commented 5 years ago

Hi, is it possible to use reliability with ordinal data? I am getting this error:

$ operator not defined for this S4 class

library(tidyverse); library(psych); library(lavaan); library(semTools)
data(iqitems)
​
# Example with continuous data
data <- iqitems %>% as_tibble()
data %>% glimpse()
#> Observations: 1,525
#> Variables: 16
#> $ reason.4  <int> 3, 3, 3, 4, 0, 4, 4, 1, 4, 4, 1, 4, 2, 4, 4, 4, 4, 4, …
#> $ reason.16 <int> 3, 3, 4, 0, 4, 4, 4, 4, 4, 4, 1, 4, 2, 4, 4, 4, 4, 4, …
#> $ reason.17 <int> 6, 4, 4, 6, 4, 4, 4, 5, 2, 4, 6, 4, 6, 4, 4, 4, 4, 4, …
#> $ reason.19 <int> 3, 4, 2, 1, 5, 6, 6, 2, 6, 6, 2, 5, 4, 6, 5, 6, 6, 6, …
#> $ letter.7  <int> 5, 6, 6, 5, 5, 6, 6, 4, 5, 2, 0, 6, 5, 6, 5, 6, 6, 6, …
#> $ letter.33 <int> 3, 2, 5, 1, 3, 3, 3, 1, 3, 1, 2, 3, 4, 3, 3, 1, 3, 3, …
#> $ letter.34 <int> 5, 4, 3, 4, 3, 4, 4, 1, 4, 4, 2, 4, 4, 4, 3, 1, 4, 1, …
#> $ letter.58 <int> 2, 2, 3, 1, 0, 4, 4, 2, 4, 3, 2, 4, 4, 1, 5, 4, 4, 4, …
#> $ matrix.45 <int> 4, 4, 5, 2, 5, 5, 5, 4, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, …
#> $ matrix.46 <int> 3, 1, 2, 0, 2, 2, 2, 2, 5, 2, 5, 2, 2, 2, 2, 2, 2, 2, …
#> $ matrix.47 <int> 4, 4, 4, 6, 4, 2, 2, 2, 4, 6, 2, 2, 5, 2, 2, 2, 2, 2, …
#> $ matrix.55 <int> 4, 6, 6, 2, 6, 2, 2, 3, 3, 6, 3, 2, 4, 6, 4, 4, 4, 4, …
#> $ rotate.3  <int> 5, 6, 8, 1, 5, 3, 3, 5, 6, 8, 1, 4, 6, 4, 5, 7, 6, 3, …
#> $ rotate.4  <int> 6, 5, 8, 7, 7, 2, 2, 2, 8, 0, 3, 4, 5, 7, 4, 7, 2, 2, …
#> $ rotate.6  <int> 5, 6, 8, 1, 1, 6, 6, 1, 8, 4, 6, 4, 3, 8, 6, 1, 6, 6, …
#> $ rotate.8  <int> 5, 1, 8, 6, 6, 4, 7, 1, 2, 8, 3, 4, 6, 2, 6, 2, 7, 7, …
​
model <- "X =~ reason.4 + reason.16 + reason.17 + reason.19"
fit <- cfa(model, data = data, std.lv=T, estimator="WLSMV")
reliability(fit)
#>                X     total
#> alpha  0.6114101 0.6114101
#> omega  0.6229061 0.6229061
#> omega2 0.6229061 0.6229061
#> omega3 0.6227137 0.6227137
#> avevar 0.3037535 0.3037535
​
# Example with categorical data
data <- iqitems %>% as_tibble() %>% 
  mutate_at(vars(starts_with("reason")), ordered)
data %>% glimpse()
#> Observations: 1,525
#> Variables: 16
#> $ reason.4  <ord> 3, 3, 3, 4, 0, 4, 4, 1, 4, 4, 1, 4, 2, 4, 4, 4, 4, 4, …
#> $ reason.16 <ord> 3, 3, 4, 0, 4, 4, 4, 4, 4, 4, 1, 4, 2, 4, 4, 4, 4, 4, …
#> $ reason.17 <ord> 6, 4, 4, 6, 4, 4, 4, 5, 2, 4, 6, 4, 6, 4, 4, 4, 4, 4, …
#> $ reason.19 <ord> 3, 4, 2, 1, 5, 6, 6, 2, 6, 6, 2, 5, 4, 6, 5, 6, 6, 6, …
#> $ letter.7  <int> 5, 6, 6, 5, 5, 6, 6, 4, 5, 2, 0, 6, 5, 6, 5, 6, 6, 6, …
#> $ letter.33 <int> 3, 2, 5, 1, 3, 3, 3, 1, 3, 1, 2, 3, 4, 3, 3, 1, 3, 3, …
#> $ letter.34 <int> 5, 4, 3, 4, 3, 4, 4, 1, 4, 4, 2, 4, 4, 4, 3, 1, 4, 1, …
#> $ letter.58 <int> 2, 2, 3, 1, 0, 4, 4, 2, 4, 3, 2, 4, 4, 1, 5, 4, 4, 4, …
#> $ matrix.45 <int> 4, 4, 5, 2, 5, 5, 5, 4, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, …
#> $ matrix.46 <int> 3, 1, 2, 0, 2, 2, 2, 2, 5, 2, 5, 2, 2, 2, 2, 2, 2, 2, …
#> $ matrix.47 <int> 4, 4, 4, 6, 4, 2, 2, 2, 4, 6, 2, 2, 5, 2, 2, 2, 2, 2, …
#> $ matrix.55 <int> 4, 6, 6, 2, 6, 2, 2, 3, 3, 6, 3, 2, 4, 6, 4, 4, 4, 4, …
#> $ rotate.3  <int> 5, 6, 8, 1, 5, 3, 3, 5, 6, 8, 1, 4, 6, 4, 5, 7, 6, 3, …
#> $ rotate.4  <int> 6, 5, 8, 7, 7, 2, 2, 2, 8, 0, 3, 4, 5, 7, 4, 7, 2, 2, …
#> $ rotate.6  <int> 5, 6, 8, 1, 1, 6, 6, 1, 8, 4, 6, 4, 3, 8, 6, 1, 6, 6, …
#> $ rotate.8  <int> 5, 1, 8, 6, 6, 4, 7, 1, 2, 8, 3, 4, 6, 2, 6, 2, 7, 7, …
​
fit <- cfa(model, data = data, std.lv=T, estimator="WLSMV")
reliability(fit)
#> Error: $ operator not defined for this S4 class

Created on 2019-09-10 by the reprex package (v0.3.0)

TDJorgensen commented 5 years ago

Yes, this issue was resolved immediately after (unfortunately not before) sending the latest version to CRAN. You can install the development version though:

devtools::install_github("simsem/semTools/semTools")
andresimi commented 5 years ago

Ok, that solved the problem. Thank you.