如何更改通用函数图中的参数?

时间:2016-07-04 17:54:19

标签: r plot generic-function

为什么我的代码无法正常工作。我怎样才能正确地将参数hist.args = NULL,plot.args = NULL传递给plot.gevp函数中的函数图和hist?

    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)
      }

  }

当我调用函数时:

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

我收到了错误:

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

我如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

如何使用...构造?

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", ...)
}

您将传递给函数的所有其他参数将逐字传递给plot和hist。

修改的: 为了避免传递给两个不同的函数,一个稍微复杂的例子。在这种情况下,您应该在列表中传递所有其他参数。

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)
}