[R] argument "x" is missing, with no default - Please help find argument x

dunner ross.dunne at tcd.ie
Fri Jun 15 22:27:54 CEST 2012


R programming question, not machine learning, although that's the content.

Apologies to all for whom the following code is eye-burning. I am using
foreach() to run a simulation on a randomForest model (actually conditional
randomForest ... "party" package). The simulation is in two dimensions.
examining how "mtry" and "ntrees" are related in terms of predictive
accuracy in ten-fold cross validation.
My problem is one of functional programming. The "loops" for simulation are
functionalised so they can be passed to foreach and bundled of to my 4
cores. However, I'm making a mess of passing in arguments - I think. I get
the following error message :

Error in toploop(ntrees = ntrees, ml.frame = ml.frame) : 
  task 2 failed - "argument "x" is missing, with no default"

as if it couldn't see the arguments.

Long segment of actual code below.

Thank you for your time and patience.

Ross

<code>

bottomloop<-function(i=i, mtry=mtry, rownumber=rownumber, ntrees=ntrees,
shuffled=shuffled) {
  require(party)
  require(epiR)
  test<-rownumber[[i]]
  train<-shuffled[shuffled!=test]
  cv.train <- ml.frame[train,]
  cv.evaluate <- ml.frame[test,]
  cv.model <- cforest(as.factor(remitter)~., data=cv.train,
control=cforest_control(mtry=mtry, ntree=ntrees, mincriterion=0.95))
  cv.evaluate$prediction <- predict(cv.model, newdata=cv.evaluate)
  obj.pred<-predict(cv.model)
  pred.table<-table(cv.evaluate$prediction,  cv.evaluate$remitter)
  
  truepos<-pred.table[1,1]
  trueneg<-pred.table[2,2]
  falsepos<-pred.table[2,1]
  falseneg<-pred.table[1,2]
  sumvar<-epi.tests(c(truepos,  falsepos , falseneg, trueneg), verbose=T)
  epi.tests(c(truepos,  falsepos , falseneg, trueneg), verbose=F)
  # Calculate the overall accuracy.
  cv.evaluate$correct <- cv.evaluate$prediction == cv.evaluate$remitter
  mymean<-mean(cv.evaluate$correct)
  retlist<-data.frame(mtry, sumvar$se, sumvar$sp, mean(cv.evaluate$correct))
  return(retlist)
}

subloop<- function(mtry=mtry, ml.frame=ml.frame, ntrees=ntrees) {
  nfolds<- 10
  # shuffle the numbers and divide into 10 groups
  numberOfRows<-dim(ml.frame)[1]
  lengthOfDiv<-numberOfRows/nfolds
  shuffled<-sample(c(1:numberOfRows), numberOfRows, replace=F)
  rownumber<-split(shuffled, 1:nfolds)
  #combine mymean into my vec
  myvec<-foreach (i = 1:length(rownumber), .combine="rbind") %dopar%
bottomloop(i, mtry = mtry, rownumber=rownumber, ntrees=ntrees,
shuffled=shuffled)
  #print(myvec)
  return(myvec)
}

toploop <- function (ntrees=ntrees, ml.frame=ml.frame) {
  require(foreach)
  require(ggplot2)
  require(reshape)
  require(foreign)
  mtry = c(5:25)
  ddd<-foreach(mtry, .combine="rbind")  %dopar% subloop (mtry=mtry,
ml.frame=ml.frame, ntrees=10)
  ddd<-as.data.frame(ddd)
  mdd<-melt(ddd, id="mtry")
  g <- ggplot(mdd, aes(mtry, value))
  pdf(file=paste(ntrees, "-trees.pdf"))
  g + geom_point(aes(colour=as.factor(variable))) + 
    geom_smooth(aes(group=as.factor(mdd$variable),
colour=as.factor(mdd$variable)), size=1.2)
  dev.off()
  write.csv(file=paste(ntrees, "-trees.csv"))
}     

