Skip to content

Commit 94577a7

Browse files
committed
minor cleaning of error messages
1 parent 816217c commit 94577a7

File tree

3 files changed

+53
-60
lines changed

3 files changed

+53
-60
lines changed

R/validate.R

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,24 @@ extract_effects <- function(formula, family = NA, nonlinear = NULL,
115115
}
116116
}
117117
} else {
118-
stop("Argument '", af, "' is not supported for family '",
119-
family$family, "'", call. = FALSE)
118+
stop2("Argument '", af, "' is not supported for ",
119+
"family '", family$family, "'.")
120120
}
121121
} else if (length(matches) > 1L) {
122-
stop("Addition arguments may be only defined once.",
123-
call. = FALSE)
122+
stop2("Addition arguments may be only defined once.")
124123
}
125124
}
126125
if (length(add_terms)) {
127-
stop("Invalid addition part of formula. \nPlease see the ",
128-
"'Details' section of help(brmsformula).", call. = FALSE)
126+
stop2("Invalid addition part of formula. \nPlease see the ",
127+
"'Details' section of help(brmsformula).")
129128
}
130129
if (is.formula(x$se) && is.formula(x$disp)) {
131-
stop("Addition arguments 'se' and 'disp' cannot be used ",
132-
"at the same time.", call. = FALSE)
130+
stop2("Addition arguments 'se' and 'disp' cannot ",
131+
"be used at the same time.")
133132
}
134133
cens_or_weights <- is.formula(x$cens) || is.formula(x$weights)
135134
if (is.formula(x$trunc) && cens_or_weights) {
136-
stop2("truncation is not yet possible in censored or weighted models.")
135+
stop2("Truncation is not yet possible in censored or weighted models.")
137136
}
138137
}
139138
}
@@ -168,8 +167,8 @@ extract_effects <- function(formula, family = NA, nonlinear = NULL,
168167
}
169168
if (is.linear(family) && length(x$response) > 1L &&
170169
length(rmNULL(x[c("se", "cens", "trunc")]))) {
171-
stop("Multivariate models currently allow only ",
172-
"weights as addition arguments", call. = FALSE)
170+
stop2("Multivariate models currently allow only ",
171+
"addition argument 'weights'.")
173172
}
174173
if (old_mv) {
175174
# multivariate ('trait') syntax is deprecated as of brms 1.0.0
@@ -192,9 +191,9 @@ extract_effects <- function(formula, family = NA, nonlinear = NULL,
192191
inv_auxpars <- setdiff(auxpars(), valid_auxpars(family, effects = x))
193192
inv_auxpars <- intersect(inv_auxpars, names(x))
194193
if (length(inv_auxpars)) {
195-
stop("Prediction of the parameter(s) ",
196-
paste0("'", inv_auxpars, "'", collapse = ", "),
197-
" is not allowed for this model.", call. = FALSE)
194+
inv_auxpars <- paste0("'", inv_auxpars, "'", collapse = ", ")
195+
stop2("Prediction of parameter(s) ", inv_auxpars,
196+
" is not allowed for this model.")
198197
}
199198
}
200199
x
@@ -212,7 +211,7 @@ extract_mono <- function(formula) {
212211
mono_terms <- substr(mono_terms, 11, nchar(mono_terms) - 1)
213212
mono_terms <- formula(paste("~", paste(mono_terms, collapse = "+")))
214213
if (!length(all.vars(mono_terms))) {
215-
stop("No variable supplied to function 'monotonic'.", call. = FALSE)
214+
stop2("No variable supplied to function 'monotonic'.")
216215
}
217216
attr(mono_terms, "rsv_intercept") <- TRUE
218217
}
@@ -226,8 +225,8 @@ extract_cse <- function(formula, family = NA) {
226225
cse_terms <- all_terms[pos_cse_terms]
227226
if (length(cse_terms)) {
228227
if (!is.na(family[[1]]) && !allows_cse(family)) {
229-
stop("Category specific effects are only meaningful for ",
230-
"families 'sratio', 'cratio', and 'acat'.", call. = FALSE)
228+
stop2("Category specific effects are only meaningful for ",
229+
"families 'sratio', 'cratio', and 'acat'.")
231230
}
232231
cse_terms <- substr(cse_terms, 5, nchar(cse_terms) - 1)
233232
cse_terms <- formula(paste("~", paste(cse_terms, collapse = "+")))
@@ -305,7 +304,7 @@ extract_response <- function(formula, keep_dot_usc = FALSE) {
305304
formula <- lhs(as.formula(formula))
306305
all_vars <- all.vars(formula)
307306
if (!length(all_vars)) {
308-
stop("formula contains no response variables", call. = FALSE)
307+
stop2("The formula contains no response variables.")
309308
}
310309
mf <- as.data.frame(named_list(all_vars, values = 1))
311310
mf <- model.frame(formula, data = mf, na.action = NULL)
@@ -321,7 +320,7 @@ extract_response <- function(formula, keep_dot_usc = FALSE) {
321320
response[empty_names] <- paste0("response", empty_names)
322321
}
323322
} else {
324-
stop("invalid response part of 'formula'", call. = FALSE)
323+
stop2("Response part of 'formula' is invalid.")
325324
}
326325
response <- make.names(response, unique = TRUE)
327326
if (!keep_dot_usc) {
@@ -343,13 +342,13 @@ extract_time <- function(formula) {
343342
}
344343
formula <- as.formula(formula)
345344
if (!is.null(lhs(formula))) {
346-
stop2("autocorrelation formula must be one-sided")
345+
stop2("Autocorrelation formula must be one-sided.")
347346
}
348347
formula <- formula2string(formula)
349348
time <- as.formula(paste("~", gsub("~|\\|[[:print:]]*", "", formula)))
350349
time <- all.vars(time)
351350
if (length(time) > 1L) {
352-
stop2("Autocorrelation structures may only contain 1 time variable")
351+
stop2("Autocorrelation structures may only contain 1 time variable.")
353352
}
354353
x <- list(time = ifelse(length(time), time, ""))
355354
group <- sub("^\\|*", "", sub("~[^\\|]*", "", formula))
@@ -382,8 +381,8 @@ extract_nonlinear <- function(x, model = ~1, family = NA) {
382381
model_vars <- all.vars(rhs(model))
383382
missing_pars <- setdiff(names(nleffects), model_vars)
384383
if (length(missing_pars)) {
385-
stop("Some non-linear parameters are missing in formula: ",
386-
paste(missing_pars, collapse = ", "), call. = FALSE)
384+
stop2("Some non-linear parameters are missing in formula: ",
385+
paste0("'", missing_pars, "'", collapse = ", "))
387386
}
388387
} else {
389388
nleffects <- list()
@@ -433,8 +432,8 @@ update_formula <- function(formula, data = NULL, family = gaussian(),
433432
old_attributes <- attributes(formula)
434433
fnew <- ". ~ ."
435434
if (!is.null(partial)) {
436-
warning("Argument 'partial' is deprecated. Please use the 'cse' ",
437-
"function inside the model formula instead.", call. = FALSE)
435+
warning2("Argument 'partial' is deprecated. Please use the 'cse' ",
436+
"function inside the model formula instead.")
438437
partial <- formula2string(as.formula(partial), rm = 1)
439438
fnew <- paste(fnew, "+ cse(", partial, ")")
440439
}
@@ -452,8 +451,8 @@ update_formula <- function(formula, data = NULL, family = gaussian(),
452451
model_response <- model.response(model.frame(respform, data = data))
453452
response <- levels(factor(model_response))
454453
if (length(response) <= 2L) {
455-
stop("At least 3 response categories are required ",
456-
"for categorical models", call. = FALSE)
454+
stop2("At least 3 response categories are required ",
455+
"for family 'categorical'.")
457456
}
458457
# the first level will serve as the reference category
459458
attr(formula, "response") <- response[-1]
@@ -515,8 +514,8 @@ split_re_terms <- function(re_terms) {
515514
comb_mono_terms <- paste(lhs_all_terms[pos_mono], collapse = "+")
516515
comb_mono_terms <- gsub("[ \t\r\n]+", "", comb_mono_terms, perl = TRUE)
517516
if (!identical(lhs_terms[i], comb_mono_terms)) {
518-
stop("Please specify monotonic effects in separate ",
519-
"group-level terms.", call. = FALSE)
517+
stop2("Please specify monotonic effects ",
518+
"in separate group-level terms.")
520519
}
521520
lhs_terms[i] <- formula2string(lhs_form_mono, rm = 1)
522521
type[[i]] <- "mono"
@@ -527,8 +526,8 @@ split_re_terms <- function(re_terms) {
527526
comb_cse_terms <- paste(lhs_all_terms[pos_cse], collapse = "+")
528527
comb_cse_terms <- gsub("[ \t\r\n]+", "", comb_cse_terms, perl = TRUE)
529528
if (!identical(lhs_terms[i], comb_cse_terms)) {
530-
stop("Please specify category specific effects in separate ",
531-
"group-level terms.", call. = FALSE)
529+
stop2("Please specify category specific effects ",
530+
"in separate group-level terms.")
532531
}
533532
lhs_terms[i] <- formula2string(lhs_form_cse, rm = 1)
534533
type[[i]] <- "cse"
@@ -539,9 +538,8 @@ split_re_terms <- function(re_terms) {
539538
new_groups <- c(groups[1], rep("", length(groups) - 1L))
540539
for (j in seq_along(groups)) {
541540
if (illegal_group_expr(groups[j])) {
542-
stop("Illegal grouping term: ", rhs_terms[i], "\n may contain ",
543-
"only variable names combined by the symbols ':' or '/'",
544-
call. = FALSE)
541+
stop2("Illegal grouping term: ", rhs_terms[i], "\n may contain ",
542+
"only variable names combined by the symbols ':' or '/'")
545543
}
546544
if (j > 1L) {
547545
new_groups[j] <- paste0(new_groups[j - 1], ":", groups[j])
@@ -836,26 +834,23 @@ all_terms <- function(formula) {
836834
lhs_terms <- function(re_terms) {
837835
out <- get_matches("^[^\\|]*", re_terms)
838836
if (length(out) != length(re_terms)) {
839-
stop("One or more group-levels terms were invalid.",
840-
call. = FALSE)
837+
stop2("One or more group-levels terms were invalid.")
841838
}
842839
out
843840
}
844841

845842
mid_terms <- function(re_terms) {
846843
out <- get_matches("\\|([^\\|]*\\||)", re_terms)
847844
if (length(out) != length(re_terms)) {
848-
stop("One or more group-levels terms were invalid.",
849-
call. = FALSE)
845+
stop2("One or more group-levels terms were invalid.")
850846
}
851847
out
852848
}
853849

854850
rhs_terms <- function(re_terms) {
855851
out <- sub("^\\|", "", get_matches("\\|[^\\|]*$", re_terms))
856852
if (length(out) != length(re_terms)) {
857-
stop("One or more group-levels terms were invalid.",
858-
call. = FALSE)
853+
stop2("One or more group-levels terms were invalid.")
859854
}
860855
out
861856
}
@@ -878,12 +873,12 @@ amend_terms <- function(x) {
878873
term_labels <- attr(y, "term.labels")
879874
if (any(grepl("(^|:)(main|spec)($|:)", term_labels))) {
880875
if (any(grepl("(^|:)trait($|:)", term_labels))) {
881-
stop("formula may not contain variable 'trait' when ",
882-
"using variables 'main' or 'spec'", call. = FALSE)
876+
stop2("formula may not contain variable 'trait' when ",
877+
"using variables 'main' or 'spec'")
883878
}
884879
if (attr(y, "intercept")) {
885-
stop("formula may not contain an intercept when ",
886-
"using variables 'main' or 'spec'", call. = FALSE)
880+
stop2("formula may not contain an intercept when ",
881+
"using variables 'main' or 'spec'")
887882
}
888883
attr(x, "rsv_intercept") <- TRUE
889884
}
@@ -1002,8 +997,8 @@ tidy_ranef <- function(effects, data = NULL, all = TRUE, ncat = NULL) {
1002997
k <- match(id, used_ids)
1003998
rdat$id <- new_ids[k]
1004999
if (!identical(random$group[[i]], id_groups[k])) {
1005-
stop("Can only combine group-level terms of the ",
1006-
"same grouping factor.", call. = FALSE)
1000+
stop2("Can only combine group-level terms of the ",
1001+
"same grouping factor.")
10071002
}
10081003
} else {
10091004
used_ids <- c(used_ids, id)
@@ -1018,8 +1013,7 @@ tidy_ranef <- function(effects, data = NULL, all = TRUE, ncat = NULL) {
10181013
ranef <- do.call(rbind, c(list(empty_ranef()), ranef))
10191014
dup <- duplicated(ranef[, c("group", "coef", "nlpar")])
10201015
if (any(dup)) {
1021-
stop("Duplicated group-level effects are not allowed.",
1022-
call. = FALSE)
1016+
stop2("Duplicated group-level effects are not allowed.")
10231017
}
10241018
if (nrow(ranef)) {
10251019
for (id in unique(ranef$id)) {
@@ -1133,17 +1127,16 @@ check_brm_input <- function(x) {
11331127
# Args:
11341128
# x: A named list
11351129
if (x$chains %% x$cluster != 0L) {
1136-
stop("chains must be a multiple of cluster", call. = FALSE)
1130+
stop2("'chains' must be a multiple of 'cluster'")
11371131
}
11381132
family <- check_family(x$family)
11391133
if (family$family == "inverse.gaussian") {
1140-
warning("Inverse gaussian models require carefully chosen ",
1141-
"prior distributions to ensure convergence of the chains.",
1142-
call. = FALSE)
1134+
warning2("Inverse gaussian models require carefully chosen ",
1135+
"prior distributions to ensure convergence of the chains.")
11431136
}
11441137
if (family$link == "sqrt") {
1145-
warning(family$family, " model with sqrt link may not be ",
1146-
"uniquely identified", call. = FALSE)
1138+
warning2(family$family, " model with sqrt link may not be ",
1139+
"uniquely identified")
11471140
}
11481141
invisible(NULL)
11491142
}

tests/testthat/tests.make_stancode.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ test_that("make_stancode detects invalid combinations of modeling options", {
173173
"Please set cov = TRUE", fixed = TRUE)
174174
expect_error(make_stancode(y1 | trunc(lb = -50) | weights(wi) ~ y2,
175175
data = data),
176-
"truncation is not yet possible")
176+
"Truncation is not yet possible")
177177
})
178178

179179
test_that("make_stancode is silent for multivariate models", {

tests/testthat/tests.validate.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ test_that("extract_effects returns expected error messages", {
126126
"Addition arguments 'se' and 'disp' cannot be used")
127127
expect_error(extract_effects(cbind(y1, y2) | se(z) ~ x,
128128
family = gaussian()),
129-
"Multivariate models currently allow only weights")
129+
"allow only addition argument 'weights'")
130130
expect_error(extract_effects(bf(y ~ x, shape ~ x), family = gaussian()),
131-
"Prediction of the parameter(s) 'shape' is not allowed",
131+
"Prediction of parameter(s) 'shape' is not allowed",
132132
fixed = TRUE)
133133
})
134134

@@ -172,7 +172,7 @@ test_that("extract_effects handles very long RE terms", {
172172

173173
test_that("extract_nonlinear finds missing parameters", {
174174
expect_error(extract_nonlinear(list(a = a ~ 1, b = b ~ 1), model = y ~ a^x),
175-
"missing in formula: b")
175+
"missing in formula: 'b'")
176176
})
177177

178178
test_that("extract_nonlinear accepts valid non-linear models", {
@@ -199,7 +199,7 @@ test_that("extract_time returns all desired variables", {
199199
expect_error(extract_time(~t1+t2|g1),
200200
"Autocorrelation structures may only contain 1 time variable")
201201
expect_error(extract_time(x~t1|g1),
202-
"autocorrelation formula must be one-sided")
202+
"Autocorrelation formula must be one-sided")
203203
expect_error(extract_time(~1|g1/g2),
204204
paste("Illegal grouping term: g1/g2"))
205205
})
@@ -298,7 +298,7 @@ test_that("tidy_ranef works correctly", {
298298

299299
test_that("check_brm_input returns correct warnings and errors", {
300300
expect_error(check_brm_input(list(chains = 3, cluster = 2)),
301-
"chains must be a multiple of cluster", fixed = TRUE)
301+
"'chains' must be a multiple of 'cluster'", fixed = TRUE)
302302
x <- list(family = inverse.gaussian(), chains = 1, cluster = 1,
303303
algorithm = "sampling")
304304
expect_warning(check_brm_input(x))

0 commit comments

Comments
 (0)