[R] loess plotting problem

John Fox j|ox @end|ng |rom mcm@@ter@c@
Thu Mar 23 16:45:35 CET 2023


Dear ,

On 2023-03-23 11:08 a.m., Anupam Tyagi wrote:
> Thanks, John.
> 
> However, loess.smooth() is producing a very different curve compared to 
> the one that results from applying predict() on a loess(). I am guessing 
> they are using different defaults. Correct?

No need to guess. Just look at the help pages ?loess and ?loess.smooth. 
If you don't like the default for loess.smooth(), just specify the 
arguments you want.

Best,
  John

> 
> 
> On Thu, 23 Mar 2023 at 20:20, John Fox <jfox using mcmaster.ca 
> <mailto:jfox using mcmaster.ca>> wrote:
> 
>     Dear Anupam Tyagi,
> 
>     You didn't include your data, so it's not possible to see exactly what
>     happened, but I think that you misunderstand the object that loess()
>     returns. It returns a "loess" object with several components, including
>     the original data in x and y. So if pass the object to lines(), you'll
>     simply connect the points, and if x isn't sorted, the points won't
>     be in
>     order. Try, e.g.,
> 
>     plot(speed ~ dist, data=cars)
>     m <- loess(speed ~ dist, data=cars)
>     names(m)
>     lines(m)
> 
>     You'd do better to use loess.smooth(), which is intended for adding a
>     loess regression to a scatterplot; for example,
> 
>     plot(speed ~ dist, data=cars)
>     with(cars, lines(loess.smooth(dist, speed)))
> 
>     Other points: You don't have to load the stats package which is
>     available by default when you start R. It's best to avoid attach(), the
>     use of which can cause confusion.
> 
>     I hope this helps,
>        John
> 
>     -- 
>     * preferred email: john.david.fox using proton.me
>     <mailto:john.david.fox using proton.me>
>     John Fox, Professor Emeritus
>     McMaster University
>     Hamilton, Ontario, Canada
>     web: https://www.john-fox.ca/ <https://www.john-fox.ca/>
> 
>     On 2023-03-23 10:18 a.m., Anupam Tyagi wrote:
>      > For some reason the following code is not plotting as I want it
>     to. I want
>      > to plot a "loess" line plotted over a scatter plot. I get a
>     jumble, with
>      > lines connecting all the points. I had a similar problem with
>     "lowess". I
>      > solved that by dropping "NA" rows from the data columns. Please help.
>      >
>      > library(stats)
>      > attach(gini_pci_wdi_narm)
>      > plot(ny_gnp_pcap_pp_kd, si_pov_gini)
>      > lines(loess(si_pov_gini ~ ny_gnp_pcap_pp_kd, gini_pci_wdi_narm))
>      > detach(gini_pci_wdi_narm)
>      >
> 
> 
> 
> -- 
> Anupam.
>



More information about the R-help mailing list