只是另一个有点简单的变体,但我认为在没有明确使用库的情况下传递信息,boot
这可能会使一些人对它使用的语法感到困惑。
我们有一个线性模型:$y = X \beta + \epsilon$, $\quad \epsilon \sim N(0,\sigma^2)$y=Xβ+ϵ, ϵ∼N(0,σ2)
以下是该线性模型的参数引导程序,这意味着我们不会重新采样原始数据,但实际上我们会从拟合模型中生成新数据。此外,我们假设回归系数 $\beta$ 的自举分布是对称的,并且是平移不变的。(非常粗略地说,我们可以通过影响其属性来移动它的轴)背后的想法是 $\beta$ 的波动是由于 $\epsilon$ 造成的,因此如果有足够的样本,它们应该提供一个很好的近似值$\beta$的真实分布。和之前一样,我们再次测试 $H_0 : 0 = \beta_j$ 并且我们将 p 值定义为β is symmetric and that is translation invariant. (Very roughly speaking that we can move the axis of it with affecting its properties) The idea behind is that the fluctuations in the β 's are due to ϵ and therefore with enough samples they should provide a good approximation of the β 's. As before we test again H0:0=βj and we defined our p-values as “在给定数据概率分布的零假设的情况下,结果将与观察到的结果一样极端或更极端的概率”(在这种情况下观察到的结果是 $\beta$ 的我们得到了我们的原始模型)。所以这里是:β 's we got for our original model). So here goes:
# Sample Size
N <- 2^12;
# Linear Model to Boostrap
Model2Boot <- lm( mpg ~ wt + disp, mtcars)
# Values of the model coefficients
Betas <- coefficients(Model2Boot)
# Number of coefficents to test against
M <- length(Betas)
# Matrix of M columns to hold Bootstraping results
BtStrpRes <- matrix( rep(0,M*N), ncol=M)
for (i in 1:N) {
# Simulate data N times from the model we assume be true
# and save the resulting coefficient in the i-th row of BtStrpRes
BtStrpRes[i,] <-coefficients(lm(unlist(simulate(Model2Boot)) ~wt + disp, mtcars))
}
#Get the p-values for coefficient
P_val1 <-mean( abs(BtStrpRes[,1] - mean(BtStrpRes[,1]) )> abs( Betas[1]))
P_val2 <-mean( abs(BtStrpRes[,2] - mean(BtStrpRes[,2]) )> abs( Betas[2]))
P_val3 <-mean( abs(BtStrpRes[,3] - mean(BtStrpRes[,3]) )> abs( Betas[3]))
#and some parametric bootstrap confidence intervals (2.5%, 97.5%)
ConfInt1 <- quantile(BtStrpRes[,1], c(.025, 0.975))
ConfInt2 <- quantile(BtStrpRes[,2], c(.025, 0.975))
ConfInt3 <- quantile(BtStrpRes[,3], c(.025, 0.975))
如前所述,整个想法是你有 $\beta$ 的自举分布近似于它们的真实分布。(很明显,这段代码针对速度和可读性进行了优化。:))β 's approximates their true one.
(Clearly this code is optimized for speed but for readability. :) )