Skip to content

Instantly share code, notes, and snippets.

@ssh352
Created January 5, 2022 02:43
Show Gist options
  • Save ssh352/26eaa9fc99a1dcee2ff50664bfef3c09 to your computer and use it in GitHub Desktop.
Save ssh352/26eaa9fc99a1dcee2ff50664bfef3c09 to your computer and use it in GitHub Desktop.
styler format
library(dplyr)
library(tidyr)
library(purrr) # map
library(slider)
# only returns rsquared
get_coef1 <- function(data) {
coef1 <- lm(data = data, r1 ~ r2 + r3) %>%
coef() %>%
.["r2"] %>%
unname()
return(coef1)
}
data <- tibble(
t = rep(1:10, 3),
case = c(rep("a", 10), rep("b", 10), rep("c", 10)),
r1 = rnorm(30),
r2 = rnorm(30),
r3 = rnorm(30)
)
stylerFormat %>%
group_by(case) %>%
nest() %>%
mutate(rollreg = map(data, ~ .x %>%
mutate(coef1 = slider::slide_dbl(., ~ get_coef1(.x),
.before = Inf,
.complete = TRUE
)))) %>%
select(-data) %>%
unnest(rollreg)
desiredFormat %>%
group_by(case) %>%
nest() %>%
mutate(
rollreg = map(data, ~ .x %>%
mutate(
coef1 = slider::slide_dbl(
.,
~ get_coef1(.x),
.before = Inf,
.complete = TRUE
)
)
)
) %>%
select(-data) %>%
unnest(rollreg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment