Home > Net >  Levene's F test for several variables and extracting the p-values
Levene's F test for several variables and extracting the p-values

Time:01-31

I'm trying to do a Levene's test for several variables in a dataframe and extract the p-values. Would like to do something like below....

df <- data.frame(EMO_ETA.3 = c(1, 4, 5, 6), EMO_ETA.6 = c(4, 1, 8, 9), zyg = c("1", "1", "2", "2"))

> sapply(df[c("EMO_ETA.3", "EMO_ETA.6")], function(i) t.test(i ~ df$zyg)$p.value)
EMO_ETA.3 EMO_ETA.6 
0.2725896 0.1281349 

Here's my attempt to do it with Levene's test...

> lapply(df[c("EMO_ETA.3", "EMO_ETA.6")], leveneTest, group = df$zyg)$'Pr(>F)'
NULL

CodePudding user response:

Try

sapply(names(df)[1:2], function(nm)
    leveneTest(reformulate("zyg", response = nm), data = df)$'Pr(>F)'[1])
   EMO_ETA.3    EMO_ETA.6 
1.232595e-32 1.232595e-32 

Or using the OP's approach

sapply(df[c("EMO_ETA.3", "EMO_ETA.6")],
  function(x) leveneTest(x, group = df$zyg)$'Pr(>F)'[1])
   EMO_ETA.3    EMO_ETA.6 
1.232595e-32 1.232595e-32 
  •  Tags:  
  • r
  • Related