找回密码
 立即注册
查看: 7312|回复: 2

求助:

[复制链接]
发表于 2013-8-13 07:31:03 | 显示全部楼层 |阅读模式
为什么会报错:
我在演习R IN ACTION(中文版)中的BOOT自助法报错;按书中277页的作法来运行,如下:
> library(boot)
> data=mtcars
> matcars

>rsq=function(formula,data,indices) {d=data[indices,]
                                                    fit=lm(formula,data=d)
                                                     return(coef(fit))}

> library(boot)
> set.seed(1234)
> results=boot(data=mtcars,statistic=bs,R=1000,formula=mpg~wt+disp)
Error in boot(data = mtcars, statistic = bs, R = 1000, formula = mpg ~  :
  object 'bs' not found
BS是什么意思?为什么会报错;其间作单个统计量的BOOT法能够不出错,同时对此问题,在网上查了“statistic=bs”,无论是中文的还是英文的也都是这么写的。
请高手解答,谢谢!

回复

使用道具 举报

发表于 2013-8-13 09:49:15 | 显示全部楼层
你看看boot的帮助文档,
>?boot
这个里面说的意思是statistic是一个函数,

statistic       
A function which when applied to data returns a vector containing the statistic(s) of interest. When sim = "parametric", the first argument to statistic must be the data. For each replicate a simulated dataset returned by ran.gen will be passed. In all other cases statistic must take at least two arguments. The first argument passed will always be the original data. The second will be a vector of indices, frequencies or weights which define the bootstrap sample. Further, if predictions are required, then a third argument is required which would be a vector of the random indices used to generate the bootstrap predictions. Any further arguments can be passed to statistic through the ... argument.


看他的例子:
# Usual bootstrap of the ratio of means using the city data
ratio <- function(d, w) sum(d$x * w)/sum(d$u * w)
boot(city, ratio, R = 999, stype = "w")

运行一下:
> ratio <- function(d, w) sum(d$x * w)/sum(d$u * w)
> boot(city, ratio, R = 999, stype = "w")

ORDINARY NONPARAMETRIC BOOTSTRAP


Call:
boot(data = city, statistic = ratio, R = 999, stype = "w")


Bootstrap Statistics :
    original     bias    std. error
t1* 1.520313 0.02101307   0.2211474
> ratio
function(d, w) sum(d$x * w)/sum(d$u * w)
> city
     u   x
1  138 143
2   93 104
3   61  69
4  179 260
5   48  75
6   37  63
7   29  50
8   23  48
9   30 111
10   2  50
回复

使用道具 举报

 楼主| 发表于 2013-8-19 07:04:17 | 显示全部楼层
我知道了。谢谢!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|R语言中文网

GMT+8, 2024-11-25 07:18 , Processed in 0.032247 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表