我正在尝试为3个参数估计构建一个bootstrap t置信区间,但我只能构造第一个参数。下面是我的代码:
beta0<--8
beta1<-0.03
gamma<-0.0105
alpha<-0.05
n<-100
N<-10
for (i in 1:N)
{
u<-runif(n)
x<-rnorm(n)
c<-rexp(n,1/1255)
t1<-(1/gamma)*log(1-((gamma/exp(beta0+beta1*x))*log(1-u)))
t<-pmin(t1,c)
delta<-1*(t1>c)
length(delta)
delta[delta==1]<-ifelse(rbinom(length(delta[delta==1]),1,0.75),1,2)
deltae<-ifelse(delta==0, 1,0)
deltar<-ifelse(delta==1, 1,0)
deltai<-ifelse(delta==2, 1,0)
dat=data.frame(t,delta, deltae,deltar,deltai,x)
dat$interval[delta==2] <- as.character(cut(dat$t[delta==2], breaks=seq(0, 600, 100)))
labs <- cut(dat$t[delta==2], breaks=seq(0, 600, 100))
dat$lower[delta==2]<-as.numeric( sub("\\((.+),.*", "\\1", labs) )
dat$upper[delta==2]<-as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", labs) )
beta0hat.boot <- function(data,j)
{
dat<-data[j,]
data0<-dat[which(dat$delta==0),]#uncensored data
data1<-dat[which(dat$delta==1),]#right censored data
data2<-dat[which(dat$delta==2),]#interval censored data
library(maxLik)
#without imputataion
ll<-function(para)
{
b0<-para[1]
b1<-para[2]
g<-para[3]
e<-sum((b0+b1*data0$x)+g*data0$t+(1/g)*exp(b0+b1*data0$x)*(1-exp(g*data0$t)))
r<-sum((1/g)*exp(b0+b1*data1$x)*(1-exp(g*data1$t)))
i<-sum(log(exp((1/g)*exp(b0+b1*data2$x)*(1-exp(g*data2$lower)))-exp((1/g)*exp(b0+b1*data2$x)*(1-exp(g*data2$upper)))))
l<-e+r+i
return(l)
}
est<-maxLik(logLik=ll,start=c(para<-c(-8,0.03,0.0105)))
beta0hat<-est$estimate[1]
beta1hat<-est$estimate[2]
gammahat<-est$estimate[3]
observed<-solve(-est$hessian)
return(c(beta0hat,beta1hat,gammahat,observed[1,1],observed[2,2],observed[3,3]))
}
library(boot)
out<- boot(dat,beta0hat.boot,100)
ci<-boot.ci(out,type =c("stud","perc"),var.t0= out$t0[4],var.t=out$t[,4])
ci1<-boot.ci(out,type = c("stud","perc"),var.t0= out$t0[4],var.t=out$t[,4],index=1)
}
我只能在不使用index = 1的情况下构建第一个参数的置信区间,如下所示
ci<-boot.ci(out,type =c("stud","perc"),var.t0= out$t0[4],var.t=out$t[,4])
当我添加index = 1时,
ci1<-boot.ci(out,type = c("stud","perc"),var.t0= out$t0[4],var.t=out$t[,4],index=1)
我收到了警告:
In boot.ci(out, type = c("stud", "perc"), var.t0 = out$t0[4], var.t = out$t[, :
bootstrap variances needed for studentized intervals.
导致置信区间无法建立。
我想添加index = 1,因为我需要使用index = 2和index = 3来构建第二个和第三个参数的置信区间,但是在我添加索引之后,我无法获得置信区间。知道如何获得多个统计数据的bootstrap t置信区间吗?我只能通过使用上面的代码获得百分位数区间,但不能获得自举t区间。
dput(head(dat, 30))
:
structure(list(t = c(143.786081550783, 104.647251273501, 65.5655695306165,
322.260530195967, 307.324160556309, 123.725827237157, 143.083942557736,
426.646805286557, 360.799323547846, 246.295906287976, 315.638222801499,
421.155652813492, 374.160625875751, 123.570819609099, 389.553807438155,
449.110810924173, 162.547725691726, 296.674617375856, 229.680453578098,
343.823664337269, 268.797764971971, 205.704838761594, 14.8630247008987,
91.6607201565057, 260.886289739501, 193.278377859747, 143.269719508224,
27.4780640122481, 267.191708749538, 39.8027657018974), delta = c(1,
0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
1, 1, 0, 1, 0, 1, 0, 1), deltae = c(0, 1, 1, 1, 0, 1, 1, 1, 1,
1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0
), deltar = c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1), deltai = c(0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0), x = c(-0.377643157724086, 0.218708630964153,
0.153162542263512, 1.28222373181234, 1.1423312203422, -1.04726335577352,
-0.360028857222088, 0.336098821643731, 0.758860540656158, 0.0533940581013279,
-0.0562132826178519, 0.0798656325287969, -0.748956798800072,
-0.235929730488004, -0.737049993834757, 1.05819046250488, 1.28776064495481,
0.457930197196181, -1.45563588128583, -1.1074384621643, -0.829026816498185,
-1.3824961444269, -1.58951008909026, -0.95052226776903, 0.0145909317659764,
-0.198775419436042, 0.0481467746529365, -0.136098038815233, -0.118319488454131,
-0.498263758291143), interval = c(NA, NA, NA, NA, "(300,400]",
NA, NA, NA, NA, NA, "(300,400]", NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), lower = c(NA,
NA, NA, NA, 300, NA, NA, NA, NA, NA, 300, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), upper = c(NA,
NA, NA, NA, 400, NA, NA, NA, NA, NA, 400, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("t",
"delta", "deltae", "deltar", "deltai", "x", "interval", "lower",
"upper"), row.names = c(NA, 30L), class = "data.frame")