[R] Bootstrapping help

Andrew Robinson A.Robinson at ms.unimelb.edu.au
Thu Jan 19 02:07:27 CET 2006


Ben,

Ok, it's clear now, thanks.  Note that your boot call 

boot(mydata,cs,R=999)

does not specify an "stype" argument.  The boot help file notes that
the default value for stype is "i", which means that boot will pass an
index to the function, not a weight, regardless of whether you call it
w, i, or whatever. 

The index that boot sends to the function is then used to index the
dataframe, thus selecting rows randomly with replacement.  Previously
you passed the dataframe to the function, which did not alter it, so
it passed through undisturbed.  In this incarnation the data<-data[w,]
command provides you with the (pseudo-)random sample with replacement
of the data.

I hope that this clears up the confusion.

Cheers,

Andrew

ps it's always good to provide a brief bit of sample code when you ask
a question.  Also, let me recommend that you omit semi-colons and
space the code to make it easier to read.  Thus

cs <- function(data, w) { 
     data<-data[w, ] 
     ...



On Wed, Jan 18, 2006 at 04:35:47PM -0800, Ben Ridenhour wrote:
> 
>    Thanks for responding :) Again...
>    I understand how indexing works (basically as in any other programming
>    language), that is why I am so confused as to why that statement made
>    my bootstraping work!  It seems like, if anything, it would completely
>    screw up everything.
>    Here is the (now working) cs function after I amended it to what you
>    said to do (i.e. I added the _very confusing_ statement
>    data<-data[w,]):
>    >cs<-function(data, w){
>    data<-data[w,];
>    y<-data[1];
>    x1<-data[2];
>    x2<-data[3];
>    x3<-data[4];
>    z<-data[5];
>    c1<-x1*z;
>    c2<-x2*z;
>    c3<-x3*z;
>    X<-cbind(x1,x2,x3,z,c1,c2,c3);
>    regcoef<-lsfit(X,y)$coefficients;
>    bx1<-regcoef[[2]];
>    bx2<-regcoef[[3]];
>    bx3<-regcoef[[4]];
>    bz<-regcoef[[5]];
>    bc1<-regcoef[[6]];
>    bc2<-regcoef[[7]];
>    bc3<-regcoef[[8]];
>    fx<-bx1*x1+bx2*x2+bx3*x3;
>    gy<-bz*z;
>    hxy<-bc1*c1+bc2*c2+bc3*c3;
>    sfx1<-cov(fx,x1);
>    sfx2<-cov(fx,x2);
>    sfx3<-cov(fx,x3);
>    sgx1<-cov(gy,x1);
>    sgx2<-cov(gy,x2);
>    sgx3<-cov(gy,x3);
>    shx1<-cov(hxy,x1);
>    shx2<-cov(hxy,x2);
>    shx3<-cov(hxy,x3);
>    sTx1<-cov(y,x1);
>    sTx2<-cov(y,x2);
>    sTx3<-cov(y,x3);
>    dataout<-c(sfx1,sgx1,shx1,sTx1,sfx2,sgx2,shx2,sTx2,sfx3,sgx3,shx3,sTx3
>    );
>    dataout
>    }
>    An example data frame would be
>     >mydata<-data.frame(Y=rnorm(20,0,1),X1=rnorm(20,0,1),X2=rnorm(20,0,1)
>    ,X3=rnorm(20,0,1),Z=rnorm(20,0,1))
>    The  boot statement is
>    >boot(mydata,cs,R=999)
>    Why does this rather mysterious indexing statement "data<-data[w,]"
>    make the bootstrap work when it didn't beforehand?
>    Thanks,
>    Ben
>    ps. I'll tell Melanie and Storfer hello.
>    -------------------------------
>    Benjamin Ridenhour
>    School of Biological Sciences
>    Washigton State University
>    P.O. Box 644236
>    Pullman, WA 99164-4236
>    Phone (509)335-7218
>    --------------------------------
>    "Nothing in biology makes sense except in the light of evolution."
>    -T. Dobzhansky
>    http://www.ms.unimelb.edu.au

-- 
Andrew Robinson  
Department of Mathematics and Statistics            Tel: +61-3-8344-9763
University of Melbourne, VIC 3010 Australia         Fax: +61-3-8344-4599
Email: a.robinson at ms.unimelb.edu.au         http://www.ms.unimelb.edu.au




More information about the R-help mailing list