# SAS PROC MCMC in R: Nonlinear Poisson Regression Models

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In exercise 61.1 the problem is that the model has bad mixing. In the SAS manual the mixing is demonstrated after which a modified distribution is used to fix the model.**Wiekvoet**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In this post the same problem is tackled in R; MCMCpack, RJags, RStan and LaplaceDemon. MCMCpack has quite some mixing problems, RStan seems to do best.

### Data

To quote the SAS manual “*This example shows how to analyze count data for calls to a technical support help line in the weeks immediately following a product release. (…) You can model the number of daily calls as a Poisson random variable, with the average number of calls modeled as a nonlinear function of the number of weeks that have elapsed since the product’s release. (…) During the first several weeks after a new product is released, the number of questions that technical support receives concerning the product increases in a sigmoidal fashion. The expression for the mean value in the classic Poisson regression involves the log link. There is some theoretical justification for this link, but with MCMC methodologies, you are not constrained to exploring only models that are computationally convenient. The number of calls to technical support tapers off after the initial release, so in this example you can use a logistic-type function to model the mean number of calls received weekly for the time period immediately following the initial release.*“

observed <- scan(text='

1 0 1 2 2 2 2 1 3 1 3 3

4 5 4 8 5 5 5 9 6 17 6 9

7 24 7 16 8 23 8 27′,

what=list(integer(),integer()),

sep=’ ‘,

)

names(observed) <- c('weeks','calls')

observed <- as.data.frame(observed)

### Analysis

#### MCMCpack

The MCMCpack solution is derived from LaplacesDemon solution below. It is placed as first because it shows some of the problems with the mixing. As a change from LaplacesDemon, gamma could get negative, for which I made a -Inf likelihood.library(MCMCpack)

MCMCfun <- function(parm) {

names(parm) <- c('alpha','beta','gamma')

if (parm[‘gamma’]<0) return(-Inf)

mu <-parm['gamma']*

LaplacesDemon::invlogit(parm[‘alpha’]+parm[‘beta’]*observed$weeks)

LL <- sum(dpois(observed$calls,mu,log=TRUE))

LP <- LL+ dgamma(parm['gamma'],shape=3.4,scale=12,log=TRUE) +

dnorm(parm[‘alpha’],-5,sd=.25,log=TRUE) +

dnorm(parm[‘beta’],0.75,.5,log=TRUE)

return(LP)

}

mcmcout <- MCMCmetrop1R(MCMCfun,

c(alpha=-4,beta=1,gamma=2))

The figures show bad mixing. Parameters, especially Beta and Gamma, get stuck. There is quite some autocorrelation.

plot(mcmcout)

acf(mcmcout)

The cause is a nasty correlation between Beta and Gamma

pairs(as.data.frame(mcmcout))

#### LaplacesDemon

I chose an adaptive algorithm for LaplacesDemon. For the specs, the numbers are derived from the standard deviation of a previous run. Stepsize keeps reasonably constant through the latter part of run. The samples look much better than MCMCpack, although the mixing is not ideal either.At a later stage I tried this same analysis with reflective Slice Sampler, however, that did was quite a bit more difficult and the end result was not better than this.

library(‘LaplacesDemon’)

mon.names <- "LP"

parm.names <- c('alpha','beta','gamma')

PGF <- function(Data) c(rnorm(3,0,1))

N <-1

MyData <- list(mon.names=mon.names,

parm.names=parm.names,

PGF=PGF,

calls=observed$calls,

weeks=observed$weeks)

Model <- function(parm, Data)

{

mu <-parm['gamma']*

invlogit(parm[‘alpha’]+parm[‘beta’]*Data$weeks)

LL <- sum(dpois(Data$calls,mu,log=TRUE))

LP <- LL+ dgamma(parm['gamma'],shape=3.4,scale=12,log=TRUE) +

dnorm(parm[‘alpha’],-5,sd=.25,log=TRUE) +

dnorm(parm[‘beta’],0.75,.5,log=TRUE)

Modelout <- list(LP=LP, Dev=-2*LL, Monitor=LP,

yhat=mu,

parm=parm)

return(Modelout)

}

Initial.Values <- c(alpha=-4,beta=1,gamma=2) #GIV(Model, MyData, PGF=TRUE)

Fit1 <- LaplacesDemon(Model,

Data=MyData,

Initial.Values = Initial.Values,

Algorithm = “AHMC”,

Specs = list(epsilon = c(.23,.2,13.5)/4,

L = 2, Periodicity = 10),

Iterations=40000,Status=2000

)

BurnIn <- Fit1$Rec.BurnIn.Thinned

plot(Fit1, BurnIn, MyData, PDF=FALSE)

#### Jags

I do not think using one chain is advisable, especially since Jags makes more chains so easy. But in the spirit of this analysis I am using one. Coda plots are used since they are a bit more compact.library(R2jags)

datain <- list(

calls=observed$calls,

weeks=observed$weeks,

n=nrow(observed))

parameters <- c('alpha','beta','gamma')

jmodel <- function() {

for (i in 1:n) {

mu[i] <- gamma*ilogit(alpha+beta*weeks[i])

calls[i] ~ dpois(mu[i])

}

alpha ~ dnorm(-5,1/(.25*.25))

gamma ~ dgamma(3.4,1/12)

beta ~ dnorm(.75,1/(.5*.5))

}

jj <- jags(model.file=jmodel,

data=datain,

parameters=parameters,

n.chains=1,

inits=list(list(alpha=-4,beta=1,gamma=2))

)

cc <- as.mcmc(jj$BUGSoutput$sims.array[,1,])

plot(cc)

acfplot(cc)

#### Stan

Stan probably does best handling this typical distribution. Again, one chain is only in the context of this posting.library(rstan)

smodel <- '

data {

int

int calls[n];

real weeks[n];

}

parameters {

real Alpha;

real Beta;

real Gamma;

}

transformed parameters {

vector[n] mu;

for (i in 1:n) {

mu[i] <- Gamma*inv_logit(Alpha+Beta*weeks[i]);

}

}

model {

calls ~ poisson(mu);

Gamma ~ gamma(3.4,1./12.);

Beta ~ normal(.75,.5);

Alpha ~ normal(-5,.25);

}

‘

fstan <- stan(model_code = smodel,

data = datain,

pars=c(‘Alpha’,’Beta’,’Gamma’),

chains=1,

init=list(list(Alpha=-4,Beta=1,Gamma=2)))

traceplot(fstan,inc_warmup=FALSE)

smc <- as.mcmc(as.matrix(fstan))

acfplot(smc)

To

**leave a comment**for the author, please follow the link and comment on their blog:**Wiekvoet**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.