Simple Multiple Regression in Nimble

78 views
Skip to first unread message

Chris Carleton

unread,
Sep 14, 2017, 7:42:12 PM9/14/17
to nimble-users
Hi All,

I'm learning Nimble and Bayesian state space models with the aim of producing a Poisson state space model with covariates. To start, I've been working on simple regression models in Nimble. I was able to get a basic regression model to work with a single covariate (https://github.com/wccarleton/nimbletesting), but now that I'm trying to extend it to multiple regression, I'm running into trouble. The biggest issue is that I'm getting a C segfault error. I've pasted in my code below (note that I started passing in the precision matrix for the slopes via "Data" as I was troubleshooting another issue, but I think the matrix could be defined in the call to nimbleModel()). Anyone have any ideas what I've done wrong? Does anyone have a working example of multiple regression I could look at?

First the call that throws an error:

lmModel <- nimbleModel(code=lmCode,data=lmData,inits=lmInits,constants=lmConsts)

which returns:

defining model...
Error: segfault from C stack overflow

Now the rest of the code:

#invent some data
x1
<- seq(1,10)*0.005
x2
<- seq(1,10)*0.005
x
<- cbind(x1,x2)
y
<- 1 + x1*2 + x2*2.5 + rnorm(10,0,sd=0.005)
K
<- 2


#create the nimble model
lmCode
<- nimbleCode({
   A
<- chol(A)
   mMean
<- c(0,0)
   intercept
~ dnorm(0, sd=1000)
   slope
[1:K] ~ dmnorm(mean=mMean, prec=A)
   sigma
~ dunif(0, 100)
   
for (j in 1:N){
      predicted
.y[j] <- intercept + t(slope[1:K]) %*% x[j,1:K]
      y
[j] ~ dnorm(predicted.y[j], sd=sigma)
   
}
})


#create precision matrix
A
<- solve(diag(rep(1000,K)))


#data to be passed to nimbleModel()
lmData
<- list(y=y,
               x
=x,
               A
=A)


#constants to be passed to nimbleModel()
lmConsts
<- list(N=length(y),
                 K
=ncol(x))


#initial paramter settings to be passed to nimbleModel()
lmInits
<- list(intercept=1,slope=matrix(c(1,1)),sigma=1)


#create nimble model
lmModel
<- nimbleModel(code=lmCode,data=lmData,inits=lmInits,constants=lmConsts)


#compile nimble model to C++ code—much faster runtime
C_lmModel
<- compileNimble(lmModel)


#configure the MCMC
lmModel_conf
<- configureMCMC(lmModel,print=T)


#select the variables that we want to monitor in the MCMC chain
lmModel_conf$addMonitors
(c("intercept","slope","sigma"))


#build the MCMC model
lmModelMCMC
<- buildMCMC(lmModel_conf)


#compile MCMC to C++—much faster
C_lmModelMCMC
<- compileNimble(lmModelMCMC,project=lmModel)


#number of MCMC iterations
niter
=10000


#set seed for replicability
set.seed(1)


#call the C++ compiled MCMC model
C_lmModelMCMC$run
(niter)


#save the MCMC chain (monitored variables) as a matrix
samples
<- as.matrix(C_lmModelMCMC$mvSamples)



Perry de Valpine

unread,
Sep 14, 2017, 8:00:38 PM9/14/17
to Chris Carleton, nimble-users
Thanks for the message. 

Please try the following version of your model code:

lmCode <- nimbleCode({
    cholA[1:K, 1:K] <- chol(A[1:K, 1:K])
   mMean[1:K] <- c(0,0)
   intercept ~ dnorm(0, sd=1000)
   slope[1:K] ~ dmnorm(mean=mMean[1:K], prec=cholA[1:K, 1:K])
   sigma ~ dunif(0, 100)
   for (j in 1:N){
       predicted.y[j] <- intercept + inprod(slope[1:K], x[j,1:K])
       y[j] ~ dnorm(predicted.y[j], sd=sigma)
   }
})

There were a few issues.  

The BUGS language is declarative, so one can't have something like A <- chol(A).  

NIMBLE currently requires very explicit indexing for any non-scalar variable.  An alternative is to provide empty square brackets and use the dimensions argument to nimbleModel if necessary.

NIMBLE is strongly typed, including for dimensions.  The matrix multiplication returns a matrix.  inprod() returns a scalar.

An alternative to the last 4 lines would be:

 predicted.y[1:N] <- intercept + x[1:N, 1:K] %*% slope[1:K]
 for (j in 1:N){
   y[j] ~ dnorm(predicted.y[j], sd=sigma)
 }

-Perry

--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users+unsubscribe@googlegroups.com.
To post to this group, send email to nimble...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/57489007-93d0-4013-b623-7ed9be8113e6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Carleton

unread,
Sep 14, 2017, 8:16:23 PM9/14/17
to nimble-users
Hi Perry,

Thanks very much for the incredibly quick reply! It's working now. 

Best,

Chris
Thanks for the message. 

To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.

Chris Carleton

unread,
Sep 14, 2017, 8:17:33 PM9/14/17
to nimble-users
Sorry—should have been more explicit. The code changes you made fixed the problem and the model is performing as expected now.

Thanks again,

C
Reply all
Reply to author
Forward
0 new messages