I started doing Linear Regression in RStudio but I'm just not able to figure out how to declare the fitness function required in the GA package that I pretend to use.
It shows the problem is related to the class object of the function, according to the following recurring error:
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
However, I've already tried changing the type of the train_set, with no success. I don't really know if the issue is more related to the proper management of two independent variables used.
Here is what I'm trying:
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)
Any thoughts?
