为什么只有当参数的长度大于等于3时才起作用:R函数 [英] Why only working when arguments have a length >= 3: R function

查看:169
本文介绍了为什么只有当参数的长度大于等于3时才起作用:R函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个函数,当它的参数长度> = 2



<但是我想知道为什么这个函数只有当它的参数的长度都是> = 3



我错过了什么? (任何修正,所以当它的参数的长度都为2时,该函数的工作原理是什么?)
$ b

[ 注意: CI )是一个有2列的矩阵, length(n)行,除非 length(n)== 2 。当 length(n)== 2 我期望输出有1行和2列。 $($,$,$,$,$,$,$,$,$,$,$,$,$,$) (in 1:length(n)){
p [[i]] -rbeta(1e3,a [i] + yes [i],b [i] +(n [i] - yes [i ]))
}
ps < - combn(p,2,FUN = function(x)x [[1]] - x [[2]])

CI(矩阵(NA),长度(n),2)

[,i],c(.025,.975))
}
CI
}

例如:

  abc(n = c(10,20,30),yes = rep(5,3),a = rep(1,3))#很好用:-) 

abc(n = c(10,20),yes = rep(5,2), a = rep(1,2))#不起作用! :-(
#在ps [,i]中的错误:下标越界


ncol(ps) length(n)解决方案

创建结果矩阵并运行 for循环将值复制到 CI 。由'combn 生成将会比 n`的实际长度更长。

  abc < - 函数(n,是,a,b = a){

p < - list()

for(i in 1:length(n)) {
p [[i]] < - rbeta(1e3,a [i] + yes [i],b [i] +(n [i] - yes [i])
}
$ b $ str(p)

ps < - combn(p,2,FUN = function(x)x [[1]] - x [[2]])

$ b $ CI < - 矩阵(NA,ncol(ps),2)

for(i in 1:ncol(ps)){
CI [i,] - 分位数(ps [,i],c(.025,.975),na.rm = TRUE)
}
CI
}

$ b #Results
#> abc(n = c(10,20,30),yes = rep(5,3),a = rep(1,3))
#[,1] [,2]
#[1,] -0.10141014 0.5774627
#[2,] 0.02638096 0.6159326
#[3,] -0.12473451 0.3069135

#> abc(n = c(10,20),yes = rep(5,2),a = rep(1,2))
#[,1] [,2]
#[1, ] -0.1228497 0.5304606


I have made a function so that it works when its arguments each have a length >= 2.

But I'm wondering why the function only works when its argument have each have a length of >= 3!

Am I missing something? (Any fix so the function works when length of its args are each of 2 as well?)

[Note: I always expect the output of function (i.e., CI) to be a matrix with 2 columns, length(n) rows, except when length(n) == 2. When length(n) == 2 I expect the output to have 1 row, and 2 columns.]

abc <- function(n, yes, a, b = a){

  p <- list()

 for(i in 1:length(n)){
   p[[i]] <- rbeta(1e3, a[i] + yes[i], b[i] + (n[i] - yes[i]))
   }
 ps <- combn(p, 2, FUN = function(x) x[[1]]- x[[2]])

 CI <- matrix(NA, length(n), 2)

 for(i in 1:length(n)){
 CI[i, ] <- quantile(ps[, i], c(.025, .975))
   }
 CI
  }

For example:

  abc(n = c(10, 20, 30), yes = rep(5, 3), a = rep(1, 3)) # Works well :-)

  abc(n = c(10, 20), yes = rep(5, 2), a = rep(1, 2))  # Doesn't work! :-(
  # Error in ps[, i] : subscript out of bounds

解决方案

There is easy fix to problem. Replace length(n) with ncol(ps) while creating result matrix and running for loop to copy values to CI. It makes more sense as number of combinations generate by 'combnwill more than actual length ofn`.

abc <- function(n, yes, a, b = a){

  p <- list()

  for(i in 1:length(n)){
    p[[i]] <- rbeta(1e3, a[i] + yes[i], b[i] + (n[i] - yes[i]))
  }

  str(p)

  ps <- combn(p, 2, FUN = function(x) x[[1]]- x[[2]])


  CI <- matrix(NA, ncol(ps), 2)

  for(i in 1:ncol(ps)){
    CI[i, ] <- quantile(ps[, i], c(.025, .975), na.rm = TRUE)
  }
  CI
}


#Results
#> abc(n = c(10, 20, 30), yes = rep(5, 3), a = rep(1, 3))
#           [,1]      [,2]
#[1,] -0.10141014 0.5774627
#[2,]  0.02638096 0.6159326
#[3,] -0.12473451 0.3069135

#> abc(n = c(10, 20), yes = rep(5, 2), a = rep(1, 2))
#           [,1]      [,2]
#[1,] -0.1228497 0.5304606

这篇关于为什么只有当参数的长度大于等于3时才起作用:R函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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