Dear R-experts, I am trying to fit a gaussian density curve. More precisely, I would like to obtain the fitted "Gaussian curve". "m" is the gaussian mean, "sd" is the standard deviation and "k" is an arbitrary scaling parameter (since the gaussian density is constrained to integrate to 1, whereas my data isn't). There is no error message. In my opinion, the last command of my reproducible R script is not doing its job ! Many thanks for your lights. ############################################################ 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) d <-as.numeric(a) m<-11.84 sd<-3.93 k<-359.77 plot(b~d) plot(function(d) k*exp(-0.5*(d-m)^2/sd^2),col=2,add=TRUE,xlim=range(d)) ############################################################ ?
On Sat, 25 Apr 2020 19:02:59 +0000 (UTC) varin sacha via R-help <r-help at r-project.org> wrote:> plot(function(d) > k*exp(-0.5*(d-m)^2/sd^2),col=2,add=TRUE,xlim=range(d))Remove the `add=TRUE` or calculate the values manually and you'll see that the function value is zero because the values in `d` are too big compared to `m` (where the function peak is). Compare your result to the following: plot( function(d) k*exp(-0.5*(d-m)^2/sd^2), xlim = m + 3*sd * c(-1,+1) ) Your parameters don't quite fit the observed values. -- Best regards, Ivan
Have a look at the dnorm function: ?dnorm Notes: (1) You'd be better to say estimate (or fit) parameters (or coefficients) and plot the resulting normal distribution (or normal density), rather than say fit a density curve, because someone may think you want a kernel density estimate with a Gaussian kernel. (2) Densities integrate to one by definition. i.e. If a function didn't integrate to one it wouldn't be a probability density function, but still could be a function representing a probability distribution... On Sun, Apr 26, 2020 at 7:09 AM varin sacha via R-help <r-help at r-project.org> wrote:> > Dear R-experts, > > I am trying to fit a gaussian density curve. More precisely, I would like to obtain the fitted "Gaussian curve". > "m" is the gaussian mean, "sd" is the standard deviation and "k" is an arbitrary scaling parameter (since the gaussian density is constrained to integrate to 1, whereas my data isn't). > There is no error message. In my opinion, the last command of my reproducible R script is not doing its job ! > > Many thanks for your lights. > > ############################################################ > 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) > > d <-as.numeric(a) > > m<-11.84 > > sd<-3.93 > > k<-359.77 > > plot(b~d) > > plot(function(d) k*exp(-0.5*(d-m)^2/sd^2),col=2,add=TRUE,xlim=range(d)) > ############################################################ > > > ______________________________________________ > R-help at 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.
Ivan, Abby, Many thanks for your help. Best, Le samedi 25 avril 2020 ? 22:09:03 UTC+2, Abby Spurdle <spurdle.a at gmail.com> a ?crit : Have a look at the dnorm function: ?dnorm Notes: (1) You'd be better to say estimate (or fit) parameters (or coefficients) and plot the resulting normal distribution (or normal density), rather than say fit a density curve, because someone may think you want a kernel density estimate with a Gaussian kernel. (2) Densities integrate to one by definition. i.e. If a function didn't integrate to one it wouldn't be a probability density function, but still could be a function representing a probability distribution... On Sun, Apr 26, 2020 at 7:09 AM varin sacha via R-help <r-help at r-project.org> wrote:> > Dear R-experts, > > I am trying to fit a gaussian density curve. More precisely, I would like to obtain the fitted "Gaussian curve". > "m" is the gaussian mean, "sd" is the standard deviation and "k" is an arbitrary scaling parameter (since the gaussian density is constrained to integrate to 1, whereas my data isn't). > There is no error message. In my opinion, the last command of my reproducible R script is not doing its job ! > > Many thanks for your lights. > > ############################################################ > 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) > > d <-as.numeric(a) > > m<-11.84 > > sd<-3.93 > > k<-359.77 > > plot(b~d) > > plot(function(d) k*exp(-0.5*(d-m)^2/sd^2),col=2,add=TRUE,xlim=range(d))> ############################################################ > > > ______________________________________________ > R-help at 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.