醫學統計與R語言:雙因素重複測量方差分析(Two-way repeated measures ANOVA)

2021-01-14 醫學統計與R語言

微信公眾號:醫學統計與R語言
如果你覺得對你有幫助,歡迎轉發

輸入1:

setwd("C:\\Users\\mooshaa\\Desktop")
rma <- read.csv("rma.csv",header=T)
rma$group <- factor(rma$group,levels = c(1,2),labels = c("treatment","control"))
rma[sample(nrow(rma),5,replace=F),]

結果1:

   id     group before middle after
56 56   control     58     63    68
19 19 treatment     58     60    70
47 47   control     56     50    69
28 28 treatment     51     52    70
59 59   control     57     64    71

輸入2:

library(tidyr)
longrma <-  gather(rma,key=time,value=score,-id,-group)
longrma$time <- factor(longrma$time,levels = c("before","middle","after"))
longrma$group <- factor(longrma$group,levels = c("control","treatment"))
longrma[sample(nrow(longrma),5,replace=F),]

結果2:

    id     group   time score
179 59   control  after    71
163 43   control  after    51
14  14 treatment before    44
101 41   control middle    41
153 33   control  after    60

輸入3:

funx <- function(x){
  n <- length(x)
  mean <- mean(x)
  sd <- sd(x)
  se <- sd/sqrt(n)
  l1 <- mean-1.96*se
  u1 <- mean+1.96*se
  result <-c(n=n,mean=mean,sd=sd,se=se,l1=l1,u1=u1)
  return(result)
}
aggregate(longrma$score,list(longrma$time,longrma$group),FUN=funx )

結果3:

  Group.1   Group.2   x.n x.mean  x.sd  x.se  x.l1  x.u1
1  before   control 30.00  54.50  8.36  1.53 51.51 57.49
2  middle   control 30.00  60.27 10.54  1.92 56.50 64.04
3   after   control 30.00  64.37  8.60  1.57 61.29 67.44
4  before treatment 30.00  54.83  8.88  1.62 51.66 58.01
5  middle treatment 30.00  61.23  8.06  1.47 58.35 64.12
6   after treatment 30.00  74.90  7.91  1.44 72.07 77.73

輸入4:

library(nlme)
result.lme <- lme(score~time*group,random=~1|id,data=longrma)
anova(result.lme )

結果4:

             numDF denDF F-value p-value
(Intercept)     1   116    4516  <.0001
time            2   116      86  <.0001
group           1    58       5  0.0359
time:group      2   116      12  <.0001

shapiro.test(result.lme residuals);qqnorm(result.lmeresiduals)
qqline(result.lme $residuals)
plot(fitted(result.lme),residuals(result.lme),xlab = "Predicted Values",ylab = "Residuals")

輸入5:

library(ggplot2)
library(afex)    
afexout1 <- aov_ez("id", "score",  between=c("group"),within = c("time"),type=3,
                   data = longrma)    ##  return="Anova" 輸出多變量分類結果
summary(afexout1)

其它的表達方式:
 aov_4(score ~ group + (time|id),data = longrma)
 aov_car(score ~ group + Error(id|time),data = longrma)

結果5:

Univariate Type III Repeated-Measures ANOVA Assuming Sphericity

            Sum Sq num Df Error SS den Df F value  Pr(>F)    
(Intercept) 684870      1     8797     58 4515.55 < 2e-16 ***
group          700      1     8797     58    4.62   0.036 *  
time          6798      2     4576    116   86.17 < 2e-16 ***
group:time     980      2     4576    116   12.42 1.3e-05 ***
---
Signif. codes:  0 『***』 0.001 『**』 0.01 『*』 0.05 『.』 0.1 『 』 1


Mauchly Tests for Sphericity

           Test statistic p-value
time                0.949   0.225
group:time          0.949   0.225


Greenhouse-Geisser and Huynh-Feldt Corrections
 for Departure from Sphericity

           GG eps Pr(>F[GG])    
