Prediction with latent variables in brms multivariate models

659 views
Skip to first unread message

ViktorR

unread,
Mar 23, 2018, 8:17:36 AM3/23/18
to brms-users
Thanks Paul for bringing multi-variate models to brms! I'd like to use a multivariate model to infere a latent variable to make predictions when no observations of this latent variable is available. However, I'm struggling of finding a good implementation of such a model. The idea is to fit a model that can predict a latent variable and then use draws from the posterior predictive distribution of this model as input for another model to incorporate the uncertainties of the previous prediction. 

Here's a small toy example to show what I'd like to do:

library(brms)
data
(mtcars)


#Multi-variate model
bf_hp
<-bf(hp ~ cyl, family = gaussian())
bf_mpg
<-bf(mpg ~ hp, family = gaussian)
fit
<-brm(bf_mpg+bf_hp, data=mtcars)


newdata_NA
<-data.frame(hp=c(NA,NA),
                       cyl
=c(10,4))

#Fails because of missing input data
post_pred
<-posterior_predict(fit, newdata = newdata_NA)

My idea for now would be to fit the first model, take draws of the posterior predictive distribution and use these as input data for the next model. However, this leads either to huge sample sizes when taking all samples from the  posterior predictive distribution or I have to take again a sample of the full sample. I'm neither sure if this is at all the right way to do it nor if there is a more elegant way for such a problem in brms. 

library(brms)
library
(reshape2)


#Init
data
(mtcars)
chains
<-4
iter
<-2000
warmup
<-iter/2
samples
<-(iter-warmup)*chains


#Fit model to predict latent variable horse power (hp) by the number of cylinders (cyl)
fit_hp
<-brm(hp~cyl, data=mtcars, family=gaussian(), chains=chains, iter=iter, warmup=warmup)
fit_mpg
<-brm(mpg ~ hp, data = mtcars, chains=chains, iter=iter, warmup=warmup)


# Draws from posterior predictive distribution of latent variable
post_pred_hp
<-posterior_predict(fit_hp, newdata=data.frame(cyl=c(10,4)))


#Create input data from predictions of latent variable
newdata_mpg
<-data.frame(hp=c(post_pred_hp[,1],post_pred_hp[,2]),
                    cyl
=c(rep(10,samples),rep(4,samples)))


#Draws from posterior predictive distribution for all samples from posterior predictive distribution of the previous model
post_pred_mpg
<-posterior_predict(fit_mpg,newdata=newdata_mpg)
post_pred_mpg
<-cbind(melt(post_pred_mpg[,1:samples])$value,melt(post_pred_mpg[,(samples+1):ncol(post_pred_mpg)])$value)

Thanks, Viktor

Paul Buerkner

unread,
Mar 23, 2018, 9:01:26 AM3/23/18
to ViktorR, brms-users
Hi Viktor,

maybe my last post in https://github.com/paul-buerkner/brms/issues/303 can help you.

Best
Paul

--
You received this message because you are subscribed to the Google Groups "brms-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brms-users+unsubscribe@googlegroups.com.
To post to this group, send email to brms-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brms-users/102b7c72-3b26-459f-b64f-99eadf6e3600%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages