R:自举多重回归

时间:2019-03-02 18:59:57

标签: r model linear-regression bootstrapping

我正在尝试进行多元回归分析,以发现水质对特定位置(又名古斯勒)的浮游生物丰富度的影响。我能够运行我的模型和摘要,但是数据是非参数的,因此典型的摘要将不可靠。这主要是由于在几周的时间内完成了较小的样本量,并且每周进行一次采样。

当时,我以为它的非参数版本可能是引导程序。我之前在其他数据上运行过引导程序,但从未使用过多元回归模型。我似乎找不到有关如何执行此操作的代码,因此从我过去执行引导程序开始。我很好奇我需要编辑什么才能使该引导程序运行。

这是dput(head(Guzzler1)的输出:

    structure(list(Abundance = c(98L, 43L, 65L, 55L, 54L), Phospates = c(2L, 
2L, 2L, 2L, 2L), Nitrates = c(0, 0.3, 0, 0.15, 0), pH = c(7.5, 
8, 7.5, 7, 7)), .Names = c("Abundance", "Phospates", "Nitrates", 
"pH"), row.names = c(NA, 5L), class = "data.frame")

这是我的模型和摘要:

    Guzzler1model<-lm(Abundance ~ Phospates + Nitrates + pH, data=Guzzler1)
> summary(Guzzler1model)

Call:
lm(formula = Abundance ~ Phospates + Nitrates + pH, data = Guzzler1)

Residuals:
     1      2      3      4      5 
 20.75  -4.25 -12.25   8.50 -12.75 

Coefficients: (1 not defined because of singularities)
            Estimate Std. Error t value Pr(>|t|)
(Intercept)   -80.25     209.62  -0.383    0.739
Phospates         NA         NA      NA       NA
Nitrates     -135.00      90.02  -1.500    0.272
pH             21.00      28.87   0.727    0.543

Residual standard error: 20.41 on 2 degrees of freedom
Multiple R-squared:  0.5302,    Adjusted R-squared:  0.06032 
F-statistic: 1.128 on 2 and 2 DF,  p-value: 0.4698

***请注意:我相信磷酸盐具有NA,因为在此特定位置每个值都等于2。

这是我最初执行引导程序并不确定要更改的内容:

n=length(Guzzler1Abundance)
B = 1000
results = numeric(B)
for(b in 1:B){
  i = sample(x = 1:n, size=n, replace=TRUE)
  bootSample = Guzzler1Abundance[i]
  thetahat= mean(bootSample)
  results[b] = thetahat
}

非常感谢您!

1 个答案:

答案 0 :(得分:0)

我不太了解非参数数据的含义,但我知道,您想从数据中提取引导样本并对其进行线性回归。

一种可能的方法是

Guzzler1 <-  structure(list(Abundance = c(98L, 43L, 65L, 55L, 54L), Phospates = c(2L, 
                      2L, 2L, 2L, 2L), Nitrates = c(0, 0.3, 0, 0.15, 0), pH = c(7.5, 
                      8, 7.5, 7, 7)), .Names = c("Abundance", "Phospates", "Nitrates", 
                      "pH"), row.names = c(NA, 5L), class = "data.frame")
lines <- nrow(Guzzler1)
replicate(5, lm(Abundance ~ Phospates + Nitrates + pH, 
                         data=Guzzler1[sample(lines, replace = TRUE),])$coefficients)

这将报告像这样的5个线性回归的系数

> replicate(5, lm(Abundance ~ Phospates + Nitrates + pH, 
+                          data=Guzzler1[sample(lines, replace = TRUE),])$coefficients)
                 [,1]       [,2]      [,3]       [,4] [,5]
(Intercept)  65.00000 145.000000 -408.0000 145.000000 -100
Phospates          NA         NA        NA         NA   NA
Nitrates    -73.33333   6.666667 -256.6667   6.666667 -110
pH                 NA -13.000000   66.0000 -13.000000   22

可以通过更改我的replicate调用的第一个参数来任意选择5个重复的数目。正如{IRTFM所预测和解释的那样,许多NA值是由于数据稀少所致。随着将要采样更多数据,这种情况将有所改善。

让我们采样5000个自举样本并研究硝酸盐系数的分布:

reps <- replicate(5000, lm(Abundance ~ Phospates + Nitrates + pH, 
                           data=Guzzler1[sample(lines, replace = TRUE),])$coefficients)
plot(table(reps["Nitrates",]))
plot(ecdf(reps["Nitrates",]))
quantile(reps["Nitrates",], c(.025, .25, .5, .75, .975), na.rm = TRUE)

可以对此进行编辑,其中有一个变量数据:

boxplot(reps["(Intercept)",], reps["Nitrates",], reps["pH",]
        , names = c("Intercept", "Nitrates", "pH"), ylab="bootstrapped coefficients")
abline(h=0, col="firebrick", lty=3)

enter image description here