问题描述
我开始在RStudio中进行线性回归,但是我无法弄清楚如何声明我假装使用的GA软件包中所需的适应度函数。
(函数(cl,name,valueClass)中的错误: 对于“ ga”类的对象,“ list”类的对象的分配对@“ fitness”无效; is(value,“ numericOrNA”)不为真
但是,我已经尝试更改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