"形式论证"foo"匹配多个参数"- 如何在 R 中处理这个问题? [英] "Formal argument "foo" matched by multiple arguments" - how to deal with this in R?

查看:26
本文介绍了"形式论证"foo"匹配多个参数"- 如何在 R 中处理这个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,调用带有某些参数的函数会导致错误消息形式参数foo"与多个实际参数匹配.是否可以打印不明确的实参列表?

我问这个的原因目前是 plot 类的对象的问题 mixEM(由 normalmixEMmixtools 包).它不接受产生上述错误的参数 ylim,但是当我尝试使用 ylim2(它适用于 xlab2 的方式,main2col2 等),它说 "ylim2" 不是图形参数.所以我想知道 ylim 匹配的实际参数是什么?

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

更新:MWE:

库(mixtools)等待=忠实$等待mixmdl = normalmixEM(等待)情节(mixmdl,其中= 2,xlim = c(25, 110),nclass=20)线(密度(等待),lty = 2,lwd = 2)

这会产生一个错误:

plot(mixmdl, which = 2, xlim = c(25, 110), ylim = c(0, .5), nclass=20)# hist.default(x, prob = TRUE, main = main2, xlab = xlab2, ylim = c(0, :# 形参 "ylim" 匹配多个实参`

这根本行不通:

plot(mixmdl, which = 2, xlim = c(25, 110), ylim2 = c(0, .5), nclass=20)# 警告信息:# 1: 在 title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...) 中:# "ylim2" 不是图形参数# 2:在轴(1,...)中:ylim2"不是图形参数# 3:在轴(2,...)中:ylim2"不是图形参数

解决方案

你的问题本质上是这样的:

plot(1:10, rnorm(10), ylim=c(0,1), ylim=c(-1,100))plot.default(1:10, rnorm(10), ylim = c(0, 1), ylim = c(-1, 100)) 中的错误:形式参数ylim"与多个实际参数匹配

因为您的 ylim 定义被传递给带有 "..." 参数的绘图函数,在 plot.mixEM 的以下行中:

hist(x, prob = TRUE, main = main2, xlab = xlab2, ylim = c(0, maxy), ...)

ylim 已被定义为具有以下上限:

maxy <- max(max(a$密度), 0.3989 * mix.object$lambda/mix.object$sigma)

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

你有几个选择:

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

这就是选项 2 的工作原理:

库(mixtools)等待=忠实$等待mixmdl = normalmixEM(等待)情节.new()plot.window(xlim=c(25,110), ylim=c(0,0.5))情节(mixmdl,其中= 2,nclass=20,添加=真)线(密度(等待),lty = 2,lwd = 2)盒子();轴(1);轴(2);标题(xlab =数据",ylab =密度")

Sometimes, calling a function with certain arguments results in the error message formal argument "foo" matched by multiple actual arguments. Is it possible to print the list of the ambiguous actual arguments?

The reason I'm asking this is currently a problem with the plot function for objects of class mixEM (generated by normalmixEM from the mixtools package). It doesn't accept the argument ylim yielding the error above, but when I try to use ylim2 (the way it works for xlab2, main2, col2 etc.), it says "ylim2" is not a graphical parameter. So I wonder what are the actual arguments that are matched by ylim?

Using formals(plot.mixEM) doesn't help because it doesn't contain anything starting with ylim, but then at the end it refers to ... which are the graphical parameters passed to plot. However, for the plot function, ylim would be unambiguous. Getting a more exact error description from R with a list of the conflicting arguments would be helpful.

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)

This produces an error:

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`

This simply doesn't work:

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

解决方案

Your problem is essentially of type:

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

because your ylim-definition gets passed on to a plot function with the "..."-argument, in the following line of plot.mixEM:

hist(x, prob = TRUE, main = main2, xlab = xlab2, ylim = c(0, maxy), ...)

while ylim has been defined with the following upper limit:

maxy <- max(max(a$density), 0.3989 * mix.object$lambda/mix.object$sigma)

Notice that you are calling plotting function for an object of type mixEM. Looking at the code of the default plotting function plot.default will leave you puzzled, as it is actually plot.mixEM that you are calling. If you type plot.mixEM in the terminal you will see its code, and ?plot.mixEM will help you out as well. This is a typical approach in R, where the default function functionname.default is replaced by a class-specific function provided by the package in the format functionname.classname.

You got couple options:

  1. Write your own plot.mixEM by replacing the hard-coded part in the original function, you only need to change couple lines.
  2. Plot the window before plot.mixEM and add the "add=TRUE" parameter, which means that we will not create a new plotting window but instead add to the existing one.

This is how option 2 works:

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

这篇关于&quot;形式论证&quot;foo&quot;匹配多个参数"- 如何在 R 中处理这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