R:在网格上评估和绘制函数
R: Evaluating and Plotting Functions Over a Grid
我正在使用 R 编程语言。我有以下功能:
#function
my_function_b <- function(input_1, input_2, input_3, input_4) {
final_value = sin(input_1) + cos(input_2) + input_3 + input_4
}
问题:
对于“my_function_b”,我正在尝试针对“input_1”、“input_2”的不同值评估“final_value” 、“input_3”和“input_4”。例如。 input_1、input_2、input_3、input_4 从 1 到 100,增量为 0.1。
然后,我想用“input_1”、“input_2”和“input_3”制作一个 3 维图。
下一步,我想在这个图上拟合一个 3 维表面
最后,我想根据“final_input”
的值给这个3维表面“上色”
到目前为止我尝试了什么:
我想出了如何为第二个函数制作一个“网格框架”,然后使用这个“网格框架”评估“final_value”,例如
#create grid and evaluate function
input_1 <- seq(0,100,0.1)
input_2 <- seq(0,100,0.1)
input_3 <- seq(0,100,0.1)
input_4 <- seq(0,100,0.1)
my_grid <- data.frame(input_1, input_2, input_3, input_4)
my_grid$final_value = sin(input_1) + cos(input_2) + input_3 + input_4
但我不确定这是否是解决此问题的最佳方法。当我尝试绘制结果时,这现在会产生问题,例如
#make a 3d plot for two of the inputs and the output, and fit surface over the plot
persp(my_grid$input_1, my_grid$input_2, my_grid$final_value)
Error in persp.default(my_grid$input_1, my_grid$input_2, my_grid$final_value) :
invalid 'z' argument
备选方案 #2:行不通
library(plotly)
a = my_grid[,c(1,2,5)]
fig <- plot_ly(a = ~as.matrix(a))
fig <- fig %>% add_surface()
Error: Must supply `z` attribute
备选方案 #3:不起作用 - 创建空图
plot_ly() %>%
add_trace(data = my_grid, x=my_grid$input_1, y=my_grid$input_2, z=my_grid$final_value, type="mesh3d" )
问题:有人可以告诉我怎么做吗?这可以使用“lattice”或“rsm”库来完成吗?或者可以按照我建议的方式完成吗?
谢谢
如果你想让你的 plotly 方法继续下去,你需要将你的 z 配置为一个矩阵,即一个具有定义的行和列维度的对象!
Plotly 不需要数据框。您可以安全地直接提供变量(对象)。
library(plotly)
x <- my_grid$input_1
y <- my_grid$input_2
z <- matrix(my_grid$final_value, nrow = length(x), ncol = length(y)) # proper matrix & dimensions
plot_ly(x = x, y = y, z = z) %>% add_surface()
瞧瞧:
根据评论中的问题修改了答案 - 如何设置色阶
这在文档中有点隐藏。原则上,您可以使用 endpoints/colours 为您喜欢的渐变定义自己的色阶,并确保它根据您的表面数据进行缩放。表面数据必须再次是定义明确的矩阵,即具有 nrow/ncol 维度 .
通常,色标是为区间 c(0,1) 定义的。 plotly documentation 表示最低 (0) 和最高 (1) 值的映射。 colorscale 的第二个元素是光谱端色的定义!或者,您可以使用一种公认的调色板。
SO post 提供了一个示例:
colorscale = list(c(0, 1), c("tan", "blue"))
因此对于映射,您可以使用最小值和最大值。
# --- define coordinate vectors
x = input_1
y = input_2
z = matrix(input_3, nrow = length(x), ncol = length(y)) # z must be well-dimensioned matrix
# --- define a scale for your colour setting
my_colorscale = list(
c(min(final_value),max(final_value)) # set min/max value, or use c(0,1)
, c("tan", "blue") # define desired grad. colours
)
# --- plot
plot_ly(x = x, y = y, z = z) %>%
add_surface(
surfacecolor = matrix(final_value, nrow = length(x), ncol = length(y)) # your surface data points
,colorscale = my_colorscale) # scale for your surface
我正在使用 R 编程语言。我有以下功能:
#function
my_function_b <- function(input_1, input_2, input_3, input_4) {
final_value = sin(input_1) + cos(input_2) + input_3 + input_4
}
问题:
对于“my_function_b”,我正在尝试针对“input_1”、“input_2”的不同值评估“final_value” 、“input_3”和“input_4”。例如。 input_1、input_2、input_3、input_4 从 1 到 100,增量为 0.1。
然后,我想用“input_1”、“input_2”和“input_3”制作一个 3 维图。
下一步,我想在这个图上拟合一个 3 维表面
最后,我想根据“final_input”
的值给这个3维表面“上色”
到目前为止我尝试了什么:
我想出了如何为第二个函数制作一个“网格框架”,然后使用这个“网格框架”评估“final_value”,例如
#create grid and evaluate function
input_1 <- seq(0,100,0.1)
input_2 <- seq(0,100,0.1)
input_3 <- seq(0,100,0.1)
input_4 <- seq(0,100,0.1)
my_grid <- data.frame(input_1, input_2, input_3, input_4)
my_grid$final_value = sin(input_1) + cos(input_2) + input_3 + input_4
但我不确定这是否是解决此问题的最佳方法。当我尝试绘制结果时,这现在会产生问题,例如
#make a 3d plot for two of the inputs and the output, and fit surface over the plot
persp(my_grid$input_1, my_grid$input_2, my_grid$final_value)
Error in persp.default(my_grid$input_1, my_grid$input_2, my_grid$final_value) :
invalid 'z' argument
备选方案 #2:行不通
library(plotly)
a = my_grid[,c(1,2,5)]
fig <- plot_ly(a = ~as.matrix(a))
fig <- fig %>% add_surface()
Error: Must supply `z` attribute
备选方案 #3:不起作用 - 创建空图
plot_ly() %>%
add_trace(data = my_grid, x=my_grid$input_1, y=my_grid$input_2, z=my_grid$final_value, type="mesh3d" )
问题:有人可以告诉我怎么做吗?这可以使用“lattice”或“rsm”库来完成吗?或者可以按照我建议的方式完成吗?
谢谢
如果你想让你的 plotly 方法继续下去,你需要将你的 z 配置为一个矩阵,即一个具有定义的行和列维度的对象!
Plotly 不需要数据框。您可以安全地直接提供变量(对象)。
library(plotly)
x <- my_grid$input_1
y <- my_grid$input_2
z <- matrix(my_grid$final_value, nrow = length(x), ncol = length(y)) # proper matrix & dimensions
plot_ly(x = x, y = y, z = z) %>% add_surface()
瞧瞧:
根据评论中的问题修改了答案 - 如何设置色阶
这在文档中有点隐藏。原则上,您可以使用 endpoints/colours 为您喜欢的渐变定义自己的色阶,并确保它根据您的表面数据进行缩放。表面数据必须再次是定义明确的矩阵,即具有 nrow/ncol 维度
通常,色标是为区间 c(0,1) 定义的。 plotly documentation 表示最低 (0) 和最高 (1) 值的映射。 colorscale 的第二个元素是光谱端色的定义!或者,您可以使用一种公认的调色板。
SO post 提供了一个示例:
colorscale = list(c(0, 1), c("tan", "blue"))
因此对于映射,您可以使用最小值和最大值。
# --- define coordinate vectors
x = input_1
y = input_2
z = matrix(input_3, nrow = length(x), ncol = length(y)) # z must be well-dimensioned matrix
# --- define a scale for your colour setting
my_colorscale = list(
c(min(final_value),max(final_value)) # set min/max value, or use c(0,1)
, c("tan", "blue") # define desired grad. colours
)
# --- plot
plot_ly(x = x, y = y, z = z) %>%
add_surface(
surfacecolor = matrix(final_value, nrow = length(x), ncol = length(y)) # your surface data points
,colorscale = my_colorscale) # scale for your surface