User defined non linear model in brms


#1

Dear All,
I am pretty new with brms and I was wondering if it is possible to specify a user defined function for parameters estimate of a non linear model.

In my case the dependent variables are a summation of other variables. A reproducible example could be something below:

### create data
set.seed(123)
class <-c(rep(3,10),rep(5,10),rep(6,10),rep(2,10),rep(8,10),rep(1,10),rep(7,10))
x=runif(length(class), 1,10)

### create the true response
allclasses=(unique(class))
dependent=rep(NA,length(allclasses))
for(k in 1:length(allclasses)){
  deptemp=x[which(class==allclasses[k])]
  sumvalt=sum(deptemp,na.rm=TRUE)
  dependent[k]=sumvalt
}
response=2+5*dependent

### define the model
model<-function(vect1,class,param){
  
  allclasses=(unique(class))
  dependent=rep(NA,length(allclasses))
  for(k in 1:length(allclasses)){
    deptemp=vect1[which(class==allclasses[k])]
    sumvalt=sum(deptemp,na.rm=TRUE)
    dependent[k]=sumvalt
  }
  output=param[1]+param[2]*dependent
  return(output)
}

#2

Yes, this is possible via the stanvars argument of brm.