如何循环解决“下标越界”错误

时间:2019-06-10 19:20:34

标签: r loops if-statement

我正在为我的学士论文编写规范曲线。为了构建一个图表,以显示哪些规格组合显示了重要的结果,哪些不是,我试图创建一个循环以导致R测试所有可能的组合。即使循环很短,是一些错误消息,并且不了解如何解决该问题。

首先,我创建了变量来定义可能的规范:

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")

data.frame“规范”定义如下:

specifications <- expand.grid(outlier = outlier, ineligble = ineligble, gender = gender, regression = regression,
                              control = control)
specifications <- data.frame(specifications, p_value = rep(NA, nrow(specifications)), f_value = rep(NA, nrow(specifications)), 
                             partial_eta_square = rep(NA, nrow(specifications)), r = rep(NA, nrow(specifications)))

如您所见,我从可能的组合中构建了一个数据框,并添加了p值,f值,部分eta平方和效果大小的列。为了用值填充额外的列,我在循环的末尾添加了所需的命令。 循环如下所示:

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)
  }
}  

所以我想要做的是用p值,f值等填充四个额外的列。但是我收到以下行的错误消息“下标越界”:

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

我知道错误消息的意思,但我不知道如何解决。在循环外的随机方差测试中测试相同的命令时,它将起作用。

所使用的数据可以通过以下链接找到,称为ccy-source-data: https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/FMEGS6

1 个答案:

答案 0 :(得分:0)

[[3]]表示您正在获取方差分析表第三行上的信息。没有任何控件的模型的方差分析表只有两行。

如果您是对变量posecondition的结果感兴趣,那么通过行名直接为该变量的相关信息子集要比假定其始终位于位置3更为安全。

以下是p值和F值的处理方法示例:

results <- drop1(anova, test = "F")

specifications[i, c("p_value", "F_value")] <- 
  results["posecondition", c("Pr(>F)", "F value")]