0

I'm new in R and coding in general... I have computed multiple anova analysis on multiple columns (16 in total). For that purpose, the method "Purr" helped me :

anova_results_5sector <- purrr::map(df_anova_ch[,3:18], ~aov(.x ~ df_anova_ch$Own_5sector))


summary(anova_results_5sector[[1]])

So the dumbest way to retrieve output (p-value, etc) is the following method

summary(anova_results_5sector$Env_Pillar)
summary(anova_results_5sector$Gov_Pillar)
summary(anova_results_5sector$Soc_Pillar)
summary(anova_results_5sector$CSR_Strat)
summary(anova_results_5sector$Comm)
summary(anova_results_5sector$ESG_Comb)
summary(anova_results_5sector$ESG_Contro)
summary(anova_results_5sector$ESG_Score)
summary(anova_results_5sector$Env_Innov)
summary(anova_results_5sector$Human_Ri)
summary(anova_results_5sector$Management)
summary(anova_results_5sector$Prod_Resp)

I've tried to use a loop :

for(i in 1:length(anova_results_5sector)){
  summary(anova_results_5sector$[i])
}

It didn't work, I dont know and did not find how to deal with $ in for loop

Here you have a look of the structure of the output vector Structure of output

I have tried several times with others methods, more or less complicated. Often the examples found online are too simple and does not allow me to adapt to my data. Any tips ?

Thank you and sorry for such an noobie question

5
  • Try without the $ i.e. anova_results_5sector[[i]] or for (i in anova_results_5sector) { summary(i) } which will loop directly over the elements of your list. Commented Jan 28, 2022 at 10:53
  • Hello Stefan First option : return this error message Error in anova_results_5sector[i] : type 'list' d'indice incorrect. Second option did not print anything =( Commented Jan 28, 2022 at 13:26
  • 1
    Hm Both options work fine. But you are right. We have to wrap inside print() when using a for loop. So. Better option would be lapply(anova_results_5sector, summary) or if you want your results as a tidy dataframe you could do dplyr::bind_rows(lapply(anova_results_5sector, broom::tidy), .id = "var") Commented Jan 28, 2022 at 14:33
  • 1
    Hello Stefan , this exact line of code gave me the output for my 16 anova test lapply(anova_results_5sector, summary). Exactly what I was looking for ! Thank you Commented Feb 1, 2022 at 10:17
  • 1
    And this line of code works perfectly to extract results and display in datafram,. I have no idea i could use ``bind_rows` in this way ( with indented list of my output) dplyr::bind_rows(lapply(anova_results_5sector, broom::tidy), .id = "var") Thank you so much it's perfect, and I'm learning a lot ! Commented Feb 1, 2022 at 10:48

1 Answer 1

1

Whenever I use a loop for an analysis I like to store the results in a data.frame, it allows to keep a good overview. Since you did not provide a reproducible example I used the iris dataset:

data("iris")

#make a data frame to store the results with as many columns and rows as you need
anova_results <- data.frame(matrix(ncol = 3, nrow = 3))
#one column per value you want to store and one row per anova you want to run
x <- c("number", "Mean_Sq", "p_value") #assign all values you want to store as column names
colnames(anova_results) <- x

anova_results$number <- 1:3 #assign numers for each annova you want to run, eg. 3

In the loop you can now extract the results of the anova that you are interested in, I use mean squares and p-value as an example, but you can of course add others. Don't forget to add a coulmn for other values you want to add.

for (i in 2:4){
  my_anova <- aov(iris[[1]] ~ iris[[i]]) 
  p <- summary(my_anova)[[1]][["Pr(>F)"]][1] #extract the p value
  anova_results$p_value[anova_results$number == i-1] <- p
  mean <- summary(my_anova)[[1]][["Mean Sq"]][1] #extract the mean quares
  anova_results$Mean_Sq[anova_results$number == i-1] <- mean
}
View(anova_results)
Sign up to request clarification or add additional context in comments.

4 Comments

I found your answer interesting but I've already performed multiple anova requested : my main problem is to extract p-value for exemple from the weird structure of the output I've just upload on my question. I've tried your code but doen't get same results and I can't change paramaters of the anova at my convenience.
I still cannot reproduce your code entirely, because I don't know the original data structure. But if I understand correctly, you just want to access the output in the lists you already created. So the map() command always creats a list and as visible in the picture you added, the results are stored in the list elements. You can access them by: summary(anova_results_5sector[[1]]) this way you get all the anova statistics you get when using the summary() comand on a regulat single anova.
If you now want to store for example all p-values in a list you can use this code, "Pr(>F)" can be replaced by all statistics that occur in the output: p_values <- list() for (i in 1:16) { x[i] <- summary(anova_results_5sector[[1]])[[1]][["Pr(>F)"]][1] } x
As I mentioned in my question, my output is like indented a lists in lists. (you can check the link to the screenshot that summarize the situation. So this line of code doesnt help in retrieving output for each anova (16 in total) summary(anova_results_5sector[[1]]). But what stefan bring up there seems to work nicely. Thank you very much for your help cucumber, I think your solution with creation of preexistent data frame to store results will surely help in my next analyses !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.