Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"unexpected numeric constant" when using mo() and $ factor level in interaction #1754

Open
shirdekel opened this issue Mar 14, 2025 · 1 comment
Labels
Milestone

Comments

@shirdekel
Copy link

I'd like to run a model with a monotonic predictor and an interaction with a variable that includes a dollar sign ("$") in the factor levels. I run into the following error:

suppressMessages({
  library(brms)
})

data <-
  data.frame(
    x = factor(letters[1:5], ordered = T),
    y = rnorm(1000),
    z = factor(c("a$", "b$"))
  )

## Works

brm(
  y ~ x * z,
  data = data,
  silent = 2,
  refresh = 0
)
#>  Family: gaussian 
#>   Links: mu = identity; sigma = identity 
#> Formula: y ~ x * z 
#>    Data: data (Number of observations: 1000) 
#>   Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
#>          total post-warmup draws = 4000
#> 
#> Regression Coefficients:
#>           Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
#> Intercept    -0.01      0.04    -0.10     0.08 1.00     4838     2738
#> x.L          -0.01      0.10    -0.20     0.19 1.00     3275     2768
#> x.Q           0.10      0.10    -0.10     0.29 1.00     3285     2791
#> x.C           0.02      0.10    -0.17     0.22 1.00     4100     2781
#> xE4          -0.12      0.10    -0.32     0.08 1.00     3585     2566
#> zb$          -0.00      0.07    -0.13     0.12 1.00     4948     2839
#> x.L:zb$       0.10      0.14    -0.18     0.37 1.00     3760     2753
#> x.Q:zb$      -0.06      0.14    -0.34     0.21 1.00     3259     2799
#> x.C:zb$      -0.06      0.14    -0.34     0.22 1.00     4179     3195
#> xE4:zb$       0.25      0.14    -0.03     0.52 1.00     3824     2900
#> 
#> Further Distributional Parameters:
#>       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
#> sigma     0.99      0.02     0.95     1.04 1.00     5001     3102
#> 
#> Draws were sampled using sampling(NUTS). For each parameter, Bulk_ESS
#> and Tail_ESS are effective sample size measures, and Rhat is the potential
#> scale reduction factor on split chains (at convergence, Rhat = 1).

## Doesn't work

brm(
  y ~ mo(x) * z,
  data = data,
  silent = 2,
  refresh = 0
)
#> Error in parse(text = x, keep.source = FALSE): <text>:2:0: unexpected end of input
#> 1: zb$
#>    ^

