knitr::opts_chunk$set(echo = TRUE)
data(mtcars)
summary(mtcars)
##       mpg             cyl             disp             hp       
##  Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0  
##  1st Qu.:15.43   1st Qu.:4.000   1st Qu.:120.8   1st Qu.: 96.5  
##  Median :19.20   Median :6.000   Median :196.3   Median :123.0  
##  Mean   :20.09   Mean   :6.188   Mean   :230.7   Mean   :146.7  
##  3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0  
##  Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0  
##       drat             wt             qsec             vs        
##  Min.   :2.760   Min.   :1.513   Min.   :14.50   Min.   :0.0000  
##  1st Qu.:3.080   1st Qu.:2.581   1st Qu.:16.89   1st Qu.:0.0000  
##  Median :3.695   Median :3.325   Median :17.71   Median :0.0000  
##  Mean   :3.597   Mean   :3.217   Mean   :17.85   Mean   :0.4375  
##  3rd Qu.:3.920   3rd Qu.:3.610   3rd Qu.:18.90   3rd Qu.:1.0000  
##  Max.   :4.930   Max.   :5.424   Max.   :22.90   Max.   :1.0000  
##        am              gear            carb      
##  Min.   :0.0000   Min.   :3.000   Min.   :1.000  
##  1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000  
##  Median :0.0000   Median :4.000   Median :2.000  
##  Mean   :0.4062   Mean   :3.688   Mean   :2.812  
##  3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :1.0000   Max.   :5.000   Max.   :8.000

MANOVA

Iris data

yvars<- cbind(mtcars$mpg, mtcars$cyl, mtcars$disp, mtcars$hp, mtcars$drat,mtcars$wt, mtcars$qsec, mtcars$gear, mtcars$vs)
colnames(yvars) <-c("mpg","cyl","disp","hp","drat","wt","qsec","gear","vs")
factor_c <-factor(mtcars$carb)
factor_am <-factor(mtcars$am)

mma<-manova(yvars~ factor_am +factor_c, data=mtcars)
mma
## Call:
##    manova(yvars ~ factor_am + factor_c, data = mtcars)
## 
## Terms:
##                 factor_am factor_c Residuals
## mpg                405.15   407.78    313.12
## cyl                 27.00    35.18     36.69
## disp             166450.1 106702.0  203032.7
## hp                8619.50 90571.44  46535.94
## drat                 4.50     0.81      3.55
## wt                  14.23     7.48      7.97
## qsec                 5.23    45.10     48.65
## gear                10.64     1.78      4.46
## vs                   0.22     3.58      4.07
## Deg. of Freedom         1        5        25
## 
## Residual standard errors: 3.539016 1.21144 90.1183 43.14438 0.3769924 0.5646695 1.395061 0.4223634 0.4036956
## Estimated effects may be unbalanced

NULL hypthoesis

Means are equal to each other

#wilks test
summary(mma,test="Wilks", intercept=TRUE)
##             Df    Wilks approx F num Df den Df    Pr(>F)    
## (Intercept)  1 0.000501   3765.2      9 17.000 < 2.2e-16 ***
## factor_am    1 0.153018     10.5      9 17.000 2.427e-05 ***
## factor_c     5 0.009093      3.3     45 79.148 1.984e-06 ***
## Residuals   25                                              
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Roys Test
summary(mma, test="Roy", intercept=TRUE)
##             Df     Roy approx F num Df den Df    Pr(>F)    
## (Intercept)  1 1993.33   3765.2      9     17 < 2.2e-16 ***
## factor_am    1    5.54     10.5      9     17 2.427e-05 ***
## factor_c     5   11.02     25.7      9     21 2.190e-09 ***
## Residuals   25                                             
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Hotelling lawyley test
summary(mma, test="Hotelling-Lawley", intercept=TRUE)
##             Df Hotelling-Lawley approx F num Df den Df    Pr(>F)    
## (Intercept)  1          1993.33   3765.2      9     17 < 2.2e-16 ***
## factor_am    1             5.54     10.5      9     17 2.427e-05 ***
## factor_c     5            14.50      5.0     45     77 3.969e-10 ***
## Residuals   25                                                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Pillai Test 
summary(mma, test="Pillai", intercept=TRUE)
##             Df  Pillai approx F num Df den Df    Pr(>F)    
## (Intercept)  1 0.99950   3765.2      9     17 < 2.2e-16 ***
## factor_am    1 0.84698     10.5      9     17 2.427e-05 ***
## factor_c     5 2.46882      2.3     45    105  0.000304 ***
## Residuals   25                                             
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Relation between IV and DV

