[R] change the for loops with lapply

David Winsemius dwinsemius at comcast.net
Wed Sep 8 00:35:06 CEST 2010


On Sep 7, 2010, at 5:43 PM, Changbin Du wrote:

> cv.fold<-function(i, size=3, rang=0.3){
>       cat('Fold ', i, '\n')
>       out.fold.c <-((i-1)*c.each.part +1):(i*c.each.part)
>       out.fold.n <-((i-1)*n.each.part +1):(i*n.each.part)
>
>      train.cv <- n.cc[-out.fold.c, c(2:2401, 2417)]
>       train.nv <- n.nn[-out.fold.n, c(2:2401, 2417)]
>
>       train.v<-rbind(train.cv, train.nv) #training data for feature
> selection
>
>    # grow tree
> fit.dimer <- rpart(as.factor(out) ~ ., method="class", data=train.v)
> at<-grep("<leaf>", fit.dimer$frame[, "var"], value=FALSE,  
> ignore.case=TRUE)
> varr<-as.character(unique(fit.dimer$frame[-at, "var"]))
>
>       train.cc <- n.cc[-out.fold.c,]
>       valid.cc <- n.cc[out.fold.c,]
>
>       train.nn <- n.nn[-out.fold.n,]
>       valid.nn <- n.nn[out.fold.n,]
>
>       train<-rbind(train.cc, train.nn) #training data
>       valid<-rbind(valid.cc, valid.nn) # validation data
>
> #creat data set contains the following variables
> myvar<-names(gh9_h) %in% c(varr, "out")
>
>       train<-train[myvar] # update training set
>       valid<-valid[myvar]
>
> nnet.fit<-nnet(as.factor(out) ~ ., data=train,  size=size, rang=rang,
> decay=5e-4, maxit=500)  # model fitting
>
>       #get the validation error
>     mc<-table(valid$out, predict(nnet.fit, valid, type="class"))  
> #confusion
> matrix
>
>       fp<-mc[1,2]/sum(mc[1,]) #false positive
>       fn<- mc[2,1]/sum(mc[2,]) #false negative
>      accuracy.r<-1-(mc[1,2]+mc[2,1])/sum(mc) #total accuracy rate
>
> return(c(fp, fn, accuracy.r))
>
>                               }
>
> result.fun <- lapply(1:2, cv.fold(i, size=5, rang=0.3))
>
> I got the following error message:
>
> *Error in match.fun(FUN) :
>  'cv.fold(i, size = 5, rang = 0.3)' is not a function, character or  
> symbol

Generally when one is passing an atomic vector argument to a function  
one would use sapply (but it may be a distinction withou a difference  
here.) ... and furthermore the additional arguments would be given as  
named constants:

?sapply

Perhaps (untested):

  result.fun <- sapply(1:2, cv.fold, size=5, rang=0.3))

or perhaps:

result.fun <- sapply(1:2, function(i) cv.fold(i, size=5, rang=0.3))


As always the provision of a working example, perhaps even from one of  
the help pages, would allow testing, and it's always good manners to  
specify which package has non-base functions:

 > ?n.cc
No documentation for 'n.cc' in specified packages and libraries:
you could try '??n.cc'
 > ?rpart
No documentation for 'rpart' in specified packages and libraries:
you could try '??rpart'
 > ?train
No documentation for 'train' in specified packages and libraries:
you could try '??train'

(I have suspicions which packages they come from, but one never  
knows....)

-- 
David.

>
>
> I do want to change the size and rang parameters some time.
>
> *
> Can anyone help me this this?  Thanks so much!
>
>
> -- 
> Sincerely,
> Changbin


David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list