[R] Command to combine means?

Jim Lemon drjimlemon at gmail.com
Fri Jul 1 01:33:00 CEST 2016


Hi Carlos,
The STATA function assumes estimated population SDs. If you have
sample SDs you can specify with this:

combine<-function(n,mu,sd,sd.type=c("pop","sample")) {
 N<-sum(n)
 mean<-sum(n*mu)/N
 if(sd.type[1]=="sample") {
  meanss<-(n[1]*(mean-mu[1])^2+n[2]*(mean-mu[2])^2)/N
  SD<-sqrt(sum(sd*sd*n)/N+meanss)
 } else {
  meanss<-(n[1]*(mean-mu[1])^2+n[2]*(mean-mu[2])^2)/(N-1)
  SD<-sqrt(sum(sd*sd*(n-1))/(N-1)+meanss)
 }
 return(list(N,mean,SD))
}
combine(c(12,34),c(3,45),c(1,4))
combine(c(12,34),c(3,45),c(1,4),"sample")

Jim


On Fri, Jul 1, 2016 at 1:02 AM, Carlos R. Moreira Maia
<crm.maia at gmail.com> wrote:
> Dear all,
> I'm newbie with R (changing from STATA), and I can't find some commands.
> One of those is the "combine", which I use to combine means like this:
>
> --------------------------------------------
> n1 m1 sd1  n2 m2 sd2
>
> combine 12 3 1 34 45 4
>
> Combine has calculated the following values:
>     combined n = 46
>     combined mean = 34.043478
>     combined SD = 18.964829
> --------------------------------------------
>
> Does anybody knows a simmilar command in R to combine means?
>
> Thanks in advance.
>
> Carlos.
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list