data(rapi.saemix)
saemix.data<-saemixData(name.data=rapi.saemix, name.group=c("id"),
name.predictors=c("time","rapi"),name.response=c("rapi"),
name.covariates=c("gender"),units=list(x="months",y="",covariates=c("")))
hist(rapi.saemix$rapi, main="", xlab="RAPI score", breaks=30)
# \donttest{
# Fitting a Poisson model
count.poisson<-function(psi,id,xidep) {
time<-xidep[,1]
y<-xidep[,2]
intercept<-psi[id,1]
slope<-psi[id,2]
lambda<- exp(intercept + slope*time)
logp <- -lambda + y*log(lambda) - log(factorial(y))
return(logp)
}
countsimulate.poisson<-function(psi, id, xidep) {
time<-xidep[,1]
y<-xidep[,2]
ymax<-max(y)
intercept<-psi[id,1]
slope<-psi[id,2]
lambda<- exp(intercept + slope*time)
y<-rpois(length(time), lambda=lambda)
y[y>ymax]<-ymax+1 # truncate to maximum observed value to avoid simulating aberrant values
return(y)
}
# Gender effect on intercept and slope
rapimod.poisson<-saemixModel(model=count.poisson, simulate.function=countsimulate.poisson,
description="Count model Poisson",modeltype="likelihood",
psi0=matrix(c(log(5),0.01),ncol=2,byrow=TRUE,dimnames=list(NULL, c("intercept","slope"))),
transform.par=c(0,0), omega.init=diag(c(0.5, 0.5)),
covariance.model =matrix(data=1, ncol=2, nrow=2),
covariate.model=matrix(c(1,1), ncol=2, byrow=TRUE))
saemix.options<-list(seed=632545,save=FALSE,save.graphs=FALSE, displayProgress=FALSE, fim=FALSE)
poisson.fit<-saemix(rapimod.poisson,saemix.data,saemix.options)
# Fitting a ZIP model
count.poissonzip<-function(psi,id,xidep) {
time<-xidep[,1]
y<-xidep[,2]
intercept<-psi[id,1]
slope<-psi[id,2]
p0<-psi[id,3] # Probability of zero's
lambda<- exp(intercept + slope*time)
logp <- log(1-p0) -lambda + y*log(lambda) - log(factorial(y)) # Poisson
logp0 <- log(p0+(1-p0)*exp(-lambda)) # Zeroes
logp[y==0]<-logp0[y==0]
return(logp)
}
countsimulate.poissonzip<-function(psi, id, xidep) {
time<-xidep[,1]
y<-xidep[,2]
ymax<-max(y)
intercept<-psi[id,1]
slope<-psi[id,2]
p0<-psi[id,3] # Probability of zero's
lambda<- exp(intercept + slope*time)
prob0<-rbinom(length(time), size=1, prob=p0)
y<-rpois(length(time), lambda=lambda)
y[prob0==1]<-0
y[y>ymax]<-ymax+1 # truncate to maximum observed value to avoid simulating aberrant values
return(y)
}
rapimod.zip<-saemixModel(model=count.poissonzip, simulate.function=countsimulate.poissonzip,
description="count model ZIP",modeltype="likelihood",
psi0=matrix(c(1.5, 0.01, 0.2),ncol=3,byrow=TRUE,
dimnames=list(NULL, c("intercept", "slope","p0"))),
transform.par=c(0,0,3), covariance.model=diag(c(1,1,0)), omega.init=diag(c(0.5,0.3,0)),
covariate.model = matrix(c(1,1,0),ncol=3, byrow=TRUE))
zippoisson.fit<-saemix(rapimod.zip,saemix.data,saemix.options)
# Using simulations to compare the predicted proportion of 0's in the two models
nsim<-100
yfit1<-simulateDiscreteSaemix(poisson.fit, nsim=nsim)
yfit2<-simulateDiscreteSaemix(zippoisson.fit, nsim=100)
{
nobssim<-length(yfit1@sim.data@datasim$ysim)
cat("Observed proportion of 0's",
length(yfit1@data@data$rapi[yfit1@data@data$rapi==0])/yfit1@data@ntot.obs,"\n")
cat(" Poisson model, p=",
length(yfit1@sim.data@datasim$ysim[yfit1@sim.data@datasim$ysim==0])/nobssim,"\n")
cat(" ZIP model, p=",
length(yfit2@sim.data@datasim$ysim[yfit2@sim.data@datasim$ysim==0])/nobssim,"\n")
}
# }
Run the code above in your browser using DataLab