【问题标题】:Selective text for mtext() based on the input argument in R?基于 R 中的输入参数的 mtext() 的选择性文本?
【发布时间】:2017-09-21 03:07:42
【问题描述】:

我有一个矢量化的 R 函数,可以生成一些直方图(如下)。我想在其中一个直方图中添加一个mtext()(请在下面找到它)。

问题

但我希望mtext() 中的文本根据参数n 是长度> 1 的向量还是参数es 是长度> 1 的向量来更改。

在我的代码中,我使用以下代码没有成功:

txt = if(length(n) > 1) "Group Sample Size = " else if(length(es) > 1) "Effect Size = " else "Group Sample Size = "

mtext(paste0(txt, input), 3) [请在下面找到这些]

这是我完整的 R 代码:

 p.es = function(n, es, n.sim){

input = if(length(n) > 1) n else if(length(es) > 1) es else n

t.sim = Vectorize(function(n, es){

d = numeric(n.sim)
p = numeric(n.sim)

for(i in 1:n.sim){
   N = sqrt((n^2)/(2*n))
   x = rnorm(n, es, 1)
   y = rnorm(n, 0, 1)
   a = t.test(x, y, var.equal = TRUE)
d[i] = a[[1]]/N
p[i] = a[[3]] }

txt = if(length(n) > 1) "Group Sample Size = " else if(length(es) > 1) "Effect Size = " else "Group Sample Size = "
hist(p) ; mtext(paste0(txt, input), 3) ## Here I need help!!!!

hist(d) 
}, c("n", "es"))

par(mfcol = c(2, length(input)), xpd = NA)
invisible(t.sim(n, es)) }
# Example of use:
p.es(n = 20, es = c(.1, .2), n.sim = 20) # Here I expect the mtext to show "Effect Size = "
                                         # but it doesn't why?

【问题讨论】:

  • input 是具有2 个或更多元素的向量时,您的mtext 调用将在同一位置打印多个字符串。例如Effect size = 0.1Effect size = 0.2 是重叠的。
  • @DamianoFantini,是的,但是你建议如何解决这个问题?

标签: r function plot histogram


【解决方案1】:

我认为问题不在于mtextif...else 语句。看起来问题是因为Vectorize 而发生的。让我们从您的内部函数开始,这里称为myfun。它似乎工作得很好。

myfun <- function(n, es, nsim, input){

  d = numeric(n.sim)
  p = numeric(n.sim)

  for(i in 1:n.sim){
    N = sqrt((n^2)/(2*n))
    x = rnorm(n, es, 1)
    y = rnorm(n, 0, 1)
    a = t.test(x, y, var.equal = TRUE)
    d[i] = a[[1]]/N
    p[i] = a[[3]] }

  txt <- if (length(n) > 1) {"Group Sample Size = "} else if (length(es) > 1) {"Effect Size = "} else {"Group Sample Size = "}
  hist(p) ; mtext(paste0(txt, input, collapse = " "), 3) ## Here I need help!!!!

  hist(d) 
}

# Test Run
par(mfrow = c(1,2))
myfun(n = 10, 
      es = c(0.1, 0.2), 
      nsim = 10, 
      input = c(0.1, 0.2))

现在,如果你实现Vectorize,就会出现问题。

t.sim <- Vectorize(myfun, c('n', 'es', 'nsim', 'input'))
n <- 10
n.sim <- 10
es <- c(0.1, 0.2)
input <- c(0.1, 0.2)
t.sim(n, es, n.sim, input)

解决此问题的一种方法是将txt 分配移到Vectorize 调用之外(您可以在定义input 时分配txt 的值吗?)。

p.es = function(n, es, n.sim){

  input = if(length(n) > 1) n else if(length(es) > 1) es else n
  txt = if(length(n) > 1) "Group Sample Size = " else if(length(es) > 1) "Effect Size = " else "Group Sample Size = "

  t.sim = Vectorize(function(n, es){
    d = numeric(n.sim)
    p = numeric(n.sim)

    for(i in 1:n.sim){
      N = sqrt((n^2)/(2*n))
      x = rnorm(n, es, 1)
      y = rnorm(n, 0, 1)
      a = t.test(x, y, var.equal = TRUE)
      d[i] = a[[1]]/N
      p[i] = a[[3]] }

    hist(p) ; mtext(paste0(txt, paste(input, collapse = "-")), 3) ## Here I need help!!!!

    hist(d) 
  }, c("n", "es"))

  par(mfcol = c(2, length(input)), xpd = NA)
  invisible(t.sim(n, es)) }
# Example of use:
p.es(n = 20, es = c(.1, .2), n.sim = 20) # Is this what you want?

【讨论】:

  • 达米亚诺,非常感谢!但是是的,我需要Vectorize,并注意即使我已经矢量化了nes,我不需要同时使用长度> 1 的nes。相反,我将使用n 长度 > 1 或 es 长度 > 1。所以,我想让函数知道的是它匹配(同步)mtext() 中的文本与对象 input 的输出,就是这样!
  • 那么我要做的就是在Vectorize f(x) 之外分配txt。否则,我真的不知道。我更新了答案以显示这一点,顺便说一句
  • 很遗憾,您更新的答案有问题,最后一部分,仔细查看生成的 mtext 以捕捉到这一点。
  • 什么?如果您指的是 0.1-0.2 的东西,我只是将输入的值折叠在一起。如果您只想显示一个数字,则必须决定放哪一个...
  • 啊!不好意思,知道了,谢谢!哦不,还有更多的问题,但算了吧!这需要我们的时间。
猜你喜欢
  • 1970-01-01
  • 2015-05-06
  • 2012-01-06
  • 1970-01-01
  • 2012-05-18
  • 1970-01-01
  • 2010-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多