data(iris)
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

plotids <- with(iris, plot3d(Sepal.Length, Sepal.Width, Petal.Length, type="s", col=as.numeric(Species), size=as.numeric(Petal.Width)))
#rglwidget()

You must enable Javascript to view this page properly.

Funkcia mvn

Univerzálna funkcia na testovanie jedno- aj viacrozmernej normality, detekciu odľahlých pozorovaní a grafické znázornenie testov.

mvn(data = iris[-4], subset = "Species", mvnTest = "hz",univariateTest = "AD", univariatePlot = "histogram", multivariatePlot = "qq", multivariateOutlierMethod = "adj", showOutliers = TRUE, showNewData = TRUE, bc=TRUE)
library(MVN)
## sROC 0.1-2 loaded
res <- mvn(iris[,-5], multivariateOutlierMethod = "adj", showOutliers = TRUE, showNewData = TRUE)

Test viacrozmernej normality

res$multivariateNormality
##              Test          Statistic              p value Result
## 1 Mardia Skewness   67.4305087780622 4.75799820400835e-07     NO
## 2 Mardia Kurtosis -0.230112114480973    0.818004651478034    YES
## 3             MVN               <NA>                 <NA>     NO

Test jednorozmernej normality

res$univariateNormality
##           Test     Variable Statistic   p value Normality
## 1 Shapiro-Wilk Sepal.Length    0.9761  0.0102      NO    
## 2 Shapiro-Wilk Sepal.Width     0.9849  0.1012      YES   
## 3 Shapiro-Wilk Petal.Length    0.8763  <0.001      NO    
## 4 Shapiro-Wilk Petal.Width     0.9018  <0.001      NO

Deskriptívne štatistiky

res$Descriptives
##                n     Mean   Std.Dev Median Min Max 25th 75th       Skew
## Sepal.Length 150 5.843333 0.8280661   5.80 4.3 7.9  5.1  6.4  0.3086407
## Sepal.Width  150 3.057333 0.4358663   3.00 2.0 4.4  2.8  3.3  0.3126147
## Petal.Length 150 3.758000 1.7652982   4.35 1.0 6.9  1.6  5.1 -0.2694109
## Petal.Width  150 1.199333 0.7622377   1.30 0.1 2.5  0.3  1.8 -0.1009166
##                Kurtosis
## Sepal.Length -0.6058125
## Sepal.Width   0.1387047
## Petal.Length -1.4168574
## Petal.Width  -1.3581792

Odľahlé pozorovania

res$multivariateOutliers
##    Observation Mahalanobis Distance Outlier
## 16          16               57.630    TRUE
## 15          15               51.827    TRUE
## 34          34               51.665    TRUE
## 33          33               46.571    TRUE
## 17          17               43.523    TRUE
## 6            6               36.532    TRUE
## 19          19               36.061    TRUE
## 23          23               36.035    TRUE
## 20          20               35.559    TRUE
## 47          47               34.862    TRUE
## 11          11               34.711    TRUE
## 37          37               34.549    TRUE
## 49          49               34.116    TRUE
## 22          22               32.859    TRUE
## 38          38               31.825    TRUE
## 5            5               31.628    TRUE
## 41          41               30.801    TRUE
## 45          45               30.505    TRUE
## 18          18               29.758    TRUE
## 1            1               29.748    TRUE
## 28          28               28.867    TRUE
## 32          32               28.787    TRUE
## 29          29               28.286    TRUE
## 44          44               27.088    TRUE
## 36          36               26.659    TRUE
## 40          40               26.261    TRUE
## 7            7               25.886    TRUE
## 8            8               25.829    TRUE
## 21          21               25.298    TRUE
## 50          50               25.206    TRUE
## 27          27               24.391    TRUE
## 12          12               24.295    TRUE
## 3            3               23.499    TRUE
## 43          43               22.958    TRUE
## 25          25               22.188    TRUE
## 48          48               22.048    TRUE
## 24          24               21.955    TRUE
## 14          14               21.731    TRUE
## 10          10               20.267    TRUE
## 30          30               20.173    TRUE
## 35          35               20.076    TRUE
## 2            2               20.031    TRUE
## 46          46               19.793    TRUE
## 39          39               19.569    TRUE
## 13          13               19.523    TRUE
## 4            4               19.270    TRUE
## 31          31               18.615    TRUE
## 26          26               17.921    TRUE
## 9            9               17.104    TRUE

Nové dáta bez odľahlých pozorovaní

res$newData
##     Sepal.Length Sepal.Width Petal.Length Petal.Width
## 100          5.7         2.8          4.1         1.3
## 101          6.3         3.3          6.0         2.5
## 102          5.8         2.7          5.1         1.9
## 103          7.1         3.0          5.9         2.1
## 104          6.3         2.9          5.6         1.8
## 105          6.5         3.0          5.8         2.2
## 106          7.6         3.0          6.6         2.1
## 107          4.9         2.5          4.5         1.7
## 108          7.3         2.9          6.3         1.8
## 109          6.7         2.5          5.8         1.8
## 110          7.2         3.6          6.1         2.5
## 111          6.5         3.2          5.1         2.0
## 112          6.4         2.7          5.3         1.9
## 113          6.8         3.0          5.5         2.1
## 114          5.7         2.5          5.0         2.0
## 115          5.8         2.8          5.1         2.4
## 116          6.4         3.2          5.3         2.3
## 117          6.5         3.0          5.5         1.8
## 118          7.7         3.8          6.7         2.2
## 119          7.7         2.6          6.9         2.3
## 120          6.0         2.2          5.0         1.5
## 121          6.9         3.2          5.7         2.3
## 122          5.6         2.8          4.9         2.0
## 123          7.7         2.8          6.7         2.0
## 124          6.3         2.7          4.9         1.8
## 125          6.7         3.3          5.7         2.1
## 126          7.2         3.2          6.0         1.8
## 127          6.2         2.8          4.8         1.8
## 128          6.1         3.0          4.9         1.8
## 129          6.4         2.8          5.6         2.1
## 130          7.2         3.0          5.8         1.6
## 131          7.4         2.8          6.1         1.9
## 132          7.9         3.8          6.4         2.0
## 133          6.4         2.8          5.6         2.2
## 134          6.3         2.8          5.1         1.5
## 135          6.1         2.6          5.6         1.4
## 136          7.7         3.0          6.1         2.3
## 137          6.3         3.4          5.6         2.4
## 138          6.4         3.1          5.5         1.8
## 139          6.0         3.0          4.8         1.8
## 140          6.9         3.1          5.4         2.1
## 141          6.7         3.1          5.6         2.4
## 142          6.9         3.1          5.1         2.3
## 143          5.8         2.7          5.1         1.9
## 144          6.8         3.2          5.9         2.3
## 145          6.7         3.3          5.7         2.5
## 146          6.7         3.0          5.2         2.3
## 147          6.3         2.5          5.0         1.9
## 148          6.5         3.0          5.2         2.0
## 149          6.2         3.4          5.4         2.3
## 150          5.9         3.0          5.1         1.8
## 42           4.5         2.3          1.3         0.3
## 51           7.0         3.2          4.7         1.4
## 52           6.4         3.2          4.5         1.5
## 53           6.9         3.1          4.9         1.5
## 54           5.5         2.3          4.0         1.3
## 55           6.5         2.8          4.6         1.5
## 56           5.7         2.8          4.5         1.3
## 57           6.3         3.3          4.7         1.6
## 58           4.9         2.4          3.3         1.0
## 59           6.6         2.9          4.6         1.3
## 60           5.2         2.7          3.9         1.4
## 61           5.0         2.0          3.5         1.0
## 62           5.9         3.0          4.2         1.5
## 63           6.0         2.2          4.0         1.0
## 64           6.1         2.9          4.7         1.4
## 65           5.6         2.9          3.6         1.3
## 66           6.7         3.1          4.4         1.4
## 67           5.6         3.0          4.5         1.5
## 68           5.8         2.7          4.1         1.0
## 69           6.2         2.2          4.5         1.5
## 70           5.6         2.5          3.9         1.1
## 71           5.9         3.2          4.8         1.8
## 72           6.1         2.8          4.0         1.3
## 73           6.3         2.5          4.9         1.5
## 74           6.1         2.8          4.7         1.2
## 75           6.4         2.9          4.3         1.3
## 76           6.6         3.0          4.4         1.4
## 77           6.8         2.8          4.8         1.4
## 78           6.7         3.0          5.0         1.7
## 79           6.0         2.9          4.5         1.5
## 80           5.7         2.6          3.5         1.0
## 81           5.5         2.4          3.8         1.1
## 82           5.5         2.4          3.7         1.0
## 83           5.8         2.7          3.9         1.2
## 84           6.0         2.7          5.1         1.6
## 85           5.4         3.0          4.5         1.5
## 86           6.0         3.4          4.5         1.6
## 87           6.7         3.1          4.7         1.5
## 88           6.3         2.3          4.4         1.3
## 89           5.6         3.0          4.1         1.3
## 90           5.5         2.5          4.0         1.3
## 91           5.5         2.6          4.4         1.2
## 92           6.1         3.0          4.6         1.4
## 93           5.8         2.6          4.0         1.2
## 94           5.0         2.3          3.3         1.0
## 95           5.6         2.7          4.2         1.3
## 96           5.7         3.0          4.2         1.2
## 97           5.7         2.9          4.2         1.3
## 98           6.2         2.9          4.3         1.3
## 99           5.1         2.5          3.0         1.1

