0

I start saying that i am really new to R, hence this could be a very simple problem.

I want to do a monte carlo simulation with a beta distribution. The case is that i have a website, which will receive from 0 to 10 visualisations each day for one year, and i want to simulate this.

The first step in my dissertation is to produce a diagram showing the shape of the beta distribution.

To do so, i do

x=rbeta(365,1,4,ncp=0)
y=dbeta(x,1,4)
plot(10*x,y,type="l")

which gives to me an ugly diagram like this: first diagram

then i tried to do

x=rbeta(365,1,4,ncp=0)
plot(density(10*x))

in this case i obtain the right diagram which has this flaw: it exceedes the domain [0,10] on the 0 side:

second diagram

can anyone help me? thank you for your help in advance!

Answer part: by using plot(density(10*x), xlim = c(0, 10)) i obtain answer 1 which again has the ptoblem with the offset of the domain and not only, it seems like if the function was moved to the right, since i expect that given x1

Pezze
  • 741
  • 1
  • 7
  • 14

1 Answers1

0

Do you literally just want a plot of the Beta distribution with parameters 1, 4?:

# Points over which we will evaluate Beta PDF
x <- seq(0, 1, 0.05)

# PDF values
y <- dbeta(x, 1, 4, ncp=0)

plot(x*10, y, type="l")

enter image description here

If you really want something simulated you could also do a histogram like this:

hist(rbeta(2000, 1, 4, ncp=0), breaks=25)

enter image description here

andybega
  • 1,387
  • 12
  • 19
  • You almost solved all of my problems my friend. Can you also tell me how can i switch from absolute frequency to relative frequency on the y axis? – Pezze Oct 02 '14 at 15:51
  • For the histogram? `hist(..., freq=TRUE)` – andybega Oct 02 '14 at 15:54
  • I tried it out, and still shows on the y axis the value of the absolute frequencies – Pezze Oct 02 '14 at 16:00
  • since we are here, and now sorry if I keep asking, but I am researching how to solve the problem without asking anymore as well. How do i insert in the histogram a line which represent the approximation of the bars? – Pezze Oct 02 '14 at 16:10
  • You can use density for this, like you asked in your original question: after `hist()` do `lines(density(y, from=0, to=1))`. It's also possible to figure out what the x-y coordinates for each bin in the histogram are and fit some smoothed curve to it, but that would be more complicated as far as I know. – andybega Oct 02 '14 at 19:23