# Data from binomial(15, θ) for an unknown θ x = c(3, 5, 7, 9, 11, 13) binomSize = 15 # known size of binomial n = length(x) # sample size thetahat = mean(x)/binomSize # MLE for θ nboot = 5000 # number of bootstrap samples to use # nboot parametric samples of size n; organize in a matrix tmpdata = rbinom(n*nboot, binomSize, thetahat) bootstrapsample = matrix(tmpdata, nrow=n, ncol=nboot) # Compute bootstrap means thetahat* and differences delta* thetahatstar = colMeans(bootstrapsample)/binomSize deltastar = thetahatstar - thetahat # Find quantiles and make the bootstrap confidence interval d = quantile(deltastar, c(.1,.9)) ci = thetahat - c(d[2], d[1])
时间: 2024-04-09 08:33:48 浏览: 85
Binomial的快速高质量全功能SIMD BC7编码器。-C/C++开发
The code you provided demonstrates how to compute a bootstrap confidence interval for an unknown parameter θ in a binomial distribution.
Here's a breakdown of the code:
```R
# Data from binomial(15, θ) for an unknown θ
x = c(3, 5, 7, 9, 11, 13)
binomSize = 15 # known size of binomial
n = length(x) # sample size
# Maximum Likelihood Estimate (MLE) for θ
thetahat = mean(x)/binomSize
nboot = 5000 # number of bootstrap samples to use
# Generate nboot parametric samples of size n
tmpdata = rbinom(n*nboot, binomSize, thetahat)
bootstrapsample = matrix(tmpdata, nrow=n, ncol=nboot)
# Compute bootstrap means (thetahat*) and differences (delta*)
thetahatstar = colMeans(bootstrapsample)/binomSize
deltastar = thetahatstar - thetahat
# Find quantiles and make the bootstrap confidence interval
d = quantile(deltastar, c(.1,.9))
ci = thetahat - c(d[2], d[1])
```
In this code, `x` represents the observed data from a binomial distribution with an unknown parameter θ. The `binomSize` variable represents the known size of the binomial distribution. The MLE for θ is computed using the `mean` function.
The code then generates `nboot` parametric bootstrap samples of size `n` using the `rbinom` function. The bootstrap samples are organized in a matrix called `bootstrapsample`.
Next, the code computes the bootstrap means (thetahat*) by taking the column means of `bootstrapsample` and dividing them by `binomSize`. It also calculates the differences (delta*) between the bootstrap means and the MLE.
Finally, the code finds the 10th and 90th percentiles of `deltastar` using the `quantile` function and constructs the bootstrap confidence interval by subtracting these quantiles from the MLE.
Please let me know if you have any further questions!
阅读全文