2016-07-04 19 views
-3

为什么我的代码无法正常工作。如何正确地将参数hist.args = NULL,plot.args = NULL传递给plot.gevp函数中的函数plot和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

如何使用...构造?

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

我必须把你的建议,在我的代码的结束,当我去打电话给我的功能,例如,'图(P,nidd.annual,“预测”,hist.args = C( main =“Pred”))'?因为我已经尝试过这一点,但它不工作。我喜欢你的建议,我想在我的代码中使用这个@Choubi – user95060

+0

我必须像这样:'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) hist(dat, freq = F,ylim = c(min(res),max(res)),main = NULL, xlab =“data”,ylab =“density”) lines(x,res)'? @Choubi – user95060

+0

你不需要第二次调用'hist'。 'do.call(“hist”,hist.args.all)'是对'hist'的调用。 – Choubi

相关问题