[R] Add GAM fit on the plot ?

David Winsemius dw|n@em|u@ @end|ng |rom comc@@t@net
Thu Apr 23 01:30:09 CEST 2020


On 4/22/20 3:29 PM, varin sacha via R-help wrote:
> 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"))
> ############################################################


`plot` and `lines` will accept a list with named components 'x' and 'y'  
as its first argument.  The `mgcv:gam` function does not return such a 
value. Read (and examine the code for the functions):

?lines

?points

?scatter.smooth

library(mgcv)

?gamObject

# And then read

?predict.gam   # to see what might be useful as a y-argument to lines

# and do look at the Examples section of ?perdict.gam

-- 

David

>
> ______________________________________________
> 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.



More information about the R-help mailing list