yvars
##        mpg cyl  disp  hp drat    wt  qsec gear vs
##  [1,] 21.0   6 160.0 110 3.90 2.620 16.46    4  0
##  [2,] 21.0   6 160.0 110 3.90 2.875 17.02    4  0
##  [3,] 22.8   4 108.0  93 3.85 2.320 18.61    4  1
##  [4,] 21.4   6 258.0 110 3.08 3.215 19.44    3  1
##  [5,] 18.7   8 360.0 175 3.15 3.440 17.02    3  0
##  [6,] 18.1   6 225.0 105 2.76 3.460 20.22    3  1
##  [7,] 14.3   8 360.0 245 3.21 3.570 15.84    3  0
##  [8,] 24.4   4 146.7  62 3.69 3.190 20.00    4  1
##  [9,] 22.8   4 140.8  95 3.92 3.150 22.90    4  1
## [10,] 19.2   6 167.6 123 3.92 3.440 18.30    4  1
## [11,] 17.8   6 167.6 123 3.92 3.440 18.90    4  1
## [12,] 16.4   8 275.8 180 3.07 4.070 17.40    3  0
## [13,] 17.3   8 275.8 180 3.07 3.730 17.60    3  0
## [14,] 15.2   8 275.8 180 3.07 3.780 18.00    3  0
## [15,] 10.4   8 472.0 205 2.93 5.250 17.98    3  0
## [16,] 10.4   8 460.0 215 3.00 5.424 17.82    3  0
## [17,] 14.7   8 440.0 230 3.23 5.345 17.42    3  0
## [18,] 32.4   4  78.7  66 4.08 2.200 19.47    4  1
## [19,] 30.4   4  75.7  52 4.93 1.615 18.52    4  1
## [20,] 33.9   4  71.1  65 4.22 1.835 19.90    4  1
## [21,] 21.5   4 120.1  97 3.70 2.465 20.01    3  1
## [22,] 15.5   8 318.0 150 2.76 3.520 16.87    3  0
## [23,] 15.2   8 304.0 150 3.15 3.435 17.30    3  0
## [24,] 13.3   8 350.0 245 3.73 3.840 15.41    3  0
## [25,] 19.2   8 400.0 175 3.08 3.845 17.05    3  0
## [26,] 27.3   4  79.0  66 4.08 1.935 18.90    4  1
## [27,] 26.0   4 120.3  91 4.43 2.140 16.70    5  0
## [28,] 30.4   4  95.1 113 3.77 1.513 16.90    5  1
## [29,] 15.8   8 351.0 264 4.22 3.170 14.50    5  0
## [30,] 19.7   6 145.0 175 3.62 2.770 15.50    5  0
## [31,] 15.0   8 301.0 335 3.54 3.570 14.60    5  0
## [32,] 21.4   4 121.0 109 4.11 2.780 18.60    4  1
summary.aov(mma)
##  Response mpg :
##             Df Sum Sq Mean Sq F value    Pr(>F)    
## factor_am    1 405.15  405.15 32.3483 6.368e-06 ***
## factor_c     5 407.78   81.56  6.5117  0.000527 ***
## Residuals   25 313.12   12.52                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response cyl :
##             Df Sum Sq Mean Sq F value    Pr(>F)    
## factor_am    1 27.005 27.0046 18.4007 0.0002348 ***
## factor_c     5 35.181  7.0362  4.7944 0.0032913 ** 
## Residuals   25 36.690  1.4676                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response disp :
##             Df Sum Sq Mean Sq F value    Pr(>F)    
## factor_am    1 166450  166450 20.4955 0.0001269 ***
## factor_c     5 106702   21340  2.6277 0.0483721 *  
## Residuals   25 203033    8121                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response hp :
##             Df Sum Sq Mean Sq F value    Pr(>F)    
## factor_am    1   8619  8619.5  4.6306   0.04126 *  
## factor_c     5  90571 18114.3  9.7313 2.966e-05 ***
## Residuals   25  46536  1861.4                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response drat :
##             Df Sum Sq Mean Sq F value    Pr(>F)    
## factor_am    1 4.5017  4.5017 31.6745 7.412e-06 ***
## factor_c     5 0.8076  0.1615  1.1364    0.3673    
## Residuals   25 3.5531  0.1421                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response wt :
##             Df  Sum Sq Mean Sq F value    Pr(>F)    
## factor_am    1 14.2324 14.2324 44.6365 5.313e-07 ***
## factor_c     5  7.4750  1.4950  4.6887  0.003714 ** 
## Residuals   25  7.9713  0.3189                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response qsec :
##             Df Sum Sq Mean Sq F value   Pr(>F)   
## factor_am    1  5.230  5.2301  2.6874 0.113672   
## factor_c     5 45.103  9.0206  4.6350 0.003951 **
## Residuals   25 48.655  1.9462                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response gear :
##             Df  Sum Sq Mean Sq F value   Pr(>F)    
## factor_am    1 10.6402 10.6402 59.6454 4.44e-08 ***
## factor_c     5  1.7750  0.3550  1.9901    0.115    
## Residuals   25  4.4598  0.1784                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response vs :
##             Df Sum Sq Mean Sq F value   Pr(>F)   
## factor_am    1 0.2232 0.22318  1.3694 0.252943   
## factor_c     5 3.5776 0.71551  4.3905 0.005254 **
## Residuals   25 4.0743 0.16297                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Boxplot

