\subsection{Collaboration with {\tt predict.tgp}}
\label{sec:apred}

<<echo=false,results=hide>>=
library(tgp)
library(maptree)
##options(width=65)
seed <- 0; set.seed(seed)
@ 

In this section I revisit the motorcycle accident data in order to
demonstrate how the {\tt predict.tgp} function can be helpful in
collaborative uses of {\tt tgp}. Consider a fit of the motorcycle
data, and suppose that infer the model parameters only (obtaining no
samples from the posterior predictive distribution).  The
\verb!"tgp"!-class output object can be saved to a file using the {\tt
  R}--internal {\tt save} function.
<<>>=
library(MASS)
out <- btgpllm(X=mcycle[,1], Z=mcycle[,2], bprior="b0", 
	       pred.n=FALSE, verb=0) 
save(out, file="out.Rsave")
out <- NULL
@
Note that there is nothing to plot here because there is no predictive
data.  (\verb!out <- NULL! is set for illustrative purposes.)

Now imagine e--mailing the ``out.Rsave'' file to a collaborator who
wishes to use your fitted {\tt tgp} model.  S/he could first load in
the \verb!"tgp"!--class object we just saved, design a new set of predictive
locations {\tt XX} and obtain kriging estimates from the MAP model.
<<>>=
load("out.Rsave")
XX <- seq(2.4, 56.7, length=200)
out.kp <- predict(out, XX=XX, pred.n=FALSE)
@ 

Another option would be to sample from the posterior
predictive distribution of the MAP model.
<<>>=
out.p <- predict(out, XX=XX, pred.n=FALSE, BTE=c(0,1000,1))
@ 
This holds the parameterization of the {\tt tgp} model {\em fixed}
at the MAP, and samples from the GP or LM posterior predictive
distributions at the leaves of the tree.

Finally, the MAP parameterization can be used as a jumping-off point
for more sampling from the joint posterior and posterior predictive
distribution.
<<>>=
out2 <- predict(out, XX, pred.n=FALSE, BTE=c(0,2000,2), MAP=FALSE)
@ 
Since the return--value of a {\tt predict.tgp} call is also a
\verb!"tgp"!--class object the process can be applied iteratively.
That is, {\tt out2} can also be passed to {\tt predict.tgp}.

\begin{figure}[hp]
\centering
<<label=pred-kp,fig=TRUE,echo=TRUE,include=FALSE,width=8,height=4>>=
plot(out.kp, center="km", as="ks2")
@ 
<<echo=false,results=hide>>=
graphics.off()
@
\vspace{-0.1cm}
\includegraphics[trim=50 30 50 25]{tgp-pred-kp}
<<label=pred-p,fig=TRUE,echo=TRUE,include=FALSE,width=8,height=4>>=
plot(out.p)
@
<<echo=false,results=hide>>=
graphics.off()
@
\vspace{-0.1cm}
\includegraphics[trim=50 30 50 25]{tgp-pred-p}
<<label=pred-2,fig=TRUE,echo=TRUE,include=FALSE,width=8,height=4>>=
plot(out2)
@ 
<<echo=false,results=hide>>=
graphics.off()
@
\includegraphics[trim=50 30 50 25]{tgp-pred-2}
\caption{Predictive surfaces ({\em left}) and
  error/variance plots ({\em right}) resulting
  from three different uses of the {\tt predict.tgp} function:
  MAP kriging ({\em top}), sampling from the MAP ({\em middle}),
  sampling from the joint posterior and posterior predictive starting
  from the MAP ({\em bottom}).}
\label{f:pred}
\end{figure} 
Figure \ref{f:pred} plots the posterior predictive surfaces for each
of the three calls to {\tt predict.tgp} above.  The kriging surfaces
are smooth within regions of the partition, but the process is
discontinuous across partition boundaries.  The middle surface is
simply a Monte Carlo--sample summarization of the kriging one above
it.  The final surface summarizes samples from the posterior
predictive distribution when obtained jointly with samples from
$\mathcal{T}|\bm{\theta}$ and $\bm{\theta}|\mathcal{T}$.  Though these
summaries are still ``noisy'' they depict a process with smoother
transitions across partition boundaries than ones conditioned only on
the MAP parameterization.

<<echo=FALSE,results=hide>>=
unlink("out.Rsave")
@ 

Finally, the {\tt predict.tgp} function can also sample from the ALC
statistic and calculate expected improvements (EI) at the {\tt XX}
locations.  While the function was designed with prediction in mind,
it is actually far more general.  It allows a continuation of MCMC
sampling where the {\tt b*} function left off (when {\tt MAP=FALSE})
with a possibly new set of predictive locations {\tt XX}.  The
intended use of this function is to obtain quick kriging--style
predictions for a previously-fit MAP estimate (contained in a
\verb!"tgp"!-class object) on a new set of predictive locations {\tt
XX}.  However, it can also be used simply to extend the search for an
MAP model when {\tt MAP=FALSE}, {\tt pred.n=FALSE}, and {\tt XX=NULL}.


