R - 克服 "Undefined Columns Selected"

R - Overcoming "Undefined Columns Selected"

编辑:现在回答,找到下面更新的代码:

我正在使用 R 将一组系统发育树迭代到具有不同数量预测变量的各种数据集上。因此,我无法明确定义我的回归函数(仍在努力实现正确的实现)。

具体来说,我正在使用 "caper" 和 "picante" 包,我的注释代码如下(未完成)。有人会对我做错了什么提出建议吗?我今天刚开始学习 R,但熟悉 java/python/windows 批处理等

crunchMod <- crunch(names(kap)[2] ~ ., data=contrasts)

我的错误发生在上面的代码行。

library(picante)
library(gsubfn)
library(caper)
library(ape)

#This function works off the ordering of files on windows
#because I add .treLog.tre to the file name so 1&2 are the
#same tree and 3&4 are the same tree.

setwd("C:/mywork2/MED/Male_Only99")

Dat.analysis <- function() {
  treList <- dir(pattern="*.tre")
  caperDS <- read.table("dataSet.txt", header = TRUE)
  picanDS <- read.table("dataSet.txt", row.names = 1, header = TRUE)

  for (i in 1:length(names(picanDS))) {
    varName <- gsub("_|[0-9]|\.", "", names(picanDS)[i])
    names(caperDS)[i+1] <- varName
    names(picanDS)[i]   <- varName
  }

  for (i in 1:1) { #length(treList)
    myTrees = read.nexus(treList[i])
    for (j in 1:1) { #length(myTrees)
    cat(paste("\n\n", treList[i]))
    print(multiPhylosignal(picanDS, myTrees[[j]]))

    contrasts <- comparative.data(myTrees[[i]], caperDS, Species)
    if (caperDS[3] == "MEDF" || caperDS[3] == "MAXF") {
      f <- as.formula(parse(paste(names(caperDS)[2],"~"), paste(names(caperDS)[4:ncol(caperDS)], collapse="+")))
      crunchMod <- crunch(f, data = contrasts)
      print(summary(crunchMod))

      f <- as.formula(paste(paste(names(caperDS)[3],"~"), paste(names(caperDS)[4:ncol(caperDS)], collapse="+")))
      crunchMod <- crunch(f, data = contrasts)
      print(summary(crunchMod))
    } else {
      f <- as.formula(paste(paste(names(caperDS)[2],"~"), paste(names(caperDS)[4:ncol(caperDS)], collapse="+")))
      crunchMod <- crunch(f, data = contrasts)
      print(summary(crunchMod))
    }
    }
  }
}

folders <- c("C:/mywork2/MAX","C:/mywork2/MED")
for (i in 1:length(folders)) {
  paths <- list.dirs(path = folders[i], full.names = TRUE, recursive = TRUE)
  for (j in 1:length(paths)) {
    if (!(paths[j] == folders[i])) {
      setwd(paths[j])
      contrasts <- Dat.analysis()
    }
  } 
}

print("finished")

根据 jraab 的建议,我目前将我的 crunchMod 变量编码为:

f <- as.formula(paste(paste(names(caperDS)[2],"~"), paste(names(caperDS)[4:ncol(caperDS)], collapse="+")))
crunchMod <- crunch(f, data = contrasts)
print(summary(crunchMod))

而且效果很好。非常感谢,我会更新上面的代码,尽管它还远未完成。