Skip to content

Commit

Permalink
coverage now over 85%!
Browse files Browse the repository at this point in the history
  • Loading branch information
njtierney committed Dec 10, 2024
1 parent 1e4069f commit 8d9a1df
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 9 deletions.
18 changes: 17 additions & 1 deletion R/checkers.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ warn_if_offsets_present <- function(jags_stuff,
warn_if_formula_has_lhs <- function(formula,
arg = rlang::caller_arg(formula),
call = rlang::caller_env()) {
if (length(formula) > 2) {
has_lhs <- rlang::is_formula(formula, lhs = TRUE)
if (has_lhs) {
cli::cli_warn(
c(
"Formula has a left hand side",
Expand All @@ -53,3 +54,18 @@ warn_if_formula_has_lhs <- function(formula,
)
}
}

check_if_formula <- function(formula,
arg = rlang::caller_arg(formula),
call = rlang::caller_env()){
not_formula <- !rlang::is_bare_formula(formula)
if (not_formula) {
cli::cli_abort(
c(
"Input must be a formula",
"We see that {.code formula} has class, {.cls {class(formula)}}."
),
call = call
)
}
}
10 changes: 7 additions & 3 deletions R/jagam2greta.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ jagam2greta <- function(formula,

# do the jagam call, store the JAGS code gets stored in jags_spec
jags_spec <- ""
jags_stuff <- mgcv::jagam(formula, data,
family = stats::gaussian(), knots = knots,
file = textConnection("jags_spec",
jags_stuff <- mgcv::jagam(
formula = formula,
data = data,
family = stats::gaussian(),
knots = knots,
file = textConnection(
"jags_spec",
open = "a",
local = TRUE
)
Expand Down
1 change: 1 addition & 0 deletions R/smooths.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ smooths <- function(formula,
knots = NULL,
sp = NULL,
tol = 0) {
check_if_formula(formula)
warn_if_formula_has_lhs(formula)

# get all the MGCV objects for Bayesian version, converted to greta arrays
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/_snaps/evaluate-smooths.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# evaluate_smooths errors appropriately

Can only evaluate smooths from greta arrays created with `greta.gam::smooths()`
Code
evaluate_smooths("thing")
Condition
Error in `evaluate_smooths()`:
! Can only evaluate smooths from greta arrays created with `greta.gam::smooths()`

17 changes: 15 additions & 2 deletions tests/testthat/_snaps/stop-when-dummy-in-data.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# stop_when_dummy_in_data errors appropriately

Data cannot already contain column named `dummy`
i Rename existing column, perhaps to `dummy1`
Code
stop_when_dummy_in_data(test_df)
Condition
Error:
! Data cannot already contain column named `dummy`
i Rename existing column, perhaps to `dummy1`

---

Code
smooths(~ s(dummy), data = data.frame(dummy = x))
Condition
Error in `jagam2greta()`:
! Data cannot already contain column named `dummy`
i Rename existing column, perhaps to `dummy1`

16 changes: 16 additions & 0 deletions tests/testthat/test-check-formula.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
test_that("check_if_formula works", {
expect_snapshot(
error = TRUE,
check_if_formula("potato")
)
expect_snapshot(
error = TRUE,
check_if_formula("y~x")
)
expect_success(
check_if_formula(y~x)
)
expect_success(
check_if_formula(y~s(x))
)
})
18 changes: 17 additions & 1 deletion tests/testthat/test-evaluate-smooths.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
n <- 30
x <- runif(n, 0, 10)
f <- function(x) {
sin(x * 2) + 1.6 * (x < 3) - 1.4 * (x > 7)
}
y <- f(x) + rnorm(n, 0, 0.3)
x_plot <- seq(0, 10, length.out = 200)

z <- smooths(~ s(x), data = data.frame(x = x))

distribution(y) <- normal(z, 0.3)

test_that("evaluate_smooths errors appropriately", {
expect_snapshot_error(
expect_snapshot(
error = TRUE,
evaluate_smooths("thing")
)
expect_no_error(
z_pred <- evaluate_smooths(z, newdata = data.frame(x = x_plot))
)
})
20 changes: 19 additions & 1 deletion tests/testthat/test-stop-when-dummy-in-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,30 @@ test_df <- data.frame(
thing = LETTERS[1:5]
)

n <- 30
x <- runif(n, 0, 10)
f <- function(x) {
sin(x * 2) + 1.6 * (x < 3) - 1.4 * (x > 7)
}
y <- f(x) + rnorm(n, 0, 0.3)
x_plot <- seq(0, 10, length.out = 200)

z <- smooths(~ s(x), data = data.frame(x = x))


test_that("stop_when_dummy_in_data errors appropriately", {
expect_snapshot_error(
expect_snapshot(
error = TRUE,
stop_when_dummy_in_data(test_df)
)

expect_no_error(
stop_when_dummy_in_data(mtcars)
)

expect_snapshot(
error = TRUE,
smooths(~ s(dummy), data = data.frame(dummy = x))
)

})

0 comments on commit 8d9a1df

Please sign in to comment.