Dear R-community, I am trying to get a function for a weighted averaging of d's and var(d) (within study) to output properly. When I average one study at a time the output is precise. When using the function with multiple studies simultaneously the weighted average of d is precise but var(d) is inaccurate. Been struggling with this for some time. Any help is much appreciated--thanks! AC Del Re # function for a weighted average of d & var(d) Agg_d <- function(d) { N<- mean(n) N_ES <- length(d) corr.mat <- matrix (rep(.5, N_ES^2), nrow=N_ES) diag(corr.mat) <- 1 d1d2 <- cbind(d) %*% d PSI <- (8*corr.mat + d1d2*corr.mat^2)/(2*N) PSI.inv <- solve(PSI) a <- rowSums(PSI.inv)/sum(PSI.inv) var_d.agg <- 1/sum(PSI.inv) d.agg <- sum(d*a) out<-cbind(d.agg,var_d.agg) return(out) } # weighted average of d's & var(d) for study 1 (Output is correct) id<-c(1,1) n<-c(19,19) d<-c(0.984,0.764) dagg1<-tapply(d,id,Agg_d) dagg1 $`1` d.agg var_d.agg [1,] 0.86939 0.1704719 # weighted average of d's and var(d) for study 2 (Output is correct) id<-c(2,2,2) n<-c(40,40,40) d<-c(0.823,0.863,0.823) dagg2<-tapply(d,id,Agg_d) dagg2 $`2` d.agg var_d.agg [1,] 0.8361852 0.0710382 # weighted average of d's and var(d) for both study 1 & 2 simultaneously (Output is WRONG for var!) id<-c(1,1,2,2,2) n<-c(19,19,40,40,40) d<-c(0.984,0.764,0.823,0.863,0.823) dagg3<-tapply(d,id,Agg_d) dagg3 $`1` d.agg var_d.agg [1,] 0.86939 0.1024989 $`2` d.agg var_d.agg [1,] 0.8361852 0.08992177 [[alternative HTML version deleted]]