I am working with R. I am learning about optimization and trying to follow the instructions from the following references: psoptim function - RDocumentation and RPubs - Introduction to Particle Swarm Optimization

For this example, I first generate some random 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)
```

From here, I define the function that I want to optimize ("fitness"). This function takes 7 inputs and calculates a "total" mean (a single scalar value). The inputs required for this function are:

- "random_1" (between 80 and 120)
- "random_2" (between "random_1" and 120)
- "random_3" (between 85 and 120)
- "random_4" (between random_2 and 120)
- "split_1" (between 0 and 1)
- "split_2" (between 0 and 1)
- split_3" (between 0 and 1 )

The function to optimize ("fitness") is defined as follows (takes these 7 numbers and returns a single scalar "total" - the goal is to get the largest value of "total" for these 7 numbers):

```
fitness <- function(random_1, random_2, random_3, random_4, split_1, split_2, split_3) {
#bin data according to random criteria
train_data <- train_data %>% mutate(cat = ifelse(a1 <= random_1 & b1 <= random_3, "a", ifelse(a1 <= random_2 & b1 <= random_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)
b_table = train_data %>%
filter(cat == "b") %>%
select(a1, b1, c1, cat)
c_table = train_data %>%
filter(cat == "c") %>%
select(a1, b1, c1, cat)
#calculate quantile ("quant") for each bin
table_a = data.frame(a_table%>% group_by(cat) %>%
mutate(quant = quantile(c1, prob = split_1)))
table_b = data.frame(b_table%>% group_by(cat) %>%
mutate(quant = quantile(c1, prob = split_2)))
table_c = data.frame(c_table%>% group_by(cat) %>%
mutate(quant = quantile(c1, prob = split_3)))
#create a new variable ("diff") that measures if the quantile is bigger than the value of "c1"
table_a$diff = ifelse(table_a$quant > table_a$c1,1,0)
table_b$diff = ifelse(table_b$quant > table_b$c1,1,0)
table_c$diff = ifelse(table_c$quant > table_c$c1,1,0)
#group all tables
final_table = rbind(table_a, table_b, table_c)
# calculate the total mean : this is what needs to be optimized
mean = mean(final_table$diff)
}
```

From here, I am interested in using the "ps_optim" function to optimize the function I just defined:

```
library(psoptim)
set.seed(90)
psoptim(rep(NA,3), fn = fitness, lower = c(80, random_1, 85, random_2, 0,0,0), upper = c(120,120,120,120,1,1,1))
```

But this returns the following error, suggesting that there are some "unused arguments":

```
Error in psoptim(rep(NA, 3), fn = fitness, lower = c(80, random_1, 85, :
unused arguments (fn = fitness, lower = c(80, random_1, 85, random_2, 0, 0, 0), upper = c(120, 120, 120, 120, 1, 1, 1))
```

Can someone please show me why this error is being produced?

Thanks