[R] Add Gauss normal curve ?

peter dalgaard pd@|gd @end|ng |rom gm@||@com
Sat Apr 11 17:02:33 CEST 2020


Two obvious problems: 

1. mean(nc) is a count, not a date, sd likewise
2. the scale of dnorm() is density, not count

So (slightly inefficient, but who cares...):

y <- rep(mydates, nc)
n <- sum(nc)
curve(n*dnorm(x, mean(y), sd(y)), add=TRUE, col="red", lwd=2)

-pd

> On 11 Apr 2020, at 16:00 , varin sacha via R-help <r-help using r-project.org> wrote:
> 
> Dear R-experts,
> 
> Here below my reproducible example. I would like to fit/add the Gauss normal curve to this data. 
> I don't get it. There is no error message but I don't get what I am looking for. 
> Many thanks for your help.
> 
> ############################################################
> mydates <- as.Date(c("2020-03-15", "2020-03-16","2020-03-17","2020-03-18","2020-03-19","2020-03-20","2020-03-21","2020-03-22","2020-03-23","2020-03-24","2020-03-25","2020-03-26","2020-03-27","2020-03-28","2020-03-29","2020-03-30","2020-03-31","2020-04-01","2020-04-02","2020-04-03","2020-04-04","2020-04-05","2020-04-06","2020-04-07","2020-04-08","2020-04-09","2020-04-10"))
> 
> nc<-c(1,1,2,7,3,6,6,20,17,46,67,71,56,70,85,93,301,339,325,226,608,546,1069,1264,1340,813,608)
> 
> plot(as.Date(mydates),nc,pch=16,type="o",col="blue",ylim=c(1,1400), xlim=c(min(as.Date(mydates)),max(as.Date(mydates))))
> 
> x <- seq(min(mydates), max(mydates), 0.1) 
> 
> curve(dnorm(x, mean(nc), sd(nc)), add=TRUE, col="red", lwd=2)
> ############################################################
> 
> ______________________________________________
> 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com



More information about the R-help mailing list