"形式论证"foo"由多个参数匹配" - 如何在R中处理这个问题?

Anj*_*jaM 12 plot arguments r function

有时,使用某些参数调用函数会导致错误消息formal argument "foo" matched by multiple actual arguments.是否可以打印含糊不清的实际参数列表?

我问这个的原因是目前与有问题plot的类的对象功能mixEM(通过产生normalmixEMmixtools包中).它不接受的说法ylim产生上述错误,但是当我尝试使用ylim2(它适用于道路xlab2,main2,col2等),它说"ylim2" is not a graphical parameter.所以我想知道匹配的实际论点是ylim什么?

使用formals(plot.mixEM)没有帮助,因为它不包含任何开头的内容ylim,但最后它指的...是传递给它的图形参数plot.但是,对于该plot功能,ylim将是明确的.从R冲突参数列表中获取更准确的错误描述会很有帮助.

UPD:MWE:

library(mixtools)
wait = faithful$waiting
mixmdl = normalmixEM(wait)
plot(mixmdl, which = 2, xlim = c(25, 110), nclass=20)
lines(density(wait), lty = 2, lwd = 2)
Run Code Online (Sandbox Code Playgroud)

这会产生错误:

plot(mixmdl, which = 2, xlim = c(25, 110), ylim = c(0, .5), nclass=20)

# Error in hist.default(x, prob = TRUE, main = main2, xlab = xlab2, ylim = c(0, :
# formal argument "ylim" matched by multiple actual arguments`
Run Code Online (Sandbox Code Playgroud)

这根本不起作用:

plot(mixmdl, which = 2, xlim = c(25, 110), ylim2 = c(0, .5), nclass=20)

# Warning messages:
# 1: In title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...) :
#   "ylim2" is not a graphical parameter
# 2: In axis(1, ...) : "ylim2" is not a graphical parameter
# 3: In axis(2, ...) : "ylim2" is not a graphical parameter
Run Code Online (Sandbox Code Playgroud)

Tee*_*ala 8

您的问题基本上是类型:

plot(1:10, rnorm(10), ylim=c(0,1), ylim=c(-1,100))
Error in plot.default(1:10, rnorm(10), ylim = c(0, 1), ylim = c(-1, 100)) : 
  formal argument "ylim" matched by multiple actual arguments
Run Code Online (Sandbox Code Playgroud)

因为你的ylim -definition被传递给带有"..." - 参数的绘图函数,在plot.mixEM的下一行中:

hist(x, prob = TRUE, main = main2, xlab = xlab2, ylim = c(0, maxy), ...)
Run Code Online (Sandbox Code Playgroud)

ylim的定义有以下上限:

maxy <- max(max(a$density), 0.3989 * mix.object$lambda/mix.object$sigma)
Run Code Online (Sandbox Code Playgroud)

请注意,您正在为mixEM类型的对象调用绘图函数.查看默认绘图函数plot.default的代码会让你感到困惑,因为它实际上是你调用的plot.mixEM.如果在终端中键入plot.mixEM,您将看到它的代码,并且?plot.mixEM也会帮助您.这是R中的一种典型方法,其中默认函数functionname.default由包提供的类特定函数替换,格式为functionname.classname.

你有几个选择:

  1. 通过替换原始函数中的硬编码部分来编写自己的plot.mixEM,您只需要更改几行.
  2. plot.mixEM之前绘制窗口并添加" add = TRUE "参数,这意味着我们不会创建新的绘图窗口,而是添加到现有窗口.

选项2的工作原理如下:

library(mixtools)
wait = faithful$waiting
mixmdl = normalmixEM(wait)
plot.new()
plot.window(xlim=c(25,110), ylim=c(0,0.5))
plot(mixmdl, which = 2, nclass=20, add = TRUE)
lines(density(wait), lty = 2, lwd = 2)
box(); axis(1); axis(2); title(xlab="Data", ylab="Density")
Run Code Online (Sandbox Code Playgroud)

示例图