ANOVA 2 facteurs de mesures répétées avec R
Voici un exemple de réalisation d’ANOVA à mesures répétées avec R. Les résultats obtenus ont été vérifiés avec le logiciel JASP, qui utilise également R pour faire ses calculs.
Example of processing of an ANOVA with repeated measurements with R. The results obtained were verified with the JASP software, which also uses R to make its calculations.
NB : Cliquer sur les boutons noirs pour voir le code ! Click on black boxes to see the code
Lecture des données / Read data
Il s’agit ici d’un fichier comprenant des mesures de temps de réaction soit visuels (TRV) soit auditifs (TRA) chez 20 personnes de tout âge. Dans le fichier, on a relevé l’âge du participant, son genre, le fait qu’il soit sportif ou non. Chaque personne a testé son temps de réaction visuel et son temps de réaction auditif. Cette variable (temps de réaction) est donc une mesure répétée. On a également deux variables potentielles de groupement : le genre, et la pratique d’un sport.
This is a file comprising reaction time measurements, either visual (TRV) or auditory (TRA) in 20 people of all ages. In the file, we noted the age of the participant, his gender, whether he was athletic or not. Each person tested their visual reaction time and their auditory reaction time. This variable (reaction time) is therefore a repeated measurement. We also have two potential grouping variables: gender, and the practice of a sport.
D <- read.table("temps_de_reaction.csv",sep=";",dec=",",header=TRUE) ;
summary(D)
S GENRE AGE SPORT TRV TRA
Min. : 1.00 F: 8 Min. : 9.00 Non: 9 Min. :240.4 Min. :261.3
1st Qu.: 5.75 M:12 1st Qu.:18.75 Oui:11 1st Qu.:289.2 1st Qu.:300.4
Median :10.50 Median :34.00 Median :307.1 Median :327.2
Mean :10.50 Mean :38.80 Mean :322.2 Mean :341.5
3rd Qu.:15.25 3rd Qu.:55.00 3rd Qu.:348.1 3rd Qu.:376.1
Max. :20.00 Max. :81.00 Max. :454.8 Max. :497.3
head(D) ;
S
<int>
|
GENRE
<fctr>
|
AGE
<int>
|
SPORT
<fctr>
|
TRV
<dbl>
|
TRA
<dbl>
|
|
---|---|---|---|---|---|---|
1 | 1 | F | 9 | Oui | 306.79 | 297.31 |
2 | 2 | M | 10 | Oui | 265.76 | 270.84 |
3 | 3 | M | 10 | Oui | 301.58 | 324.12 |
4 | 4 | M | 11 | Non | 344.04 | 316.64 |
5 | 5 | M | 12 | Non | 284.04 | 313.97 |
6 | 6 | F | 21 | Oui | 290.92 | 322.64 |
Remise en forme des données / Reshape data
-> Les données doivent être transformées pour que cela corresponde au format attendu par la fonction aov(). En effet, les conditions (TRA,TRV) doivent être codées dans une colonne spécifique unique. On renomme ensuite la colonne de la variable dépendante qui est par défaut appelée “variable” par R.
-> The data must be transformed so that it corresponds to the format expected by the aov () function. In fact, the conditions (TRA, TRV) must be coded in a specific single column. We then rename the column of the dependent variable which is by default called “variable” by R.
library(reshape) ;
DM <- melt(D,id = c("S","GENRE","AGE","SPORT")) ;
colnames(DM) <- c("S","GENRE","AGE","SPORT","STIM","TR") ;
head(DM)
S
<int>
|
GENRE
<fctr>
|
AGE
<int>
|
SPORT
<fctr>
|
STIM
<fctr>
|
TR
<dbl>
|
|
---|---|---|---|---|---|---|
1 | 1 | F | 9 | Oui | TRV | 306.79 |
2 | 2 | M | 10 | Oui | TRV | 265.76 |
3 | 3 | M | 10 | Oui | TRV | 301.58 |
4 | 4 | M | 11 | Non | TRV | 344.04 |
5 | 5 | M | 12 | Non | TRV | 284.04 |
6 | 6 | F | 21 | Oui | TRV | 290.92 |
Seulement le facteur “STIM” comme facteur de mesures répétées / Only STIM as repeated measures factor
Important : On reconstruit une table spécifique pour l’analyse. En effet, les colonnes STIM (visuel ou auditif) et SUJET (le numéro du participant) doivent être de type “facteur” et non de type “entier” ou “string” car sinon cela conduit à des erreurs (nombre de degrés de liberté incorrect). Si l’on ne fait pas cela, alors les résultats de l’analyse ne seront pas corrects.
Important: We reconstruct a specific table for analysis. Indeed, the columns STEM (visual or auditory) and SUBJECT (the number of the participant) must be of type “factor” and not of type “whole” or “string” because if not that that leads to errors (number of degrees of freedom incorrect). If this is not done, then the results of the analysis will not be correct.
DA1 <- data.frame(sujet = factor(DM$S),stim = factor(DM$STIM),vd = DM$TR) ;
head(DA1)
sujet
<fctr>
|
stim
<fctr>
|
vd
<dbl>
|
|
---|---|---|---|
1 | 1 | TRV | 306.79 |
2 | 2 | TRV | 265.76 |
3 | 3 | TRV | 301.58 |
4 | 4 | TRV | 344.04 |
5 | 5 | TRV | 284.04 |
6 | 6 | TRV | 290.92 |
A1 <- with(DA1,aov(vd ~ stim + Error(sujet/stim))) ;
summary(A1)
Error: sujet
Df Sum Sq Mean Sq F value Pr(>F)
Residuals 19 115123 6059
Error: sujet:stim
Df Sum Sq Mean Sq F value Pr(>F)
stim 1 3745 3745 8.198 0.00995 **
Residuals 19 8680 457
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Le facteur STIM a une influence significative. Cela veut dire que le temps de réaction moyen est influencé par le type de stimulation (visuelle ou auditive).
STIM et GENRE / STIM and GENRE
DA2 <- data.frame(sujet = factor(DM$S),genre = factor(DM$GENRE),stim = factor(DM$STIM),vd = DM$TR) ;
head(DA2)
sujet
<fctr>
|
genre
<fctr>
|
stim
<fctr>
|
vd
<dbl>
|
|
---|---|---|---|---|
1 | 1 | F | TRV | 306.79 |
2 | 2 | M | TRV | 265.76 |
3 | 3 | M | TRV | 301.58 |
4 | 4 | M | TRV | 344.04 |
5 | 5 | M | TRV | 284.04 |
6 | 6 | F | TRV | 290.92 |
A2 <- with(DA2,aov(vd ~ genre * stim + Error(sujet/stim))) ;
summary(A2)
Error: sujet
Df Sum Sq Mean Sq F value Pr(>F)
genre 1 3502 3502 0.565 0.462
Residuals 18 111621 6201
Error: sujet:stim
Df Sum Sq Mean Sq F value Pr(>F)
stim 1 3745 3745 8.061 0.0109 *
genre:stim 1 318 318 0.684 0.4191
Residuals 18 8362 465
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Le facteur STIM reste significatif, mais l’on voit que l’effet fixe (effet between subject) n’est pas significatif. Le genre du participant n’a pas d’influence significative sur le temps de réaction moyen.
the STIM factor remains significant, but we see that the fixed effect (between subject factor) is not significant. The gender of the participant has no significant influence on the average reaction time.