Skip to content

Instantly share code, notes, and snippets.

@cszang
Created February 6, 2018 14:09
Show Gist options
  • Save cszang/fd144ae329ba6cc206ce80eb7687943d to your computer and use it in GitHub Desktop.
Save cszang/fd144ae329ba6cc206ce80eb7687943d to your computer and use it in GitHub Desktop.
Simple bivariate "Gershunov-test"
library(zoo)
g_test <- function(x, y, w = 20, l = 1000) {
cor_fun <- function(x) cor(x[,1], x[,2])
## benchmark
d0 <- data.frame(x, y)
c0 <- rollapply(d0, width = w, cor_fun, by.column = FALSE)
## l simulations
di <- lapply(1:l, function(f) {
data.frame(x, rnorm(length(x), mean = mean(y), sd = sd(y)))
})
ci <- lapply(sim, function(x) rollapply(x, width = w, cor_fun,
by.column = FALSE))
## get sds and compare
s0 <- sd(c0)
si <- sapply(ci, sd)
ecdf(si)(s0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment