Skip to content

grouped_df - k value ignored if not using run_by #79

@Aylon1

Description

@Aylon1

Hi,

I came across an issue with grouped_df/dplyr usage of runner. Using the example from the vigngette following works fine and produces the desired windows:

x <- cumsum(rnorm(20))
y <- 3 * x + rnorm(20)
date <- Sys.Date() + cumsum(sample(1:3, 20, replace = TRUE)) # unequaly spaced time series
group <-  rep(c("a", "b"), each = 10)


data.frame(date, group, y, x) %>%
  group_by(group) %>%
  run_by(idx = "date", k = "5 days") %>%
  mutate(
    alpha_5 = runner(
      x = ., 
      f = function(x) {
        print(nrow(x))
        coefficients(lm(x ~ y, x))[1]
      }
    ),
    beta_5 = runner(
      x = ., 
      f = function(x) {
        print(nrow(x))
        coefficients(lm(x ~ y, x))[1]
      }
    )
  )

# Output:
[1] 1
[1] 2
[1] 3
[1] 2
[1] 2
[1] 2
[1] 3
[1] 3
[1] 3
[1] 3
[1] 1
[1] 2
[1] 3
[1] 3
[1] 3
[1] 4
[1] 4
[1] 4
[1] 3

However if I move the idx & k into the runner calls, the window always starts at first index and the window grows each iteration:

library(runner)
x <- cumsum(rnorm(20))
y <- 3 * x + rnorm(20)
date <- Sys.Date() + cumsum(sample(1:3, 20, replace = TRUE)) # unequaly spaced time series
group <-  rep(c("a", "b"), each = 10)


data.frame(date, group, y, x) %>%
  group_by(group) %>%
  #run_by(idx = "date", k = "5 days") %>%
  mutate(
    alpha_5 = runner(
      x = ., idx = "date", k = "5 days",
      f = function(x) {
        print(nrow(x))
        coefficients(lm(x ~ y, x))[1]
      }
    ),
    beta_5 = runner(
      x = ., idx = "date", k = "5 days",
      f = function(x) {
        print(nrow(x))
        coefficients(lm(x ~ y, x))[1]
      }
    )
  )

# Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10

this might not be visible in near all contexts for users but created strage results in my anaylisis leading to me raising this issue.

The different results are however quite drastic and is this probaly a bug?

Best
Thorsten

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions