Search code examples
rfunctionplotcontourstatistics-bootstrap

Plotting influence of 2 variables on output


Hello kind and wise internet friends,

I have been toying with R from various angles, but I seem to have made little progress. This is probably basic, but my mind and experience are more so...

I ultimately want to plot the influence of varying m and n on the output (b) in the equation:

b=(0.15m+0.15n)/n. Where m and n have ranges of -1 to 1.

I envisage a contour plot to visualise this, but I am stuck at the step of getting values of b with corresponding m and n inputs.

The latest approach involved bootstrapping estimates for m and n to return b values, but as far as I am aware there is no way to obtain the corresponding input m and n values:

m<-seq(from=-1,1,length.out=100)  
n<-seq(from=-1,1,length.out=100)  
z<-rnorm(100)   
b<-((0.15*(sample(m,1,replace=T))+0.15*(sample(n,1,replace=T)))/(sample(n,1,replace=T))) 

library("boot")  
bfunc<-function(m,n){  
  (0.15*(sample(m,1,replace=T))+0.15*(sample(n,1,replace=T)))/(sample(n,1,replace=T))  
}  
bootb<-boot(data=z,statistic=(bfunc),R=1000)  
bootb$t 

My question: How do I get my output (b) and corresponding inputs (m and n), so that I can plot the data? Modifications of the above or entirely different ways are all welcome... I need to learn!

Any help is much appreciated, thank you.


Solution

  • I don't see why you would need the bootstrap

    dataset <- expand.grid(
      m = seq(-1, 1, length.out = 101),
      n = seq(-1, 1, length.out = 101)
    )
    dataset$b <- (0.15 * dataset$m + 0.15 * dataset$n) / dataset$n
    
    library(ggplot2)
    ggplot(dataset, aes(x = m, y = n, z = b)) + 
      geom_contour(aes(colour = ..level..)) + 
      scale_colour_gradient2()
    ggplot(dataset, aes(x = m, y = n, fill = b)) + 
      geom_tile() + 
      scale_fill_gradient2()