Created on 2025-03-14 with reprex v2.1.1

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.2 (2024-10-31)
#>  os       macOS Sequoia 15.3.1
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_AU.UTF-8
#>  ctype    en_AU.UTF-8
#>  tz       Australia/Sydney
#>  date     2025-03-14
#>  pandoc   2.19.2 @ /opt/homebrew/bin/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package        * version  date (UTC) lib source
#>  abind            1.4-8    2024-09-12 [1] CRAN (R 4.4.1)
#>  backports        1.5.0    2024-05-23 [1] CRAN (R 4.4.0)
#>  bayesplot        1.11.1   2024-02-15 [1] CRAN (R 4.4.0)
#>  bridgesampling   1.1-2    2021-04-16 [1] CRAN (R 4.4.0)
#>  brms           * 2.22.0   2024-09-23 [1] CRAN (R 4.4.1)
#>  Brobdingnag      1.2-9    2022-10-19 [1] CRAN (R 4.4.0)
#>  callr            3.7.6    2024-03-25 [1] CRAN (R 4.4.0)
#>  checkmate        2.3.2    2024-07-29 [1] CRAN (R 4.4.0)
#>  cli              3.6.3    2024-06-21 [1] CRAN (R 4.4.0)
#>  coda             0.19-4.1 2024-01-31 [1] CRAN (R 4.4.0)
#>  codetools        0.2-20   2024-03-31 [1] CRAN (R 4.4.2)
#>  colorspace       2.1-1    2024-07-26 [1] CRAN (R 4.4.0)
#>  curl             6.1.0    2025-01-06 [1] CRAN (R 4.4.1)
#>  digest           0.6.37   2024-08-19 [1] CRAN (R 4.4.1)
#>  distributional   0.5.0    2024-09-17 [1] CRAN (R 4.4.1)
#>  dplyr            1.1.4    2023-11-17 [1] CRAN (R 4.4.0)
#>  evaluate         1.0.1    2024-10-10 [1] CRAN (R 4.4.1)
#>  fastmap          1.2.0    2024-05-15 [1] CRAN (R 4.4.0)
#>  fs               1.6.5    2024-10-30 [1] CRAN (R 4.4.1)
#>  generics         0.1.3    2022-07-05 [1] CRAN (R 4.4.0)
#>  ggplot2          3.5.1    2024-04-23 [1] CRAN (R 4.4.0)
#>  glue             1.8.0    2024-09-30 [1] CRAN (R 4.4.1)
#>  gridExtra        2.3      2017-09-09 [1] CRAN (R 4.4.0)
#>  gtable           0.3.6    2024-10-25 [1] CRAN (R 4.4.1)
#>  htmltools        0.5.8.1  2024-04-04 [1] CRAN (R 4.4.0)
#>  inline           0.3.20   2024-11-10 [1] CRAN (R 4.4.1)
#>  jsonlite         1.8.9    2024-09-20 [1] CRAN (R 4.4.1)
#>  knitr            1.49     2024-11-08 [1] CRAN (R 4.4.1)
#>  lattice          0.22-6   2024-03-20 [1] CRAN (R 4.4.2)
#>  lifecycle        1.0.4    2023-11-07 [1] CRAN (R 4.4.0)
#>  loo              2.8.0    2024-07-03 [1] CRAN (R 4.4.0)
#>  magrittr         2.0.3    2022-03-30 [1] CRAN (R 4.4.0)
#>  Matrix           1.7-1    2024-10-18 [1] CRAN (R 4.4.2)
#>  matrixStats      1.4.1    2024-09-08 [1] CRAN (R 4.4.1)
#>  munsell          0.5.1    2024-04-01 [1] CRAN (R 4.4.0)
#>  mvtnorm          1.3-2    2024-11-04 [1] CRAN (R 4.4.1)
#>  nlme             3.1-166  2024-08-14 [1] CRAN (R 4.4.2)
#>  pillar           1.10.1   2025-01-07 [1] CRAN (R 4.4.1)
#>  pkgbuild         1.4.5    2024-10-28 [1] CRAN (R 4.4.1)
#>  pkgconfig        2.0.3    2019-09-22 [1] CRAN (R 4.4.0)
#>  plyr             1.8.9    2023-10-02 [1] CRAN (R 4.4.0)
#>  posterior        1.6.0    2024-07-03 [1] CRAN (R 4.4.0)
#>  processx         3.8.4    2024-03-16 [1] CRAN (R 4.4.0)
#>  ps               1.8.1    2024-10-28 [1] CRAN (R 4.4.1)
#>  QuickJSR         1.4.0    2024-10-01 [1] CRAN (R 4.4.1)
#>  R6               2.5.1    2021-08-19 [1] CRAN (R 4.4.0)
#>  Rcpp           * 1.0.13-1 2024-11-02 [1] CRAN (R 4.4.1)
#>  RcppParallel     5.1.9    2024-08-19 [1] CRAN (R 4.4.1)
#>  reprex           2.1.1    2024-07-06 [1] CRAN (R 4.4.0)
#>  reshape2         1.4.4    2020-04-09 [1] CRAN (R 4.4.0)
#>  rlang            1.1.4    2024-06-04 [1] CRAN (R 4.4.0)
#>  rmarkdown        2.29     2024-11-04 [1] CRAN (R 4.4.1)
#>  rstan            2.32.6   2024-03-05 [1] CRAN (R 4.4.0)
#>  rstantools       2.4.0    2024-01-31 [1] CRAN (R 4.4.0)
#>  scales           1.3.0    2023-11-28 [1] CRAN (R 4.4.0)
#>  sessioninfo      1.2.2    2021-12-06 [1] CRAN (R 4.4.0)
#>  StanHeaders      2.32.10  2024-07-15 [1] CRAN (R 4.4.0)
#>  stringi          1.8.4    2024-05-06 [1] CRAN (R 4.4.0)
#>  stringr          1.5.1    2023-11-14 [1] CRAN (R 4.4.0)
#>  tensorA          0.36.2.1 2023-12-13 [1] CRAN (R 4.4.0)
#>  tibble           3.2.1    2023-03-20 [1] CRAN (R 4.4.0)
#>  tidyselect       1.2.1    2024-03-11 [1] CRAN (R 4.4.0)
#>  V8               6.0.0    2024-10-12 [1] CRAN (R 4.4.1)
#>  vctrs            0.6.5    2023-12-01 [1] CRAN (R 4.4.0)
#>  withr            3.0.2    2024-10-28 [1] CRAN (R 4.4.1)
#>  xfun             0.51.3   2025-03-12 [1] Github (yihui/xfun@d2d45ac)
#>  yaml             2.3.10   2024-07-26 [1] CRAN (R 4.4.0)
#> 
#>  [1] /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
@paul-buerkner
Copy link
Owner

I have checked the code and I don't think I can fix it in reasonable time. I may add an informative error message before the next release.

@paul-buerkner paul-buerkner added this to the brms 2.23.0 milestone Mar 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants