0

I am a beginner in R.

I am working on creating a relative frequency histogram (from random uniform numbers with 1,000 samples of 2) with a normal curve over-layed on top.

So far this is my attempt at the code:

set.seed(32423432)
x1 <-runif(2000,0,1)
m<-matrix(x1,ncol=2)
msum<-apply(m,1,sum)
bins=seq(-4,4,by=.2)
msum2<-msum/2
msum2<-msum2-(1/2)
msum2<-msum2*sqrt(2)
hist(msum2,breaks=bins, freq= FALSE, right=FALSE)

I have a few problems with this:

  1. the percentage on the y-axis makes no sense to me (I would expect to see something between 0 and .5)
  2. I should see way more bins
  3. I have no idea how to change the x-axis labels to every .4
  4. I cannot seem to get a normal curve on top of this histogram
mrkb80
  • 581
  • 2
  • 8
  • 25
  • 1
    try `help(hist)` to find the manual for `hist` function and its parameters and try `lines(density(x1))` for normal curve after your `hist` – liuminzhao Jan 22 '13 at 19:11
  • 1
    Also, we don't know what `m` is defined as which makes it hard to help you. Please see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Michael Jan 22 '13 at 19:21
  • 1
    sorry about that. I edited the code to define m. Just learning how to use R and R studio, so thanks for understanding. – mrkb80 Jan 22 '13 at 19:30

1 Answers1

0
  1. Density sums to one over the whole plot (note that your interval sizes are less than 1). See ?hist "If all(diff(breaks) == 1), they are the relative frequencies counts/n"
  2. You would see more bins if msum2 had a greater range

    range(msum2) [1] -0.6918129 0.6795006

    hist(c(1,1,3,3 -3.5,-3.5, msum2),breaks=bins, freq= FALSE, right=FALSE)

3.

hist(msum2,breaks=bins, freq= FALSE, right=FALSE, xaxt="n")
axis(1,at=bins,labels=bins)

4 . Normal curve

x <- seq(-4, 4, length=100)

`lines(x,dnorm(x))`
Michael
  • 5,808
  • 4
  • 30
  • 39