Skip to content

Error message when (ab)using frollmean on list with adaptive = TRUE, and notes on documentation  #5306

@Henrik-P

Description

@Henrik-P

I have a list like

set.seed(1)
l = list(sample(5), sample(4))
l
# [[1]]
# [1] 1 4 3 5 2
# 
# [[2]]
# [1] 1 3 4 2

I want to apply a rolling mean with adaptive window size.

From ?frollmean:

x ... May also be a list, in which case the rolling function is applied to each of its elements

Thus, e.g. frollmean(x = l, n = 3) works just fine.

On n:

Adaptive rolling functions also accept a list of integer vectors.

If adaptive=TRUE, then n must be a list. Each list element must be integer vector of window sizes corresponding to every single observation in each column

Because I have a list, I interpret that "each column" could also mean "each list element".

So far everything seems fine and I am eager to try my code! Yes, additional relevant help text follows further down, but let's ignore it in order to demonstrate the error. So here we go, starting by creating a list of window sizes, where each list element is of the same length as the input list elements:

win_size = 3
win_sizes = lapply(lengths(l), function(len) c(seq.int(win_size), rep(win_size, len - win_size)))
win_sizes
# [[1]]
# [1] 1 2 3 3 3
#
# [[2]]
# [1] 1 2 3 3

frollmean(x = l, n = win_sizes, adaptive = TRUE)
# Error in froll(fun = "mean", x = x, n = n, fill = fill, algo = algo, align = align,  : 
#  length of integer vector(s) provided as list to 'n' argument must be equal to
# number of observations provided in 'x'

I may misinterpret the error message, but to me it suggests that the problem is "only" the n argument and that the number of values in each element in n ("win_sizes") differs from the number of values in each element in the list "l" passed to x. Which is not the case (lengths(l); lengths(win_sizes)). And the user may start to check and re-check the lengths.

However, as I wrote above, there is more to the help text:

Due to the logic of adaptive rolling functions, the following restrictions apply:
if list of vectors is passed to x, then all vectors within it must have equal length.

Thus, the main underlying issue seems to be the fact that the elements of the list passed to x do not have equal length, and not that n is specified incorrectly. If so, should the error message be adjusted accordingly?


Related notes on frollmean help text:

I'm a bit curious about the "Due to the logic of adaptive rolling functions" in

Due to the logic of adaptive rolling functions, the following restrictions apply:
if list of vectors is passed to x, then all vectors within it must have equal length.

This may come across like a more general constraint of adaptive rolling functions, which would make it impossible to apply them on lists with elements of different lengths. But I'm sure I have misinterpreted the text. Does it "only" refer to a limitation of the current implementation of frollmean? Perhaps this could be clarified.


Finally, from Details:

If adaptive=TRUE, then n must be a list.

This doesn't seem to be correct. At least in the first Example using adaptive=TRUE, a vector is passed to n. Furthermore, in Arguments the requirements on n seem less strict:

Adaptive rolling functions also accept a list of integer vectors. [not "must be a list"]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions