【问题标题】:change the default arguments of a function in R更改 R 中函数的默认参数
【发布时间】:2021-05-04 08:34:58
【问题描述】:

我正在跟进 this answer。我想知道是否有办法在library(effects) 生成的图中将参数rug 的默认值设置为FALSE,并将multiline 的参数设置为TRUE,例如,如下代码所示?

library(effects)
m <- lm(Fertility ~ Examination*Education, data = swiss)
plot(allEffects(m), rug = FALSE, multiline = TRUE)   # By default, change `rug = FALSE`
                                                     # `multiline = TRUE `

【问题讨论】:

    标签: r function plot regression lattice


    【解决方案1】:

    我认为,如果您只是想将这两个选项添加到您引用的 @MrFlick 的答案中,您可以执行以下操作:

    plot.efflist <- function (x, selection, rows, cols, graphics = TRUE, 
                              lattice, rug = FALSE, multiline = TRUE, ...) 
    {
      lattice <- if (missing(lattice)) 
        list()
      else lattice
      if (!missing(selection)) {
        if (is.character(selection)) 
          selection <- gsub(" ", "", selection)
        pp <- plot(x[[selection]], lattice = lattice, rug = rug, multiline=multiline, ...)
        pp$x.scales$tck=c(1,0)
        pp$y.scales$tck=c(1,0)
        return(pp)
      }
      effects <- gsub(":", "*", names(x))
      neffects <- length(x)
      mfrow <- mfrow(neffects)
      if (missing(rows) || missing(cols)) {
        rows <- mfrow[1]
        cols <- mfrow[2]
      }
      for (i in 1:rows) {
        for (j in 1:cols) {
          if ((i - 1) * cols + j > neffects) 
            break
          more <- !((i - 1) * cols + j == neffects)
          lattice[["array"]] <- list(row = i, col = j, 
                                     nrow = rows, ncol = cols, more = more)
          pp <- plot(x[[(i - 1) * cols + j]], lattice = lattice, rug=rug, multiline=multiline,
                     ...)
          # hack to turn off opposite side tick marks
          pp$x.scales$tck=c(1,0)
          pp$y.scales$tck=c(1,0)
          print(pp)
        }
      }
    }
    environment(plot.efflist) <- asNamespace("effects")
    
    library(effects)
    m <- lm(Fertility ~ ., data = swiss)
    plot(allEffects(m), rug = FALSE)
    
    

    【讨论】:

      【解决方案2】:

      注意:以下建议是指在函数中更改默认值的一般情况。

      是的,可能会弄乱默认参数。一种方法是修改函数的formals,在本例中为

      formals(effects:::plot.eff)$rug <- FALSE
      formals(effects:::plot.eff)$multiline <- TRUE
      

      另一种可能性是使用default 包,如

      default::default(effects:::plot.eff) <- list(rug = FALSE, 
                                     multiline = TRUE)
      

      引用包描述,

      更改函数参数默认值的简单语法,无论它们是在包中还是在本地定义。

      有关该软件包的更多信息,您可以查看 CRAN 页面。

      【讨论】:

      • 谢谢,但是,当我尝试了你的第一个解决方案,然后又做了plot(allEffects(m)),然后我得到了以下错误:Error in plot(allEffects(m)) : call to standardGeneric("plot") apparently not from the body of that generic function
      • 第二个解决方案有效吗?如果第一个解决方案不起作用,您需要查看allEffects() 如何调用plot() 函数。也许有必要将formals() 应用于allEffects() 或类似的函数。
      • 我没有尝试第二种解决方案,但我特别想知道为什么第一种解决方案会产生此错误,显然您没有收到此错误,对吧?另一方面,您是否检查了this answer 的相关问题?也许我们应该更改formals(effects:::plot.eff) 对象中的默认值。
      • 这很奇怪。 effects:::plot.eff objekt 确实有 rug 属性,但将其设置为FALSE 会导致loadNamespace 出现错误。让我想想如何处理这个异常。
      • @Taufi 我认为loadNamespace 错误是因为该函数未从命名空间导出。我下载了包并导出了plot.efflistplot.eff 并从这个本地版本重新安装。错误停止了,但使用上述任何一种方法(即使您在 plot.efflistplot.eff 上都指定它们,由前者调用,似乎不会导致预期的情节 - 它仍然有地毯和不是多线图。
      猜你喜欢
      • 2012-04-30
      • 1970-01-01
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多