I have found that specifying different "sizes" in the sample command has
a funny effect on the random sampling. The code below is a condensed
version of a function I wrote to simulate a bootstrap method. For
simplicity, I eliminated the internal bootstrap loop, but kept a
statement to draw one bootstrap sample, because this is where the
problem occurs. The output (mean(y)^2) should be the same for the two
runs of the function below, since it is based on the original rnorm
draw. For the first iteration of this function, it is, but thereafter
the results aren't the same. If I run this code in Splus, the results
are the same. Also, if I set the size argument to be the same (or
eliminate this line altogether), I get reproducible results.=20=20
=20
I'm using R 2.2.0
=20
Thanks,
-Lori
=20
test.fun<-function(nsamp, mu,q, seed, nsim=3D2){
set.seed(seed)=20=20=20=20
mnsq<-rep(NA,nsim)
for (i in 1:nsim){
y<-rnorm(nsamp,mean=3Dmu)
yboot<-sample(y, size=3Dq, replace=3DT)
mnsq[i]<-(mean(y)^2)
}
return(mnsq)
}
=20
test.fun(50,0,30,12345)
test.fun(50,0,10,12345)
=20
Lori E. Dodd, Ph.D.
National Cancer Institute
Biometric Research Branch
6130 Executive Blvd, MSC 7434
Rockville, MD 20892=20
(For overnight deliveries use 20852)
=20
Phone: 301-402-0383
FAX: 301-402-0560
Email: doddl at mail.nih.gov
=20
? bug in 'sample' (PR#8813)
2 messages · doddl at mail.nih.gov, Peter Dalgaard
doddl at mail.nih.gov writes:
I have found that specifying different "sizes" in the sample command has a funny effect on the random sampling. The code below is a condensed version of a function I wrote to simulate a bootstrap method. For simplicity, I eliminated the internal bootstrap loop, but kept a statement to draw one bootstrap sample, because this is where the problem occurs. The output (mean(y)^2) should be the same for the two runs of the function below, since it is based on the original rnorm draw. For the first iteration of this function, it is, but thereafter the results aren't the same. If I run this code in Splus, the results are the same. Also, if I set the size argument to be the same (or eliminate this line altogether), I get reproducible results.
What is the issue? To generate samples of different sizes, you may need a different number of random draws, so the RNG is not going to be in the same state the 2nd time you hit rnorm(). A current Splus (7.0.0) doesn't give you the same result either: > test.fun(50,0,30,12345) [1] 2.712664e-05 9.971680e-03
test.fun(50,0,10,12345)
[1] 2.712664e-05 2.225859e-03
I'm using R 2.2.0
Thanks,
-Lori
test.fun<-function(nsamp, mu,q, seed, nsim=2){
set.seed(seed)
mnsq<-rep(NA,nsim)
for (i in 1:nsim){
y<-rnorm(nsamp,mean=mu)
yboot<-sample(y, size=q, replace=T)
mnsq[i]<-(mean(y)^2)
}
return(mnsq)
}
test.fun(50,0,30,12345)
test.fun(50,0,10,12345)
Lori E. Dodd, Ph.D.
National Cancer Institute
Biometric Research Branch
6130 Executive Blvd, MSC 7434
Rockville, MD 20892
(For overnight deliveries use 20852)
Phone: 301-402-0383
FAX: 301-402-0560
Email: doddl at mail.nih.gov
[[alternative HTML version deleted]]
______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907