结合R中的标准偏差的现有函数? [英] existing function to combine standard deviations in R?

查看:78
本文介绍了结合R中的标准偏差的现有函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个总体,这些均值和标准偏差均已知。我想知道庄严的意思和盛大的sd。总体平均数显然很容易计算,但是R有一个方便的效用函数weighted.mean()。

计算并不复杂,但是现有功能可以使我的代码更简洁,更易于理解。



奖金问题,您使用什么工具搜索此类功能?我知道它一定在外面,但是我已经做了很多搜索,找不到它。谢谢!

解决方案

人口是否不重叠?

 库(鱼法)
Combinevar

例如维基百科将像这样工作:

  xbar<-c(70,65)
s< -c(3 ,2)
n<-c(1,1)
Combinevar(xbar,s,n)

,标准偏差为sqrt(combinevar(xbar,s,n)[2])



如果您不想下载库函数如下:

  combinevar<-
函数(xbar = NULL,s_squared = NULL,n = NULL)
{
if(length(xbar)!=长度(s_squared)|长度(xbar)!=长度(n)|
length(s_squared)!=长度(n) )
stop(向量长度不同。)
sum_of_squares<-sum((n-1)* s_squared + n * xbar ^ 2)
grand_mean<-sum(n * xbar)/ sum(n)
Combined_var<--(sum_of_squares-sum(n)* gra nd_mean ^ 2)/(sum(n)-
1)
return(c(grand_mean,Combined_var))
}


I have 4 populations with known means and standard deviations. I would like to know the grand mean and grand sd. The grand mean is obviously simple to calculate, but R has a handy utility function, weighted.mean(). Does a similar function exist for combining standard deviations?

The calculation is not complicated, but an existing function would make my code cleaner and easier to understand.

Bonus question, what tools do you use to search for functions like this? I know it must be out there, but I've done a lot of searching and can't find it. Thanks!

解决方案

Are the populations non overlapping?

library(fishmethods)
combinevar

For instance the example in wikipedia would work like this:

xbar <- c(70,65)
s<-c(3,2)
n <- c(1,1)
combinevar(xbar,s,n)

and standard deviation would be sqrt(combinevar(xbar,s,n)[2])

if you don't want to download the library the function goes like this:

combinevar <- 
function (xbar = NULL, s_squared = NULL, n = NULL) 
{
    if (length(xbar) != length(s_squared) | length(xbar) != length(n) | 
        length(s_squared) != length(n)) 
        stop("Vector lengths are different.")
    sum_of_squares <- sum((n - 1) * s_squared + n * xbar^2)
    grand_mean <- sum(n * xbar)/sum(n)
    combined_var <- (sum_of_squares - sum(n) * grand_mean^2)/(sum(n) - 
        1)
    return(c(grand_mean, combined_var))
}

这篇关于结合R中的标准偏差的现有函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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