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.math.linearmodels;
20  
21  import org.apache.commons.lang3.StringUtils;
22  
23  import java.io.Serializable;
24  
25  /**
26   * Represents one row of an ANOVA table
27   *
28   * @author paul
29   *
30   */
31  public class AnovaEffect implements Serializable {
32  
33      private static final long serialVersionUID = 1L;
34  
35      private final double dof;
36      private final String effectName;
37      private final double fStat;
38      private final boolean isInteraction;
39      private final boolean isResiduals;
40      private final double meanSq;
41      private final double pValue;
42      private final double ssQ;
43  
44      public AnovaEffect( String effectName, double pValue, double fStat, double dof, double ssQ, boolean isInteraction, boolean isResiduals ) {
45          if ( isResiduals && isInteraction ) {
46              throw new IllegalArgumentException( "An ANOVA effect cannot be both a residual and an interaction." );
47          }
48          this.effectName = effectName;
49          this.pValue = pValue;
50          this.fStat = fStat;
51          this.dof = dof;
52          this.ssQ = ssQ;
53          this.meanSq = ssQ / dof;
54          this.isInteraction = isInteraction;
55          this.isResiduals = isResiduals;
56      }
57  
58      /**
59       * @return the degreesOfFreedom
60       */
61      public double getDof() {
62          return dof;
63      }
64  
65      /**
66       * @return the effectName
67       */
68      public String getEffectName() {
69          return effectName;
70      }
71  
72      /**
73       * @return the fStatistic
74       */
75      public double getFStat() {
76          return fStat;
77      }
78  
79      /**
80       * @return the meanSq
81       */
82      public double getMeanSq() {
83          return meanSq;
84      }
85  
86      /**
87       * @return the pValue
88       */
89      public double getPValue() {
90          return pValue;
91      }
92  
93      /**
94       * @return the ssQ
95       */
96      public double getSsQ() {
97          return ssQ;
98      }
99  
100     /**
101      * @return the isInteraction
102      */
103     public boolean isInteraction() {
104         return isInteraction;
105     }
106 
107     public boolean isResiduals() {
108         return isResiduals;
109     }
110 
111     @Override
112     public String toString() {
113         StringBuilder buf = new StringBuilder();
114         buf.append( StringUtils.rightPad( StringUtils.abbreviate( getEffectName(), 10 ), 10 ) ).append( "\t" );
115         buf.append( String.format( "%.2f", getDof() ) ).append( "\t" );
116         buf.append( String.format( "%.4f", getSsQ() ) ).append( "\t" );
117         buf.append( String.format( "%.4f", getMeanSq() ) ).append( "\t" );
118         if ( !Double.isNaN( fStat ) ) {
119             buf.append( StringUtils.rightPad( String.format( "%.3f", getFStat() ), 6 ) ).append( "\t" );
120             buf.append( String.format( "%.3g", getPValue() ) );
121         }
122         return buf.toString();
123     }
124 }