context("dredge_correlation")
library(magrittr)
#library(testthat)
rm(list = ls())
set.seed(0)
seqcol <- seq(-10, 10, .5) %>% as.integer()
seqcol_tosmall <- 1:2
noisecol <- seqcol %>% length() %>% rnorm()
sncol <- seqcol + noisecol
s3ncol <- seqcol^3 + noisecol
charcol <- rep('a', seqcol %>% length())
empty_grid <-
data.frame(
pair_1 = character(),
pair_2 = character(),
lag = integer())
#### parameters invalid on its face ####
# bad df
expect_error(
NULL %>%
dredge_correlation(
grid = empty_grid,
min_pvalue = 0,
max_pvalue = .05),
"data %>% is.data.frame()",
fixed = T)
# not enough rows
expect_error(
data.frame(seqcol_tosmall) %>%
dredge_correlation(
grid = empty_grid,
min_pvalue = 0,
max_pvalue = .05),
"data %>% nrow() >= 3",
fixed = T)
# bad grid
expect_error(
data.frame(seqcol) %>%
dredge_correlation(
grid = NULL,
min_pvalue = 0,
max_pvalue = .05),
"grid %>% is.data.frame()",
fixed = T)
# bad min_pvalue
expect_error(
data.frame(seqcol) %>%
dredge_correlation(
grid = empty_grid,
min_pvalue = "a",
max_pvalue = 0.05),
"min_pvalue %>% is.numeric()",
fixed = T)
# min_pvalue too small
expect_error(
data.frame(seqcol) %>%
dredge_correlation(
grid = empty_grid,
min_pvalue = -1,
max_pvalue = 0.05),
"min_pvalue >= 0",
fixed = T)
# min_pvalue too big
expect_error(
data.frame(seqcol) %>%
dredge_correlation(
grid = empty_grid,
min_pvalue = 1,
max_pvalue = 0.05),
"min_pvalue < 1",
fixed = T)
# bad max_pvalue
expect_error(
data.frame(seqcol) %>%
dredge_correlation(
grid = empty_grid,
min_pvalue = 0,
max_pvalue = "a"),
"max_pvalue %>% is.numeric()",
fixed = T)
# max_pvalue too small
expect_error(
data.frame(seqcol) %>%
dredge_correlation(
grid = empty_grid,
min_pvalue = 0,
max_pvalue = -1),
"max_pvalue >= 0",
fixed = T)
# max_pvalue too big
expect_error(
data.frame(seqcol) %>%
dredge_correlation(
grid = empty_grid,
min_pvalue = 0,
max_pvalue = 1),
"max_pvalue < 1",
fixed = T)
# bad pvalue range
expect_error(
data.frame(seqcol) %>%
dredge_correlation(
grid = empty_grid,
min_pvalue = .5,
max_pvalue = .25),
"min_pvalue < max_pvalue",
fixed = T)
#### normal analysis ####
# empty grid
data <- data.frame(a = seqcol, b = sncol)
actual <-
data %>%
dredge_correlation(
grid = empty_grid,
min_pvalue = 0,
max_pvalue = .05)
expect_equal(actual %>% nrow(), 0)
# simple 2 column test
data <- data.frame(a = seqcol, b = sncol)
grid <- data %>% make_correlation_grid(max_lag = 0)
actual <-
data %>%
dredge_correlation(
grid = grid,
min_pvalue = 0,
max_pvalue = .05)
expected <-
data.frame(
column_a = c("a"),
column_b = c("b"),
estimate = c(0.9882),
p.value = c(0),
lag = c(0),
stringsAsFactors = F)
expect_equal(actual, expected, tolerance = 0.001)
# filter out poor correlation
data <- data.frame(a = seqcol, b = noisecol)
grid <- data %>% make_correlation_grid(max_lag = 0)
actual <-
data %>%
dredge_correlation(
grid = grid,
min_pvalue = 0,
max_pvalue = .05)
expect_equal(actual %>% nrow(), 0)
# simple 2 column test + lag
data <- data.frame(a = seqcol, b = sncol)
grid <- data %>% make_correlation_grid(max_lag = 1)
actual <-
data %>%
dredge_correlation(
grid = grid,
min_pvalue = 0,
max_pvalue = .05)
expected <-
data.frame(
column_a = c("a", "a"),
column_b = c("b", "b"),
estimate = c(0.9882, 0.9839),
p.value = c(0, 0),
lag = c(0, 1),
stringsAsFactors = F)
expect_equal(actual, expected, tolerance = 0.001)
# simple 3 column test
data <- data.frame(a = seqcol, b = sncol, c = s3ncol)
grid <- data %>% make_correlation_grid(max_lag = 0)
actual <-
data %>%
dredge_correlation(
grid = grid,
min_pvalue = 0,
max_pvalue = .05)
expected <-
data.frame(
column_a = c("a", "a", "b"),
column_b = c("b", "c", "c"),
estimate = c(0.9882, 0.9136, 0.9053),
p.value = c(0, 0, 0),
lag = c(0, 0, 0),
stringsAsFactors = F)
expect_equal(actual, expected, tolerance = 0.001)
# 3 column test + one poor column
data <- data.frame(a = seqcol, b = noisecol, c = s3ncol)
grid <- data %>% make_correlation_grid(max_lag = 0)
actual <-
data %>%
dredge_correlation(
grid = grid,
min_pvalue = 0,
max_pvalue = .05)
expected <-
data.frame(
column_a = c("a"),
column_b = c("c"),
estimate = c(0.9136),
p.value = c(0),
lag = c(0),
stringsAsFactors = F)
expect_equal(actual, expected, tolerance = 0.001)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.