[R] Bootstrapping in R

ruipbarradas at sapo.pt ruipbarradas at sapo.pt
Sun Oct 2 14:37:50 CEST 2016


Right.
To see it in action just compare the results of the two calls to boot.

library(boot)

set.seed(1007)

x <- rnorm(100)
y <- x + rnorm(100)
dat <- data.frame(x, y)

#Wrong
stat1 <- function(DF, f){
	model <- lm(DF$y ~ DF$x, data = DF[f,])  #Doesn't bootstrap DF
	coef(model)
}

#Correct
stat2 <- function(DF, f){
	model <- lm(y ~ x, data = DF[f,])
	coef(model)
}

boot(dat, stat1, R = 100)
boot(dat, stat2, R = 100)


Rui Barradas


Citando peter dalgaard <pdalgd at gmail.com>:

>> On 01 Oct 2016, at 16:11 , Daniel Nordlund <djnordlund at gmail.com> wrote:
>>
>> You haven't told us anything about the structure of your data, or  
>> the definition of the DataSummary function.
>
> Yes. Just let me add that a common error with boot() is not to pay  
> attention to the required form of the statistic= function argument.  
> It should depend on the data and a set of indices and (for  
> nonparametic bootstrap) it is the indices that are random.
>
> Typical mistakes are to completely ignore the index argument, or to  
> write clumsy code that ignores the data specification, as in
> coef(lm(df$y~df$x, data=d[f])).
>
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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