How to put findings of mvn tests in a table in R?

I am testing for multivariate normality in the Iris dataset. I want to compare the results of all the different tests under the MVN package (Mardia’s test, Henze-Zikler, Royston, Doornik-Hansen and Energy test) and have done the following code:

library(MVN) 
library(htmlTable)

attach(iris)

#Mardia's Test
Mar<-MVN::mvn(data=iris,subset="Species", mvnTest="mardia")
Mar$multivariateNormality

#Henze-Zirkler's Test
Hz<-MVN::mvn(data=iris,subset="Species",mvnTest="hz")
Hz$multivariateNormality

#Royston's Test
Roy<-MVN::mvn(data=iris,subset="Species", mvnTest="royston")
Roy$multivariateNormality

#Doornik-Hansen Test
Dh<-MVN::mvn(data=iris,subset="Species", mvnTest="dh")
Dh$multivariateNormality

I have to get the results in a way that is easy to present in my findings. I am after three tables (one for each species) which summaries the data from the output above. Essentially for each table the rows are the different tests and the columns are the Test Statistic value, P Value and mvn Result. I tried the following

   table <- rbind(Mar$multivariateNormality$setosa[1:2, ],Hz$multivariateNormality$setosa[1:2, ],Roy$multivariateNormality$setosa[1:2, ])
    htmlTable(table, caption="Testing")

However, I get this error

Error in match.names(clabs, names(xi)) : 
names do not match previous names

1 answer

  • answered 2020-09-14 08:05 Allan Cameron

    The "tables" are actually data frames (and each column is a factor variable). To make them easier to process, it would be best to put all the results into a list. We can then convert all the data frames into character matrices, ensure the column names match, then bind them together:

    tablist <- lapply(list(Mar, Hz, Roy), `[[`, "multivariateNormality")
    tablist <- lapply(tablist, function(x) 
      lapply(x, function(y) {
        y <- `colnames<-`(as.matrix(y), c("Test", "Statistic", "p value", "MVN"))
        y[,2] <- round(as.numeric(y[,2]), 2)
        y[,3] <- round(as.numeric(y[,3]), 2)
        y
      }))
    
    setosa <- as.data.frame(do.call(rbind, lapply(tablist, `[[`, "setosa")))
    versicolor <- as.data.frame(do.call(rbind, lapply(tablist, `[[`, "versicolor")))
    virginica <- as.data.frame(do.call(rbind, lapply(tablist, `[[`, "virginica")))
    
    htmlTable(setosa, caption = "Setosa")
    htmlTable(versicolor, caption = "Versicolor")
    htmlTable(virginica, caption = "Virginica")
    

    Which gives:

    enter image description here

    enter image description here

    enter image description here