自定义引导标准错误:数字' envir' arg不是长度一

时间:2014-05-08 03:29:59

标签: r save simulation glm statistics-bootstrap

我正在编写一个自定义脚本来引导R中GLM中的标准错误,并收到以下错误:

  

eval中的错误(predvars,data,env):数字'envir'arg不是长度为1

有人可以解释我做错了什么吗?我的代码:

#Number of simulations
sims<-numbersimsdesired

#Set up place to store data
saved.se<-matrix(NA,sims,numberofcolumnsdesired)
y<-matrix(NA,realdata.rownumber)
x1<-matrix(NA,realdata.rownumber)
x2<-matrix(NA,realdata.rownumber)

#Resample entire dataset with replacement
for (sim in 1:sims) {
    fake.data<-sample(1:nrow(data5),nrow(data5),replace=TRUE)

    #Define variables for GLM using fake data
    y<-realdata$y[fake.data]
    x1<-realdata$x1[fake.data]
    x2<-realdata$x2[fake.data]

    #Run GLM on fake data, extract SEs, save SE into matrix
    glm.output<-glm(y ~ x1 + x2, family = "poisson", data = fake.data)  
    saved.se[sim,]<-summary(glm.output)$coefficients[0,2]
    }

一个例子:如果我们假设sims = 1000并且我们想要10列(假设代替x1和x2,我们有x1 ... x10),则目标是包含1,000行和10列的数据集,其中包含每个解释变量的SE。

1 个答案:

答案 0 :(得分:1)

没有理由重新发明轮子。以下是使用引导程序包引导拦截标准错误的示例:

set.seed(42)
counts <- c(18,17,15,20,10,20,25,13,12)
x1 <- 1:9
x2 <- sample(9)
DF <- data.frame(counts, x1, x2)
glm1 <- glm(counts ~ x1 + x2, family = poisson(), data=DF)
summary(glm1)$coef

#              Estimate Std. Error  z value     Pr(>|z|)
#(Intercept) 2.08416378 0.42561333 4.896848 9.738611e-07
#x1          0.04838210 0.04370521 1.107010 2.682897e-01
#x2          0.09418791 0.04446747 2.118131 3.416400e-02

library(boot)

intercept.se <- function(d, i) {
  glm1.b <- glm(counts ~ x1 + x2, family = poisson(), data=d[i,])
  summary(glm1.b)$coef[1,2]
}

set.seed(42)
boot.intercept.se <- boot(DF, intercept.se, R=999)

#ORDINARY NONPARAMETRIC BOOTSTRAP
#
#
#Call:
#boot(data = DF, statistic = intercept.se, R = 999)
#
#
#Bootstrap Statistics :
#     original     bias  std. error
#t1* 0.4256133 0.103114   0.2994377

修改

如果你喜欢没有包裹的话:

n <- 999
set.seed(42)
ind <- matrix(sample(nrow(DF), nrow(DF)*n, replace=TRUE), nrow=n)
boot.values <- apply(ind, 1, function(...) {
  i <- c(...)
  intercept.se(DF, i)
})

sd(boot.values)
#[1] 0.2994377