Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:编写用于为加权数据集构造CDF的快速函数_R_Function_Statistics_Frequency - Fatal编程技术网

R:编写用于为加权数据集构造CDF的快速函数

R:编写用于为加权数据集构造CDF的快速函数,r,function,statistics,frequency,R,Function,Statistics,Frequency,我有一个加权数据集,我正试图在R中编写一个基本的累积分布函数,它通过将加权数据拆分为大小相等的容器来工作。我找不到有效执行此操作的R命令,因此我编写了此函数: cdf <- function(x,wt){ array <- rep(x,wt) l = round(length(array)/100) # grouping into percentiles c = unname(tapply(array, (seq_along(array)-1) %/% l, sum))

我有一个加权数据集,我正试图在R中编写一个基本的累积分布函数,它通过将加权数据拆分为大小相等的容器来工作。我找不到有效执行此操作的R命令,因此我编写了此函数:

cdf <- function(x,wt){
  array <- rep(x,wt)
  l = round(length(array)/100) # grouping into percentiles
  c = unname(tapply(array, (seq_along(array)-1) %/% l, sum))
  pdf = unname(tapply(array, (seq_along(array)-1) %/% l, max))
  # for uneven group sizes - adding more entries last bin
  if(length(c)>100) { 
    c[100] = sum(c[c(100,101)])
    pdf[100] = max(pdf[c(100,101)])
  }
  cdf = cumsum(as.numeric(c[1:100]))
  pdf = pdf[1:100]
  return(list(cdf = cdf, pdf = pdf))
}

cdf好吧,对于未加权的数据,这种方法相当于计算每个箱子中有多少数据,对吗?对于每个值,查看它属于哪个箱子,并将1/n添加到箱子总数中。为了推广到加权数据,只需将w/Z添加到bin总数中,其中Z=所有w的总和,其中w是一个权重。