【发布时间】:2011-09-27 12:49:26
【问题描述】:
想象一下:我已经对 10,000 个人进行了抽样,并以厘米为单位测量了他们的身高,并绘制了如下分布:
# Generate sample data
sampleSize = 10000
sampleData = round(rnorm(n=sampleSize, mean=175, sd=14))
# Draw histogram of sample
h = hist(sampleData, breaks=max(sampleData)-min(sampleData))
######################################################################
# Calculate the mean of the measurement
meanMeasure = mean(sampleData)
meanMeasure
abline(v=meanMeasure, col="red")
# Calculate the standard deviation of the measurement
sdMeasure = sd(sampleData)
sdMeasure
rect(
xleft=meanMeasure-sdMeasure,
ybottom=min(h$counts),
xright=meanMeasure+sdMeasure,
ytop=max(h$counts),
col="#0000ff22"
)
现在我想估计每个测量的身高的标准偏差有多大。我认为引导我的原始数据集是一个好方法,即从我的原始数据集中采样身体大小并进行替换。
这是一个好方法吗? 如何在 R 中执行此分析(例如,在 1000 个循环的自举分析中每个高度的标准偏差)?
【问题讨论】:
-
也许它可能是这样的:
cycles = 1000result = matrix(0, nrow = length(h$counts), ncol = cycles)for (i in 1:cycles) { tmp = hist(sample(sampleData, replace = T, size = sampleSize), breaks = h$breaks, plot=F) result[,i] = tmp$counts }for (i in 1:length(h$counts)) { h$sdboot[i] = sd(result[i,]) }plot(h)polygon(x=c(rev(h$mids), h$mids), y= c(rev(h$counts+h$sdboot), h$counts-h$sdboot), col="#ff000022")
标签: r statistics statistics-bootstrap