[英]R: Error in get(as.character(FUN), mode = “function”, envir = envir) :
I am working with R. I am trying to replicate the answer provided from this stackoverflow post over here: How can I plot 3D function in r?我正在与 R 一起工作。我试图在这里复制此 stackoverflow 帖子中提供的答案: 如何在 r 中绘制 3D 函数?
Using the "lattice" library in R, I am trying to create a 3D surface plot of "input_1", "input_2", "input_3" - and color the surface according to values of "final_value".使用 R 中的“lattice”库,我试图创建“input_1”、“input_2”、“input_3”的 3D 曲面图 - 并根据“final_value”的值为曲面着色。
I created a function for this problem:我为这个问题创建了一个函数:
my_function_b <- function(input_1, input_2, input_3, input_4) {
final_value = sin(input_1) + cos(input_2) + input_3 + input_4
}
Then, I assigned each "input" from this function a series of values:然后,我为这个函数的每个“输入”分配了一系列值:
input_1 <- seq(-10, 10, length= 30)
input_2 <- input_1
input_3 <- input_1
input_4 <- input_1
Next, I try to use the "outer" function:接下来,我尝试使用“外部”功能:
z <- outer(input_1, input_2, input_3, my_function_b)
But this returns the following error:但这会返回以下错误:
Error in get(as.character(FUN), mode = "function", envir = envir) :
object 'input_3' of mode 'function' was not found
Can someone please show me what I am doing wrong?有人可以告诉我我做错了什么吗?
Thanks谢谢
Additional References:其他参考资料:
outer
takes only two arguments. outer
只接受两个参数。 We may need pmap
我们可能需要
pmap
library(purrr)
pmap_dbl(list(input_1, input_2, input_3, input_4), my_function_b)
or Map/mapply
或
Map/mapply
mapply(my_function_b, input_1, input_2, input_3, input_4)
If we need all combinations, create the combinations with expand.grid
and apply over the rows如果我们需要所有组合,请使用
expand.grid
创建组合并应用于行
tmp <- expand.grid(input_1 = input_1, input_2 = input_2,
input_3 = input_3, input_4 = input_4)
out <- apply(tmp, 1,
FUN = function(x) do.call(my_function_b, as.list(x)))
Or may speed up with dapply
from collapse
或者可能会通过
dapply
从collapse
加速
library(collapse)
out1 <- dapply(tmp, MARGIN = 1, FUN = function(x)
my_function_b(x[1], x[2], x[3], x[4]))
Perhaps, we create the combinations on two vectors, and then add ?也许,我们在两个向量上创建组合,然后添加 ?
my_function_b <- function(input_1, input_2) sin(input_1) + cos(input_2)
tmp1 <- outer(input_1, input_2, my_function_b)
z <- tmp1 + input_3[col(tmp1)] + input_4[col(tmp1)]
library(lattice)
wireframe(z, drape=TRUE, col.regions=rainbow(100))
-output -输出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.