ranovadatasetmanova

How to access to members of a test result in R


I am conducting the Levene's test as well as Shapiro-Wilk test on a large set of data. I am wondering how do I access to particular parts of a result and store them in a CSV file to simplify reporting results.

For example, a Levene's test results look like this in R:

> leveneTest(Var~Label, data = df)

Df F value Pr(>F)
group   3  0.7595 0.5183
      166

How can I get only F-value and p-value stored in separate variables? Same as W and p-value in the following:

> shapiro.test(df[,i])

Shapiro-Wilk normality test

data:  dfc[, i]
W = 0.70952, p-value < 2.2e-16

Solution

  • Like this below? I don't know what is i in your code though:

    library(car)
    df = data.frame(Var=runif(100),Label=rep(c("A","B"),50))
    test = leveneTest(Var~Label, data = df)
    

    To know to access the data, do:

    class(test) [1] "anova" "data.frame"

    So test is a data.frame and you can either use $ or access it via rows and column names. Since the column names contains spaces in this case, using the matrix way is a bit better (and you know the column you called):

    Fvalue = test[1,"F value"]
    pvalue = test[1,"Pr(>F)"]
    
    Fvalue
    > [1] 0.3516209
    > pvalue
    [1] 0.554563
    

    For shapiro, unfortunately they created a new class, so you can check the vignette:

    Value:

     A list with class ‘"htest"’ containing the following components:
    

    statistic: the value of the Shapiro-Wilk statistic.

    p.value: an approximate p-value for the test. This is said in Royston (1995) to be adequate for ‘p.value < 0.1’.

    So we treat it as a list:

    names(test)
    [1] "statistic" "p.value"   "method"    "data.name"
    
    test$p.value
    [1] 0.0002292068