Search code examples
rplotgeneric-function

How can I change the arguments in my generic function plot?


Why my code is not working, correctly. How can I pass, properly, the argument hist.args = NULL, plot.args = NULL to the function plot and hist inside the plot.gevp function?

    plot.gevp=function (vector, type = c("predictive", "retlevel"), t, hist.args = NULL, plot.args = NULL){
      if (type=="predictive"){
      dat=vector$data
      linf = max(min(dat) - 1, 0)
      lsup = 11 * max(dat)/10
      x = seq(linf, lsup, (lsup - linf)/70)
      n = length(x)
      int = length(vector$posterior[, 1])
      res = array(0, c(n))
      for (i in 1:n) {
          for (j in 1:int) {
              if ((vector$posterior[j, 3] > 0) && (x[i] > (vector$posterior[j, 1] - 
                  vector$posterior[j, 2]/vector$posterior[j, 3]))) 
                  res[i] = res[i] + (1/int) * dgev(x[i], vector$posterior[j, 
                    3], vector$posterior[j, 1], vector$posterior[j, 2])
              if ((vector$posterior[j, 3] < 0) && (x[i] < (vector$posterior[j, 1] - 
                  vector$posterior[j, 2]/vector$posterior[j, 3]))) 
                  res[i] = res[i] + (1/int) * dgev(x[i], vector$posterior[j, 
                    3], vector$posterior[j, 1], vector$posterior[j, 2])
          }
      }
      hist.args.all <- c(list(data, freq = F, ylim = c(min(res), max(res)), 
      main = NULL, xlab = "data", ylab = "density"), hist.args)
      do.call("hist", hist.args.all)

      lines(x, res)
      out<-list(pred=res)
      return(out)
      }

      if(type=="retlevel"){
      amostra = qgev(1 - 1/t, vector$posterior[, 3], vector$posterior[, 1], vector$posterior[, 2])
      res = quantile(amostra, 0.5)

      t = seq(1, 100, 1)
      n = length(t)
      li = array(0, c(n))
      ls = array(0, c(n))
      pred = array(0, c(n))
      for (s in 1:n) {
          amostra = qgev(1 - 1/s, vector$posterior[, 3], vector$posterior[, 1], vector$posterior[, 
              2])
          li[s] = quantile(amostra, 0.025)
          ls[s] = quantile(amostra, 0.975)
          pred[s] = quantile(amostra, 0.5)
      }
      plot.args.all <- c(list(t, pred, type = "l", ylim = c(li[2], max(ls)), 
      ylab = "returns"), plot.args)
      do.call("plot", plot.args.all)

      lines(t, li, lty = 2)
      lines(t, ls, lty = 2)
      out<-list(retmedian=res, retpred=pred)

      return(out)
      }

  }

When I call the function like:

plot(p,"retlevel",t=10, plot.args=list(main="list")) 

I got the error:

Error in plot.gevp(p, "retlevel", t = 10, main = "list") : 
  unused argument (main = "list")

How I can fix this?


Solution

  • How about using the ... construct?

    plot.gevp=function (vector, data, type, t, ...)
    {
    # rest of your code
    hist(data, freq = F, ylim = c(min(res), max(res)), main = NULL, 
        xlab = "data", ylab = "density", ...)
    
    plot(t, pred, type = "l", ylim = c(li[2], max(ls)), ylab = "returns", ...)
    }
    

    All further arguments you will pass to your function will be passed verbatim to plot and hist.

    edit: To avoid passing ... to two different functions, a slightly more complex example. In this case, you should pass all further arguments inside a list.

    plot.gevp=function (vector, data, type, t, hist.args = NULL, plot.args = NULL)
    {
    # rest of your code
    hist.args.all <- c(list(data, freq = F, ylim = c(min(res), max(res)), 
        main = NULL, xlab = "data", ylab = "density"), hist.args)
    do.call("hist", hist.args.all)
    
    plot.args.all <- c(list(t, pred, type = "l", ylim = c(li[2], max(ls)), 
        ylab = "returns"), plot.args)
    do.call("plot", plot.args.all)
    }