I have some data which consists of time series for a number of sites. It appears that there is not much autocorrelation in the data and I have fitted a cubic for each site using lm. I would like to obtain a lattice plot with one panel for each site and showing the original data, and the fitted cubic. The closest I have got to doing what I want is: fit <- fitted(paraslm1) temp <- cbind(paras, fit) xyplot(Density ~ Year | LocCode, data = temp, panel = function(x, y, ...){ panel.xyplot(x,y) panel.xyplot(x[LocCode],fit[LocCode],type="l") }) This doesn't give an error (most of my other attempts did), and draws the panels correctly with the original data, but doesn't draw the fitted lines. paraslm1 is my fitted linear model with cubics for each location. David Scott _________________________________________________________________ David Scott Visiting (July 06 to January 07) Department of Probability and Statistics The University of Sheffield The Hicks Building Hounsfield Road Sheffield S3 7RH United Kingdom Phone: +44 114 222 3908 Email: d.scott at auckland.ac.nz
On 9/1/06, David Scott <d.scott at auckland.ac.nz> wrote:> > I have some data which consists of time series for a number of sites. It > appears that there is not much autocorrelation in the data and I have > fitted a cubic for each site using lm. I would like to obtain a lattice > plot with one panel for each site and showing the original data, and the > fitted cubic. > > The closest I have got to doing what I want is: > > fit <- fitted(paraslm1) > temp <- cbind(paras, fit) > xyplot(Density ~ Year | LocCode, data = temp, > panel = function(x, y, ...){ > panel.xyplot(x,y) > panel.xyplot(x[LocCode],fit[LocCode],type="l") > }) > > This doesn't give an error (most of my other attempts did), and draws the > panels correctly with the original data, but doesn't draw the fitted > lines. > > paraslm1 is my fitted linear model with cubics for each location.I'm guessing since I don't have actual data to work with, but the following is probably what you are trying to do: xyplot(Density + fit ~ Year | LocCode, data = temp, panel = panel.superpose.2, type = c('p', 'l')) -Deepayan
> I have some data which consists of time series for a number of sites. It > appears that there is not much autocorrelation in the data and I have > fitted a cubic for each site using lm. I would like to obtain a lattice > plot with one panel for each site and showing the original data, and the > fitted cubic.This is very easy to do with ggplot: install.packages("ggplot") library(ggplot) p <- ggplot(temp, . ~ LocCode, aes=list(y=Density, x=Year)) (p <- ggpoint(p)) ggsmooth(p, method=lm, formula=y~poly(x,3)) You can also do it with fitted values in another column. temp$fitted <- fitted(paraslm1) p <- ggplot(temp, . ~ LocCode, aes=list(y=Density, x=Year)) (p <- ggpoint(p)) ggline(p, aes=list(y=fitted)) Hadley