rshinybrms

How to pass used-defined priors to brm using R Shiny?


I am creating an R Shiny web-app that would allow users to input a custom dataset, select analysis variables, and fit a Bayesian ANCOVA model using user-specified prior values. This is what the relevant code looks like: (server portion)

    input$activate,
    ignoreNULL = F,
    {
      req(lmmformula)
      
      priors <- c(prior(normal(input$interceptMean,
                               input$interceptVariance),
                        class = "Intercept"),
                  prior(cauchy(0, input$sigmaScale),
                        class = "sigma"))
      
      list(
        bayes = brm(ancovaformula(), data = sel_data(), silent = 1,
                    thin = 1, iter = input$iter, warmup = (input$iter/2),
                    prior = priors),
        
        bayesx2 = brm(ancovaformula(), data = sel_data(), silent = 1,
                      thin = 1, iter = (input$iter)*2, warmup = input$iter,
                      prior = priors)
      )
    }
  )
  
  output$summary <- renderPrint({
    summary(bfit()[["bayes"]])
  })

My issue is, it returns the following error:

error in evaluating the argument 'object' in selecting a method for function 'summary': 0
Syntax error in 'string', line 27, column 40, lexing error:
   -------------------------------------------------
    25:  transformed parameters {
    26:    real lprior = 0;  // prior contributions to the log posterior
    27:    lprior += normal_lpdf(Intercept | input$interceptMean, input$interceptVariance);
                                                  ^
    28:    lprior += cauchy_lpdf(sigma | 0, input$sigmaScale)
    29:      - 1 * cauchy_lccdf(0 | 0, input$sigmaScale);
   -------------------------------------------------

Invalid character found.

Which suggests to me that the priors, instead of containing the values stored in input$, contains verbatim 'input$interceptMean' etc


Solution

  • The solution proposed by qdread was implemented to fix it as follows:

      bfit <- eventReactive(
        input$activate,
        ignoreNULL = F,
        {
          req(ancovaformula)
          
          params <- stanvar(input$interceptMean, "intercept_mean") +
            stanvar(input$interceptVariance, "intercept_var") +
            stanvar(input$sigmaScale, "sigma_scale")
          
          priors <- c(prior(normal(intercept_mean,
                                   intercept_var),
                            class = "Intercept"),
                      prior(cauchy(0, sigma_scale),
                            class = "sigma"))
          
          list(
            bayes = brm(ancovaformula(), data = sel_data(), silent = 1,
                        thin = 1, iter = input$iter, warmup = (input$iter/2),
                        prior = priors, stanvars = params, seed = input$seed),
            
            bayesx2 = brm(ancovaformula(), data = sel_data(), silent = 1,
                          thin = 1, iter = (input$iter)*2, warmup = input$iter,
                          prior = priors, stanvars = params, seed = input$seed)
          )
        }
      )
      
      output$summary <- renderPrint({
        summary(bfit()[["bayes"]])
      })