R - 克服"未定义的列选择"

时间:2015-02-03 22:53:03

标签: r regression phylogeny

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

我正在使用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")

1 个答案:

答案 0 :(得分:0)

根据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))

它的工作非常精彩。非常感谢,即使它还远未完成,我也会更新我的代码。