通过boot :: boot()传递一个额外的参数

时间:2017-10-26 15:36:28

标签: r

MWE:

library(boot)
calculate_coeffs <- function(data, index){
  return( coef(lm(mpg~hp, data = data, subset = index)) )
}
boot(data = mtcars, statistic = calculate_coeffs, R = 1000)


ORDINARY NONPARAMETRIC BOOTSTRAP


Call:
boot(data = mtcars, statistic = calculate_coeffs, R = 1000)


Bootstrap Statistics :
       original       bias    std. error
t1* 30.09886054  0.204255101  2.13998077
t2* -0.06822828 -0.002014834  0.01418847

我想传递formula,目前为mpg~hp,作为boot()的参数。

?boot::boot

  

任何进一步的参数都可以通过......传递给statistic   参数。

所以我想,让我们试试这个:

library(boot)
calculate_coeffs <- function(data, index){
  return( coef(lm(formula, data = data, subset = index)) )
}
boot(data = mtcars, statistic = calculate_coeffs, R = 1000, formula = mpg~hp)

Error in statistic(data, original, ...) : 
  unused argument (formula = mpg ~ hp)

然后我想,让我们把它放在calculate_coeffs的一个论据中:

library(boot)
calculate_coeffs <- function(data, index, formula){
  return( coef(lm(formula, data = data, subset = index)) )
}
boot(data = mtcars, statistic = calculate_coeffs, R = 1000, formula = mpg~hp)

 Error in eval(substitute(subset), data, env) : object 'index' not found 

出于某种原因,https://www.statmethods.net/advstats/bootstrapping.html处的示例与我所遇到的问题不同 - 而且它们对我来说都很好。

0 个答案:

没有答案