[R] Plotting a smooth curve from predict

Uwe Ligges ligges at statistik.uni-dortmund.de
Tue Jul 31 13:17:08 CEST 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")


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.



More information about the R-help mailing list