gpt4 book ai didi

r - "Formal argument "foo "matched by multiple arguments"- 如何在 R 中处理这个?

转载 作者:行者123 更新时间:2023-12-04 01:04:12 25 4
gpt4 key购买 nike

有时,调用带有特定参数的函数会导致错误消息 formal argument "foo" matched by multiple actual arguments .是否可以打印模棱两可的实际参数列表?

我问这个问题的原因目前是 plot 的问题。类对象的函数 mixEM (由 normalmixEMmixtools 包生成)。它不接受参数 ylim产生上面的错误,但是当我尝试使用 ylim2 时(它适用于 xlab2main2col2 等的方式),它说 "ylim2" is not a graphical parameter .所以我想知道 ylim 匹配的实际参数是什么? ?

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

更新: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)

这会产生一个错误:
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`

这根本行不通:
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

最佳答案

您的问题基本上属于以下类型:

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

因为您的 ylim-definition 在 plot.mixEM 的以下行中传递给带有 "..."参数的绘图函数:
hist(x, prob = TRUE, main = main2, xlab = xlab2, ylim = c(0, maxy), ...)

而 ylim 已定义为以下上限:
maxy <- max(max(a$density), 0.3989 * mix.object$lambda/mix.object$sigma)

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

你有几个选择:
  • 通过替换硬编码部分来编写您自己的 plot.mixEM
    原来的功能,你只需要改变几行。
  • 在 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")

    Example plot

    关于r - "Formal argument "foo "matched by multiple arguments"- 如何在 R 中处理这个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17650346/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com