1

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

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

具体来说,我使用的是“caper”和“picante”包,我的注释代码如下(未完成)。有人可以就我做错的事情提供建议吗?我今天刚开始学习 R,但熟悉 java/python/windows batch 等。

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

1 回答 1

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

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

于 2015-02-04T03:35:35.173 回答