2014-09-10 82 views
1

我已經定義此功能:錯誤xyplot面板

fresults <- function(svrFit,testSet,var) { 
    library(caret) 
    if(substr(deparse(substitute(svrFit)), 1, 3)!="svr") { 
    options(digits=3) 
    tiff(filename = paste("Predicted vs. Residuals (Train)", "_", deparse(substitute(svrFit)), ".tiff", sep=""), 
     res = 300, height = 2480, width = 3508, compression = "lzw") 
    print(xyplot(resid(svrFit) ~ predict(svrFit), 
     type = c("p", "g"), 
     xlab=list(label = "Predicted",cex = 2), ylab = list(label = "Residuals",cex = 2), 
     cex = 1.5, scales = list(cex = 1.5, tick.number = 8)), 
     panel = function(x, y, ...) { 
     panel.xyplot(x, y, ...) 
     panel.abline(h=0, lty = 1, col = "gray60", lwd=5) 
     }) 
    dev.off() 
    } 
} 

但它返回此錯誤時,它也應該繪製水平線:

Error in printFunction(x, ...) : 
    argument 2 matches multiple formal arguments 

回溯()

5: printFunction(x, ...) 
4: print.trellis(xyplot(resid(svrFit) ~ predict(svrFit), type = c("p", 
     "g"), xlab = list(label = "Predicted", cex = 2), ylab = list(label = "Residuals", 
     cex = 2), cex = 1.5, scales = list(cex = 1.5, tick.number = 8)), 
     panel = function(x, y, ...) { 
      panel.xyplot(x, y, ...) 
      panel.abline(h = 0, lty = 1, col = "gray60", 
       lwd = 5) 
     }) 
3: print(xyplot(resid(svrFit) ~ predict(svrFit), type = c("p", "g"), 
     xlab = list(label = "Predicted", cex = 2), ylab = list(label = "Residuals", 
      cex = 2), cex = 1.5, scales = list(cex = 1.5, tick.number = 8)), 
     panel = function(x, y, ...) { 
      panel.xyplot(x, y, ...) 
      panel.abline(h = 0, lty = 1, col = "gray60", 
       lwd = 5) 
     }) 
2: print(xyplot(resid(svrFit) ~ predict(svrFit), type = c("p", "g"), 
     xlab = list(label = "Predicted", cex = 2), ylab = list(label = "Residuals", 
      cex = 2), cex = 1.5, scales = list(cex = 1.5, tick.number = 8)), 
     panel = function(x, y, ...) { 
      panel.xyplot(x, y, ...) 
      panel.abline(h = 0, lty = 1, col = "gray60", 
       lwd = 5) 
     }) at Surrogate.R#67 
1: fresults(gbmFitRy, testSetRy, "Ry") 

我很確定解決方案很簡單,但我無法找到它。謝謝您的幫助。

回答

2

plot.trellis功能(什麼結束了,當你使用print被稱爲)接受幾個panel.*參數,以便當你傳遞一個命名爲panel,它不能猜你要使用哪一個。並抱怨。只要確保使用完整的參數名稱,你應該很好走。參考?plot.trellis

編輯:相反,panel傳遞給print.trellis而不是xyplot這一事實是語法問題的標誌,是括號使用不當。正確的語法應該是:

print(xyplot(resid(svrFit) ~ predict(svrFit), 
     type = c("p", "g"), 
     xlab =list(label = "Predicted",cex = 2), 
     ylab = list(label = "Residuals",cex = 2), 
     cex = 1.5, scales = list(cex = 1.5, tick.number = 8), 
     panel = function(x, y, ...) { 
     panel.xyplot(x, y, ...) 
     panel.abline(h=0, lty = 1, col = "gray60", lwd=5) 
     })) 
+0

flodel:嗨,感謝您的幫助。我試圖解決這個錯誤,但我不能。我將代碼更改爲:panel = function(...){panel.xyplot(...)panel.abline(h = 0,v = NULL,lty = 1,col.line =「gray60」,lwd = 5 ,identifier =「abline」)} – jpcgandre 2014-09-10 09:54:28

+0

問題是參數的名稱:「panel」。如果你能告訴我爲什麼你通過了一個「小組」的論點,那裏有文件記錄,那麼我可以進一步提供幫助。 – flodel 2014-09-10 09:59:12

+0

在?plot.trellis中有這樣的例子:'##使用seekViewport ##重複相同的繪圖,在每個面板中使用不同的多項式擬合 xyplot(armed.Forces〜Year,longley,index.cond = list(rep(1 ,6)), layout = c(3,2), panel = function(x,y,...) { panel.xyplot(x,y,...) fm < - lm(y 〜poly(x,panel.number())) llines(x,predict(fm)) }' – jpcgandre 2014-09-10 10:05:25