[R] Plotting a smooth curve from predict

Gavin Simpson gavin.simpson at ucl.ac.uk
Tue Jul 31 13:21:36 CEST 2007


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
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list