[R] Add GAM fit on the plot ?

varin sacha v@r|n@@ch@ @end|ng |rom y@hoo@|r
Thu Apr 23 21:35:15 CEST 2020

```David,
Rui,

Many thanks for your response. Thanks Rui it perfectly works.

Best,

Le jeudi 23 avril 2020 à 06:18:46 UTC+2, Rui Barradas <ruipbarradas using sapo.pt> a écrit :

Hello,

Inline.

Às 23:29 de 22/04/20, varin sacha via R-help escreveu:
> Dear R-experts,
>
> Here below the reproducible example. I can not add the "green" gam curve on the plot. The last line of my R code gives me problem. There is a message error that I don't understand. Many thanks for your precious help.
>
> #######################################################
> a <- as.Date(c("2020-02-25", "2020-02-26","2020-02-27","2020-02-28","2020-03-1","2020-03-2","2020-03-3","2020-03-4","2020-03-5","2020-03-6","2020-03-7","2020-03-8","2020-03-9","2020-03-10","2020-03-11","2020-03-12","2020-03-13","2020-03-14","2020-03-15","2020-03-16"))
>
> b<- c(20,28,45,68,89,123,154,190,245,302,460,379,298,300,245,189,165,100,90,78)
>
> df<-data.frame(a,b)
>
> plot(as.Date(a),b,pch=16,type="o",col="blue",ylab="nombre de nouveaux cas covid19",xlab="Dates", main="Nombre d’infectés quotidien Covid-19 en Suisse" ,ylim=c(20,460), xlim=c(min(as.Date(a)),max(as.Date(a))))
>
> ##### fit loess curve with family symmetric
> loess.smooth(a,b, span = 2/3, degree = 1,     family = c("symmetric"))
> lines(loess.smooth(a,b, span = 2/3, degree = 1,     family = "symmetric"),col="purple")
>
> d <-as.numeric(a)
>
> ##### Fit GAM
> library(mgcv)
> fit <- gam(b ~ s(d, bs="cr"),data=df)
> summary(fit)
> lines(gam(b ~ s(d, bs="cr"),data=df,col="green"))

Instead of this last line try

b.gam <- predict(gam(b ~ s(d, bs = "cr")))
lines(a, b.gam, col = "green")

Hope this helps,

> ############################################################
>
> ______________________________________________
> R-help using r-project.org 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.

>

```