setwd("~/Dropbox/R Stat")

Análise de variância fatorial

Prática com o senna v1 (two way ANOVA): Há diferenças em auto gestão entre alunos de séries diferentes ? Essa diferença é a mesma entre homes e mulheres? ANOVA 3 X 2 (Escolaridade X Sexo)

# Abrir banco de dados
load("senna.RData")

# Cria escolaridade e sexo como uma variável "factor"
sennav1$esc2 <- factor(sennav1$ESCOLARIDADE)
sennav1$sexo2 <- factor(sennav1$SEXO)

# ANOVA VD: auto gestão VI's: escolaridade e sexo
fit <- aov(F1.Cons ~ esc2*sexo2, data = sennav1)
summary(fit)
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## esc2         2 13.239   6.619  12.904 2.18e-05 ***
## sexo2        1  0.066   0.066   0.129   0.7207    
## esc2:sexo2   2  3.290   1.645   3.207   0.0475 *  
## Residuals   60 30.779   0.513                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Comparações post-hoc
TukeyHSD(fit)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = F1.Cons ~ esc2 * sexo2, data = sennav1)
## 
## $esc2
##           diff       lwr         upr     p adj
## 7-5 -0.5942266 -1.108546 -0.07990716 0.0197400
## 9-5 -1.1221600 -1.653347 -0.59097316 0.0000118
## 9-7 -0.5279334 -1.042253 -0.01361397 0.0429873
## 
## $sexo2
##           diff        lwr       upr     p adj
## 1-0 0.06220468 -0.2904923 0.4149017 0.7254824
## 
## $`esc2:sexo2`
##                diff         lwr          upr     p adj
## 7:0-5:0 -0.20508351 -1.09407169  0.683904656 0.9836108
## 9:0-5:0 -0.48148148 -1.47540047  0.512437509 0.7112231
## 5:1-5:0  0.66975309 -0.25997300  1.599479170 0.2908978
## 7:1-5:0 -0.22222222 -1.21614121  0.771696769 0.9857579
## 9:1-5:0 -0.93291576 -1.86264184 -0.003189675 0.0487391
## 9:0-7:0 -0.27639797 -1.16538614  0.612590205 0.9411012
## 5:1-7:0  0.87483660  0.05824882  1.691424379 0.0289064
## 7:1-7:0 -0.01713871 -0.90612688  0.871849464 0.9999999
## 9:1-7:0 -0.72783224 -1.54442002  0.088755533 0.1072588
## 5:1-9:0  1.15123457  0.22150848  2.080960651 0.0070750
## 7:1-9:0  0.25925926 -0.73465973  1.253178250 0.9718564
## 9:1-9:0 -0.45143428 -1.38116036  0.478291806 0.7092112
## 7:1-5:1 -0.89197531 -1.82170139  0.037750775 0.0672062
## 9:1-5:1 -1.60266885 -2.46342794 -0.741909750 0.0000128
## 9:1-7:1 -0.71069354 -1.64041962  0.219032547 0.2308620
# Alguns programas em: http://www.personality-project.org/r/r.anova.html
print(model.tables(fit,"means"),digits=3)       
## Tables of means
## Grand mean
##          
## 3.500941 
## 
##  esc2 
##         5     7     9
##      4.07  3.48  2.95
## rep 21.00 24.00 21.00
## 
##  sexo2 
##         0     1
##      3.47  3.53
## rep 33.00 33.00
## 
##  esc2:sexo2 
##      sexo2
## esc2  0     1    
##   5    3.69  4.36
##   rep  9.00 12.00
##   7    3.49  3.47
##   rep 15.00  9.00
##   9    3.21  2.76
##   rep  9.00 12.00
boxplot(F1.Cons ~ esc2*sexo2,data=sennav1 )

library(gplots)
## 
## Attaching package: 'gplots'
## The following object is masked from 'package:stats':
## 
##     lowess
plotmeans(F1.Cons ~ interaction(esc2, sexo2, sep=" "),
           connect=list(c(1,2,3),c(4,5,6)),
                      data = sennav1)

library(HH)
## Loading required package: lattice
## Loading required package: grid
## Loading required package: latticeExtra
## Loading required package: RColorBrewer
## Loading required package: multcomp
## Loading required package: mvtnorm
## Loading required package: survival
## Loading required package: TH.data
## Loading required package: MASS
## 
## Attaching package: 'TH.data'
## The following object is masked from 'package:MASS':
## 
##     geyser
## Loading required package: gridExtra
## 
## Attaching package: 'HH'
## The following object is masked from 'package:gplots':
## 
##     residplot

