我是R的新手,我发现使用启动功能生成参数自举样本有点困难。我已经计算了weibull分布的mle参数,现在我需要生成500个重复。
到目前为止我已经这样做了
#givenData is of this form---> int [1:2546] 69 49 40 33 33 .....
library("boot")
library("MASS")
library("fitdistrplus")
library("actuar")
statFunc = function(d, i)
{
d2 = d
return(mean(d2[i]))
}
#givenData is the observed data to which I am fitting a weibull distribution
fitWeibull = fitdist(givenData, "weibull")
mleParams = c(fitWeibull$estimate["shape"], fitWeibull$estimate["scale"])
#mleParams is a list - mleParams[1] is shape, mleParams[2] is scale
pBootFunc = function(d, mle)
{
out <- d
out$param <- rweibull(length(out), mle[1], mle[2])
out
}
# data has two columns - "serialNum" and "pre" (pre has integer values)
pbootstrap = boot(givenData, statFunc, R = 2, sim = "parametric", ran.gen = pBootFunc, mle = mleParams)
pVal = boot.array(pbootstrap, indices = T)
当我运行它时,我认为“boot.array中的错误(pbootstrap,indices = T): 无法找到参数自举“
的数组我希望pVal包含bootstrap重复。我该怎么做?
答案 0 :(得分:2)
1)用于参数自举 由于您已经知道了分布的mle参数,因此您可以使用&#34; rweibull&#34;产生随机偏差。并且您可以使用for循环生成这些变量500次
library(boot)
for( i in 1:500 )
{
currentIterVariates = rweibull(length(out), mleShape, mleScale)
}
如果要存储所有500个样本,请初始化矩阵,然后将值存储在那里。
2)非参数型引导程序(由于问题的标题,我写这篇文章)
library(boot)
#assume func is written, it will be similar to your statFunc
npBootstrap = boot(data, func, R=500)
samples = boot.array(npBootstrap, indices = T)
#the required resamples will be present in samples matrix (500 x length(data) matrix)
答案 1 :(得分:0)
以下是指示正态分布的参数引导程序的有用代码: 我对值进行了百分位数
B <- 1000
n <- 50
bs <- rep(NA, B)
for (b in 1:B) {
sample <- rnorm(n, mean=earlier.estimate.of.mean, sd= earlier.estimate.of.sd)
m <- fitdistr(sample, "normal")
bs[b] <- m$estimate[1]+m$estimate[2]*qnorm(0.99)
您也可以使用mvrnorm进行此操作,但随后必须使用协方差矩阵