0

I have the following piece of code in R:

w=rbeta(365,1,3,ncp=0)
hist(10*w,breaks=25,freq=TRUE,xlim=c(0,10), ylim=c(0,60))
h=seq(0,1,0.05)

So far so good. What I want to do now is to add a line representing the beta function having parameters alpha=1, beta=3 (as in the rbeta function I used), which takes into account the frequency and not the density. The total number of elements in the rbeta is 365 (the days in a year) and the reason why I multiply w by 10 is because the variable I am studying can assume value [0,10] each day, following the beta distribution described above.

What do I have to do to represent this line?

Summarizing, the histogram is based on simulated values, and I want to show how the theoretical beta function would had behaved in comparison to the simulation.

nrussell
  • 18,382
  • 4
  • 47
  • 60
Pezze
  • 741
  • 1
  • 7
  • 14
  • `dbeta` is the pdf for the beta distribution. – Neal Fultz Oct 02 '14 at 18:10
  • I am aware of that, but when I add it into the graph, its initial value is equal to 3, while the histogram with freq=TRUE starts from 45, and the histogram with freq=FALSE starts from 0.25 – Pezze Oct 02 '14 at 18:13

1 Answers1

0

If you want them to match up, you're going to want to match up the area under the curves of the histogram and the density plot. That should put them on the same scale. One way to do that would be

set.seed(15) #to make it reproducible
w <- rbeta(365, 1, 3, ncp=0)
hh <- hist(w*10, breaks=25, freq=TRUE, xlim=c(0,10), ylim=c(0,60))
ss <- sum(diff(hh$breaks)*hh$counts)
curve(dbeta(x/10, 1, 3, ncp=0)*ss/10, add=T)

This gives

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295