[英]calculate mean and variance for weighted discrete random variables in R
I have the following data frame: 我有以下数据框:
dat <- read.table(text=" X prob
1 1 0.1
2 2 0.2
3 3 0.4
4 4 0.3", header=TRUE)
Is there any built-in function or elegant way to calulate mean and variance for discrete random variables in R? 是否有任何内置函数或简洁的方法来计算R中离散随机变量的均值和方差?
There is a weighted.mean
function in base R and the Hmisc package has a bunch of wtd.* functions. 基数R中有一个weighted.mean
函数,Hmisc包中有一堆wtd。*函数。
> with(dat, weighted.mean(X, prob))
[1] 2.9
require(Hmisc)
> wtd.var(x=dat$X, weights=dat$prob)
[1] Inf
# Huh ? On investigation the weights argument is suppsed to be replicate weights
# So it's more appropriate to use normwt=TRUE
> wtd.var(x=dat$X, weights=dat$prob, normwt=TRUE)
[1] 1.186667
The survey package from Thomas Lumley provides much more than this simplistic example illustrates. 托马斯·拉姆利(Thomas Lumley)的调查软件包所提供的远远超出了这个简单例子所能说明的。 It has the mechanism for handling complex weighting schemes for a variety of statistical modeling procedures: 它具有处理各种统计建模程序的复杂加权方案的机制:
require(survey)
> dclus1<-svydesign(id=~1, weights=~prob, data=dat)
> v<-svyvar(~X, dclus1)
> v
variance SE
X 1.1867 0.7011
These are sample statistics rather than the variances that would be calculated for abstract random variables. 这些是样本统计信息,而不是为抽象随机变量计算的方差。 This result does seem appropriate for a statistical system, but might not be the correct answer for a probability homework question. 这个结果似乎确实适用于统计系统,但可能不是概率作业问题的正确答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.