Psych package error with autoR() function: Error in apply(x[1:(n.obs – lag), ], 2, sd, na.rm = na.rm): dim(X) must have a positive length

Issue

This Content is from Stack Overflow. Question asked by AcidCatfish

This is a follow up to my last question I posted. Someone did find a working solution, or so it seemed. It looked as if the code ran as expected, but I’m a bit dubious now.

I have a dataframe

data <- structure(list(study_id = structure(c(1001, 1001, 1002, 1002, 
1003, 1003), format.spss = "F6.0", display_width = 0L), Date = structure(c(18584, 
18584, 18585, 18585, 18585, 18585), format.spss = "DATE11", display_width = 0L, class = "Date"), 
    SignalNumber = structure(c(1, 2, 3, 4, 5, 6), format.spss = "F8.2", display_width = 0L), 
    NegativeAffect = structure(c(1, 2.25, 1.5, 1, 2.25, 1.5), format.spss = "F8.2", display_width = 0L), 
    PositiveAffect = structure(c(4.33333333333333, 2, 2.66666666666667, 
    3.33333333333333, 3, 4), format.spss = "F8.2", display_width = 0L), 
    Date_first = structure(c(18584, 18584, 18584, 18584, 18584, 
    18584), format.spss = "DATE11", display_width = 0L, class = "Date"), 
    ID_day = structure(c(1001, 1001, 1002, 1002, 1002, 1002), format.spss = "F8.2", display_width = 0L)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

Initially, I wanted to use the autoR() function in psych grouping PositiveAffect and NegativeAffect by study_id.

library(psych)
autoR(data$NegativeAffect, group=data$study_id, lag=1, na.rm=TRUE) #original method which gave error
autoR(data[,c(4, 5)],group=datat$study_id, lag=1, na.rm=TRUE) #suggested method which does run

So, great, the line of code from the suggestion runs. However, I am also interested in grouping with a different variable called ID_day.

I tried to use the suggested method:

autoR(data[,c(4, 5)],group=ema_affect$ID_day, lag=1, na.rm=TRUE) #gives error

I get the same error I initially got when I was writing the syntax with the $ operator: Error in apply(x[1:(n.obs – lag), ], 2, sd, na.rm = na.rm): dim(X) must have a positive length

I’m now not sure if the solution is actually running the function as intended.

As the user mentioned in my previous post, there is little documentation about this which makes it difficult for me to study up on and fix it. The RDocumentation on the package doesn’t seem to go into detail about this specific error…but maybe I’m missing something.

Anyone know what I might be doing wrong?



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?