time        0.952    < 2e-16 ***
group:time  0.952    1.9e-05 ***
---
Signif. codes:  0 『***』 0.001 『**』 0.01 『*』 0.05 『.』 0.1 『 』 1

           HF eps Pr(>F[HF])
time        0.983   2.66e-23
group:time  0.983   1.49e-05

輸入6:

p1 <- afex_plot( afexout1 , x = "time", trace = "group",error = "within",
                   mapping = c("linetype","shape", "fill"),
                   data_geom = ggplot2::geom_boxplot, 
                   data_arg = list(width = 0.4))+theme_bw() + theme(legend.position="bottom")
p2 <- afex_plot( afexout1 , x = "group", trace = "time",error = "within",
                   mapping = c("linetype","shape", "color"),
                   point_arg = list(size = 2), line_arg = list(size = 1),
                   error_arg = list(size = 1, width = 0.1, linetype = 1),
                   factor_levels = list(time = c("治療前", "治療中", "治療後"),
                   group = c("對照組", "幹預組")), legend_title = "時間" )+ 
                   labs(y = "得分", x = "分組")+theme_classic() + theme(legend.position="bottom")

library("cowplot")
plot_grid(p1,p2)  
ggsave("p1.png", device = "png", width = 25, height = 12, units = "cm", 
         dpi = 600) 

結果6:


輸入7:

nice( afexout1, correction = "GG")

結果7:

Anova Table (Type 3 tests)

Response: score
      Effect           df    MSE         F ges p.value
1      group        1, 58 151.67    4.62 * .05     .04
2       time 1.90, 110.38  41.46 86.17 *** .34  <.0001
3 group:time 1.90, 110.38  41.46 12.42 *** .07  <.0001
---
Signif. codes:  0 『***』 0.001 『**』 0.01 『*』 0.05 『+』 0.1 『 』 1

Sphericity correction method: GG 

輸入8:

library(lsmeans) ###compute least-squares means(predicted marginal means)
ref.grid(afexout1)
l1 <- lsmeans(afexout1,"group")
l2 <- lsmeans(afexout1,"time")
l3 <- lsmeans(afexout1,~time|group)
contrast(l1,method="pairwise")

結果8:

  contrast          estimate   SE  df t.ratio p.value
 control - treatment    -3.94 1.84 58 -2.149  0.0359 

Results are averaged over the levels of: time 

輸9:

 contrast(l2,method="pairwise",adjust = "bonferroni")

結果9:

 contrast        estimate   SE  df t.ratio p.value
 before - middle    -6.08 1.15 116  -5.310 <.0001 
 before - after    -14.97 1.15 116 -13.050 <.0001 
 middle - after     -8.88 1.15 116  -7.750 <.0001 

Results are averaged over the levels of: group 
P value adjustment: bonferroni method for 3 tests 

輸入10:

 contrast(l3,method="pairwise",adjust = "bonferroni")

結果10:

group = control:
 contrast        estimate   SE  df t.ratio p.value
 before - middle    -5.77 1.62 116  -3.560 0.0016 
 before - after     -9.87 1.62 116  -6.080 <.0001 
 middle - after     -4.10 1.62 116  -2.530 0.0384 

group = treatment:
 contrast        estimate   SE  df t.ratio p.value
 before - middle    -6.40 1.62 116  -3.950 0.0004 
 before - after    -20.07 1.62 116 -12.370 <.0001 
 middle - after    -13.67 1.62 116  -8.430 <.0001 

P value adjustment: bonferroni method for 3 tests 

輸入11:

   contrast(l2,method="poly")

結果11:

 contrast  estimate   SE  df t.ratio p.value
 linear        15.0 1.15 116 13.050  <.0001 
 quadratic      2.8 1.99 116  1.410  0.1613 

Results are averaged over the levels of: group 

