0

I'm puzzled by a function error & would appreciate some insight.

The function, very briefly, automates the multiple processes involved in Boosted Regression Trees using gbm.step & other gbm's.

"gbm.auto" <- function (grids, samples, 3 parameters) {

starts 2 counters, require(gbm), does various small processing jobs with grids & samples

 for parameter 1{
  for parameter 2{
   for parameter 3{

Runs 2 BRTs per parameter-combination loop, generates & iteratively updates a 'best' BRT for each, adds to counters. Extensive use of samples.

}}}

closes the loops, function continues as the first } is still open. The next BRT can't find samples, even though it's at the same environment depth (1?) as the pre-loop processing jobs which used it successfully. Furthermore, adding "globalsamples<<-samples" after the }}} loop successfully saves the object, suggesting that samples is still available. Adding env1,2 & 3<<-environment() before the {{{ loop, within it & after it results in Environment for all three. Also suggesting it's all the same function environment & samples should be available.

What am I missing here? Thanks in advance!

Edit: exact message:

Error in eval(expr, envir, enclos) : object 'samples' not found

Function - loads removed & compacted but still gives same error message:

"gbm.auto" <-
  function (samples, expvar, resvar, tc, lr, bf)
    { # open function

    require(gbm)
    require(dismo)

    # create binary (0/1) response variable, for bernoulli BRTs
samples$brv <- ifelse(samples[resvar] > 0, 1, 0)
brvcol <- which(colnames(samples)=="brv") # brv column number for BRT

for(j in tc){   # permutations of tree complexity
 for(k in lr){   # permutations of learning rate
  for(l in bf){   # permutations of bag fraction

Bin_Best_Model<- gbm.step(data=samples,gbm.x = expvar, gbm.y = brvcol, family = "bernoulli", tree.complexity = j, learning.rate = k, bag.fraction = l)
}}}        # close loops, producing all BRT/GBM objects & continue through model selection

Bin_Best_Simp_Check <- gbm.simplify(Bin_Best_Model) # simplify model

# if best number of variables to remove isn't 0 (i.e. it's worth simplifying), re-run the best model (Bin_Best_Model, using gbm.call to get its values)
# with just-calculated best number of variables to remove, removed. gbm.x asks which number of drops has the minimum mean (lowest point on the line)
# & that calls up the list of predictor variables with those removed, from $pred.list
if(min(Bin_Best_Simp_Check$deviance.summary$mean) < 0)
  assign("Bin_Best_Simp", gbm.step(data = samples,
                                 gbm.x = Bin_Best_Simp_Check$pred.list[[which.min(Bin_Best_Simp_Check$deviance.summary$mean)]],
                                 gbm.y = brvcol, family = "bernoulli", tree.complexity = j, learning.rate = k, bag.fraction = l))
}

Read in data:

mysamples<-data.frame(response=round(sqrt(rnorm(5000, mean= 2.5, sd=1.5)^2)), depth=sqrt(rnorm(5000, mean= 35, sd=24)^2), temp=rnorm(5000, mean= 15, sd=1.2), sal=rnorm(5000, mean= 34, sd=0.34))

Run this: gbm.auto(expvar=c(2,3,4),resvar=1,samples=mysamples,tc=2,lr=0.00000000000000001,bf=0.5)

Problem now: this causes a different error because my fake data are somehow wrong. ARGHG! Edit: rounded the response data to integers and kept shrinking the learning rate until it runs. If it doesn't work for you, add zeroes until it does. Edit: so this worked on my computer but reading it back to a clean sheet from online fails on a DIFFERENT count:

Error in var(cv.cor.stats, use = "complete.obs") : 
  no complete element pairs 
    In cor(y_i, u_i) : the standard deviation is zero

Is it allowed to attach or link to a csv of a small clip of my data? I'm currently burrowing deeper & deeper into bugfixing problems created by using fake data which I'm only using for this question, & thus getting off topic from the actual problem. Exasperation mode on!

Cheers

Edit2: if this is allowed: 1000row 4column csv link here: https://drive.google.com/file/d/0B6LsdZetdypkaC1WYXpKU3ZScjQ

dez93_2000
  • 1,730
  • 2
  • 23
  • 34
  • You'll need to create a minimal [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that we can run to see that the problem is. Your description isn't precise enough to be clear about what's going on. – MrFlick Aug 25 '14 at 18:22
  • And please post the *exact* error message you're getting. – Carl Witthoft Aug 25 '14 at 19:08
  • thanks guys, will do tomorrow morning. – dez93_2000 Aug 25 '14 at 22:17

0 Answers0