Grafické znázornenie

mvn(iris[ ,-5], multivariatePlot = "qq")

## $multivariateNormality
##              Test          Statistic              p value Result
## 1 Mardia Skewness   67.4305087780622 4.75799820400835e-07     NO
## 2 Mardia Kurtosis -0.230112114480973    0.818004651478034    YES
## 3             MVN               <NA>                 <NA>     NO
## 
## $univariateNormality
##           Test     Variable Statistic   p value Normality
## 1 Shapiro-Wilk Sepal.Length    0.9761  0.0102      NO    
## 2 Shapiro-Wilk Sepal.Width     0.9849  0.1012      YES   
## 3 Shapiro-Wilk Petal.Length    0.8763  <0.001      NO    
## 4 Shapiro-Wilk Petal.Width     0.9018  <0.001      NO    
## 
## $Descriptives
##                n     Mean   Std.Dev Median Min Max 25th 75th       Skew
## Sepal.Length 150 5.843333 0.8280661   5.80 4.3 7.9  5.1  6.4  0.3086407
## Sepal.Width  150 3.057333 0.4358663   3.00 2.0 4.4  2.8  3.3  0.3126147
## Petal.Length 150 3.758000 1.7652982   4.35 1.0 6.9  1.6  5.1 -0.2694109
## Petal.Width  150 1.199333 0.7622377   1.30 0.1 2.5  0.3  1.8 -0.1009166
##                Kurtosis
## Sepal.Length -0.6058125
## Sepal.Width   0.1387047
## Petal.Length -1.4168574
## Petal.Width  -1.3581792
mvn(iris[ ,1:2], multivariatePlot = "persp")

## $multivariateNormality
##              Test          Statistic            p value Result
## 1 Mardia Skewness   9.46144098216622 0.0505456076692466    YES
## 2 Mardia Kurtosis -0.853178029438543  0.393560585232763    YES
## 3             MVN               <NA>               <NA>    YES
## 
## $univariateNormality
##           Test     Variable Statistic   p value Normality
## 1 Shapiro-Wilk Sepal.Length    0.9761    0.0102    NO    
## 2 Shapiro-Wilk Sepal.Width     0.9849    0.1012    YES   
## 
## $Descriptives
##                n     Mean   Std.Dev Median Min Max 25th 75th      Skew
## Sepal.Length 150 5.843333 0.8280661    5.8 4.3 7.9  5.1  6.4 0.3086407
## Sepal.Width  150 3.057333 0.4358663    3.0 2.0 4.4  2.8  3.3 0.3126147
##                Kurtosis
## Sepal.Length -0.6058125
## Sepal.Width   0.1387047
mvn(iris[ ,1:2], multivariatePlot = "contour")

