floating point - R generates fewer random numbers than specified with rnorm, rexp, rpois and runif -
I get random numbers for two groups of a vector of size.
A group is P, and for other q = 1-p (for example, P = 0.5 for population of 1000, I have to generate 500 random numbers from distribution and 500 to 500) Since this is a simulation in which I have to be 'P' different, so I have written my code to generate it like this:
group1 = rnorm (n = n * p) group2 = rnorm (N = n * * Q) # First method group 2 = rnorm (n = (n - n * p)) # second method With all the above methods, r is a less random The number generates in many rows of group 2 (approximately 35% of the rows with the first, and approximately 12% of the rows with the second method).
I also go to the same bug with the Resposi, RPI, and Runif. / P>
Below is a snapshot of both methods for your reference.
#### Example script ##### N = 1000 p1 = seq (0.01, 0.99, 0.001) q1 = 1 - p1 ### First METHOD ### X = Data For x (i in 1: length (p1)) {x [i, 1] = p1 [i] x [i, 2] = q1 [i] x [i, 3] = length ( Rainifice (x * i, ii) 1) x [i, 4] = length (runoff (n * x [i, 2])) x [ii, 5] = x [ii] 4] + x (I, 3)} table (x [, 5] == 1000) # column three + colman four should be added to 1000 ### SECOND METHOD # ## for Y = data.fr () (i in 1: Length (P1)) {Y [i, 1] = P1 [i] y [i, 2] = q1 [i] y [i, 3] = length (ranif ((* n [y, 1]))) Y [m, 4] = length (runinif (n - n) y [i, 1]) y [i, 5] = y [i, 3] + y [i, 4]} table (Y [, 5] == 1000) # Column Three + Column For Four Yoga 1000
R General Question 7.31 - Spherical Error - Your special problem proves to be:
& gt; P = 0.32 & gt; P * 1000 + (1-P) * 1000 [1] 1000 looks well but is it really?
& gt; (P * 1000 + (1-P) * 1000) == 1000 [1] FALSE No. why not? How is this wrong?
& gt; (P * 1000 + (1-P) * 1000) - 1000 [1] -1.136868e-13 10 ^ -13 in 1 part of it: < Pre> & gt; Length (runiff (1000 * P)) [1] 320 & gt; Length (runoff (1000 * (1-p))) [1] 679 Because:
& gt; As.integer (1000 * p) [1] 320 & gt; As.integer (1000 * (1-p)) [1] 679 which increases until 999. See RF 7.31 for details of floating point estimation.
The solution is to work in an integer as much as possible when dealing with matters.
& gt; NP = as.integer (1000 * p) & gt; Length (runiff (NP)) [1] 320 & gt; Computing as q as 1-p and by multiplying it by (runoff (1000-NP)) [1] 680 1000-N * p to try and obtain N .
Comments
Post a Comment