function [Qb,P] = test_Qb_mixed_2(R1,R2,sv1,sv2);
% [Qb,P] = test_Qb_mixed_2(R1,R2,sv1,sv2);
% tests for a significant between-class sum of squares, Qb, in a mixed-model
% meta-analysis comparing two classes, where P is the probability of seeing
% a greater Qb if classes don't differ. R1,R2 are the effect sizes and
% sv1,sv2 are the sampling variances of the effect sizes for the two classes;
% formulae from Gurevitch & Hedges (G&H) chapter in Design and Analysis of Ecological
% Experiments (S. Scheiner and J. Gurevitch, eds.)
W1=1./sv1; % weights are inverse of sampling variance
W2=1./sv2;
n1=length(R1);
n2=length(R2);
df1=n1-1;
df2=n2-1;
SW1=ones(1,n1)*W1; % sum of weights for group 1
SW2=ones(1,n2)*W2; % sum of weights for group 2
c1=SW1 - W1'*W1/SW1; % G&E eq. 18.16
c2=SW2 - W2'*W2/SW2;
Qw1=Q(R1,W1);
Qw2=Q(R2,W2);
Qw=Qw1+Qw2;
s2pooled=( Qw - (df1+df2) )/(c1+c2); % within-class variance pooled across classes
W1 = ( W1.^-1 + s2pooled ).^-1; % change weights to include within-class variance
W2 = ( W2.^-1 + s2pooled ).^-1;
SW1=sum(W1);
SW2=sum(W2);
E1dot=W1'*R1/SW1; % weighted mean effect sizes within classes
E2dot=W2'*R2/SW2;
Qb=SW1*E1dot^2 + SW2*E2dot^2 - (SW1*E1dot+SW2*E2dot)^2/(SW1+SW2); % G&H 18.21
P = 1 - chi2cdf(Qb,1); % prob. of seeing a greater Qb if no class differences