R 错误-此S4类不可子集
我知道这个问题有几个答案,但是我找不到任何适用于我的问题的答案。有人能帮我解决这个错误吗?这个S4类在代码的底部是不可子集的。我不确定这个错误是从哪里来的。输出结果应为DWT的阈值系数R 错误-此S4类不可子集,r,R,我知道这个问题有几个答案,但是我找不到任何适用于我的问题的答案。有人能帮我解决这个错误吗?这个S4类在代码的底部是不可子集的。我不确定这个错误是从哪里来的。输出结果应为DWT的阈值系数 xx <- list(list(c(1,2,3,4,5,6,7,5,4,3,2,4,3,2,3,5,4,3,2,3,4,5,6,3,4,3,3), c(0,3,1,4,1,2,7,5,4,1,3,4,9,2,7,5,1,3,2,2,1,1,1,5,1,3,1)), list(c
xx <- list(list(c(1,2,3,4,5,6,7,5,4,3,2,4,3,2,3,5,4,3,2,3,4,5,6,3,4,3,3),
c(0,3,1,4,1,2,7,5,4,1,3,4,9,2,7,5,1,3,2,2,1,1,1,5,1,3,1)),
list(c(0,3,1,4,1,2,7,5,4,1,3,4,9,2,2,4,7,6,4,2,1,1,1,5,1,3,1),
c(1,2,3,4,5,6,7,5,4,3,5,4,3,2,3,4,5,6,3,2,1,2,3,5,4,3,3)),
list(c(0,3,1,4,1,2,7,5,4,1,3,4,3,2,2,4,7,6,4,2,1,1,1,5,1,3,1),
c(1,2,3,4,5,6,4,3,2,3,5,4,3,2,3,4,5,6,3,2,1,2,3,5,4,3,3)))
# Select Filter.
library(wavelets)
filter <- c ("d2","d4","d6", "c6","d8","la8","la10","d12","c12","la12","la14","d14","d16","la16","d18","c18","la18")
boundary <- c("periodic","reflection")
g <- seq(1:length(xx))
fun <- function (x) seq(1: as.integer (floor (logb ((length(xx[[x]][[1]])),base=2))))
nlevel <- lapply( g,fun)
fun <- function(x) expand.grid(filter=filter,nlevel=nlevel[[x]],boundary=boundary, stringsAsFactors=FALSE)
w3<- lapply(g,fun)
z <- c(seq(1:length(w3)))
mapply3 <- function(i) {
w4 <- w3[[i]]
mapply ( function ( m,k,p,x ) modwt ( x, filter = m, n.levels = k, boundary=p) , w3[[i]]$filter, w3[[i]]$nlevel, w3[[i]]$boundary , MoreArgs = list(x = (xx[[i]][[1]])) )
}
DWT <- lapply ( z, mapply3 )
#---------------------------------------------------------------------------
vscale <- c("level")
# smooth.levels <- c(nlevel)
prior <- c("laplace")
a <- c(0.1,0.3)
bayesfac <- c("TRUE")
threshrule <- c("median","mean")
#---------------------------------------------------------------------------
X <- seq(1:length(DWT))
fun <- function (x) DWT[x]
u <- lapply(X,fun)
fun <- function (x) seq(1:length(DWT[[x]]))
U <- lapply(X,fun)
L1 <- expand.grid ( vscale = vscale, prior = prior, a = a , bayesfac = bayesfac , threshrule = threshrule , stringsAsFactors = FALSE )
# --------------------------------------------------------------------------
library ( EbayesThresh )
mapply2 <- function ( DWTi , LL ) {
mapply ( function ( c,e,f,g,h,x ) ebayesthresh.wavelet ( x, vscale = c, prior = e, a = f, bayesfac = g, threshrule = h ) , LL$vscale , LL$prior , LL$a , LL$bayesfac , LL$threshrule , MoreArgs = list ( x = DWTi ) )
}
mapply3 <- function( i, L1, DWT ) {
DWTi <- DWT [[i]][U[[i]]]
w3 <- L1
lapply( DWTi, mapply2, w3 )
}
M1 <- lapply(z, mapply3, L1, DWT)
# Error in x.dwt[[j]] : this S4 class is not subsettable
xx这可能是“小波”包中的错误。我查找了ebayesthresh.wavelet的源代码,复制了它,并添加了一些“打印”调试:
#------------------------------------------------------------------------
# The same as "ebayesthresh.wavelet.dwt" plus some "print" for debugging:
ebayesthresh.wvlt.dwt <-
function (x.dwt, vscale = "independent", smooth.levels = Inf,
prior = "laplace", a = 0.5, bayesfac = FALSE, threshrule = "median")
{
nlevs <- length(x.dwt) - 1
slevs <- min(nlevs, smooth.levels)
print("nlevs:")
print(nlevs)
print("slevs")
print(slevs)
if (is.character(vscale)) {
vs <- substring(vscale, 1, 1)
if (vs == "i")
vscale <- mad(x.dwt[[1]])
if (vs == "l")
vscale <- NA
}
print("1:slevs:")
print(1:slevs)
for (j in 1:slevs) {
print("j:")
print(j)
x.dwt[[j]] <- ebayesthresh(x.dwt[[j]], prior, a, bayesfac,
vscale, FALSE, threshrule)
print("OK")
}
return(x.dwt)
}
#----------------------------------------------------------------------------
# The same as "ebayesthresh.wavelet",
# but it calls "ebayesthresh.wvlt.dwt" instead of "ebayesthresh.wavelet.dwt":
ebayesthresh.wvlt <-
function (xtr, vscale = "independent", smooth.levels = Inf, prior = "laplace",
a = 0.5, bayesfac = FALSE, threshrule = "median")
{
xcl <<- class(xtr)
if (class(xcl) == "dwt " && length(xcl) > 1) {
xtr <- ebayesthresh.wavelet.splus(xtr, vscale, smooth.levels,
prior, a, bayesfac, threshrule)
return(xtr)
}
if (xcl == "wd") {
xtr <- ebayesthresh.wavelet.wd(xtr, vscale, smooth.levels,
prior, a, bayesfac, threshrule)
return(xtr)
}
if (xcl == "dwt" || xcl == "modwt") {
xtr <- ebayesthresh.wvlt.dwt(xtr, vscale, smooth.levels,
prior, a, bayesfac, threshrule)
return(xtr)
}
print("Unknown wavelet transform type; no smoothing performed")
return(xtr)
}
让我们看看:
> M1 <- lapply(z, mapply3, L1, DWT)
[1] "nlevs:"
[1] 0
[1] "slevs"
[1] 0
[1] "1:slevs:"
[1] 1 0
[1] "j:"
[1] 1
Error in x.dwt[[j]] : this S4 class is not subsettable
>
如果n
为0或负值,则不为空。(另一个避免for
-loops的原因?j
以-1的步骤从1运行到n
。我发现了一个类似的错误
如果将ebayesthresh.wvlt.dwt
中的for
-循环替换为while
-循环,则错误消息消失:
j <- 1
while (j<=slevs) {
x.dwt[[j]] <- ebayesthresh(x.dwt[[j]], prior, a, bayesfac,
vscale, FALSE, threshrule)
j <- j+1
}
j这可能是“小波”包中的错误。我查找了ebayesthresh.wavelet的源代码,复制了它,并添加了一些“打印”调试:
#------------------------------------------------------------------------
# The same as "ebayesthresh.wavelet.dwt" plus some "print" for debugging:
ebayesthresh.wvlt.dwt <-
function (x.dwt, vscale = "independent", smooth.levels = Inf,
prior = "laplace", a = 0.5, bayesfac = FALSE, threshrule = "median")
{
nlevs <- length(x.dwt) - 1
slevs <- min(nlevs, smooth.levels)
print("nlevs:")
print(nlevs)
print("slevs")
print(slevs)
if (is.character(vscale)) {
vs <- substring(vscale, 1, 1)
if (vs == "i")
vscale <- mad(x.dwt[[1]])
if (vs == "l")
vscale <- NA
}
print("1:slevs:")
print(1:slevs)
for (j in 1:slevs) {
print("j:")
print(j)
x.dwt[[j]] <- ebayesthresh(x.dwt[[j]], prior, a, bayesfac,
vscale, FALSE, threshrule)
print("OK")
}
return(x.dwt)
}
#----------------------------------------------------------------------------
# The same as "ebayesthresh.wavelet",
# but it calls "ebayesthresh.wvlt.dwt" instead of "ebayesthresh.wavelet.dwt":
ebayesthresh.wvlt <-
function (xtr, vscale = "independent", smooth.levels = Inf, prior = "laplace",
a = 0.5, bayesfac = FALSE, threshrule = "median")
{
xcl <<- class(xtr)
if (class(xcl) == "dwt " && length(xcl) > 1) {
xtr <- ebayesthresh.wavelet.splus(xtr, vscale, smooth.levels,
prior, a, bayesfac, threshrule)
return(xtr)
}
if (xcl == "wd") {
xtr <- ebayesthresh.wavelet.wd(xtr, vscale, smooth.levels,
prior, a, bayesfac, threshrule)
return(xtr)
}
if (xcl == "dwt" || xcl == "modwt") {
xtr <- ebayesthresh.wvlt.dwt(xtr, vscale, smooth.levels,
prior, a, bayesfac, threshrule)
return(xtr)
}
print("Unknown wavelet transform type; no smoothing performed")
return(xtr)
}
让我们看看:
> M1 <- lapply(z, mapply3, L1, DWT)
[1] "nlevs:"
[1] 0
[1] "slevs"
[1] 0
[1] "1:slevs:"
[1] 1 0
[1] "j:"
[1] 1
Error in x.dwt[[j]] : this S4 class is not subsettable
>
如果n
为0或负值,则不为空。(另一个避免for
-loops的原因?j
以-1的步骤从1运行到n
。我发现了一个类似的错误
如果将ebayesthresh.wvlt.dwt
中的for
-循环替换为while
-循环,则错误消息消失:
j <- 1
while (j<=slevs) {
x.dwt[[j]] <- ebayesthresh(x.dwt[[j]], prior, a, bayesfac,
vscale, FALSE, threshrule)
j <- j+1
}
j此代码不运行:没有g
(第6行)。@jlhoward,近似值已更正。此代码不运行:没有g
(第6行)。@jlhoward,近似值已更正。我不遵循错误解决方案。这似乎与问题无关,因为问题中有几个变量进入上述DWT。M1也不是一个解,M1应该产生一个阈值系数列表,而不是从你的解得到的列表。通常S4解决方案涉及对象类,我认为在这种情况下,该类对象与代码的最后一部分有关,我认为DWT部分和单个元素的子集。可能有一个bug我不知道,但我认为与问题或解决方案无关。很多人感谢M1中出现的错误,因为错误“此S4类不可子集”,到目前为止,您还无法检查M1
是否产生了您所期望的结果。如果现在你发现情况并非如此,那么原因与问题无关。谢谢你的解释,我理解,但根据你的解释,可能是这样的,当您用于函数ebayesthresh.wvlt.dwt和ebayesthresh.wvlt的变量不是问题中所述的变量,或者您包含的变量未包含在我的示例中时,我如何知道这是正确的。另一方面,您使用的代码与示例中的代码不同,并且不使用apply family函数,这是必需的,因为有多个变量。根据上面的解释,我的印象是您正在解决一个不同的问题。我确实使用了与您相同的代码。正如我前面提到的,为了调试的目的,我没有做任何其他事情,只是将ebayesthresh.wavelet
和ebayesthresh.wavelet
替换为ebayesthresh.wvlt
和ebayesthresh.wvlt.dwt
。…wavelet
和…wvlt
之间的唯一区别是附加的“print”语句。在我的回答中,我没有显示与问题中显示的代码相同的代码部分。但是我使用了相同的mapply
函数。在mapply2
中,只有ebayesthresh.wavelet
必须被ebayesthresh.wvlt
替换,如我的回答所示。我不遵循错误解决方案。这似乎与问题无关,因为问题中有几个变量进入上述DWT。M1也不是一个解,M1应该产生一个阈值系数列表,而不是从你的解得到的列表。通常S4解决方案涉及对象类,我认为在这种情况下,该类对象与代码的最后一部分有关,我认为DWT部分和单个元素的子集。可能有一个bug我不知道,但我认为与问题或解决方案无关。很多人感谢M1中出现的错误,因为错误“此S4类不可子集”,到目前为止,您还无法检查M1
是否产生了您所期望的结果。如果现在你发现情况并非如此,那么原因与问题无关。谢谢你的解释,我理解,但根据你的解释,可能是这样的,当您用于函数ebayesthresh.wvlt.dwt和ebayesthresh.wvlt的变量不是问题中所述的变量,或者您包含的变量未包含在我的示例中时,我如何知道这是正确的。另一方面,您使用的代码与示例中的代码不同,并且不使用apply family函数,这是必需的,因为有多个变量。根据上面的解释,我的印象是您正在解决一个不同的问题。我确实使用了与您相同的代码。正如我前面提到的,为了调试的目的,我没有做任何其他事情,只是将ebayesthresh.wavelet
和ebayesthresh.wavelet
替换为ebayesthresh.wvlt
和ebayesthresh.wvlt.dwt
。…wavelet
和…wvlt
之间的唯一区别是附加的“print”语句。在我的回答中,我没有显示与问题中显示的代码相同的代码部分。但是我使用了相同的mapply
函数。在mapply2
中,只有ebayesthresh.wavelet
必须被ebayesthresh.wvlt
替换,如我的回答所示。