R Error: Contour plot only works with two dimensional functions

R Error: Contour plot only works with two dimensional functions

我正在使用 R。我正在学习优化函数以及如何可视化结果。

例如,假设我有一些数据和一个我想优化的函数。

这是数据:

#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)

这里是函数:

library(optimization)

fitness <- 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)
    
    b_table = train_data %>%
        filter(cat == "b") %>%
        select(a1, b1, c1, cat)
    
    c_table = train_data %>%
        filter(cat == "c") %>%
        select(a1, b1, c1, cat)
    
    
    x[5:7] <- prop.table(abs(x[5:7]))
    
    #calculate  quantile ("quant") for each bin
    
    table_a = data.frame(a_table%>% group_by(cat) %>%
                             mutate(quant = quantile(c1, prob = x[5])))
    
    table_b = data.frame(b_table%>% group_by(cat) %>%
                             mutate(quant = quantile(c1, prob = x[6])))
    
    table_c = data.frame(c_table%>% group_by(cat) %>%
                             mutate(quant = quantile(c1, prob = x[7])))
    
    
    #create a new variable ("diff") that measures if the quantile is bigger tha 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) 
}

现在,我运行优化算法:

Output <- optim_nm(fitness, k = 7, trace = TRUE)

“优化”包中的文档(例如 https://cloud.r-project.org/web/packages/optimization/vignettes/vignette_master.pdf )表明对于 2 个变量,您可以可视化优化结果。例如:

可以使用“plot”功能制作右边的图,当然只能是二维的。

当您 运行 单独执行“plot”命令时,会产生以下错误:

#plot
plot(Output, 'contour')

Error in plot.optim_nmsa(Output, "contour") : 
  Contour plot only works with two dimensional functions.

我试图手动指定仅使用两个变量来绘制此图,例如

plot(Output[1], Output[2], 'contour')

但这会产生以下错误:

Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'list' object cannot be coerced to type 'double'

我尝试了以下命令,但这导致了一张奇怪的图片和一条警告消息:

 plot(train_data$a1, train_data$b1, 'contour')

Warning message:
In plot.xy(xy, type, ...) :
  plot type 'contour' will be truncated to first character

问题: 有什么方法可以只用两个变量得到“黄色轮廓”的图片吗?

谢谢

Output 中包含 x_1x_2 的元素是 Output[[3]]。我相信这就是你想要绘制的那个(那些是黄色图中的名字)。

然后您可以使用:

library(ggplot2)

set.seed(1000)
# 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)

Output <- optim_nm(fitness, k = 7, trace = TRUE)

df <- data.frame(Output[[3]])

ggplot(df, aes(x = x_1, y = x_2, z = function_value))+
    geom_density_2d(binwidth = 0.005)+
    geom_point()