I am working with the R programming language. I have the following function :
#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
}
Question :
- For "my_function_b", I am trying to evaluate "final_value" for different values of "input_1", "input_2", "input_3" and "input_4" . E.g. input_1, input_2, input_3, input_4 from 1 to 100 at increments of 0.1.
- Then, I want to make a 3 Dimensional plot with "input_1", "input_2" and "input_3".
- Next, I want to a fit a 3 Dimensional surface over this plot
- Finally, I want to "color" this 3 Dimensional surface according to the values of "final_input"
What I tried so far:
I figured out how to make a "grid frame" for the second function and then evaluate "final_value" using this "grid frame", e.g.
#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
But I am not sure if this is the best way to solve this question. This is now creating problems when I try to plot the results, e.g.
#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
Alternative #2: Does Not Work
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
Alternative #3 : Does Not Work - Creates an Empty Plot
plot_ly() %>%
add_trace(data = my_grid, x=my_grid$input_1, y=my_grid$input_2, z=my_grid$final_value, type="mesh3d" )
Problem: Can someone please show me how to do this? Can this be done using the "lattice" or "rsm" libraries? Or can it be done using the ways I suggested?
Thanks