[R] Random # generator accuracy

Greg Snow Greg.Snow at imail.org
Thu Jul 23 20:35:53 CEST 2009


Try adding replace=TRUE to your call to sample, then you will get numbers closer to what you are expecting.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Jim Bouldin
> Sent: Thursday, July 23, 2009 12:00 PM
> To: r-help at r-project.org
> Subject: [R] Random # generator accuracy
> 
> 
> Dan Nordlund wrote:
> 
> "It would be necessary to see the code for your 'brief test' before
> anyone
> could meaningfully comment on your results.  But your results for a
> single
> test could have been a valid "random" result."
> 
> I've re-created what I did below.  The problem appears to be with the
> weighting process: the unweighted sample came out much closer to the
> actual
> than the weighted sample (>1% error) did.  Comments?
> Jim
> 
> > x
>  [1]  1  2  3  4  5  6  7  8  9 10 11 12
> > weights
>  [1] 1 1 1 1 1 1 2 2 2 2 2 2
> 
> > a = mean(replicate(1000000,(sample(x, 3, prob = weights))));a  # (1
> million samples from x, of size 3, weighted by "weights"; the mean
> should
> be 7.50)
> [1] 7.406977
> > 7.406977/7.5
> [1] 0.987597
> 
> > b = mean(replicate(1000000,(sample(x, 3))));b  # (1 million samples
> from
> x, of size 3, not weighted this time; the mean should be 6.50)
> [1] 6.501477
> > 6.501477/6.5
> [1] 1.000227
> 
> 
> Jim Bouldin, PhD
> Research Ecologist
> Department of Plant Sciences, UC Davis
> Davis CA, 95616
> 530-554-1740
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list