interaction2wt(F1.Cons ~ esc2*sexo2, data = sennav1)

Prática com o senna v1 (two way ANOVA): Há diferenças em auto gestão entre alunos de séries diferentes? Essa diferença é a mesma entre alunos com desemenho acima ou abaixo da mediana ? ANOVA 3 X 2 ( Escolaridade X Desempenho)

# Cria uma divisão pela mediana 
sennav1$m_notas2 <-cut(sennav1$m_notas, 
        breaks =c(0, median(sennav1$m_notas, na.rm =TRUE), 10))

sennav1$sexo2 <- factor(sennav1$SEXO)

# ANOVA VD: auto gestão VI's: escolaridade e sexo
fit <- aov(F1.Cons ~ esc2*m_notas2, data = sennav1)
summary(fit)
##               Df Sum Sq Mean Sq F value   Pr(>F)    
## esc2           2 13.228   6.614  13.446 1.54e-05 ***
## m_notas2       1  2.413   2.413   4.906   0.0306 *  
## esc2:m_notas2  2  2.599   1.300   2.642   0.0796 .  
## Residuals     59 29.020   0.492                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 1 observation deleted due to missingness
# Comparações post-hoc
TukeyHSD(fit)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = F1.Cons ~ esc2 * m_notas2, data = sennav1)
## 
## $esc2
##         diff       lwr         upr     p adj
## 7-5 -0.58061 -1.089540 -0.07168007 0.0216407
## 9-5 -1.12216 -1.642528 -0.60179196 0.0000082
## 9-7 -0.54155 -1.050480 -0.03262000 0.0345090
## 
## $m_notas2
##                         diff        lwr       upr     p adj
## (7.33,10]-(0,7.33] 0.3617318 0.01355727 0.7099064 0.0419823
## 
## $`esc2:m_notas2`
##                                diff         lwr        upr     p adj
## 7:(0,7.33]-5:(0,7.33]   -0.01388889 -1.04675554  1.0189778 1.0000000
## 9:(0,7.33]-5:(0,7.33]   -0.37843137 -1.37627536  0.6194126 0.8723959
## 5:(7.33,10]-5:(0,7.33]   1.01111111  0.01326712  2.0089551 0.0452725
## 7:(7.33,10]-5:(0,7.33]   0.31124975 -0.73714960  1.3596491 0.9511430
## 9:(7.33,10]-5:(0,7.33]  -0.45370370 -1.64635539  0.7389480 0.8709519
## 9:(0,7.33]-7:(0,7.33]   -0.36454248 -1.16459755  0.4355126 0.7604845
## 5:(7.33,10]-7:(0,7.33]   1.02500000  0.22494493  1.8250551 0.0048276
## 7:(7.33,10]-7:(0,7.33]   0.32513864 -0.53714710  1.1874244 0.8750733
## 9:(7.33,10]-7:(0,7.33]  -0.43981481 -1.47268147  0.5930518 0.8081073
## 5:(7.33,10]-9:(0,7.33]   1.38954248  0.63524333  2.1438416 0.0000164
## 7:(7.33,10]-9:(0,7.33]   0.68968112 -0.13032851  1.5096908 0.1476458
## 9:(7.33,10]-9:(0,7.33]  -0.07527233 -1.07311632  0.9225717 0.9999217
## 7:(7.33,10]-5:(7.33,10] -0.69986136 -1.51987099  0.1201483 0.1366823
## 9:(7.33,10]-5:(7.33,10] -1.46481481 -2.46265880 -0.4669708 0.0008166
## 9:(7.33,10]-7:(7.33,10] -0.76495346 -1.81335281  0.2834459 0.2771982
# Figuras
print(model.tables(fit,"means"),digits=3)       
## Tables of means
## Grand mean
##          
## 3.506083 
## 
##  esc2 
##         5     7     9
##      4.07  3.49  2.95
## rep 21.00 23.00 21.00
## 
##  m_notas2 
##     (0,7.33] (7.33,10]
##         3.33      3.69
## rep    33.00     32.00
## 
##  esc2:m_notas2 
##      m_notas2
## esc2  (0,7.33] (7.33,10]
##   5    3.35     4.36    
##   rep  6.00    15.00    
##   7    3.34     3.66    
##   rep 12.00    11.00    
##   9    2.97     2.90    
##   rep 15.00     6.00
boxplot(F1.Cons ~ esc2*m_notas2,data=sennav1)