Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

R: Getting around periodic random numbers

I'm building a distribution of proportions of random values lower than a specified threshold.

ret = c(); thr = 0.5; n = 15; reps = 10000
for (x in 1:reps) {
  prop = sum(runif(n, 0, 1) < thr) / n
  ret = c(ret, prop)
}
plot(density(ret), main=NA)

The resulting density distribution is highly periodic and is not something one would theoretically expect:

enter image description here

I presume this has something to do with the periodicity of the random number generator. Is there any way around this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your distribution is discrete, and hence a kernel density estimate isn't the right tool. Instead you should be plotting the frequencies, as indicated by @nicola and other commenters.

i.e.

plot(table(ret))

gives:

plot

Or, if you want probabilities, you could do:

plot(table(ret) / length(ret))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...