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.
}
}
}
}
}