R:找不到用户定义的函数(嵌套设计)

时间:2015-02-07 18:37:16

标签: r function

请记住,这只是我第二天编写R代码而不是使用它,而且我几乎肯定会超出我的水平。我的很多代码可能效率低下。

我正在尝试编写R代码,这将使我的大多数回归分析自动化,同时仍然允许根据预测变量,数据转换和模型假设进行手动微调。我总是得到错误:

Error: could not find function "Dat.assumptn"

有关使嵌套功能设计正常工作的一般建议表示赞赏。另外,有人可以在R上发布一些写得很好的函数来解决一系列难度吗?

至于我的其他问题,例如通过CRAN的R.oo或来自R: Pass by reference的源程序实现传递引用行为,我想我可以弄清楚。这是我的代码的一部分(不完整,需要重写):

Dat.assumptn <- function(f, final, caperDS, picanDS) {
  print(f)
  crunchMod <- crunch(f, data = contrasts)
  print(caic.table(crunchMod))
  print(caic.diagnostics(crunchMod))
  print(summary(crunchMod))

  #If independent contrasts assumptions fail, return me to the second for loop
  #within Dat.analysis() [Not Yet Implemented]

  #Implement code to reduce and check the model (whether final = true/false)

  if (final == TRUE) {
    retry <- Dat.Msucess(crunchMod)
    #The above function will recommend additional transformations if the final 
    #reduced model significantly violated model assumptions.
  }
}

Dat.analysis <- function() {
  treList <- dir(pattern="*.tre") //All of my phylogenetic tree files
  caperDS <- read.table("dataSet.txt", header = TRUE)
  picanDS <- read.table("dataSet.txt", row.names = 1, header = TRUE)
      #Dat.assumptn() requires a different format from Dat.analysis()

  #The loop below changes the names from my data set to be proper variable names
  for (i in 1:length(names(picanDS))) {
    varName <- gsub("_|[0-9]|\\.", "", names(picanDS)[i])
    names(caperDS)[i+1] <- varName
    names(picanDS)[i]   <- varName

    caperDS[,paste(varName,"2",sep="")] <- caperDS[i+1]*caperDS[i+1]
  }

  #Implement a for loop to transform the data based upon specifications from both
  #Dat.assumptn() [called from Dat.analysis] and Dat.Msuccess [called from Dat.assumptn].
  #Likely using pass by reference.

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

        contrasts <- comparative.data(myTrees[[j]], caperDS, Species)
        if (names(caperDS)[3] == "MedF" || names(caperDS)[3] == "MaxF") {
          final <- FALSE
          f <- as.formula(paste(paste(names(caperDS)[2],"~"),
               paste(paste(paste("(",paste(names(caperDS)[4:(ncol(picanDS)+1)], collapse="+"))),")^2"),
               paste("+", paste(names(caperDS)[(ncol(picanDS)+4):ncol(caperDS)], collapse = "+"))))
          while (final == FALSE) {
            f <- Dat.assumptn(f, final, caperDS, picanDS)
              #Pass final by reference, and set to true if the final reduced model
              #is achieved. Otherwise, iterate to reduce the model.
          }

          final <- FALSE
          f <- as.formula(paste(paste(names(caperDS)[3],"~"),
               paste(paste(paste("(",paste(names(caperDS)[4:(ncol(picanDS)+1)], collapse="+"))),")^2"),
               paste("+", paste(names(caperDS)[(ncol(picanDS)+4):ncol(caperDS)], collapse = "+"))))
          while (final == FALSE) {
            f <- Dat.assumptn(f, final, caperDS, picanDS)
                #Pass final by reference, and set to true if the final reduced model
                #is achieved. Otherwise, iterate to reduce the model.
          }

        } else {
          final <- FALSE
          f <- as.formula(paste(paste(names(caperDS)[2],"~"),
               paste(paste(paste("(",paste(names(caperDS)[3:(ncol(picanDS)+1)], collapse="+"))),")^2"),
               paste("+", paste(names(caperDS)[(ncol(picanDS)+3):ncol(caperDS)], collapse = "+"))))
          while (final == FALSE) {
            f <- Dat.assumptn(f, final, caperDS, picanDS)
                #Pass final by reference, and set to true if the final reduced model
                #is achieved. Otherwise, iterate to reduce the model.
          }     
        }
    }
  }
}

0 个答案:

没有答案