Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading