Alex Karner
2008-Oct-10 20:42 UTC
[R] predicting from a local regression and plotting in lattice
Hi R community, I'm running R 2.7.2 on Windows XP SP2. I'm trying to (1) plot loess lines for each of my groupings using the same color for each group; (2) plot loess predicted values. The first part is easy: data1 <- data.frame(Names=c(rep("Jon",9),rep("Karl",9)),Measurements=c(2,4,16,25,36,49,64,81,100,1,2,5,12,17,21,45,54,67),PlotAt=c(1:9,1:9)) data2 <- data.frame(Names=c(rep("Jonah",9),rep("Beth",9)),Measurements=c(1:5,1,1,1,1,rep(20,9)),PlotAt=c(1:9,1:9)) xyplot(Measurements ~ PlotAt, data = data1, groups=Names, panel = function(...) { panel.superpose(...,col="gray",lty=1,lwd=2); }, panel.groups = function(...) panel.loess(...) ) My question is, how do I plot predicted values from loess to extend the lines beyond the range of x? Let's say I want all lines to stop at x = 15. Is there an easy way to do this? Thanks for any help, -AK [[alternative HTML version deleted]]
Dieter Menne
2008-Oct-11 10:34 UTC
[R] predicting from a local regression and plotting in lattice
Alex Karner <aakarner <at> ucdavis.edu> writes:> I'm trying to (1) plot loess lines for each of my groupings using the same > color for each group; (2) plot loess predicted values. > > The first part is easy:.. Example removed... Thanks, it was a good example of what you wanted!> My question is, how do I plot predicted values from loess to extend the > lines beyond the range of x? Let's say I want all lines to stop at x = 15.loess is "local polynomial regression fitting". It is good to guide your eyes through confusing data points, but not at all suitable for extrapolation (that's why it is good locally). I would say, it is not even good for interpolation, but that a matter of discussion. I doubt that an extrapolation of a factor 2 beyond the initial range is possible at all without a VERY good model of your data. But in each case, you need a model, for example from earlier longer records. How does the poor function know that your data do not bend down logistic-like after you last value? Dieter
Alex Karner
2008-Oct-13 04:53 UTC
[R] predicting from a local regression and plotting in lattice
Thanks for your response, Dieter. I realize these limitations. However, I know that my actual dataset is reasonably well behaved in the range I want to predict, and I'm not using the predicted values for any further analysis, only for schematic purposes in the plot. I'm still curious if this type of extension of a loess line is possible, notwithstanding its statistical shortcomings. Cheers, Alex.>loess is "local polynomial regression fitting". It is good to guide youreyes>through confusing data points, but not at all suitable for extrapolation(that's>why it is good locally). I would say, it is not even good forinterpolation, but>that a matter of discussion.>I doubt that an extrapolation of a factor 2 beyond the initial range ispossible>at all without a VERY good model of your data. But in each case, you need a >model, for example from earlier longer records. How does the poor functionknow>that your data do not bend down logistic-like after you last value? > > >DieterAlex Karner <aakarner <at> ucdavis.edu> writes:> I'm trying to (1) plot loess lines for each of my groupings using thesame> color for each group; (2) plot loess predicted values. > > The first part is easy:.. Example removed... Thanks, it was a good example of what you wanted!> My question is, how do I plot predicted values from loess to extend the > lines beyond the range of x? Let's say I want all lines to stop at x = 15.[[alternative HTML version deleted]]
Gabor Grothendieck
2008-Oct-13 05:33 UTC
[R] predicting from a local regression and plotting in lattice
locfit can extrapolate: library(locfit) X <- seq(min(time(Nile)), max(time(Nile))+50) plot(range(X), range(Nile), type = "n") lines(Nile) fit <- locfit(Nile ~ time(Nile)) lines(X, predict(fit, new = X), col = "red") On Fri, Oct 10, 2008 at 4:42 PM, Alex Karner <aakarner at ucdavis.edu> wrote:> Hi R community, > > I'm running R 2.7.2 on Windows XP SP2. > > I'm trying to (1) plot loess lines for each of my groupings using the same > color for each group; (2) plot loess predicted values. > > The first part is easy: > data1 <- > data.frame(Names=c(rep("Jon",9),rep("Karl",9)),Measurements=c(2,4,16,25,36,49,64,81,100,1,2,5,12,17,21,45,54,67),PlotAt=c(1:9,1:9)) > > data2 <- > data.frame(Names=c(rep("Jonah",9),rep("Beth",9)),Measurements=c(1:5,1,1,1,1,rep(20,9)),PlotAt=c(1:9,1:9)) > > xyplot(Measurements ~ PlotAt, data = data1, groups=Names, > > panel = function(...) { > panel.superpose(...,col="gray",lty=1,lwd=2); > }, > panel.groups = function(...) panel.loess(...) > ) > > My question is, how do I plot predicted values from loess to extend the > lines beyond the range of x? Let's say I want all lines to stop at x = 15. > Is there an easy way to do this? > > > Thanks for any help, > -AK > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Apparently Analagous Threads
- Lattice question: plotting two sets of data, defining groups for the second set
- Forcing the extrapolation of loess through the origin
- lattice: plotting an arbitrary number of panels, defining arbitrary groups
- Predict with loess
- predict.loess and NA/NaN values