Kay0818
Kay0818

Reputation: 79

How to show multiple test result of the same data.frame in a summary table

The iris dataset (built-in in R) includes 50 observations, each observation has data of Sepal.Length, Sepal.Width, Petal.Length and Petal.Width. I want to use multiple tests (Shapiro.test, ks.test, cvm.test, ad.test, ) to test the normality of each column and show the result in on table.

The code below shows the result using only one test.

dat <- iris %>%
  filter(Species == "setosa")

df <- dat %>%
  select(-Species)
test <- lapply(df, shapiro.test)
table <- sapply(test, `[`,c("statistic","p.value"))
table
R > 
$Sepal.Length.p.value
[1] 0.4595132

$Sepal.Width.p.value
[1] 0.2715264

$Petal.Length.p.value
[1] 0.05481147

$Petal.Width.p.value
[1] 8.658573e-07

I wish to summarize and compare between different tests in one table, where the row represents different test and the column represents Sepal.Length, Sepal.Width, Petal.Length and Petal.Width.

Upvotes: 2

Views: 472

Answers (1)

akrun
akrun

Reputation: 887118

One option would be

library(nortest)
out <- lapply(c("shapiro.test", "cvm.test", "ad.test"), 
    function(x) sapply(df, function(y) get(x)(y)[c("statistic", "p.value")]))
do.call(rbind, Map(cbind, test = c("shapiro.test", "cvm.test", "ad.test"), out))

Or using tidyverse

library(tidyverse)
lst(shapiro.test, cvm.test, ad.test) %>% 
   map_df(~ df  %>% 
             summarise_all(list(~ list(.x(.)[c("statistic", "p.value")]))) %>% 
             unnest, .id = "test")

If we don't need the intercept

lst(shapiro.test, cvm.test, ad.test) %>%
      map_df(~ df  %>% 
           summarise_all(list(~ list(.x(.)[c("statistic", "p.value")][2]))) %>%
           unnest, .id = "test")
#          test Sepal.Length Sepal.Width Petal.Length  Petal.Width
#1 shapiro.test    0.4595132   0.2715264   0.05481147 8.658573e-07
#2     cvm.test    0.2596871   0.2324437  0.006874393 1.717773e-09
#3      ad.test    0.3352439   0.2101787   0.01079067 7.437223e-12

Upvotes: 0

Related Questions