Problem with a LOESS curve

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with a LOESS curve

R help mailing list-2
Dear R-experts,

Here below my R code. What is happening with my green curve ? How to solve the problem ?

Many thanks,

##############################
#DATA
y<-c(499,491,500,517,438,495,501,525,516,494,500,453,479,481,505,465,477,520,520,480,477,416,502,503,497,513,492,469,504,482,502,498,463,504,495)
x<-c(499,496,424,537,480,484,503,575,540,436,486,506,496,481,508,425,501,519,546,507,452,498,471,495,499,522,509,474,502,534,504,466,527,485,525)

#PLOT
plot(x,y)

#GAM
library(mgcv)
fit3=gam(y~s(x,bs='ps'))
x.new<- seq(range(x)[1], range(x)[2], len=1000)
gamfit.new <- as.vector(predict.gam(fit3,data.frame(x=x.new),type="response"))
lines(x.new, gamfit.new, col="red", lwd=2)

# LOESS
mod=loess(y~x, span=0.8)
yfit=predict(mod,newdata=x)
lines(x,yfit,col="green",lwd=2)
##############################

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with a LOESS curve

Duncan Murdoch-2
Your x values aren't in increasing order.  When you plot lines(x, yfit),
it joins the points in the order you give them, which jumps back and
forth.  To fix, try

o <- order(x)
lines(x[o], yfit[o], col="green", lwd=2)

Duncan Murdoch

On 09/01/2021 2:58 p.m., varin sacha via R-help wrote:

> ##############################
> #DATA
> y<-c(499,491,500,517,438,495,501,525,516,494,500,453,479,481,505,465,477,520,520,480,477,416,502,503,497,513,492,469,504,482,502,498,463,504,495)
> x<-c(499,496,424,537,480,484,503,575,540,436,486,506,496,481,508,425,501,519,546,507,452,498,471,495,499,522,509,474,502,534,504,466,527,485,525)
>
> #PLOT
> plot(x,y)
>
> #GAM
> library(mgcv)
> fit3=gam(y~s(x,bs='ps'))
> x.new<- seq(range(x)[1], range(x)[2], len=1000)
> gamfit.new <- as.vector(predict.gam(fit3,data.frame(x=x.new),type="response"))
> lines(x.new, gamfit.new, col="red", lwd=2)
>
> # LOESS
> mod=loess(y~x, span=0.8)
> yfit=predict(mod,newdata=x)
> lines(x,yfit,col="green",lwd=2)
> ##############################

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with a LOESS curve

R help mailing list-2
Duncan,

Great, many thanks, it works.

Best,




Le samedi 9 janvier 2021 à 21:13:30 UTC+1, Duncan Murdoch <[hidden email]> a écrit :





Your x values aren't in increasing order.  When you plot lines(x, yfit),
it joins the points in the order you give them, which jumps back and
forth.  To fix, try

o <- order(x)
lines(x[o], yfit[o], col="green", lwd=2)

Duncan Murdoch

On 09/01/2021 2:58 p.m., varin sacha via R-help wrote:

> ##############################
> #DATA
> y<-c(499,491,500,517,438,495,501,525,516,494,500,453,479,481,505,465,477,520,520,480,477,416,502,503,497,513,492,469,504,482,502,498,463,504,495)
> x<-c(499,496,424,537,480,484,503,575,540,436,486,506,496,481,508,425,501,519,546,507,452,498,471,495,499,522,509,474,502,534,504,466,527,485,525)
>
> #PLOT
> plot(x,y)
>
> #GAM
> library(mgcv)
> fit3=gam(y~s(x,bs='ps'))
> x.new<- seq(range(x)[1], range(x)[2], len=1000)
> gamfit.new <- as.vector(predict.gam(fit3,data.frame(x=x.new),type="response"))
> lines(x.new, gamfit.new, col="red", lwd=2)
>
> # LOESS
> mod=loess(y~x, span=0.8)
> yfit=predict(mod,newdata=x)
> lines(x,yfit,col="green",lwd=2)
> ##############################

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.