To check the mean differences

ggboxplot(mtcars, x="carb", y=c("mpg", "cyl","qsec"), merge=TRUE, palette="jco")

ggboxplot(mtcars, x="carb", y=c("disp","hp"), merge=TRUE, palette="jco")

ggboxplot(mtcars, x="carb", y=c("drat","wt","am"), merge=TRUE, palette="jco")

MANOVA Linearity

Key :MANOVA consider the relationship of dependent variable together !

m2<-lm(yvars~ am, data=mtcars)
lin_comb <- matrix(c(1,-1), nrow=1, ncol=2)
linearHypothesis(m2,lin_comb)
## 
## Sum of squares and products for the hypothesis:
##               mpg          cyl        disp          hp        drat          wt
## mpg    341.131711   303.767474  15062.1666   6672.1452   86.902881  176.614287
## cyl    303.767474   270.495752  13412.4039   5941.3436   77.384388  157.269683
## disp 15062.166567 13412.403935 665047.7062 294598.7107 3837.068305 7798.142844
## hp    6672.145184  5941.343559 294598.7107 130499.5108 1699.720734 3454.372981
## drat    86.902881    77.384388   3837.0683   1699.7207   22.138402   44.992271
## wt     176.614287   157.269683   7798.1428   3454.3730   44.992271   91.438601
## qsec   654.754194   583.038814  28909.7039  12806.2414  166.797821  338.986208
## gear    70.153829    62.469864   3097.5386   1372.1285   17.871601   36.320776
## vs       6.834071     6.085533    301.7483    133.6666    1.740971    3.538207
##             qsec        gear          vs
## mpg    654.75419   70.153829   6.8340708
## cyl    583.03881   62.469864   6.0855334
## disp 28909.70386 3097.538650 301.7482979
## hp   12806.24142 1372.128471 133.6665906
## drat   166.79782   17.871601   1.7409711
## wt     338.98621   36.320776   3.5382068
## qsec  1256.70831  134.650378  13.1170348
## gear   134.65038   14.427154   1.4054285
## vs      13.11703    1.405429   0.1369105
## 
## Sum of squares and products for error:
##                mpg         cyl        disp          hp        drat           wt
## mpg   7.208966e+02 -179.744939 -11413.9880  -8073.9522   25.340316  -82.6812053
## cyl  -1.797449e+02   71.870445   4069.3482   2677.4170   -9.693684   22.7838947
## disp -1.141399e+04 4069.348178 309734.6793 170478.2668 -593.360474 1799.0578579
## hp   -8.073952e+03 2677.417004 170478.2668 137107.3765 -313.001579 1019.7205263
## drat  2.534032e+01   -9.693684   -593.3605   -313.0016    4.360642   -3.5499774
## wt   -8.268121e+01   22.783895   1799.0579   1019.7205   -3.549977   15.4463138
## qsec  1.858162e+02  -70.376842  -3910.6387  -2902.1958    7.553621  -18.0976537
## gear  5.489879e-01   -3.174089   -244.0696    105.7166    1.634737   -0.7475789
## vs    5.302227e+01  -20.170040  -1182.9680   -730.7652    2.675789   -6.7012632
##              qsec         gear            vs
## mpg    185.816158    0.5489879    53.0222672
## cyl    -70.376842   -3.1740891   -20.1700405
## disp -3910.638737 -244.0696356 -1182.9680162
## hp   -2902.195789  105.7165992  -730.7651822
## drat     7.553621    1.6347368     2.6757895
## wt     -18.097654   -0.7475789    -6.7012632
## qsec    93.758011   -1.2326316    21.8678947
## gear    -1.232632    6.2348178     0.8340081
## vs      21.867895    0.8340081     7.6518219
## 
## Multivariate Tests: 
##                  Df test stat approx F num Df den Df     Pr(>F)    
## Pillai            1   0.99459 449.3124      9     22 < 2.22e-16 ***
## Wilks             1   0.00541 449.3124      9     22 < 2.22e-16 ***
## Hotelling-Lawley  1 183.80961 449.3124      9     22 < 2.22e-16 ***
## Roy               1 183.80961 449.3124      9     22 < 2.22e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

ANOVA

an <- aov(hp ~ am, data=mtcars)
summary(an)
##             Df Sum Sq Mean Sq F value Pr(>F)
## am           1   8619    8619   1.886   0.18
## Residuals   30 137107    4570
ggboxplot(mtcars, x="hp", y="am", palette="jco")

an1 <- aov(am ~ mpg, data=mtcars)
summary(an1)
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## mpg          1  2.777  2.7772   16.86 0.000285 ***
## Residuals   30  4.942  0.1647                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
ggboxplot(mtcars, x="am", y="mpg", palette="jco")

ref:ANOVA and MANOVA Analysis in R - Spencer Pao