0

I want to write a code to perform anova where I don't have to change the code every time I change a dataset or when I change the variables for parsing. As my code is it is strict strict there is only one type of dataset. For example; Answer ~ Factor1 + factor2 for condition A but if I change the condition to B, C etc. I would like to initially parameterize to avoid hard codes. How can I make my code more functional and less rigid?

Example

input <- data.frame(
   order = gl(2,50, label = c(paste('area', LETTERS[1:2]))),
   f1 = gl(5,10, label = c(paste('conc', LETTERS[1:5]))),
   f2 = gl(5,2, label = c(paste('plot', LETTERS[1:5]))),
   f3 = factor(rep(paste("cond", 1:2, sep =""), 5)),
   values = abs(rnorm(100))
   )

model <- by(input,input$order, function(x){
   f1 = levels(factor(x$f1))
   f2 = levels(factor(x$f2))
   f3 = levels(factor(x$f3))
   order = levels(factor(x$order))

     for( i in (1: length(f1))){
        for(j in (1: length(order))){

        di <- x[x$f1 == f1[i] & x$order == order[j] ,]

        write(paste('\nf1:', "f1", f1[i],order[j],'\n'), stderr())
        anova.1 <- aov(values ~ f2 * f3, di)

        print(summary(anova.1))
        }
        } 
        write("Analyse Finished! \n", stderr())  
       })
  • Please make your question reproducible by adding the complete code (e.g. curly brackets). See also [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) – help-info.de Dec 18 '19 at 17:35
  • Thank you. As you suggested: input <- data.frame( order = gl(2,50, label = c(paste('area', LETTERS[1:2]))), f1 = gl(5,10, label = c(paste('conc', LETTERS[1:5]))), f2 = gl(5,2, label = c(paste('plot', LETTERS[1:5]))), f3 = factor(rep(paste("cond", 1:2, sep =""), 5)), values = abs(rnorm(100)) ) input – Salatiel Souza Dec 18 '19 at 18:05
  • 1
    It's unclear to me exactly what your question here. Just generally describing "different datasets in different conditions" isn't very specific. What exactly is the problem you are experiencing here? – MrFlick Dec 18 '19 at 18:49
  • I want to write a code to perform anova where I don't have to change the code every time I change a dataset. As my code is, it is rigid for just one type of dataset. For example; **Answer ~ Factor1 + factor2** for condition A but if I change the condition to B, C etc. I would like to initially parameterize to avoid hard codes. – Salatiel Souza Dec 18 '19 at 20:05

1 Answers1

0

For any dynamic element in processing, simply store it as a data point. So, consider saving your different formula directly in the corresponding data frame:

input <- data.frame(
   order = gl(2,50, label = c(paste('area', LETTERS[1:2]))),
   f1 = gl(5,10, label = c(paste('conc', LETTERS[1:5]))),
   f2 = gl(5,2, label = c(paste('plot', LETTERS[1:5]))),
   f3 = factor(rep(paste0("cond", 1:2), 5)),                 # USE paste0 WHEN sep=""
   values = abs(rnorm(100)),
   aov_formula = "values ~ f2 * f3",                         # NEW SCALAR COLUMN
   stringsAsFactors = FALSE                                  # AVOID STRING TO FACTORS
)

input
#    order     f1     f2    f3    values      aov_formula
# 1 area A conc A plot A cond1 0.3673828 values ~ f2 * f3
# 2 area A conc A plot A cond2 1.5170873 values ~ f2 * f3
# 3 area A conc A plot B cond1 1.0677476 values ~ f2 * f3
# 4 area A conc A plot B cond2 1.7239528 values ~ f2 * f3
# 5 area A conc A plot C cond1 0.3247375 values ~ f2 * f3
# 6 area A conc A plot C cond2 0.7483484 values ~ f2 * f3

model <- by(input,input$order, function(x){
     ...
     anova.1 <- aov(as.formula(x$aov_formula[1]), di)        # STRING-FORMULA CONVERSION
     ...
})
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • Thanks for your help @Parfait, I implemented the modifications you suggested. I am still learning how to communicate here on stackoverflow. Thanks for the help – Salatiel Souza Dec 22 '19 at 03:48