如何在 GA 包中正确声明 Function() 以适应健身?

How to properly declare a Function() for fitness in GA package?

我开始在 RStudio 中进行线性回归,但我无法弄清楚如何声明我假装使用的 GA 包中所需的适应度函数。

这表明问题与函数的 class 对象有关,根据以下重复错误:

Error in (function (cl, name, valueClass) : assignment of an object of class “list” is not valid for @‘fitness’ in an object of class “ga”; is(value, "numericOrNA") is not TRUE

但是,我已经尝试更改 train_set 的类型,但没有成功。我真的不知道这个问题是否与使用的两个自变量的正确管理更相关。

这是我正在尝试的:

library(GA)

#from train, which is a list of 4 variables(total columns) and 80 lines
#Let's take the first 4 lines to exemplify

i  j k oil_cumulative_production
12 10 1                    2201.7
15 14 1                    2192.6
13  5 1                    2190.1
 9  4 1                    2185.3
15 11 1                    2193.1

train_set <- data.frame(y1 = train$oil_cumulative_production, x1 = train$i, x2 = train$j)
f <- function(x1, x2) { lmNp <- lm(y1 ~ x1 + x2, data = train_set)}
GA <- ga(type = "real-valued", fitness = function(x) f(x[1], x[2]), lower = c(1, 1), upper = c(20, 20), popSize = 50, maxiter = 100, optim = TRUE)

有什么想法吗?

如果你想最大化'oil_cumulative_production',首先训练你的回归模型,然后通过说线性模型使用x1和x2所做的预测是你想要最大化的来最大化。

代码如下:

library(GA)
#> Loading required package: foreach
#> Loading required package: iterators
#> Package 'GA' version 3.2
#> Type 'citation("GA")' for citing this R package in publications.
#> 
#> Attaching package: 'GA'
#> The following object is masked from 'package:utils':
#> 
#>     de

train <- readr::read_table("i   j    k  oil_cumulative_production
12 10   1                    2201.7
15 14   1                    2192.6
13  5   1                    2190.1
9  4    1                    2185.3
15 11   1                    2193.1")

train_set <- data.frame(y1 = train$oil_cumulative_production, x1 = train$i, x2 = train$j)
lmNp <- lm(y1 ~ x1 + x2, data = train_set)
f <- function(x) {
    df4pred <- data.frame(x1 = x[1], x2 = x[2])
    predict(lmNp, df4pred)
}
GA <- ga(type = "real-valued", fitness = f, 
         lower = c(1, 1), upper = c(20, 20), popSize = 50, maxiter = 100, optim = TRUE)
plot(GA)

GA@solution
#>      x1 x2
#> [1,]  1 20
GA@fitnessValue
#> [1] 2210.604