-
Notifications
You must be signed in to change notification settings - Fork 5
Closed
Description
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] 10this 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
Labels
No labels