View Javadoc
1   /*
2    * The baseCode project
3    * 
4    * Copyright (c) 2006 University of British Columbia
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   *
18   */
19  package ubic.basecode.math.metaanalysis;
20  
21  import cern.colt.list.DoubleArrayList;
22  import cern.jet.stat.Descriptive;
23  import cern.jet.stat.Probability;
24  
25  /**
26   * Meta-analysis methods from chapter 18 of Cooper and Hedges, sections 2.1 and 3.1
27   * <p>
28   * These methods use the standardized mean difference statistic d:
29   * 
30   * <pre>
31   * d_i = ( X_i &circ; t - X_i &circ; c ) / s_i
32   * </pre>
33   * 
34   * where X <sub>i </sub> <sup>t </sup> is the mean of the treatment group in the ith study, X <sub>i </sub> <sup>ct
35   * </sup> is the mean of the control group in the treatment group in the ith study, and s <sub>i </sub> is the pooled
36   * standard deviation of the two groups. Essentially this is a t statistic.
37   * 
38   * @author pavlidis
39   * 
40   */
41  public class MeanDifferenceMetaAnalysis extends MetaAnalysis {
42  
43      private double bsv; // between-studies variance component;
44  
45      private double e; // unconditional effect;
46      private boolean fixed = true;
47      private double n; // total sample size
48      private double p; // probability
49      private double q; // q-score;
50      private double v; // unconditional variance;
51      private double z; // z score
52  
53      /**
54       * @param b
55       */
56      public MeanDifferenceMetaAnalysis( boolean fixed ) {
57          this.fixed = fixed;
58      }
59  
60      public double getBsv() {
61          return bsv;
62      }
63  
64      public double getE() {
65          return e;
66      }
67  
68      public double getN() {
69          return n;
70      }
71  
72      public double getP() {
73          return p;
74      }
75  
76      public double getQ() {
77          return q;
78      }
79  
80      public double getV() {
81          return v;
82      }
83  
84      public double getZ() {
85          return z;
86      }
87  
88      /**
89       * @param effects
90       * @param cvar Conditional variances.
91       * @return
92       */
93      public double run( DoubleArrayList effects, DoubleArrayList cvar ) {
94          DoubleArrayList weights;
95          DoubleArrayList conditionalVariances;
96          // this.n = Descriptive.sum( controlSizes ) + Descriptive.sum( testSizes );
97  
98          conditionalVariances = cvar.copy();
99          weights = metaFEWeights( conditionalVariances );
100         this.q = super.qStatistic( effects, conditionalVariances, super.weightedMean( effects, weights ) );
101 
102         if ( !fixed ) { // adjust the conditional variances and weights.
103             this.bsv = metaREVariance( effects, conditionalVariances, weights );
104 
105             for ( int i = 0; i < conditionalVariances.size(); i++ ) {
106                 conditionalVariances.setQuick( i, conditionalVariances.getQuick( i ) + bsv );
107             }
108 
109             weights = metaFEWeights( conditionalVariances );
110         }
111 
112         this.e = super.weightedMean( effects, weights );
113         this.v = super.metaVariance( conditionalVariances );
114         this.z = Math.abs( e ) / Math.sqrt( v );
115         this.p = Probability.errorFunctionComplemented( z );
116         return p;
117     }
118 
119     public double run( DoubleArrayList effects, DoubleArrayList controlSizes, DoubleArrayList testSizes ) {
120         DoubleArrayList weights;
121         DoubleArrayList conditionalVariances;
122         this.n = Descriptive.sum( controlSizes ) + Descriptive.sum( testSizes );
123 
124         conditionalVariances = samplingVariances( effects, controlSizes, testSizes );
125         weights = metaFEWeights( conditionalVariances );
126         this.q = super.qStatistic( effects, conditionalVariances, super.weightedMean( effects, weights ) );
127 
128         if ( !fixed ) { // adjust the conditional variances and weights.
129             this.bsv = metaREVariance( effects, conditionalVariances, weights );
130 
131             for ( int i = 0; i < conditionalVariances.size(); i++ ) {
132                 conditionalVariances.setQuick( i, conditionalVariances.getQuick( i ) + bsv );
133             }
134 
135             weights = metaFEWeights( conditionalVariances );
136         }
137 
138         this.e = super.weightedMean( effects, weights );
139         this.v = super.metaVariance( conditionalVariances );
140         this.z = Math.abs( e ) / Math.sqrt( v );
141         this.p = Probability.errorFunctionComplemented( z );
142         return p;
143     }
144 
145     /**
146      * CH eqn 18-7
147      * 
148      * @param d effect size
149      * @param nC number of samples in control group
150      * @param nT number of samples in test group
151      * @return
152      */
153     public double samplingVariance( double d, double nC, double nT ) {
154         return ( nT + nC ) / ( nT * nC ) + d * d / 2 * ( nT + nC );
155     }
156 
157     /**
158      * Run eqn 18-7 on a set of effect sizes.
159      * 
160      * @param effects
161      * @param controlSizes
162      * @param testSizes
163      * @return
164      */
165     public DoubleArrayList samplingVariances( DoubleArrayList effects, DoubleArrayList controlSizes,
166             DoubleArrayList testSizes ) {
167         if ( effects.size() != controlSizes.size() || controlSizes.size() != testSizes.size() )
168             throw new IllegalArgumentException( "Unequal sample sizes." );
169 
170         DoubleArrayList answer = new DoubleArrayList( controlSizes.size() );
171         for ( int i = 0; i < controlSizes.size(); i++ ) {
172             answer.add( samplingVariance( effects.getQuick( i ), controlSizes.getQuick( i ), testSizes.getQuick( i ) ) );
173         }
174         return answer;
175     }
176 
177 }