Probably a very simple query: When I try to plot a curve from a fitted polynomial, it comes out rather jagged, not smooth like fitted curves in other stats software. Is there a way of getting a smooth curve in R? What I'm doing at the moment (for the sake of example) is:> x <- c(1,2,3,4,5,6,7,8,9,10)> y <- c(10,9,8,7,6,6.5,7,8,9,10)> b <- data.frame(cbind(x,y))> w <- gls(y ~ I(x)+I(x^2),correlation=corARMA(p=1),method="ML",data=b)> plot(predict(w),type="l")Many thanks, Andrew Wilson
Wilson, Andrew wrote:> Probably a very simple query: > > When I try to plot a curve from a fitted polynomial, it comes out rather > jagged, not smooth like fitted curves in other stats software. Is there > a way of getting a smooth curve in R? > > What I'm doing at the moment (for the sake of example) is: > >> x <- c(1,2,3,4,5,6,7,8,9,10) > >> y <- c(10,9,8,7,6,6.5,7,8,9,10) > >> b <- data.frame(cbind(x,y)) > >> w <- gls(y ~ I(x)+I(x^2),correlation=corARMA(p=1),method="ML",data=b) > >> plot(predict(w),type="l")Make predictions for more than 10 values of x: x <- c(1,2,3,4,5,6,7,8,9,10) y <- c(10,9,8,7,6,6.5,7,8,9,10) b <- data.frame(cbind(x,y)) library(nlme) w <- gls(y ~ I(x)+I(x^2), correlation=corARMA(p=1), method="ML", data=b) plot(seq(1,10,len=100), predict(w, data.frame(x = seq(1,10, len=100))), xlab="x", ylab="Predicted y", type="l")> Many thanks, > > Andrew Wilson > > ______________________________________________ > R-help at stat.math.ethz.ch 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.-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Wilson, Andrew wrote:> Probably a very simple query: > > When I try to plot a curve from a fitted polynomial, it comes out rather > jagged, not smooth like fitted curves in other stats software. Is there > a way of getting a smooth curve in R? > > What I'm doing at the moment (for the sake of example) is: > >> x <- c(1,2,3,4,5,6,7,8,9,10) > >> y <- c(10,9,8,7,6,6.5,7,8,9,10) > >> b <- data.frame(cbind(x,y)) > >> w <- gls(y ~ I(x)+I(x^2),correlation=corARMA(p=1),method="ML",data=b) > >> plot(predict(w),type="l")predict() predicts at all locations in x and you are drawing straight lines between these points. Hence you need to predict in another resolution, e.g.: dat <- data.frame(x = seq(1, 10, by = 0.1)) plot(predict(w, newdata = dat), type="l") Uwe Ligges> Many thanks, > > Andrew Wilson > > ______________________________________________ > R-help at stat.math.ethz.ch 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.
On Tue, 2007-07-31 at 11:21 +0100, Wilson, Andrew wrote:> Probably a very simple query: > > When I try to plot a curve from a fitted polynomial, it comes out rather > jagged, not smooth like fitted curves in other stats software. Is there > a way of getting a smooth curve in R? > > What I'm doing at the moment (for the sake of example) is: > > > x <- c(1,2,3,4,5,6,7,8,9,10) > > > y <- c(10,9,8,7,6,6.5,7,8,9,10) > > > b <- data.frame(cbind(x,y)) > > > w <- gls(y ~ I(x)+I(x^2),correlation=corARMA(p=1),method="ML",data=b) > > > plot(predict(w),type="l")replace the line above with the following: pred.dat <- data.frame(x = seq(min(x), max(x), length.out = 100)) plot(predict(w, pred.dat), type = "l") The general idea is to produce predictions over the range of x, so we produce a new data frame with component x, that contains 100 values from min(x) to max(x). We then get predicted values for each of these new values of the predictor in pred.dat, and plot them Increase/decrease length.out to get something suitably smooth without sending your computer into meltdown. HTH G> > Many thanks, > > Andrew Wilson > > ______________________________________________ > R-help at stat.math.ethz.ch 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.-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
You are using fitted() implicitly here, so you are not plotting a smooth curve but a set of fitted values. You need to really predict at a suitable range of data points, e.g. xx <- seq(1, 10, len=500) plot(xx, predict(w, list(x=xx)), type="l") BTW, why are you not using poly()? On Tue, 31 Jul 2007, Wilson, Andrew wrote:> Probably a very simple query: > > When I try to plot a curve from a fitted polynomial, it comes out rather > jagged, not smooth like fitted curves in other stats software. Is there > a way of getting a smooth curve in R? > > What I'm doing at the moment (for the sake of example) is: > >> x <- c(1,2,3,4,5,6,7,8,9,10) > >> y <- c(10,9,8,7,6,6.5,7,8,9,10) > >> b <- data.frame(cbind(x,y)) > >> w <- gls(y ~ I(x)+I(x^2),correlation=corARMA(p=1),method="ML",data=b) > >> plot(predict(w),type="l") > > Many thanks, > > Andrew Wilson > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
What you need is b <- data.frame(x = 1:10, y = c(10,9,8,7,6,6.5,7,8,9,10)) w <- gls(y ~ I(x)+I(x^2),correlation=corARMA(p=1),method="ML",data=b) Newdata <- data.frame(x = seq(1, 10, length = 41)) plot(predict(w, newdata = Newdata), type="l") ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney> -----Oorspronkelijk bericht----- > Van: r-help-bounces op stat.math.ethz.ch > [mailto:r-help-bounces op stat.math.ethz.ch] Namens Wilson, Andrew > Verzonden: dinsdag 31 juli 2007 12:22 > Aan: r-help op stat.math.ethz.ch > Onderwerp: [R] Plotting a smooth curve from predict > > Probably a very simple query: > > When I try to plot a curve from a fitted polynomial, it comes > out rather jagged, not smooth like fitted curves in other > stats software. Is there a way of getting a smooth curve in R? > > What I'm doing at the moment (for the sake of example) is: > > > x <- c(1,2,3,4,5,6,7,8,9,10) > > > y <- c(10,9,8,7,6,6.5,7,8,9,10) > > > b <- data.frame(cbind(x,y)) > > > w <- gls(y ~ > I(x)+I(x^2),correlation=corARMA(p=1),method="ML",data=b) > > > plot(predict(w),type="l") > > Many thanks, > > Andrew Wilson > > ______________________________________________ > R-help op stat.math.ethz.ch 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. >