## $multivariateNormality
##              Test          Statistic            p value Result
## 1 Mardia Skewness   9.46144098216622 0.0505456076692466    YES
## 2 Mardia Kurtosis -0.853178029438543  0.393560585232763    YES
## 3             MVN               <NA>               <NA>    YES
## 
## $univariateNormality
##           Test     Variable Statistic   p value Normality
## 1 Shapiro-Wilk Sepal.Length    0.9761    0.0102    NO    
## 2 Shapiro-Wilk Sepal.Width     0.9849    0.1012    YES   
## 
## $Descriptives
##                n     Mean   Std.Dev Median Min Max 25th 75th      Skew
## Sepal.Length 150 5.843333 0.8280661    5.8 4.3 7.9  5.1  6.4 0.3086407
## Sepal.Width  150 3.057333 0.4358663    3.0 2.0 4.4  2.8  3.3 0.3126147
##                Kurtosis
## Sepal.Length -0.6058125
## Sepal.Width   0.1387047
mvn(iris[ ,-5], univariatePlot = "histogram")

## $multivariateNormality
##              Test          Statistic              p value Result
## 1 Mardia Skewness   67.4305087780622 4.75799820400835e-07     NO
## 2 Mardia Kurtosis -0.230112114480973    0.818004651478034    YES
## 3             MVN               <NA>                 <NA>     NO
## 
## $univariateNormality
##           Test     Variable Statistic   p value Normality
## 1 Shapiro-Wilk Sepal.Length    0.9761  0.0102      NO    
## 2 Shapiro-Wilk Sepal.Width     0.9849  0.1012      YES   
## 3 Shapiro-Wilk Petal.Length    0.8763  <0.001      NO    
## 4 Shapiro-Wilk Petal.Width     0.9018  <0.001      NO    
## 
## $Descriptives
##                n     Mean   Std.Dev Median Min Max 25th 75th       Skew
## Sepal.Length 150 5.843333 0.8280661   5.80 4.3 7.9  5.1  6.4  0.3086407
## Sepal.Width  150 3.057333 0.4358663   3.00 2.0 4.4  2.8  3.3  0.3126147
## Petal.Length 150 3.758000 1.7652982   4.35 1.0 6.9  1.6  5.1 -0.2694109
## Petal.Width  150 1.199333 0.7622377   1.30 0.1 2.5  0.3  1.8 -0.1009166
##                Kurtosis
## Sepal.Length -0.6058125
## Sepal.Width   0.1387047
## Petal.Length -1.4168574
## Petal.Width  -1.3581792
mvn(iris[ ,-5], univariatePlot = "scatter")

## $multivariateNormality
##              Test          Statistic              p value Result
## 1 Mardia Skewness   67.4305087780622 4.75799820400835e-07     NO
## 2 Mardia Kurtosis -0.230112114480973    0.818004651478034    YES
## 3             MVN               <NA>                 <NA>     NO
## 
## $univariateNormality
##           Test     Variable Statistic   p value Normality
## 1 Shapiro-Wilk Sepal.Length    0.9761  0.0102      NO    
## 2 Shapiro-Wilk Sepal.Width     0.9849  0.1012      YES   
## 3 Shapiro-Wilk Petal.Length    0.8763  <0.001      NO    
## 4 Shapiro-Wilk Petal.Width     0.9018  <0.001      NO    
## 
## $Descriptives
##                n     Mean   Std.Dev Median Min Max 25th 75th       Skew
## Sepal.Length 150 5.843333 0.8280661   5.80 4.3 7.9  5.1  6.4  0.3086407
## Sepal.Width  150 3.057333 0.4358663   3.00 2.0 4.4  2.8  3.3  0.3126147
## Petal.Length 150 3.758000 1.7652982   4.35 1.0 6.9  1.6  5.1 -0.2694109
## Petal.Width  150 1.199333 0.7622377   1.30 0.1 2.5  0.3  1.8 -0.1009166
##                Kurtosis
## Sepal.Length -0.6058125
## Sepal.Width   0.1387047
## Petal.Length -1.4168574
## Petal.Width  -1.3581792

