1

I have a program that simulates a fair dice 100 times. Using this program I need to have a bell curve over the histogram showing normal/Gaussian distribution. In my code The variable representing the outcome of the experiment is z. The definition of the bell curve contains two parameters: ave determines where on the z-axis the curve must have a maximum and sig determines the width of the peak.

 set.seed(123)

 x <- sample(1:6, size=100, replace = TRUE)

 hist(x,
 main="100 fair rolls",
 xlab = "Dice Result",
 ylab = "Probability",
 xlim=c(0.5,6.5), 
 breaks=-1:100+.5,
 prob=TRUE )

Gaussian distribution equation I have so far:

    bell<-function(z,ave,sig){
exp(-(z-ave)^2/(2*sig))/sqrt(2*pi*sig)
    }

    points<-0:600

    lines(points,bell(points,350,292))
JaredJoss
  • 19
  • 3
  • 1
    There is a built-in function, `dnorm`, which does what you want `bell` to do. – IceCreamToucan Apr 22 '19 at 20:56
  • 2
    Have you seen [Overlay normal curve to histogram in R](https://stackoverflow.com/q/20078107/4752675) ? – G5W Apr 22 '19 at 21:00
  • @G5W I have but I need to use the equation: exp(-(z-ave)^2/(2*sig))/sqrt(2*pi*sig) and not just use the dnorm function – JaredJoss Apr 22 '19 at 21:33
  • @JaredJoss `dnorm` uses the same formula which you are using. If you want to see what logic the code of dnorm uses, please find it here: https://github.com/wch/r-source/blob/trunk/src/nmath/dnorm.c – humblecoder Apr 22 '19 at 23:00
  • Could you please show me how to do it? I am new to R – JaredJoss Apr 23 '19 at 07:20

0 Answers0