![](/img/trans.png)
[英]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
[英]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
我有一個 dataframe。 為簡單起見,將其稱為“數據”。
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"))
如您所見,在不同的會話中有重復的 study_id 和觀察結果。 有些觀察結果只有Positive
或Negative
的 NA,有些觀察結果兩者都有 NA。
我正在嘗試使用psych
package 對我的完整數據運行自相關:
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()
和rmssd()
按預期處理我的完整數據。 運行autoR()
得到以下錯誤:應用錯誤(x[1:(n.obs - lag), ], 2, sd, na.rm = na.rm): dim(X) must have a positive length
不知道這里發生了什么。 我的完整數據集有更多的數據觀察(數以千計)。 但是,我發布的這個示例與我的代碼的工作方式相同。
是因為常設NA嗎? 如果是這種情況, na.rm
參數不應該刪除它們嗎? 此外,當我對我的數據使用na.omit()
時,我仍然收到此錯誤。 似乎找不到特定於autoR
function 的答案。
文檔相對稀疏,但根據源代碼,一個問題可能是傳遞給 function 的變量數量。 如果您將 dataframe 子集為“study_id、Negative、Positive”列並將“group”更改為 NULL,則 function 將按預期運行:
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
由代表 package (v2.0.1) 於 2022 年 8 月 23 日創建
當您在實際數據上嘗試此操作時,請嘗試“group=data$study_id”並查看 function 是否拋出錯誤; 它可以幫助解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.