jpcgandre
jpcgandre

Reputation: 1505

Error in xyplot panel

I have defined this function:

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

But it returns this error when it should plot also a horizontal line:

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

traceback()

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

I'm certain the solution is trivial but I cannot find it. Thanks for the help.

Upvotes: 1

Views: 636

Answers (1)

flodel
flodel

Reputation: 89057

The plot.trellis function (what ends up being called when you use print) accepts a few panel.* arguments so when you are passing it one named panel, it cannot guess which one you are trying to use. And complains. Just make sure to use the full argument name and you should be good to go. Refer to ?plot.trellis.

Edit: rather, the fact that panel was passed to print.trellis and not xyplot was the sign of a syntax issue, a bad use of parenthesis. The correct syntax should be:

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

Upvotes: 2

Related Questions