2014-09-10 59 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