[R] Fitting a gaussian distribution to a plot

Rui Barradas ruipbarradas at sapo.pt
Thu Feb 21 11:36:49 CET 2013


Hello,

You've got a bug, Arun, the parenthesis are not right in 
((1/sd1)*sqrt(2*pi)). It should be (1/(sd1*sqrt(2*pi))).
The requsted values can also be computed using ?dnorm.



d1 <- (1/(sd1*sqrt(2*pi)))*exp(-((x1-mean1)^2)/(2*(sd1^2)))
d2 <- dnorm(x1, mean = mean1, sd = sd1)

all.equal(d1, d2)  # TRUE



Hope this helps,

Rui Barradas


Em 21-02-2013 02:55, arun escreveu:
> Hi,
>
> Do you need "exp^"?  Should it be "exp(-((...?
>
> x1<-as.numeric(x)
>   sd1<-sd(small)
> mean1<- mean(small)
> ((1/sd1)*sqrt(2*pi))*exp(-((x1-mean1)^2)/(2*(sd1^2)))
> # [1] 4.189541e-27 4.190295e-27 4.191049e-27 4.191803e-27 4.192557e-27
>   #[6] 4.193311e-27 4.194065e-27 4.194820e-27 4.195574e-27 4.196329e-27
> #[11] 4.197084e-27 4.197839e-27 4.198594e-27 4.199349e-27 4.200105e-27
> #[16] 4.200860e-27 4.201616e-27 4.202372e-27 4.203128e-27 4.203884e-27
> #[21] 4.204640e-27 4.205396e-27 4.206153e-27 4.206909e-27 4.207666e-27
> #[26] 4.208423e-27 4.209180e-27 4.209937e-27 4.210694e-27 4.211452e-27
> #[31] 4.212209e-27 4.212967e-27 4.213725e-27 4.214483e-27 4.215241e-27
> #[36] 4.215999e-27
> A.K.
>
>
>
> ----- Original Message -----
> From: Samantha Warnes <warnes at wisc.edu>
> To: r-help at r-project.org
> Cc:
> Sent: Wednesday, February 20, 2013 6:33 PM
> Subject: [R] Fitting a gaussian distribution to a plot
>
> I have a peak I would like to fit with a gaussian distribution, and am having difficulties. First of all I am having difficulties getting R to except my function input, and additionally I am having trouble plotting the non linear model on my scatterplot of data (the peak). Here is my log:
>
>> small <- c(507680,507670,508832,510184,511272,513380,515828,519160,525046,534046,547982,567124,590208,614506,637876,656846,669054,672976,668800,656070,637136,614342,590970,570752,554480,542882,535630,531276,528682,527682,527020,526834,526802,526860)
>> plot(small)
>> mean(small)
> [1] 563996.7
>> sd(small)
> [1] 55996.21
>
>> x <-c(0:35)
>> x <-as.numeric(x)
>> f <- ((1/(55996.21)*sqrt(2*pi)))*exp^(-((x-563996.7)^2)/(2*(55996.21^2)))
>
>
>
> Error in exp^(-((x - 563996.7)^2)/(2 * (55996.21^2))) :
> non-numeric argument to binary operator
>
>
>
>>From this point, to fit a non-linear model so I type nlm(f,507680)? I have tried reading up on this and looking for additional information on the internet but I am truly stuck. Thank you in advance!
> Samantha
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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