I am working with the R programming language.
Suppose I have some data:
#load libraries
library(dplyr)
# create some data for this example
a1 = rnorm(1000,100,10)
b1 = rnorm(1000,100,5)
c1 = sample.int(1000, 1000, replace = TRUE)
train_data = data.frame(a1,b1,c1)
And I have 2 functions ("f1" and "f2") that I am trying to jointly optimize:
#first function
f1 <- function(x) {
#bin data according to random criteria
train_data <- train_data %>%
mutate(cat = ifelse(a1 <= x[1] & b1 <= x[3], "a",
ifelse(a1 <= x[2] & b1 <= x[4], "b", "c")))
train_data$cat = as.factor(train_data$cat)
#new splits
a_table = train_data %>%
filter(cat == "a") %>%
select(a1, b1, c1, cat)
#calculate quantile ("quant") for each bin
table_a = data.frame(a_table%>% group_by(cat) %>%
mutate(quant = ifelse(c1 > x[5],1,0 )))
obj_a = mean(table_a$quant)
}
# second function
f2 <- function(x) {
#bin data according to random criteria
train_data <- train_data %>%
mutate(cat = ifelse(a1 <= x[1] & b1 <= x[3], "a",
ifelse(a1 <= x[2] & b1 <= x[4], "b", "c")))
train_data$cat = as.factor(train_data$cat)
#new splits
b_table = train_data %>%
filter(cat == "b") %>%
select(a1, b1, c1, cat)
#calculate quantile ("quant") for each bin
table_b = data.frame(b_table%>% group_by(cat) %>%
mutate(quant = ifelse(c1 > x[6],1,0 )))
obj_b = mean(table_b$quant)
}
Using the "optim" function, I know how to individually optimize both of these functions:
#optimize the first function:
optim(c(80,85,100,120,90,140,120), f1, method = "Nelder-Mead")
#optimize the second function:
optim(c(80,85,100,120,90,140,120), f1, method = "Nelder-Mead")
But is there a way to optimize both of these functions together? I know that there is a library called "mco" (CRAN - Package mco) that is made for optimizing multiple functions together - but can this also be done somehow using the built-in "optim" function in R?
Thanks