簡體   English   中英

R中分層樣本的單向ANOVA

[英]One-way ANOVA for stratified samples in R

我有一個分層的樣本,有三組(“a”,“b”,“c”),從更大的人口N中抽取。所有組有30個觀察,但它們在N中的比例不相等,因此它們的抽樣權重不同。

我使用R中的survey包來計算匯總統計和線性回歸模型,並想知道如何計算單向ANOVA校正調查設計(如有必要)。

我的假設是,如果我錯了,請糾正我,對於權重較小的人群,方差的標准誤差通常應該更高,因此不考慮調查設計的簡單方差分析不應該是可靠的。

這是一個例子。 任何幫助,將不勝感激。

## Oneway- ANOVA tests in R for surveys with stratified sampling-design
library("survey")
# create test data
test.df<-data.frame(
  id=1:90,
  variable=c(rnorm(n = 30,mean=150,sd=10),
             rnorm(n = 30,mean=150,sd=10),
             rnorm(n = 30,mean=140,sd=10)),
  groups=c(rep("a",30),
  rep("b",30),
  rep("c",30)),
  weights=c(rep(1,30), # undersampled
  rep(1,30),
  rep(100,30))) # oversampled


# correct for survey design
test.df.survey<-svydesign(id=~id,
                           strata=~groups,
                           weights=~weights,
                           data=test.df)

## descriptive statistics
# boxplot
svyboxplot(~variable~groups,test.df.survey)
# means
svyby(~variable,~groups,test.df.survey,svymean)
# variances
svyby(~variable,~groups,test.df.survey,svyvar)


### ANOVA ###
## One-way ANOVA without correcting for survey design
summary(aov(formula = variable~groups,data = test.df))

嗯,這是一個有趣的問題,據我所知,在單向anova中考慮權重是很困難的。 因此,我決定告訴你我解決這個問題的方式。

我將使用雙向anova然后進行特殊測試。

首先,讓我們根據您的數據構建一個線性模型,並檢查它的外觀。

library(car)
library(agricolae)
model.lm = lm(variable ~ groups * weights, data = test.df)
shapiro.test(resid(model.lm))

Shapiro-Wilk normality test

data:  resid(model.lm)
W = 0.98238, p-value = 0.263

leveneTest(variable ~ groups * factor(weights), data = test.df)
Levene's Test for Homogeneity of Variance (center = median)
Df F value  Pr(>F)  
group  2  2.6422 0.07692 .
      87                  
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

分布接近正常,各組之間的方差不同,因此方差不是同質的 - 應該用於參數測試 - anova。 不管怎樣,讓我們​​進行測試。

幾個圖表檢查我們的數據是否適合此測試:

hist(resid(model.lm))
plot(model.lm)

很正常 在此輸入圖像描述 在此輸入圖像描述 在此輸入圖像描述 在此輸入圖像描述

是對情節的解釋,它們實際上並不壞看。

讓我們運行雙向anova:

anova(model.lm)
Analysis of Variance Table

Response: variable
          Df Sum Sq Mean Sq F value    Pr(>F)    
groups     2 2267.8 1133.88  9.9566 0.0001277 ***
Residuals 87 9907.8  113.88                      
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

如您所見,結果非常接近您的結果。 一些事后測試:

(result.hsd = HSD.test(model.lm, list('groups', 'weights')))
$statistics
   MSerror Df     Mean     CV      MSD
  113.8831 87 147.8164 7.2195 6.570186

$parameters
   test         name.t ntr StudentizedRange alpha
  Tukey groups:weights   3         3.372163  0.05

$means
      variable       std  r      Min      Max      Q25      Q50      Q75
a:1   150.8601 11.571185 30 113.3240 173.0429 145.2710 151.9689 157.8051
b:1   151.8486  8.330029 30 137.1907 176.9833 147.8404 150.3161 154.7321
c:100 140.7404 11.762979 30 118.0823 163.9753 131.6112 141.1810 147.8231

$comparison
NULL

$groups
      variable groups
b:1   151.8486      a
a:1   150.8601      a
c:100 140.7404      b

attr(,"class")
[1] "group"

也許有一些不同的方式:

aov_cont<- aov(test.df$variable ~ test.df$groups * test.df$weights)
summary(aov_cont)
               Df Sum Sq Mean Sq F value   Pr(>F)    
test.df$groups  2   2268  1133.9   9.957 0.000128 ***
Residuals      87   9908   113.9                     
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(TukeyHSD(aov_cont))
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = test.df$variable ~ test.df$groups * test.df$weights)

$`test.df$groups`
           diff        lwr       upr     p adj
b-a   0.9884608  -5.581725  7.558647 0.9315792
c-a -10.1197048 -16.689891 -3.549519 0.0011934
c-b -11.1081657 -17.678352 -4.537980 0.0003461

總結一下,結果非常接近你的。 Personaly我會用(*)符號或(+)運行雙向anova,當你確定你的變量是獨立的 - 加法模型。

具有較大重量的組c基本上不同於組ab

根據我們研究所的主要統計學家的說法,在任何常見的建模環境中都不容易實現這種分析。 其原因在於ANOVAANCOVA是線性模型,在70年代出現通用線性模型 (后來的廣義線性模型 - GLM )后未進一步發展。

正態線性回歸模型產生與ANOVA幾乎相同的結果,但在變量選擇方面更靈活。 由於GLM存在加權方法(參見R中的survey包),因此沒有必要開發方法來對ANOVA中的分層抽樣設計進行加權......只需使用GLM代替。

summary(svyglm(variable~groups,test.df.survey))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM