Multivariate nonlinear model using brms

Hi all,
I am new here and also STAN(BRMS) new user.

I saw this code online and I am trying to run the code to see if I can get the result but I am getting an error with the “mixed effect code”.

HERE IS THE CODE:

f1 <- circumference ~ phi1 / (1 + exp(-(age - phi2)/phi3))
n1 <- nls(f1,
data = Orange,
start = list(phi1 = 200, phi2 = 700, phi3 = 350))

prior_1 <- c(set_prior(“normal(200, 50)”, nlpar = “phi1”),
set_prior(“normal(700, 50)”, nlpar = “phi2”),
set_prior(“normal(350, 50)”, nlpar = “phi3”))

n1_b <- brm(bf(f1,nonlinear = phi1 + phi2 + phi3 ~ 1,nl=TRUE),
data = Orange,
prior = prior_1,
chains = 3)
summary(n1_b)
summary(n1)

##mixed effect

n2 <- nlme(f1,
data = Orange,
fixed = phi1 + phi2 + phi3 ~ 1,
random = phi1 ~ 1,
groups = ~ Tree,
start = coef(n1))

summary(n2)

# prior_1 plus prior for random effect, is there a way to update a  `prior_frame` ?

prior_2 <- c(set_prior(“normal(200, 50)”, nlpar = “phi1”),
set_prior(“normal(700, 50)”, nlpar = “phi2”),
set_prior(“normal(350, 50)”, nlpar = “phi3”),
set_prior(“cauchy(30,2)”, class = “sd”))

n2_b <- brm(bf(f1, nonlinear = list(phi1 ~ (1|Tree),
phi2 ~ 1,
phi3 ~ 1),nl=TRUE),
data = Orange,
prior = prior_2,
chains = 3,
iter = 4000)
summary(n2_b)

####HERE IS THE ERROR###

Error: The following priors do not correspond to any model parameter:
sd ~ cauchy(30,2)
Function ‘get_prior’ might be helpful to you.

SOMEONE SHOULD PLEASE HELP ME OUT.

I am using brm 2.11.1

1 Like

Hi,

You can get/set priors in two different ways in brms:

  1. get_prior(your formula, likelihood='whatever', data='whatever') or if you have a compiled model,
  2. prior_summary(your_model)

If you store the results from the above two functions, then you can change the priors easily, e.g.,

p <- get_prior(n_bugs ~ 1 + devs_log + max_commit_age_log + commits_log + 
                        insertions_log +
                        (1 + devs_log + max_commit_age_log + commits_log + 
                           insertions_log | language_id) + 
                        (1 | project_id),
                      family = negbinomial,
                      data = data)

If we look at p I have this:

                  prior     class               coef       group resp dpar nlpar bound
1        normal(0, 0.2)         b                                                     
2                               b        commits_log                                  
3                               b           devs_log                                  
4                               b     insertions_log                                  
5                               b max_commit_age_log                                  
6  lkj_corr_cholesky(2)       cor                                                     
7                             cor                    language_id                      
8          normal(0, 1) Intercept                                                     
9        cauchy(0, 0.5)        sd                                                     
10                             sd                    language_id                      
11                             sd        commits_log language_id                      
12                             sd           devs_log language_id                      
13                             sd     insertions_log language_id                      
14                             sd          Intercept language_id                      
15                             sd max_commit_age_log language_id                      
16                             sd                     project_id                      
17                             sd          Intercept  project_id                      
18    gamma(0.01, 0.01)     shape

If I want to set a different prior on any parameter I simply do:

p$prior[1] <- "normal(0,1)" 

and, hence, my prior for \beta is now set for all betas to normal(0,1). I can now use p when running brms the next time using prior = p.

Please see ?get_prior and ?prior_summary for more information.

5 Likes

Thank you so much.