Hi


From: Clive Nicholas [mailto:clivelists@googlemail.com]
Sent: Tuesday, November 13, 2012 3:12 AM
To: PIKAL Petr
Cc: r-help@r-project.org
Subject: Re: [R] Bootstrapping issues

Petr Pikal replied:

[...]

> The following works
>
> results <- boot(data=test, statistic=bs, R=1000, A~B+C+D+C*D)
>
Actually it does not work either

Correct, but I _did_ get it to work shortly before my initial post (sorry for not showing it, but I didn't save the output - silly me).

What seems to happen is that I successfully run the version that _does_ work, which I then follow up with successful calls to -plot(results,index=#)- and -boot.ci<http://boot.ci>(results,type="bca",index=#)-, then I follow up with the unsuccessful version of the code, which won't run and produces the error, but when I go back to run the original code, this too produces the same error. This, to me at least, is very weird behaviour for R.


> results <- boot(data=test, statistic=bs, R=1000, A~B+C+D+C*D)
Error in data[indices, ] : incorrect number of dimensions
>
I am not sure but I suspect your bs function expects some indices vector and it is somehow not in accordance with your data.

This code, using different simulated data _does_ run, however:




> mytest<-data.frame(Y=rnorm(500, mean=60, sd=1), X1=sample(c("AAA", "BBB", "CCC", "DDD", "None", NA), size=500, replace=TRUE), X2=sample(c(0.88, 0.99, 1.43, 1.22, 1.1), size=500, replace=TRUE), X3=sample(c("Yes", "No"), size=500, prob=c(0.6,0.4), replace=TRUE))







> bs <- function(formula, data, indices) {

+     mytest <- data[indices,]

+     fit <- lm(formula, data=mytest)

+     return(coef(fit))

+ }

> results <- boot(data=mytest, statistic=bs, R=1000, formula=Y~X1+X2+X3)



Hm. Maybe the difference is in using formula.


> results <- boot(data=test, statistic=bs, R=1000, A~B+C+D+C*D)



>From help page, the fourth parameter is called sim and shall be character vector.



See below.



> results <- boot(data=mytest, statistic=bs, R=1000, Y~X1+X2+X3)

Error in data[indices, ] : incorrect number of dimensions

> results <- boot(data=mytest, statistic=bs, R=1000, formula=Y~X1+X2+X3)

> results



ORDINARY NONPARAMETRIC BOOTSTRAP





Call:

boot(data = mytest, statistic = bs, R = 1000, formula = Y ~ X1 +

    X2 + X3)





Bootstrap Statistics :

       original        bias    std. error

t1* 59.67822240 -0.0060177161  0.29491879

t2*  0.09745349  0.0004995554  0.15627776

t3*  0.24210280 -0.0047317673  0.15018019

t4* -0.03399254 -0.0046603446  0.15884994

t5*  0.15302117 -0.0038503464  0.14045470

t6*  0.15040121  0.0074829413  0.24225957

t7*  0.18454691 -0.0003127565  0.09321305

>




<snip>

Answers please!

Do not shout, please. I presented an answer and if you consulted help page you could find out what I also found out.

Regards
Petr

Clive Nicholas (clivenicholas.posterous.com<http://clivenicholas.posterous.com>

[Please DO NOT mail me personally here, but at <clivenicholas@hotmail.com<mailto:clivenicholas@hotmail.com>>. Please respond to contributions I make in a list thread here. Thanks!]

"My colleagues in the social sciences talk a great deal about methodology. I prefer to call it style." -- Freeman J. Dyson

	[[alternative HTML version deleted]]