Porovnanie rôznych testov pre viacrozmernú normalitu

mvn(iris[,-5], mvnTest = "mardia")$multivariateNormality
##              Test          Statistic              p value Result
## 1 Mardia Skewness   67.4305087780622 4.75799820400835e-07     NO
## 2 Mardia Kurtosis -0.230112114480973    0.818004651478034    YES
## 3             MVN               <NA>                 <NA>     NO
mvn(iris[,-5], mvnTest = "hz")$multivariateNormality
##            Test       HZ p value MVN
## 1 Henze-Zirkler 2.336394       0  NO
mvn(iris[,-5], mvnTest = "royston")$multivariateNormality
##      Test        H      p value MVN
## 1 Royston 50.39667 3.098229e-11  NO

Vezmeme iba jeden druh kosatcov:

setosa <- iris[1:50, 1:4]
mvn(setosa, mvnTest = "mardia")$multivariateNormality
##              Test        Statistic           p value Result
## 1 Mardia Skewness 25.6643445196298 0.177185884467652    YES
## 2 Mardia Kurtosis 1.29499223711606 0.195322907441934    YES
## 3             MVN             <NA>              <NA>    YES
mvn(setosa, mvnTest = "hz")$multivariateNormality
##            Test        HZ    p value MVN
## 1 Henze-Zirkler 0.9488453 0.04995356  NO
mvn(setosa, mvnTest = "royston")$multivariateNormality
##      Test        H      p value MVN
## 1 Royston 31.51803 2.187653e-06  NO

Transformácie na normálne rozdelenie

Box-Cox transformácia

Priamo v príkaze mvn:

mvn(iris[,-5], mvnTest = "mardia", bc=TRUE)$multivariateNormality
##              Test          Statistic            p value Result
## 1 Mardia Skewness   35.7910554915828 0.0162739088388607     NO
## 2 Mardia Kurtosis -0.806554908104519  0.419922961559335    YES
## 3             MVN               <NA>               <NA>     NO

Mocninová transformácia

