Skip to content

Commit

Permalink
update code styling styler
Browse files Browse the repository at this point in the history
  • Loading branch information
EuanMcGonigle committed Apr 18, 2024
1 parent 6475216 commit 111d2a8
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 130 deletions.
6 changes: 4 additions & 2 deletions R/multilag.cpts.merge.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
#' x.c
#' @seealso \link{np.mojo}, \link{np.mojo.multilag}
multilag.cpts.merge <- function(x.c, eta.merge = 1, merge.type = c("sequential", "bottom-up")[1]) {
stopifnot("Error: change point merging type must be either 'sequential' or 'bottom-up'." =
merge.type == "sequential" || merge.type == "bottom-up")
stopifnot(
"Error: change point merging type must be either 'sequential' or 'bottom-up'." =
merge.type == "sequential" || merge.type == "bottom-up"
)

cpts <- init.cpts <- matrix(NA, nrow = 0, ncol = 3)
dimnames(init.cpts)[[2]] <- c("cp", "lag", "p.val")
Expand Down
16 changes: 9 additions & 7 deletions R/multiscale.np.mojo.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
#' noise <- c(rep(1, 300), rep(0.4, 200)) * stats::arima.sim(model = list(ar = 0.3), n = n)
#' signal <- c(rep(0, 100), rep(2, 400))
#' x <- signal + noise
#' x.c <- multiscale.np.mojo(x, G = c(50,80), lags = c(0, 1))
#' x.c <- multiscale.np.mojo(x, G = c(50, 80), lags = c(0, 1))
#' x.c$cpts
#' @seealso \link{np.mojo.multilag}
multiscale.np.mojo <- function(x, G, lags = c(0, 1), kernel.f = c("quad.exp", "gauss", "euclidean", "laplace", "sine")[1],
Expand All @@ -109,8 +109,10 @@ multiscale.np.mojo <- function(x, G, lags = c(0, 1), kernel.f = c("quad.exp", "g
boot.method = c("mean.subtract", "no.mean.subtract")[1], criterion = c("eta", "epsilon", "eta.and.epsilon")[3],
eta = 0.4, epsilon = 0.02, use.mean = FALSE, eta.merge = 1, merge.type = c("sequential", "bottom-up")[1],
eta.bottom.up = 0.8) {
stopifnot("Error: change point merging type must be either 'sequential' or 'bottom-up'." =
merge.type == "sequential" || merge.type == "bottom-up")
stopifnot(
"Error: change point merging type must be either 'sequential' or 'bottom-up'." =
merge.type == "sequential" || merge.type == "bottom-up"
)

if (!is.numeric(lags)) {
stop("The set of lags must be a numeric vector of positive integer values.")
Expand Down Expand Up @@ -146,10 +148,10 @@ multiscale.np.mojo <- function(x, G, lags = c(0, 1), kernel.f = c("quad.exp", "g
cptsInOrder <- seq_len(nrow(init.cpts))

for (i in cptsInOrder) {
p <- init.cpts[i,1]
l <- init.cpts[i,2]
pVal <- init.cpts[i,3]
G.val <- init.cpts[i,4]
p <- init.cpts[i, 1]
l <- init.cpts[i, 2]
pVal <- init.cpts[i, 3]
G.val <- init.cpts[i, 4]
if (suppressWarnings(min(abs(p - points))) >= eta.bottom.up * G.val) {
points <- c(points, p)
bandwidths <- c(bandwidths, G.val)
Expand Down
3 changes: 1 addition & 2 deletions R/np.mojo.R
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ np.mojo <- function(x, G, lag = 0, kernel.f = c("quad.exp", "gauss", "euclidean"
start.r <- end.r - r$lengths + 1

epsilon.satisfied.start <- stat.exceed[start.r[r$lengths > epsilon * G]]
epsilon.satisfied.end <- stat.exceed[end.r[r$lengths > epsilon * G]]+1
epsilon.satisfied.end <- stat.exceed[end.r[r$lengths > epsilon * G]] + 1

epsilon.exceedings <- rep(FALSE, data.len)
for (i in seq_len(length(epsilon.satisfied.start))) {
Expand All @@ -248,7 +248,6 @@ np.mojo <- function(x, G, lag = 0, kernel.f = c("quad.exp", "gauss", "euclidean"
for (i in seq_len(length(cpt.locs))) {
p.vals <- c(p.vals, sum(Tstar >= test.stat[cpt.locs[i]]) / (reps + 1))
}

}
} else {
if (threshold == "bootstrap") {
Expand Down
6 changes: 4 additions & 2 deletions R/np.mojo.multilag.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ np.mojo.multilag <- function(x, G, lags = c(0, 1), kernel.f = c("quad.exp", "gau
alpha = 0.1, reps = 199, boot.dep = 1.5 * (nrow(as.matrix(x))^(1 / 3)), parallel = FALSE,
boot.method = c("mean.subtract", "no.mean.subtract")[1], criterion = c("eta", "epsilon", "eta.and.epsilon")[3],
eta = 0.4, epsilon = 0.02, use.mean = FALSE, eta.merge = 1, merge.type = c("sequential", "bottom-up")[1]) {
stopifnot("Error: change point merging type must be either 'sequential' or 'bottom-up'." =
merge.type == "sequential" || merge.type == "bottom-up")
stopifnot(
"Error: change point merging type must be either 'sequential' or 'bottom-up'." =
merge.type == "sequential" || merge.type == "bottom-up"
)

if (!is.numeric(lags)) {
stop("The set of lags must be a numeric vector of positive integer values.")
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ mojo.error.checks <- function(x, G, lag, kernel.f, kern.par, data.driven.kern.pa
stop("The lag parameter should be a single positive integer.")
}

if (!is.numeric(G) || (G < 0) || (G %% 1 != 0) ) {
if (!is.numeric(G) || (G < 0) || (G %% 1 != 0)) {
stop("Bandwidth parameter G must be numeric positive integer.")
}

Expand Down
22 changes: 12 additions & 10 deletions tests/testthat/test-multilag.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Tests for multilag.cpts.merge function
# Tests for multilag.cpts.merge function

n <- 1000
set.seed(123)
Expand All @@ -15,27 +15,29 @@ x.nocp <- stats:::rnorm(1000)

G <- 166

x.c0 <- np.mojo(x,G,0)
x.c1 <- np.mojo(x,G,1)
x.c0 <- np.mojo(x, G, 0)
x.c1 <- np.mojo(x, G, 1)

test_that("multilag.cpts.merge executes with sequential merging", {
skip_on_cran()
expect_equal(class(multilag.cpts.merge(x.c = list(x.c0,x.c1), merge.type = "sequential")), "list")
expect_equal(class(multilag.cpts.merge(x.c = list(x.c0, x.c1), merge.type = "sequential")), "list")
})

test_that("multilag.cpts.merge executes with bottom-up merging", {
skip_on_cran()
expect_equal(class(multilag.cpts.merge(x.c = list(x.c0, x.c1), merge.type = "bottom-up")), "list")
})

test_that("Change point merging type is recognised",{
expect_error(multilag.cpts.merge(x.c = list(x.c0, x.c1), merge.type = "pruning"),
"Error: change point merging type must be either 'sequential' or 'bottom-up'.")
test_that("Change point merging type is recognised", {
expect_error(
multilag.cpts.merge(x.c = list(x.c0, x.c1), merge.type = "pruning"),
"Error: change point merging type must be either 'sequential' or 'bottom-up'."
)
})

test_that("multilag.cpts.merge executes when no change points", {
skip_on_cran()
x.nocp0 <- np.mojo(x.nocp,G,0, threshold = "manual", threshold.val = 5)
x.nocp1 <- np.mojo(x.nocp,G,1, threshold = "manual", threshold.val = 5)
expect_equal(class(multilag.cpts.merge(x.c = list(x.nocp0,x.nocp1), merge.type = "sequential")), "list")
x.nocp0 <- np.mojo(x.nocp, G, 0, threshold = "manual", threshold.val = 5)
x.nocp1 <- np.mojo(x.nocp, G, 1, threshold = "manual", threshold.val = 5)
expect_equal(class(multilag.cpts.merge(x.c = list(x.nocp0, x.nocp1), merge.type = "sequential")), "list")
})
22 changes: 19 additions & 3 deletions tests/testthat/test-multiscale.np.mojo.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,27 @@ test_that("multiscale NP-MOJO executes", {
noise <- c(rep(1, 300), rep(0.4, 200)) * stats::rnorm(n)
signal <- c(rep(0, 100), rep(2, 400))
x <- signal + noise
x.c <- multiscale.np.mojo(x, G = c(50,80), lags = c(0, 1), reps = 10)
x.c <- multiscale.np.mojo(x, G = c(50, 80), lags = c(0, 1), reps = 10)
expect_equal(class(x.c), "list")
})

test_that("multiscale NP-MOJO rejects incorrect bandwidths", {
expect_error(multiscale.np.mojo(stats::rnorm(100), G = c(-10,30)),
"Bandwidth parameter G must be numeric positive integer.")
expect_error(
multiscale.np.mojo(stats::rnorm(100), G = c(-10, 30)),
"Bandwidth parameter G must be numeric positive integer."
)
})

test_that("multiscale NP-MOJO rejects non-numeric lags", {
expect_error(
multiscale.np.mojo(stats::rnorm(100), G = c(10, 30), lags = c("1", "2")),
"The set of lags must be a numeric vector of positive integer values."
)
})

test_that("multiscale NP-MOJO rejects negative lags", {
expect_error(
multiscale.np.mojo(stats::rnorm(100), G = c(10, 30), lags = c(-1, 0, 3)),
"The set of lags must be a numeric vector of positive integer values."
)
})
Loading

0 comments on commit 111d2a8

Please sign in to comment.