[R] Add GAM fit on the plot ?
Rui Barradas
ru|pb@rr@d@@ @end|ng |rom @@po@pt
Thu Apr 23 22:31:50 CEST 2020
Hello,
I'm glad it helped.
One more thing, I've just noticed that I copied the gam call in lines()
but you don't have to refit the model,
b.gam <- predict(fit)
is enough.
Hope this helps,
Rui Barradas
Às 20:35 de 23/04/20, varin sacha escreveu:
> 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,
>
> Rui Barradas
>
>> ############################################################
>>
>> ______________________________________________
>> 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