How indices calculated in package "boot"

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How indices calculated in package "boot"

Deng Nina
Hi,there,

I am using R package "boot" to bootstrap. I have one question here: does
anybody possibly know how the boot package generates the "indices" which is
used in the statistic function?

I thought "indices = sample(data, replace=TRUE)", but when I replaced
"indices" with this command and used "boot", I got different results.

Specifically, below are the codes for illustration.

(1) The typical way by generating indices in the package:
boot1 <- function (data, indices)
{
 d <- data[indices]
 return(d)
}
AA <- c(1:10)
require(boot)
set.seed(123)
results1 <- boot(data= AA, statistic=boot1, R=100)

(2) The alternative way by calculating "indices" myself:
boot2 <- function (data,indices)
{
 indices <- sample(data, replace=TRUE)
 d <- data[indices]
 return(d)
 }
AA <- c(1:10)
set.seed(123)
results2 <- boot(data= AA, statistic=boot2, R=100)

When I looked up using results1$t and results2$t, I had totoally different
bootstrap samples. I found this even had great impacts on the results in my
study. Does the second approach have any problem? Anyone could provide any
inputs on this? Thank you very much in advance!
Regards
Nina

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How indices calculated in package "boot"

glsnow
In your second example the boot function will still generate its
random indicies before your internal function calls sample, so the
seed will be different when you call sample from what you originally
set it to.  If you really want to know what the boot function does,
look at its code (it does some things to try and be more efficient
which might give different results than a simpler version).

On Wed, Feb 8, 2012 at 1:45 PM, Deng Nina <[hidden email]> wrote:

> Hi,there,
>
> I am using R package "boot" to bootstrap. I have one question here: does
> anybody possibly know how the boot package generates the "indices" which is
> used in the statistic function?
>
> I thought "indices = sample(data, replace=TRUE)", but when I replaced
> "indices" with this command and used "boot", I got different results.
>
> Specifically, below are the codes for illustration.
>
> (1) The typical way by generating indices in the package:
> boot1 <- function (data, indices)
> {
>  d <- data[indices]
>  return(d)
> }
> AA <- c(1:10)
> require(boot)
> set.seed(123)
> results1 <- boot(data= AA, statistic=boot1, R=100)
>
> (2) The alternative way by calculating "indices" myself:
> boot2 <- function (data,indices)
> {
>  indices <- sample(data, replace=TRUE)
>  d <- data[indices]
>  return(d)
>  }
> AA <- c(1:10)
> set.seed(123)
> results2 <- boot(data= AA, statistic=boot2, R=100)
>
> When I looked up using results1$t and results2$t, I had totoally different
> bootstrap samples. I found this even had great impacts on the results in my
> study. Does the second approach have any problem? Anyone could provide any
> inputs on this? Thank you very much in advance!
> Regards
> Nina
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.



--
Gregory (Greg) L. Snow Ph.D.
[hidden email]

______________________________________________
[hidden email] 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.