\documentclass{article} \usepackage{fullpage} \usepackage{animate} \usepackage{graphicx} \usepackage{url} \title{Loess Smoothing Parameter Animation} \author{Luke Tierney} \date{March 4, 2010} \begin{document} \maketitle This note shows how to create an animation of a \texttt{loess} surface fit as the smoothing parameter \texttt{span} is varied, and how to embed that animation in a PDF file. The data to be used are normally distributed, with the mean of $z$ a linear function of $x$ and $y$: <>= set.seed(54321) @ <<>>= npoints <- 4000 x <- rnorm(npoints) y <- rnorm(npoints) z <- .5 * rnorm(npoints) + dnorm(x+y) @ The fit will be computed on an equally spaced grid defined by <<>>= grsize <- 50 xseq <- seq(-3, 3, len = grsize) yseq <- seq(-3, 3, len = grsize) grid <- expand.grid(x = xseq, y = yseq) @ The function \texttt{showframe} computes the fit for a particular value of \texttt{span} and plots the result using \texttt{persp}: <<>>= showframe <- function(s) { lf <- loess(z ~ x + y, span = s) v <- matrix(predict(lf, grid), grsize) persp(x = xseq, y = yseq, v, xlab = "x", ylab = "y", zlab = "z", zlim = c(-1,1), col = "lightblue", phi=30, theta=-30, main = sprintf("Loess Fit, span = %.2f", s)) } @ The \texttt{png} device generates its plots in files named, by default, \texttt{Rplot001.png}, \texttt{Rplot002.png}, \dots. The frames for an animaiton with \texttt{span} ranginf from $0.01$ to $0.30$ in steps of $0.01$ can therefore be generated by <<>>= png() for (s in seq(0.01, 0.3, by = 0.01)) showframe(s) dev.off() @ The \LaTeX\ \texttt{animate} packages can then be used to embed the result in a PDF file. Figure \ref{fig:loessanim} shows the result and should work as a movie when viewed with the Adobe PDF reader. \begin{figure} \begin{center} \animategraphics[controls,width=3in]{5}{Rplot}{001}{030} \end{center} \caption{Animation of a \texttt{loess} fit. Click on the image or use the controls to run the animation.} \label{fig:loessanim} \end{figure} To produce the PDF file from the \texttt{loessanim.Rnw} run the commands \begin{verbatim} R CMD Sweave loessanim.Rnw pdflatex loessanim.tex \end{verbatim} The result can then be viewed with the Adobe PDF reader. You will need to have the \LaTeX\ style files \texttt{animate.sty} and \texttt{animfp.sty} in your \LaTeX\ path or in the same directory as \texttt{loessanim.tex}. The manual for the \LaTeX\ \texttt{animate} packages is available at \begin{center} \url{http://mirror.ctan.org/macros/latex/contrib/animate/animate.pdf} \end{center} The R package \texttt{animation} can also be used to create several kinds of anumations. For example, on our Linux systems the expressions <>= library(animation) saveMovie(for (s in seq(0.01, 0.3, by = 0.01)) showframe(s), interval = 0.1, outdir = "loessanim") @ will create an animated GIF file that can be viewed by most web browsers. (You will need to create the \texttt{loessanimation} directory before running this code.) \end{document} %%% Local Variables: %%% TeX-PDF-mode: t %%% End: