[R] Add GAM fit on the plot ?

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


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"))
############################################################



More information about the R-help mailing list