R/get_ts.R

Defines functions get_ts

globalVariables(c(".", "region"))

get_ts <- function(x, nregions){
  ids <- data.table(id = x$regions[[1]], region = x$regions[[9 + nregions]])
  out <- merge(x$input_dt, ids, by = "id")  
  out <- unique(out[, .(variable = mean(variable)), .(time, region)])
  return(out)
}

cor_mat <- function(x) {
  mat <- as.matrix(dcast(x, time~region, value.var = "variable")[, -1])
  out <- cor(mat)
  return(out)
}

cor_regs <- function(x, n, ...){
  reg_ts <- get_ts(x, n)
  mat <- cor_mat(reg_ts)
  return(mat)
}

Try the somspace package in your browser

Any scripts or data that you put into this service are public.

somspace documentation built on April 29, 2023, 1:11 a.m.