2016-07-04 60 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

相關問題