使用rollapply输出到列表列表 [英] Using rollapply to output to lists of lists

查看:118
本文介绍了使用rollapply输出到列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用rollapply或rollapplyr将modwt函数应用于我的时间序列数据.

I would like to use rollapply or rollapplyr to apply the modwt function to my time series data.

我熟悉rollapply/r的工作原理,但是我需要一些帮助来设置输出,以便在使用rollapply时可以正确存储结果.

I'm familiar with how rollapply/r works but I need some help setting up the output so that I can correctly store my results when using rollapply.

waveslim软件包中的modwt函数采用一个时间序列并将其分解为J层,对于我的特定问题J = 4,这意味着我将从单个时间序列中获得4组系数,并存储在5个列表中.这个清单我只关心d1,d2,d3& d4.

The modwt function in the waveslim package takes a time series and decomposes it into J levels, for my particular problem J = 4 which means I will have 4 sets of coefficients from my single time series stored in a list of 5. Of this list I am only concerned with d1,d2,d3 & d4.

modwt函数的输出如下所示

The output of the modwt function looks as follows

    > str(ar1.modwt)
List of 5
 $ d1: num [1:200] -0.223 -0.12 0.438 -0.275 0.21 ...
 $ d2: num [1:200] 0.1848 -0.4699 -1.183 -0.9698 -0.0937 ...
 $ d3: num [1:200] 0.5912 0.6997 0.5416 0.0742 -0.4989 ...
 $ d4: num [1:200] 1.78 1.86 1.85 1.78 1.65 ...
 $ s4: num [1:200] 4.64 4.42 4.19 3.94 3.71 ...
 - attr(*, "class")= chr "modwt"
 - attr(*, "wavelet")= chr "la8"
 - attr(*, "boundary")= chr "periodic"

在上面的示例中,我已将modwt函数应用于长度为200的全长时间序列,但是我希望使用rollapply将其应用于30的较小滚动窗口.

In the example above I have applied the modwt function to the full length time series of length 200 but I wish to apply it to a small rolling window of 30 using rollapply.

我已经尝试了以下方法,但是输出是一个大矩阵,我无法轻松确定哪些值属于d1,d2,d3或d4

I have already tried the following but the output is a large matrix and I cannot easily identify which values belong to d1,d2,d3 or d4

roller <- rollapplyr(ar1, 30,FUN=modwt,wf="la8",n.levels=4,boundary="periodic")

此输出是一个具有以下结构的大矩阵:

The output of this is a large matrix with the following structure:

> str(roller)
List of 855
 $ : num [1:30] 0.117 -0.138 0.199 -1.267 1.872 ...
 $ : num [1:30] -0.171 0.453 -0.504 -0.189 0.849 ...
 $ : num [1:30] 0.438 -0.3868 0.1618 -0.0973 -0.0247 ...
 $ : num [1:30] -0.418 0.407 0.639 -2.013 1.349 ...

...省略了很多行...

...lots of rows omitted...

$ : num [1:30] 0.307 -0.658 -0.105 1.128 -0.978 ...
  [list output truncated]
 - attr(*, "dim")= int [1:2] 171 5
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:5] "d1" "d2" "d3" "d4" ...

如何设置变量以存储(200-30)+1列表以及其中每个d1,d2,d3和d4标度的列表?

How can I set up a variable such that it will store the (200-30)+1 lists with lists within this for each of the scales d1,d2,d3 and d4?

有关可重现的示例,请使用以下命令:

For a reproducible example please use the following:

library(waveslim)
data(ar1)
ar1.modwt <- modwt(ar1, "la8", 4)

推荐答案

定义调用modwt的modwt2,获取前4个组件并将它们串成数字向量.然后将rollapplyr与给出的rollr一起使用,其中rollr的每一行都是对modwt2的一次调用的结果.最后,将rollr的每一行重塑为单独的矩阵,并创建这些矩阵的列表L:

Define modwt2 which invokes modwt, takes the first 4 components and strings them out into a numeric vector. Then use rollapplyr with that giving rollr where each row of rollr is the result of one call to modwt2. Finally, reshape each row of rollr into a separate matrix and create a list, L, of those matrices:

modwt2 <- function(...) unlist(head(modwt(...), 4))
rollr <- rollapplyr(ar1, 30, FUN = modwt2, wf = "la8", n.levels = 4, boundary = "periodic")
L <- lapply(1:nrow(rollr), function(i) matrix(rollr[i,], , 4))

如果需要30 x 4 x 171阵列,则以下内容会将其简化为3d阵列:

If a 30 x 4 x 171 array is desired then the following will simplify it into a 3d array:

simplify2array(L)

或作为列表列表:

lapply(L, function(x) as.list(as.data.frame(x)))

2):这是一种替代解决方案,仅直接使用lapply并返回一个列表,每个列表的组成部分都是由d1,d2,d3和d4组成的列表.

2) This is an alternate solution that just uses lapply directly and returns a list each of whose components is the list consisting of d1, d2, d3 and d4.

lapply(1:(200-30+1), function(i, ...) head(modwt(ar1[seq(i, length = 30)], ...), 4),
  wf = "la8", n.levels = 4, boundary = "periodic")

更新:代码改进,展开(1)并添加(2).

Updates: Code improvements, expand (1) and add (2).

这篇关于使用rollapply输出到列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