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 org.rosuda.REngine.RList;
24  import ubic.basecode.math.linearmodels.OneWayAnovaResult;
25  
26  /**
27   * @author paul
28   *
29   */
30  class OneWayAnovaResultImpl extends AbstractAnovaResult implements OneWayAnovaResult {
31  
32      private final String factorName;
33      private final double mainEffectDof;
34      private final double mainEffectFStat;
35      private final double mainEffectPValue;
36      private final double residualDof;
37      private final double residualFStat;
38      private final double residualPValue;
39  
40      /**
41       * A null result.
42       */
43      public OneWayAnovaResultImpl( String key ) {
44          super( key );
45          factorName = "null";
46          mainEffectDof = Double.NaN;
47          mainEffectFStat = Double.NaN;
48          mainEffectPValue = Double.NaN;
49          residualDof = Double.NaN;
50          residualFStat = Double.NaN;
51          residualPValue = Double.NaN;
52      }
53  
54      /**
55       * @param rAnovaTable from R
56       */
57      public OneWayAnovaResultImpl( String key, REXP rAnovaTable ) throws REXPMismatchException {
58          super( key );
59          this.factorName = rAnovaTable.getAttribute( "row.names" ).asStrings()[0];
60          RList list = rAnovaTable.asList();
61          this.mainEffectDof = list.at( "Df" ).asDoubles()[0];
62          this.mainEffectPValue = list.at( "Pr(>F)" ).asDoubles()[0];
63          this.mainEffectFStat = list.at( "F value" ).asDoubles()[0];
64          this.residualDof = list.at( "Df" ).asDoubles()[1];
65          this.residualFStat = list.at( "F value" ).asDoubles()[1];
66          this.residualPValue = list.at( "Pr(>F)" ).asDoubles()[1];
67      }
68  
69      @Override
70      public String getFactorName() {
71          return factorName;
72      }
73  
74      @Override
75      public double getMainEffectDof() {
76          return mainEffectDof;
77      }
78  
79      @Override
80      public double getMainEffectFStat() {
81          return mainEffectFStat;
82      }
83  
84      @Override
85      public double getMainEffectPValue() {
86          return mainEffectPValue;
87      }
88  
89      @Override
90      public boolean hasResiduals() {
91          return true;
92      }
93  
94      @Override
95      public double getResidualsDof() {
96          return residualDof;
97      }
98  
99      @Override
100     public double getResidualsFStat() {
101         return residualFStat;
102     }
103 
104     @Override
105     public double getResidualsPValue() {
106         return residualPValue;
107     }
108 
109     @Override
110     public String toString() {
111         return String.format( "Factor\tDf\tF\tP\n%s\t%.2f\t%.2f\t%.3g\nResidual\t%.2f\t%.2f\t%.3g", factorName,
112             mainEffectDof, mainEffectFStat, mainEffectPValue,
113             residualDof, residualFStat, residualPValue );
114     }
115 }