library(car)
## Loading required package: carData
p1 <- powerTransform(setosa)
summary(p1)
## bcPower Transformations to Multinormality 
##              Est Power Rounded Pwr Wald Lwr Bnd Wald Upr Bnd
## Sepal.Length    0.4166           1      -2.6421       3.4752
## Sepal.Width     1.2729           1      -0.1900       2.7358
## Petal.Length    0.7286           1      -0.8958       2.3531
## Petal.Width     0.0244           0      -0.5009       0.5498
## 
## Likelihood ratio test that transformation parameters are equal to 0
##  (all log transformations)
##                                  LRT df    pval
## LR test, lambda = (0 0 0 0) 3.922044  4 0.41666
## 
## Likelihood ratio test that no transformations are needed
##                                 LRT df     pval
## LR test, lambda = (1 1 1 1) 12.9728  4 0.011409
(tY1 <- bcPower(with(setosa, cbind(Sepal.Length,Sepal.Width,Petal.Length,Petal.Width)),coef(p1, round=TRUE)))
##       Sepal.Length^1 Sepal.Width^1 Petal.Length^1 Petal.Width^0
##  [1,]            4.1           2.5            0.4    -1.6094379
##  [2,]            3.9           2.0            0.4    -1.6094379
##  [3,]            3.7           2.2            0.3    -1.6094379
##  [4,]            3.6           2.1            0.5    -1.6094379
##  [5,]            4.0           2.6            0.4    -1.6094379
##  [6,]            4.4           2.9            0.7    -0.9162907
##  [7,]            3.6           2.4            0.4    -1.2039728
##  [8,]            4.0           2.4            0.5    -1.6094379
##  [9,]            3.4           1.9            0.4    -1.6094379
## [10,]            3.9           2.1            0.5    -2.3025851
## [11,]            4.4           2.7            0.5    -1.6094379
## [12,]            3.8           2.4            0.6    -1.6094379
## [13,]            3.8           2.0            0.4    -2.3025851
## [14,]            3.3           2.0            0.1    -2.3025851
## [15,]            4.8           3.0            0.2    -1.6094379
## [16,]            4.7           3.4            0.5    -0.9162907
## [17,]            4.4           2.9            0.3    -0.9162907
## [18,]            4.1           2.5            0.4    -1.2039728
## [19,]            4.7           2.8            0.7    -1.2039728
## [20,]            4.1           2.8            0.5    -1.2039728
## [21,]            4.4           2.4            0.7    -1.6094379
## [22,]            4.1           2.7            0.5    -0.9162907
## [23,]            3.6           2.6            0.0    -1.6094379
## [24,]            4.1           2.3            0.7    -0.6931472
## [25,]            3.8           2.4            0.9    -1.6094379
## [26,]            4.0           2.0            0.6    -1.6094379
## [27,]            4.0           2.4            0.6    -0.9162907
## [28,]            4.2           2.5            0.5    -1.6094379
## [29,]            4.2           2.4            0.4    -1.6094379
## [30,]            3.7           2.2            0.6    -1.6094379
## [31,]            3.8           2.1            0.6    -1.6094379
## [32,]            4.4           2.4            0.5    -0.9162907
## [33,]            4.2           3.1            0.5    -2.3025851
## [34,]            4.5           3.2            0.4    -1.6094379
## [35,]            3.9           2.1            0.5    -1.6094379
## [36,]            4.0           2.2            0.2    -1.6094379
## [37,]            4.5           2.5            0.3    -1.6094379
## [38,]            3.9           2.6            0.4    -2.3025851
## [39,]            3.4           2.0            0.3    -1.6094379
## [40,]            4.1           2.4            0.5    -1.6094379
## [41,]            4.0           2.5            0.3    -1.2039728
## [42,]            3.5           1.3            0.3    -1.2039728
## [43,]            3.4           2.2            0.3    -1.6094379
## [44,]            4.0           2.5            0.6    -0.5108256
## [45,]            4.1           2.8            0.9    -0.9162907
## [46,]            3.8           2.0            0.4    -1.2039728
## [47,]            4.1           2.8            0.6    -1.6094379
## [48,]            3.6           2.2            0.4    -1.6094379
## [49,]            4.3           2.7            0.5    -1.6094379
## [50,]            4.0           2.3            0.4    -1.6094379
summary(p2 <- powerTransform(cbind(Sepal.Length,Sepal.Width,Petal.Length,Petal.Width) ~ Species, iris))
## bcPower Transformations to Multinormality 
##              Est Power Rounded Pwr Wald Lwr Bnd Wald Upr Bnd
## Sepal.Length   -0.3957         0.0      -1.1615       0.3702
## Sepal.Width     0.6381         1.0      -0.0817       1.3578
## Petal.Length    0.3669         0.5       0.1880       0.5459
## Petal.Width     0.5776         0.5       0.4661       0.6891
## 
## Likelihood ratio test that transformation parameters are equal to 0
##  (all log transformations)
##                                  LRT df       pval
## LR test, lambda = (0 0 0 0) 124.7563  4 < 2.22e-16
## 
## Likelihood ratio test that no transformations are needed
##                                  LRT df       pval
## LR test, lambda = (1 1 1 1) 84.18866  4 < 2.22e-16
testTransform(p2, c(0, 1, 0.5, 0.5))
##                                      LRT df    pval
## LR test, lambda = (0 1 0.5 0.5) 5.563751  4 0.23418
tY2 <- bcPower(with(iris, cbind(Sepal.Length,Sepal.Width,Petal.Length,Petal.Width)),coef(p2, round=TRUE))
scatterplotMatrix( ~ tY2|Species, iris)