R 查找提供给未知矢量化函数的参数
我正在尝试创建一个评分函数(称为R 查找提供给未知矢量化函数的参数,r,R,我正在尝试创建一个评分函数(称为evalFunc)。为了得到分数,我试图计算生成模型的R平方值。我如何知道值是如何从rbga.bin函数中传递到“evalFunc”的 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_factore
evalFunc
)。为了得到分数,我试图计算生成模型的R平方值。我如何知道值是如何从rbga.bin
函数中传递到“evalFunc”的
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))
库(genalg)
图书馆(GG2)
种子(1)
df_factored您可以通过键入rbga.bin
查看源代码,但比运行debug(rbga.bin)
更好的是,下次调用该函数时,它允许您逐步执行该函数。在这种情况下,您第一次使用函数是在这一行(大约是函数的第82行):
此时,总体
是200x2矩阵,由0和1组成:
head(population)
# [1,] 0 1
# [2,] 0 1
# [3,] 0 1
# [4,] 1 0
# [5,] 0 1
# [6,] 1 0
而object
是数字1,因此population[object,]
是向量c(0,1)
当您完成debug
时,您可以undebug(rbga.bin)
并且它不会在每次调用rbga.bin
时都进入调试模式,但是在您的示例中,x将是长度为2的向量,带0或1?你想用x来选择回归中使用的是colB、colC还是colD?是的,没错。我假设参数是colnames,但根据您的评论,它们不是
head(population)
# [1,] 0 1
# [2,] 0 1
# [3,] 0 1
# [4,] 1 0
# [5,] 0 1
# [6,] 1 0