[R] bootstrapping
Prof Brian Ripley
ripley at stats.ox.ac.uk
Thu Jun 8 08:07:27 CEST 2006
In non-parametric bootstrapping, you do draw a sample of the same size as
the original. So
> D <- as.vector(WorldPhones[,1])
> sample(D, replace=TRUE)
[1] 45939 64721 60423 79831 71799 45939 76036
> sample(D, replace=TRUE)
[1] 45939 68484 79831 68484 76036 71799 79831
are two different bootstrap resamples. The problem in the original code
is that it is trying to replace 1 value by a sample. Removing extraneous
bracketing, it was
> boot = numeric(200)
> for (i in 1:200) boot [i] = sample(D,replace=T)
There were 50 or more warnings (use warnings() to see the first 50)
and those warnings are
> warnings()
Warning messages:
1: number of items to replace is not a multiple of replacement length
*and* the values returned are not all the same (as claimed).
To return a set of bootstrap resamples you need something like
boot <- matrix(0,200,length(D))
for (i in 1:200) boot [i,] <- sample(D, replace=TRUE)
or you might intend to save the resampled statistic and not the sample
itself.
On Wed, 7 Jun 2006, Clément Viel wrote:
> 2006/6/7, Recep Aykaya <recepaykaya at gmail.com>:
>>
>> hi.
>> i'm a statistics student and studying bootstrap in R.
>>
>> i'm trying to draw bootstrap samples from a sample, using the following R
>> code:
>>
>>> *boot = numeric(200)*
>> *> {for (i in 1:200)*
>>
>> * boot [i] = (sample(data,replace=T))}*
>>
>>
>>
>> i obtain 200 samples but all of them are the same.
>>
>> i want to obtain different samples. what should i do? can you please help
>> me
>> if possible.
>>
>>
>>
>> thank you.
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide!
>> http://www.R-project.org/posting-guide.html
>>
>
> Hi,
> I think you have forgotten an parameter of the sample function. Indeed the
> help page give these information:
>
> sample(x, size, replace = FALSE, prob = NULL)
>
> size: non-negative integer giving the number of items to choose
>
> By default 'size' is equal to 'length(x)' so that 'sample(x)'
> generates a random permutation of the elements of 'x' (or '1:x').
>
> Therefore try to add the number of observed values with this:
>
> nb=100 #the number of values for each sample
> boot = c()
> for (i in 1:200)
> boot [i] = mean(sample(x=data,size=nb,replace=TRUE)) # if you try to
> estimate the mean
>
>
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list