-->

计算样本的统计信息,其被存储为频率表与领带数据向量(Compute sample statistic

2019-09-30 22:18发布

我想从捆绑带值的数据向量的一些汇总统计(均值,方差和位数)。 特别是,它被存储在频率分布表中:唯一的数据值var和关系数frequency

我知道我可以使用rep函数首先扩展矢量充分发挥其格式为:

xx <- rep(mydata$var, mydata$frequency)

然后做标准

mean(xx)
var(xx)
quantile(xx)

不过频率实在是大,我有许多独特的价值,这使得程序非常慢。 有没有办法直接从计算这些统计varfrequency

Answer 1:

set.seed(0)
x <- runif(10)                ## unique data values
k <- sample.int(5, 10, TRUE)  ## frequency

n <- sum(k)
xx <- rep.int(x, k)           ## "expanded" data

#################
## sample mean ##
#################

mean(xx)  ## using `xx`
#[1] 0.6339458

mu <- c(crossprod(x, k)) / n  ## using `x` and `k`
#[1] 0.6339458

#####################
## sample variance ##
#####################

var(xx) * (n - 1) / n  ## using `xx`
#[1] 0.06862544

v <- c(crossprod(x ^ 2, k)) / n - mu * mu  ## using `x` and `k`
#[1] 0.06862544

计算位数是更多地参与,但可行的。 我们需要先了解位数以标准的方式计算。

xx <- sort(xx)
pp <- seq(0, 1, length = n)
plot(pp, xx); abline(v = pp, col = 8, lty = 2)

标准位数计算是线性内插的问题。 然而,当数据有关系,我们可以清楚地看到,有“运行”(同等价值的),并在图中的“跳跃”(两个值之间)。 线性插值只需要在“跳跃”,而在“运行”中位数只是运行值。

下面的函数仅找到使用分位数xk 。 出于演示的目的,有一种说法verbose 。 如果TRUE它会产生一个情节和含有“运行”(和“跳”)的信息的数据帧。

find_quantile <- function (x, k, prob = seq(0, 1, length = 5), verbose = FALSE) {

  if (is.unsorted(x)) {
    ind <- order(x); x <- x[ind]; k <- k[ind]
    }

  m <- length(x)     ## number of unique values
  n <- sum(k)        ## number of data
  d <- 1 / (n - 1)   ## break [0, 1] into (n - 1) intervals

  ## the right and left end of each run
  r <- (cumsum(k) - 1) * d
  l <- r - (k - 1) * d

  if (verbose) {

    breaks <- seq(0, 1, d)
    plot(r, x, "n", xlab = "prob (p)", ylab = "quantile (xq)", xlim = c(0, 1))
    abline(v = breaks, col = 8, lty = 2)

    ## sketch each run
    segments(l, x, r, x, lwd = 3)

    ## sketch each jump
    segments(r[-m], x[-m], l[-1], x[-1], lwd = 3, col = 2)

    ## sketch `prob`
    abline(v = prob, col = 3)

    print( data.frame(x, k, l, r) )
    }

  ## initialize the vector of quantiles 
  xq <- numeric(length(prob))

  run <- rbind(l, r)
  i <- findInterval(prob, run, rightmost.closed = TRUE)

  ## odd integers in `i` means that `prob` lies on runs
  ## quantiles on runs are just run values
  on_run <- (i %% 2) != 0
  run_id <- (i[on_run] + 1) / 2
  xq[on_run] <- x[run_id]

  ## even integers in `i` means that `prob` lies on jumps
  ## quantiles on jumps are linear interpolations
  on_jump <- !on_run
  jump_id <- i[on_jump] / 2
  xl <- x[jump_id]      ## x-value to the left of the jump
  xr <- x[jump_id + 1]  ## x-value to the right of the jump
  pl <- r[jump_id]      ## percentile to the left of the jump
  pr <- l[jump_id + 1]  ## percentile to the right of the jump
  p  <- prob[on_jump]   ## probability on the jump
  ## evaluate the line `(pl, xl) -- (pr, xr)` at `p`
  xq[on_jump] <- (xr - xl) / (pr - pl) * (p - pl) + xl

  xq
  }

将所述函数应用于所述示例性数据与上面verbose = TRUE给出:

result <- find_quantile(x, k, prob = seq(0, 1, length = 5), TRUE)

#           x k         l         r
#1  0.2016819 4 0.0000000 0.1111111
#2  0.2655087 2 0.1481481 0.1851852
#3  0.3721239 1 0.2222222 0.2222222
#4  0.5728534 4 0.2592593 0.3703704
#5  0.6291140 2 0.4074074 0.4444444
#6  0.6607978 5 0.4814815 0.6296296
#7  0.8966972 1 0.6666667 0.6666667
#8  0.8983897 3 0.7037037 0.7777778
#9  0.9082078 2 0.8148148 0.8518519
#10 0.9446753 4 0.8888889 1.0000000

该数据帧的每一行是一个“运行”。 x给出了运行值, k是游程长度,和lr是运行的左侧和右侧百分。 在图中,“运行”被描绘在黑色水平线。

信息“跳跃”是由隐含r x行和值lx下一行的值。 在图中,“跳跃”是红色的线条。

垂直绿线信号的prob我们给出的值。

计算的位数是

result
#[1] 0.2016819 0.5226710 0.6607978 0.8983897 0.9446753

其等同于

quantile(xx, names = FALSE)
#[1] 0.2016819 0.5226710 0.6607978 0.8983897 0.9446753


文章来源: Compute sample statistics for a data vector with ties which is stored as a frequency table