Search code examples
rpsych

Psych package multilevel reliability Error in contrasts<-(`*tmp*`, value = contr.funs[1+isOF[nn]]): contrasts can be applied only to factors with 2


I had a problem, and I found a solution, but I cannot create a reprex. I will describe it and hopefully this helps someone in the future.

I had a dataframe that was imported to R with readRDS from a and before it was saved in that format, it was imported to R from .xlsx files with read_xlsx.

I used dplyr's select function to get a subset of data to run with psych::mlr (multilevel reliability).

I was following along with this tutorial: https://personality-project.org/courses/350/350.wk.7b.html

While the mlr function worked with their structure of shrout data, it wouldn't work with my data. I received an error:

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels

I tried taking a subset of my data and defining it in the same way as the shrout data and it did execute then, without an error.

Typing out all of my data was not an option. Both dataframes looked the exact same when viewed in R Studio's viewer. However when examining print() of my data and the data structure that I made similar to the shrout data revealed that my data was being printed as a tibble (even though it was a dataframe) and the other was printing as a df.


Solution

  • I managed to get mlr to run and not produce the error by putting my data into as.matrix and dataframe.

    mg <- mlr(data.frame(as.matrix(my_data)),grp="ID",Time="time",items=c("item1","item2","item3"))