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.
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