0

Please bear in mind that this is only my second day writing R code instead of using it, and I'm taking on a project almost surely above my level. A lot of my code is probably inefficient.

I'm trying to write R code which will automate the majority of my multiple regression analysis, while still allowing manual fine tuning in terms of the # of predictors, data transformations and model assumptions. I always get the error:

Error: could not find function "Dat.assumptn"

General advice on getting this nested function design to work is appreciated. Also, could someone please post a few well written links on functions in R which cover a range of difficulty?

As for my other issues, such as implementing pass by reference behavior via a package like R.oo from CRAN or a source from R: Pass by reference, I think I can figure it out. Here is a part of my code (incomplete and needs rewriting):

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.
          }     
        }
    }
  }
}
Community
  • 1
  • 1
user3834916
  • 143
  • 1
  • 11
  • are you running dat.assumptn before dat.analysis? It would be more useful to you to use a debugger or `browser` rather than us sift through your code. Or make the code self-contained and the error reproducible – rawr Feb 07 '15 at 18:47
  • I am not. My main method runs Dat.analysis(), and then Dat.Analysis() calls Dat.assumptn(). Also, if I put Dat.assumptn inside Dat.analysis, I still get the same error. – user3834916 Feb 07 '15 at 18:49
  • the functions used within your functions have to exist in your workspace before they can be used – rawr Feb 07 '15 at 18:49
  • If you could edit your comment telling me how to do that, I would appreciate it. Do I just call Dat.assumptn() before using it (i.e., along with Dat.analysis) to get it in my work space? However, I can only implement Dat.assumptn() after creating the caic object under crunchMod which is created within Dat.analysis() – user3834916 Feb 07 '15 at 18:52
  • just select the code and run it. if it works, you will have two objects in your workspace called dat.assumptn and dat.analysis (`ls()` to see what is in the environment), then you can run each individually with `Dat.assumptn()` or `Dat.analysis()` – rawr Feb 07 '15 at 18:56
  • Ah it worked, thank you! I don't get the error any more. Is there any way that I can have R put the function in my work space automatically however? – user3834916 Feb 07 '15 at 19:00
  • you could put those two functions into a separate script and `source` it whenever you want, but it would be better to [make a package](http://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/) – rawr Feb 07 '15 at 19:02
  • Ok, so I'm getting a comment length warning. Thank you for all the help rawr. I'll read up how to do packages and see if it's similar to java, C++, etc. I'll edit my question and post an answer once I get everything sorted out. It may be a little while though. – user3834916 Feb 07 '15 at 19:07
  • You can also read ?Startup and add code to `.profile` or `.profile.site` – IRTFM Feb 07 '15 at 20:15

0 Answers0