R error for Psych: Error in apply(x[1:(n.obs - lag), ], 2, sd, na.rm = na.rm) : dim(X) must have a positive length

Viewed 24

I have a dataframe. Call it "data" for simplicity.

structure(list(study_id = structure(c(001, 001, 002, 002, 
                                      003, 003), 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, 1, 2, 1, 2), format.spss = "F8.2", display_width = 0L), 
               Negative = structure(c(1, 3.3, 1.5, 4, NA, 1.5), format.spss = "F8.2", display_width = 0L), 
               Positive = structure(c(4.33333333333333, NA, 2.66666666666667, NA, 3, 4), format.spss = "F8.2", display_width = 0L)), row.names = c(NA, 
                                                                                                                                                   -6L), class = c("tbl_df", "tbl", "data.frame"))

As you can see, there are repeated study_id's with observations at different sessions. Some observations have NA in either only Positive or Negative and some have NA in both.

I'm trying to use the psych package to run an autocorrelation on my full data:

mssd(data$Negative,group=data$study_id, lag = 1,na.rm=TRUE)
rmssd(data$Negative,group=data$study_id, lag=1, na.rm=TRUE)
autoR(data$Negative,group=data$study_id,lag=1,na.rm=TRUE, use="pairewise")

mssd() and rmssd() work on my full data as expected. Running autoR() gets me the following error: Error in apply(x[1:(n.obs - lag), ], 2, sd, na.rm = na.rm) : dim(X) must have a positive length

Not sure what's going on here. My full dataset has many more data observations (into the thousands). This example I posted is working the same way as my code, however.

Is it because of the standing NA's? If that's the case, shouldn't the na.rm argument remove them? Besides, when I used na.omit() on my data I still got this error. Can't seem to find an answer specific to the autoR function.

1 Answers

The docs are relatively sparse, but based on the source code one issue could be the number of variables being passed to the function. If you subset your dataframe to columns "study_id, Negative, Positive" and change "group" to NULL, the function runs as expected:

library(psych)

data <- structure(list(study_id = structure(c(001, 001, 002, 002, 
                                              003, 003), 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, 1, 2, 1, 2), format.spss = "F8.2", display_width = 0L), 
                       Negative = structure(c(1, 3.3, 1.5, 4, NA, 1.5), format.spss = "F8.2", display_width = 0L), 
                       Positive = structure(c(4.33333333333333, NA, 2.66666666666667, NA, 3, 4), format.spss = "F8.2", display_width = 0L)), row.names = c(NA, 
                                                                                                                                                           -6L), class = c("tbl_df", "tbl", "data.frame"))

mssd(data$Negative,group=data$study_id, lag = 1,na.rm=TRUE)
#>   [,1]
#> 1 5.29
#> 2 6.25
#> 3  NaN
rmssd(data$Negative,group=data$study_id, lag=1, na.rm=TRUE)
#>   [,1]
#> 1  2.3
#> 2  2.5
#> 3  NaN
autoR(data$Negative,group=data$study_id,lag=1,na.rm=TRUE, use="pairwise")
#> Error in apply(x[1:(n.obs - lag), ], 2, sd, na.rm = na.rm): dim(X) must have a positive length

autoR(data[,c(1,4,5)],group=NULL,lag=1,na.rm=TRUE, use = "pairwise")
#> 
#> Autocorrelations 
#> study_id Negative Positive 
#>     0.79    -0.89       NA

result <- autoR(data[,c(1,4,5)],group=NULL,lag=1,na.rm=TRUE, use = "pairwise")
str(result)
#> List of 2
#>  $ autoR: Named num [1:3] 0.786 -0.886 NA
#>   ..- attr(*, "names")= chr [1:3] "study_id" "Negative" "Positive"
#>  $ rssd : Named num [1:3] 0.632 1.922 0.577
#>   ..- attr(*, "names")= chr [1:3] "study_id" "Negative" "Positive"
#>  - attr(*, "class")= chr [1:2] "psych" "autoR"
result$autoR
#>   study_id   Negative   Positive 
#>  0.7857143 -0.8856090         NA
result$rssd
#>  study_id  Negative  Positive 
#> 0.6324555 1.9222383 0.5773503

Created on 2022-08-23 by the reprex package (v2.0.1)

When you try this on your actual data, try "group=data$study_id" and see if the function throws an error; it could help with troubleshooting the problem.

Related