# NOT RUN {
# }
# NOT RUN {
data(bear)
# plot length, sex, and weight of bears
ck <- c(4,3,2)
pairs(bear[,ck])
# response is length
Y <- bear$weight
# Continuous Covariate is chest
# Categorical covariate is sex
X <- bear[,c("length", "sex")]
X$sex <- as.factor(X$sex)
# Randomly partition data into 44 training and 10 testing
set.seed(1)
trainObs <- sample(1:length(Y),44, replace=FALSE)
Ytrain <- Y[trainObs]
Ytest <- Y[-trainObs]
Xtrain <- X[trainObs,,drop=FALSE]
Xtest <- X[-trainObs,,drop=FALSE]
simParms <- c(0.0, 1.0, 0.1, 1.0, 2.0, 0.1)
modelPriors <- c(0, 100^2, 0.5*sd(Y), 100)
M <- 1.0
niter <- 100000
nburn <- 50000
nthin <- 50
nout <- (niter - nburn)/nthin
mh <- c(1,10)
# Run MCMC algorithm for Gaussian PPMx model
out <- gaussian_ppmx(y=Ytrain, X=Xtrain, Xpred=Xtest, M=M,
similarity_function=1,
consim=1,
calibrate=0,
simParms=simParms,
modelPriors = modelPriors,
draws=niter, burn=nburn, thin=nthin,
mh=mh)
# plot MCMC iterats
plot(density(out$mu[,1:10]),type='l')
plot(density(out$sig2[,1:10]),type='l')
plot(density(out$nc),type='l')
plot(density(out$mu0), type='l')
plot(density(out$sig20), type='l')
# The first partition iterate is used for plotting
# purposes only. We recommended using the salso
# R-package to estimate the partition based on Si
pairs(bear[trainObs,ck],col=out$Si[1,], pch=out$Si[1,])
# To compare fit and predictions when covariates not included
# in the partition model, refit data with PPM rather than PPMx
out2 <- gaussian_ppmx(y=Ytrain, X=NULL, Xpred=Xtest, M=M,
similarity_function=1,
consim=1,
calibrate=0,
simParms=simParms,
modelPriors = modelPriors,
draws=niter, burn=nburn, thin=nthin,
mh=mh)
oldpar <- par(no.readonly = TRUE)
par(mfrow=c(1,2))
plot(Xtrain[,1], Ytrain, ylab="weight", xlab="length", pch=20)
points(Xtrain[,1], apply(out$fitted,2,mean), col='blue',pch="+", cex=1.5)
points(Xtrain[,1], apply(out2$fitted,2,mean), col='red',pch=2, cex=1)
legend(x="topleft",legend=c("Observed","PPM","PPMx"), col=c("black","red","blue"),pch=c(20,3,2))
plot(Xtest[,1], Ytest, ylab="weight", xlab="length",pch=20)
points(Xtest[,1], apply(out$ppred,2,mean), col='blue',pch="+", cex=1.5)
points(Xtest[,1], apply(out2$ppred,2,mean), col='red',pch=2, cex=1)
legend(x="topleft",legend=c("Observed","PPM","PPMx"), col=c("black","red","blue"),pch=c(20,3,2))
par(oldpar)
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab