lundi 10 juin 2019

How to fix ´Subscript out of bounds` error in a loop

I am programming a specification curve for my bachelor thesis. In order to build a graph which is showing which combinations of specifications is showing a significant result and which aren't I'm trying to create a loop which leads to R testing all the possible combinations.Even though the loop is quite short, there are some error messages and don't understand how to solve the problem.

First of all I created variables to define the possible specifications:

outlier <- c("none", "40", "33,40,63", "33,40,54,63")
ineligble <- c("28", "28,38,45,48,56")
gender <- c("together", "male", "female")
regression <- c("ANOVA")
control <- c("none", "premanipulation_mean", "all hormones", "all")

Then I builded a data frame out of the possible combinations and added columns for the p value, f value, partial eta square and effect size. In order to fill the extra columns with values I added the needed commands at the end of the loop. The Loop looks as follows:

for(i in 1:nrow(specifications)){
  dat <- ccy
  if (specifications$outlier[i] == "none") {
    dat <- dat
  } else {
    if (specifications$outlier[i] == "40") {
      dat <- dat[-11,]
    } else {
      if (specifications$outlier[i] == "33,40,63") {
        dat <- dat[-c(5,11,31),]
      } else {
        if (specifications$outlier[i] == "33,40,54,63") {
          dat <- dat[-c(5,11,23,31),]
        }
      }
    }
  }
  if(specifications$ineligble == "28") {
    dat <- ccySC
  } else {
    if(specifications$ineligble == "28,38,45,48,56") {
      dat <- ccy
    }
  }
  if (specifications$gender == "together") {
    dat <- dat
  } else {
    if(specifications$gender == "male"){
      dat <- dat[which(dat$gender == "male"),]
    } else {
      if(specifications$gender == "female") {
        dat <- dat[which(dat$gender == "female"),]
      }
    }
  }
  if (specifications$regression == "ANOVA") {
    if (specifications$control == "none") {
      anova <- aov(T_time2_mean ~ posecondition, data = dat)
    } else {
      if (specifications$control == "premanipulation_mean") {
        anova <- aov(T_time2_mean ~ T_time1_mean + posecondition, data = dat)
      } else {
        if(specifications$control == "all hormones") {
          anova <- aov(T_time2_mean ~ T_time1_mean + posecondition + C_time1_mean + C_time2_mean)
        } else {
          if (specifications$control == "all") {
            anova <- aov(T_time2_mean ~ T_time1_mean + posecondition + C_time1_mean + C_time2_mean + sex)
          }
        }
      }
    }
    specifications$p_value[i] <- drop1(anova, test = "F")$"Pr(>F)"[[3]]
    specifications$f_value[i] <- drop1(anova, test = "F")$"F value"[[3]]
    specifications$partial_eta_square[i] <- etaSquared(anova, type = 2, anova = F)$"eta.sq.part"[[2]]
    specifications$r[i] <- sqrt(specifications$partial_eta_square[i])
    specifications$k[i] <- nrow(specifications)
  }
}  

So what I want it to do is fill up the four extra columns with the p value, the f value etc. But I am receiving the error message "Subscript out of bounds" for the following line:

specifications$p_value[i] <- drop1(anova, test = "F")$"Pr(>F)"[[3]]

I know what the error message means but I don't know how to solve it. When testing the same command on a random anova outside of the loop it works.

The data used can be found through the following link and is called ccy-source-data: https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/FMEGS6

Aucun commentaire:

Enregistrer un commentaire