相關焦點

  • 最直觀的方差分析(ANOVA) 術語大全
    單因素方差分析: One-way ANOVA:3. 1 針對療法的one-way ANOVA(組間單因素方差分析) 如果將這十個人分成兩組,每組五個人分別使CBT和EMDR療法,那麼經過5周後,進行評估患者的焦慮程度在兩種療法的小組間是否有統計意義上的差異,即為針對兩個療法效果的one-way ANOVA。
  • R語言:單因素重複測量方差分析(One-Way Repeated Measures ANOVA )
    anova(am2)
  • 【實例講解】雙因素方差分析(Two-way Anova)
    (One-way Anova),但是在實際應用中我們可能經常會遇到兩個因素一起研究的情況,這就要用到雙因素方差分析(Two-way Anova)。【實例講解】單因素方差分析(One-way Anova)
  • 手把手教你用 Origin 做單次測量數據方差分析
    方差分析(ANOVA)又稱「變異數分析」或「F 檢驗」,用於兩個及兩個以上樣本均數差別的顯著性檢驗。 方差分析是數理統計中非常常見的一種分析方法,它可以研究目標變量的控制因素水平及其影響的顯著性。
  • 醫學統計與R語言:GiViTI Calibration Belt
    醫學統計與R語言:這個Calibration plot有點色!醫學統計與R語言:樣本方差的抽樣分布醫學統計與R語言:標準Z值一定服從標準正態分布?>醫學統計與R語言:Probit回歸模型及邊際效應(Marginal effects)醫學統計與R語言:Lord’s Paradox醫學統計與R語言:協方差分析(ANCOVA)+plus醫學統計與R語言:Kendall是誰?
  • Two-way ANOVA分析
    之前講的t檢驗,單因素方差分析(one-way ANOVA)等,都是單因素水平的比較,t檢驗是比較一個因素兩個水平上的差異:比如不同性別(男、女)之間體重的差異,單因素方差分析則比較的是一個因素多個水平上的差異(>2組):比如不同國家的人口差異,不同民族的年齡差異等。
  • r語言的p值檢驗 - CSDN
    (Marginal effects)醫學統計與R語言:Lord’s Paradox醫學統計與R語言:協方差分析(ANCOVA)+plus醫學統計與R語言:Kendall是誰?醫學統計與R語言:方差分析中計劃好的多重比較(Planned Comparisons and Post Hoc Tests)醫學統計與R語言:圓形樹狀圖(circular dendrogram)醫學統計與R語言:畫一個姑娘陪著我,再畫個花邊的被窩醫學統計與R語言:雙因素重複測量方差分析(Two-way repeated measures ANOVA
  • SPSS超詳細教程:雙因素方差分析(Two-way ANOVA)
    3.2 假設4-6  檢驗假設4-6需要用到殘差,因此我們先運行雙因素方差分析的SPSS操作,得到主要結果和相應殘差變量後,再逐一進行對假設的檢驗。  我們可以採取以下4種辦法:  (1) 轉換數據;  (2) 因為方差分析對假設5並不是非常敏感,即使殘差不接近正態分布,我們也可以嘗試採用雙因素方差模型;  (3) 檢驗模型結果。
  • 【科研加油站】SPSS操作之雙因素方差分析(Two-way ANOVA)
    上一期我們討論了單因素方差分析,本期「科研加油站」欄目,我們一起來探討雙因素方差分析(Two-way ANOVA)。>與其他方差分析一樣,雙因素方差分析對異常值非常敏感。我們可以採取以下4種辦法:(1) 轉換數據;(2) 因為方差分析對假設5並不是非常敏感,即使殘差不接近正態分布,我們也可以嘗試採用雙因素方差模型;(3) 檢驗模型結果。因
  • 醫學統計與R語言:Kendall是誰?樣本量是自變量的10倍?
    R語言:多列分組正態性檢驗醫學統計與R語言:方差分析中計劃好的多重比較(Planned Comparisons and Post Hoc Tests)醫學統計與R語言:圓形樹狀圖(circular dendrogram)醫學統計與R語言:畫一個姑娘陪著我,再畫個花邊的被窩醫學統計與R語言:雙因素重複測量方差分析(Two-way
  • 重複測量數據的方差分析在SPSS中的應用——【杏花開醫學統計】
    關 注 重複測量數據的方差分析 在SPSS中的應用 關鍵詞:spss、重複測量方差 導 讀 在醫學研究中,很多實驗都涉及到重複測量的數據資料
  • SPSS醫學統計高能方法:單因素方差分析(One Way ANOVA)——【杏花開醫學統計】
    ——單因素方差分析(One Way ANOVA)①單因素方差分析的分組要求:三組或者三組以上的差異對比;②單因素方差分析的指標類型要求:連續數值型變量且服從正態分布(每一組數據都要服從正態分布) 請觀看下方視頻教程
  • r語言卡方檢驗和似然比檢驗_r語言似然比檢驗代碼 - CSDN
    例如,對於雙因素方差分析,若不同處理方式中的觀測數不同,那麼模型y ~ A * B與模型y ~ B * A的結果不同R默認類型1(序貫型)方法計算ANOVA效應。:受試者被測量不止一次,重點關注含一個組內和一個組間因子的重複測量方差分析。
  • R語言統計篇: 單因素協方差分析
    >協方差分析(One-way ANCOVA)可以研究一個分類變量對一個連續變量的影響,同時校正其他變量的作用,這些變量也稱為協變量(Covariate)。也是單因素方差分析(One-way ANOVA,R語言統計篇:單因素方差分析)的一個延伸。比方說,我們現在想要研究不同BMI(偏輕,正常與超重)與空腹血糖的關係,同時校正血壓水平。在此研究中,BMI分組是一個分類變量(自變量),血糖是一個連續變量(因變量),血壓則是一個協變量(covariate)。c.
  • R語言統計原創視頻7 | ANOVA方差分析
    In its simplest form, ANOVA provides a statistical test of whether the population means of several groups are equal, and therefore generalizes the t-test to more than two groups
  • 重複測量方差分析的操作教程及結果解讀
    重複測量數據是指對同一個體在不同時間點的測量,這種數據在醫學研究中較為常見,比較典型的數據形式如: 對一組人群分別在幹預前後不同的時間點觀察其結局情況。這種研究通常是為了比較不同時間點的差異情況,或者分析時間變化趨勢。
  • 單因素方差分析(one-way ANOVA)
    來源:網絡單因素方差分析 (一)單因素方差分析概念
  • 方差分析 (ANOVA)-29
    課程目標▶概念性認識「方差分析」和「ANOVA輸出表」▶能夠設計並實施一個「單因素」或「雙因素」實驗▶認識並解釋 交互作用因素A的不同水平是否存在差異?  為什麼?單個因素的 ANOVA▶單向方差分析(ANOVA)是比較兩組以上數據均值的差異的統計方法▶假設性檢驗為:
  • 兩因素方差分析怎麼理解?
    文章來源: 丁點幫你作者:丁點helper看完單因素方差分析,一般的統計學中並不會直接講two-way(雙因素)方差分析,而是講「隨機區組設計的方差分析」,那這兩者有什麼關係嗎?從統計方法的角度來看,隨機區組設計的方差分析其實就屬於兩因素(或多因素)方差分析,一種說法認為,為什麼不直接叫兩因素,是因為不把「區組因素」算作一類真正的「因素」,而重點研究隨機分組因素。我們認為,實際稱雙因素方差分析可能更好理解。不過這裡稱作「隨機區組設計」,也是有其他特別的考慮。
  • 教學視頻| 單因素方差分析(one-way ANOVA)及SPSS操作
    單因素方差分析(one-way ANOVA)也稱為F檢驗,是通過對數據變異的分析來推斷兩個或多個樣本均數所代表的總體均數是否有差別的一種統計推斷方法