【问题标题】:error in cdfCompare, object 'discrete' not foundcdfCompare 中的错误,找不到对象“离散”
【发布时间】:2021-07-11 00:14:35
【问题描述】:

我正在为数据框中的 2 列数据绘制 2 个累积分布。我的代码是:

library(EnvStats)
cdfCompare(Ratio1,Ratio2 discrete = FALSE, 
           prob.method = ifelse(discrete, "emp.probs", "plot.pos"), plot.pos.con = NULL, 
           distribution = "norm", param.list = NULL, 
           estimate.params = is.null(param.list), est.arg.list = NULL, 
           x.col = "blue", y.or.fitted.col = "black", 
           x.lwd = 3 * par("cex"), y.or.fitted.lwd = 3 * par("cex"), 
           x.lty = 1, y.or.fitted.lty = 2, digits = .Options$digits, 
           type = ifelse(discrete, "s", "l"), main = NULL, xlab = NULL, ylab = NULL, 
           xlim = NULL, ylim = NULL)

当我运行这段代码时,产生了一个错误:

Error in ifelse(discrete, "emp.probs", "plot.pos") : 
  object 'discrete' not found

当我使用较短的代码时:

cdfCompare(Ratio1,Ratio2 discrete = FALSE) 

没有错误。我也试过设置

离散=真

,看起来它不会影响绘图的形状(不平滑,我有大约 170,000 个值)。非常感谢。

【问题讨论】:

  • ifelse是在找一个名为discrete的对象,不能用来测试函数参数discrete是否设置为TRUE你写的方式它。

标签: r cdf


【解决方案1】:

您可以定义一个单独的变量来传递离散值并使用if/else 选择prob.method

discrete_value <- FALSE

cdfCompare(Ratio1,Ratio2 discrete = discrete_value, 
           prob.method = if(discrete_value) "emp.probs" else "plot.pos", plot.pos.con = NULL, 
           distribution = "norm", param.list = NULL, 
           estimate.params = is.null(param.list), est.arg.list = NULL, 
           x.col = "blue", y.or.fitted.col = "black", 
           x.lwd = 3 * par("cex"), y.or.fitted.lwd = 3 * par("cex"), 
           x.lty = 1, y.or.fitted.lty = 2, digits = .Options$digits, 
           type = ifelse(discrete, "s", "l"), main = NULL, xlab = NULL, ylab = NULL, 
           xlim = NULL, ylim = NULL)

【讨论】:

  • 谢谢。它成功了。只需要将:type = ifelse(discrete, "s", "l") 改为 type = ifelse(discrete_value, "s", "l")