Code Monkey home page Code Monkey logo

Comments (4)

mattmills49 avatar mattmills49 commented on August 22, 2024
  • rolling_func: A function that can return a rolling function for whatever function you want to roll. Here is just rolling mean but I think we can abstract this further
roll_mean <- function(n, vec) seq(from = 0, to = n) %>% map(~ lag(vec, n = .x)) %>% do.call(cbind, .) %>% rowMeans(na.rm = T)

from modeler.

mattmills49 avatar mattmills49 commented on August 22, 2024
  • safe_ifelse returns the arguments in the same structure as the true and false options
safe_ifelse <- function(cond, yes, no) structure(ifelse(cond, yes, no), class = class(yes))

from modeler.

mattmills49 avatar mattmills49 commented on August 22, 2024
  • acf_by_group: finds auto correlations by group
acf_by_group <- function(.df, .group_var, .value_var, ...){
  group_var <- enquo(.group_var)
  value_var <- deparse(substitute(.value_var))
  nest_cols <- names(.df)
  group_col <- deparse(substitute(.group_var))
  nest_cols <- nest_cols[nest_cols != group_col]
  
  .df %>%
    tidyr::nest_(key_col = "data", nest_cols = nest_cols) %>%
    dplyr::mutate(acf_results = purrr::map(data, ~ acf(.x[[value_var]], plot = F, ...)),
                  acf_values = purrr::map(acf_results, ~ drop(.x$acf))) %>%
    tidyr::unnest(acf_values) %>%
    dplyr::group_by(!!group_var) %>%
    dplyr::mutate(lag = seq(0, n() - 1)) %>%
    dplyr::ungroup()
}

from modeler.

mattmills49 avatar mattmills49 commented on August 22, 2024

Rolling functions could also just use sequences like so

roll_numeric <- function(x, n, func, ...){
  stopifnot(length(x) > 0)
  positions <- seq_along(x)
  vapply(positions, function(i, .x = x, .n = n){
    start <- max(i - .n, 0) + 1
    func(.x[seq(start, i)], ...)
  }, numeric(1))
}

roll_mean <- function(...) roll_numeric(func = mean, ...)
roll_sum <- function(...) roll_numeric(func = sum, ...)

from modeler.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.