View Javadoc
1   /*
2    * The baseCode project
3    *
4    * Copyright (c) 2010 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.util.r;
20  
21  import org.rosuda.REngine.REXP;
22  import org.rosuda.REngine.REXPMismatchException;
23  import ubic.basecode.math.linearmodels.TwoWayAnovaResult;
24  
25  /**
26   * Represents a two-way ANOVA table
27   *
28   * @author paul
29   */
30  class TwoWayAnovaResultImpl extends AbstractAnovaResult implements TwoWayAnovaResult {
31  
32      private final String factorAName;
33      private final String factorBName;
34      private final double mainEffectADof;
35      private final double mainEffectAFStat;
36      private final double mainEffectAPValue;
37      private final double mainEffectBDof;
38      private final double mainEffectBFStat;
39      private final double mainEffectBPValue;
40      private final boolean hasInteraction;
41      private final double interactionDof;
42      private final double interactionFStat;
43      private final double interactionPValue;
44      private final double residualDof;
45      private final double residualFStat;
46      private final double residualPValue;
47  
48      /**
49       * A null result.
50       */
51      public TwoWayAnovaResultImpl( String key ) {
52          super( key );
53          this.factorAName = "null";
54          this.factorBName = "null";
55          this.hasInteraction = false;
56          this.interactionDof = Double.NaN;
57          this.interactionFStat = Double.NaN;
58          this.interactionPValue = Double.NaN;
59          this.mainEffectADof = Double.NaN;
60          this.mainEffectAFStat = Double.NaN;
61          this.mainEffectAPValue = Double.NaN;
62          this.mainEffectBDof = Double.NaN;
63          this.mainEffectBFStat = Double.NaN;
64          this.mainEffectBPValue = Double.NaN;
65          this.residualDof = Double.NaN;
66          this.residualPValue = Double.NaN;
67          this.residualFStat = Double.NaN;
68      }
69  
70      /**
71       * @param rAnovaTable from R
72       */
73      public TwoWayAnovaResultImpl( String key, REXP rAnovaTable ) throws REXPMismatchException {
74          super( key );
75          String[] names = rAnovaTable.getAttribute( "row.names" ).asStrings();
76          this.factorAName = names[0];
77          this.factorBName = names[1];
78  
79          double[] pvs = rAnovaTable.asList().at( "Pr(>F)" ).asDoubles();
80  
81          this.hasInteraction = pvs.length == 4; // interaction is present if there are 4 p-values, f-stats, dof, etc.
82  
83          this.mainEffectAPValue = pvs[0];
84          this.mainEffectBPValue = pvs[1];
85          if ( hasInteraction ) {
86              this.interactionPValue = pvs[2];
87              this.residualPValue = pvs[3];
88          } else {
89              this.interactionPValue = Double.NaN;
90              this.residualPValue = pvs[2];
91          }
92  
93          double[] dfs = rAnovaTable.asList().at( "Df" ).asDoubles();
94          this.mainEffectADof = dfs[0];
95          this.mainEffectBDof = dfs[1];
96          if ( dfs.length == 4 ) {
97              this.interactionDof = dfs[2];
98              this.residualDof = dfs[3];
99          } else {
100             this.interactionDof = Double.NaN;
101             this.residualDof = dfs[2];
102         }
103 
104         double[] fs = rAnovaTable.asList().at( "F value" ).asDoubles();
105 
106         this.mainEffectAFStat = fs[0];
107         this.mainEffectBFStat = fs[1];
108         if ( hasInteraction ) {
109             this.interactionFStat = fs[2];
110             this.residualFStat = fs[3];
111         } else {
112             this.interactionFStat = Double.NaN;
113             this.residualFStat = fs[2];
114         }
115     }
116 
117     /**
118      * @return the factorAName
119      */
120     @Override
121     public String getFactorAName() {
122         return factorAName;
123     }
124 
125     /**
126      * @return the factorBName
127      */
128     @Override
129     public String getFactorBName() {
130         return factorBName;
131     }
132 
133     /**
134      * @return the interactionDof
135      */
136     @Override
137     public double getInteractionDof() {
138         return interactionDof;
139     }
140 
141     @Override
142     public double getInteractionFStat() {
143         return interactionFStat;
144     }
145 
146     @Override
147     public double getInteractionPValue() {
148         return interactionPValue;
149     }
150 
151     @Override
152     public double getMainEffectADof() {
153         return mainEffectADof;
154     }
155 
156     @Override
157     public double getMainEffectAFStat() {
158         return mainEffectAFStat;
159     }
160 
161     @Override
162     public double getMainEffectAPValue() {
163         return mainEffectAPValue;
164     }
165 
166     @Override
167     public double getMainEffectBDof() {
168         return mainEffectBDof;
169     }
170 
171     @Override
172     public double getMainEffectBFStat() {
173         return mainEffectBFStat;
174     }
175 
176     @Override
177     public double getMainEffectBPValue() {
178         return mainEffectBPValue;
179     }
180 
181     @Override
182     public boolean hasResiduals() {
183         return true;
184     }
185 
186     @Override
187     public double getResidualsDof() {
188         return residualDof;
189     }
190 
191     @Override
192     public double getResidualsFStat() {
193         return residualFStat;
194     }
195 
196     @Override
197     public double getResidualsPValue() {
198         return residualPValue;
199     }
200 
201     @Override
202     public boolean hasInteraction() {
203         return hasInteraction;
204     }
205 
206     @Override
207     public String toString() {
208 
209         return String.format(
210             "Factor\tDof\tF\tP\n%s\t%.2f\t%.2f\t%.3g\n%s\t%.2f\t%.2f\t%.3g\nInteraction\t%.2f\t%.2f\t%.3g\nResidual\t%.2f",
211             factorAName, mainEffectADof, mainEffectAFStat, mainEffectAPValue, factorBName, mainEffectBDof,
212             mainEffectBFStat, mainEffectBPValue, interactionDof, interactionFStat, interactionPValue, residualDof );
213     }
214 
215 }