Rilcon42
Rilcon42

Reputation: 9763

Find the parameters provided to an unknown vectorized function

I am trying to create an scoring function (called evalFunc). To get a score I am trying to calculate the R-squared value of a generated model. How do I know how the values are being passed to 'evalFunc' from within the rbga.bin function?

library(genalg)
library(ggplot2)
set.seed(1)
df_factored<-data.frame(colA=runif(30),colB=runif(30),colC=runif(30),colD=runif(30))
dataset <- colnames(df_factored)[2:length(df_factored)]
chromosome = sample(c(0,1),length(dataset),replace=T)
#dataset[chromosome == 1]

evalFunc <- function(x) {
  #My end goal is to have the values passed into evalFunc be evaluated as the IV's in a linear model
  res<-summary(lm(as.numeric(colA)~x,
                  data=df_factored))$r.squared 
  return(res)
}

iter = 10
GAmodel <- rbga.bin(size = 2, popSize = 200, iters = iter, mutationChance = 0.01, elitism = T, evalFunc = evalFunc)
cat(summary(GAmodel))

Upvotes: 2

Views: 37

Answers (1)

Sam Dickson
Sam Dickson

Reputation: 5239

You can view the source by typing rbga.bin, but better than that you can run debug(rbga.bin), then the next time you call that function, it allows you to step through the function. In this case the first time you get to your function is in this line (approximately line 82 of the function):

                  evalVals[object] = evalFunc(population[object,
                    ]) 

At this point, population is 200x2 matrix consisting of 0s and 1s:

head(population)
# [1,]    0    1
# [2,]    0    1
# [3,]    0    1
# [4,]    1    0
# [5,]    0    1
# [6,]    1    0

And object is the number 1, so population[object,] is the vector c(0,1).

When you've finished with debug you can undebug(rbga.bin) and it won't go into debug mode every time you call rbga.bin.

Upvotes: 1

Related Questions