ntrees=c(1,2,3)
foreach(ntrees, .combine="cbind" )  %dopar% toploop (ntrees=ntrees,
ml.frame=ml.frame)



> foreach(ntrees, .combine="cbind" )  %dopar% toploop (ntrees=ntrees,
> ml.frame=ml.frame)

bottomloop<-function(i=i, mtry=mtry, rownumber=rownumber, ntrees=ntrees,
shuffled=shuffled) {
  require(party)
  require(epiR)
  test<-rownumber[[i]]
  train<-shuffled[shuffled!=test]
  cv.train <- ml.frame[train,]
  cv.evaluate <- ml.frame[test,]
  cv.model <- cforest(as.factor(remitter)~., data=cv.train,
control=cforest_control(mtry=mtry, ntree=ntrees, mincriterion=0.95))
  cv.evaluate$prediction <- predict(cv.model, newdata=cv.evaluate)
  obj.pred<-predict(cv.model)
  pred.table<-table(cv.evaluate$prediction,  cv.evaluate$remitter)
  
  truepos<-pred.table[1,1]
  trueneg<-pred.table[2,2]
  falsepos<-pred.table[2,1]
  falseneg<-pred.table[1,2]
  sumvar<-epi.tests(c(truepos,  falsepos , falseneg, trueneg), verbose=T)
  epi.tests(c(truepos,  falsepos , falseneg, trueneg), verbose=F)
  # Calculate the overall accuracy.
  cv.evaluate$correct <- cv.evaluate$prediction == cv.evaluate$remitter
  mymean<-mean(cv.evaluate$correct)
  retlist<-data.frame(mtry, sumvar$se, sumvar$sp, mean(cv.evaluate$correct))
  return(retlist)
}

subloop<- function(mtry=mtry, ml.frame=ml.frame, ntrees=ntrees) {
  nfolds<- 10
  # shuffle the numbers and divide into 10 groups
  numberOfRows<-dim(ml.frame)[1]
  lengthOfDiv<-numberOfRows/nfolds
  shuffled<-sample(c(1:numberOfRows), numberOfRows, replace=F)
  rownumber<-split(shuffled, 1:nfolds)
  #combine mymean into my vec
  myvec<-foreach (i = 1:length(rownumber), .combine="rbind") %dopar%
bottomloop(i, mtry = mtry, rownumber=rownumber, ntrees=ntrees,
shuffled=shuffled)
  #print(myvec)
  return(myvec)
}

toploop <- function (ntrees=ntrees, ml.frame=ml.frame) {
  require(foreach)
  require(ggplot2)
  require(reshape)
  require(foreign)
  mtry = c(5:25)
  ddd<-foreach(mtry, .combine="rbind")  %dopar% subloop (mtry=mtry,
ml.frame=ml.frame, ntrees=100)
  ddd<-as.data.frame(ddd)
  mdd<-melt(ddd, id="mtry")
  g <- ggplot(mdd, aes(mtry, value))
  pdf(file=paste(ntrees, "-trees.pdf"))
  g + geom_point(aes(colour=as.factor(variable))) + 
    geom_smooth(aes(group=as.factor(mdd$variable),
colour=as.factor(mdd$variable)), size=1.2)
  dev.off()
  write.csv(file=paste(ntrees, "-trees.csv"))
}     

ntrees=c(1,2,3)
foreach(ntrees, .combine="cbind" )  %dopar% toploop (ntrees=ntrees,
ml.frame=ml.frame)

### following error ###

> foreach(ntrees, .combine="cbind" )  %dopar% toploop (ntrees=ntrees,
> ml.frame=ml.frame)
Error in toploop(ntrees = ntrees, ml.frame = ml.frame) : 
  task 2 failed - "argument "x" is missing, with no default"

</code>


--
View this message in context: http://r.789695.n4.nabble.com/argument-x-is-missing-with-no-default-Please-help-find-argument-x-tp4633551.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list