1919
2020package ubic .basecode .math .linearmodels ;
2121
22+ import java .io .File ;
23+ import java .io .IOException ;
24+ import java .io .OutputStream ;
25+ import java .io .PrintStream ;
2226import java .util .List ;
2327
2428import cern .colt .function .DoubleFunction ;
@@ -96,7 +100,17 @@ public static void ebayes(LeastSquaresFit fit) {
96100 * @param x
97101 * @return
98102 */
99- private static final BooleanArrayList ok (DoubleMatrix1D x ) {
103+ private static final BooleanArrayList okVars (DoubleMatrix1D x ) {
104+ assert x .size () > 0 ;
105+ BooleanArrayList answer = new BooleanArrayList (x .size ());
106+ for (int i = 0 ; i < x .size (); i ++) {
107+ double a = x .getQuick (i );
108+ answer .add (!(Double .isNaN (a ) || Double .isInfinite (a ) || a < -1e-15 ));
109+ }
110+ return answer ;
111+ }
112+
113+ private static final BooleanArrayList okDfs (DoubleMatrix1D x ) {
100114 assert x .size () > 0 ;
101115 BooleanArrayList answer = new BooleanArrayList (x .size ());
102116
@@ -110,11 +124,11 @@ private static final BooleanArrayList ok(DoubleMatrix1D x) {
110124 /*
111125 * @param vars variances
112126 * @param df1s vector of degrees of freedom
113- * @return the scale and df2 in a double array of length 2
127+ * @return the scale (aka s2.prior or s20 in limma) and df2 (aka df.prior) in a double array of length 2
114128 */
115129 protected static double [] fitFDist (final DoubleMatrix1D vars , final DoubleMatrix1D df1s ) {
116130
117- BooleanArrayList ok = MatrixUtil .conjunction (ok (vars ), ok (df1s ));
131+ BooleanArrayList ok = MatrixUtil .conjunction (okVars (vars ), okDfs (df1s ));
118132
119133 DoubleMatrix1D x = MatrixUtil .stripNonOK (vars , ok );
120134 DoubleMatrix1D df1 = MatrixUtil .stripNonOK (df1s , ok );
@@ -123,8 +137,8 @@ protected static double[] fitFDist(final DoubleMatrix1D vars, final DoubleMatrix
123137 throw new IllegalStateException ("There were no valid values of variance to perform eBayes parameter estimation" );
124138 }
125139
126- // stay away from zero values of variance
127- x = x .assign (Functions .max (1e-5 * DescriptiveWithMissing .median ( new DoubleArrayList (vars .toArray ()) )));
140+ // stay away from zero variance
141+ x = x .assign (Functions .max (1e-5 * DescriptiveWithMissing .median (new DoubleArrayList (vars .toArray ()))));
128142
129143 // z <- log(x)
130144 DoubleMatrix1D z = x .copy ().assign (Functions .log );
@@ -169,6 +183,7 @@ protected static double[] fitFDist(final DoubleMatrix1D vars, final DoubleMatrix
169183 /*
170184 * Return the scale and df2. Original implementation, does not handle missing values, kept here for posterity/comparison/debugging.
171185 */
186+ @ Deprecated
172187 private static double [] fitFDistNoMissing (DoubleMatrix1D x , double df1 ) {
173188
174189 // stay away from zero valuess
@@ -227,10 +242,10 @@ protected static DoubleMatrix1D squeezeVar(DoubleMatrix1D var, DoubleMatrix1D df
227242 }
228243
229244 /**
230- * @param var vector of estimated residual variances
245+ * @param var vector of estimated residual variances from original model fit
231246 * @param df vector of dfs
232- * @param fit result of fitFDist()
233- * @return vector of squeezed variances (varPost)
247+ * @param fit result of fitFDist() (s2.prior and dfPrior)
248+ * @return vector of squeezed variances (varPost or s2.post )
234249 */
235250 protected static DoubleMatrix1D squeezeVariances (DoubleMatrix1D var , DoubleMatrix1D df , double [] fit ) {
236251 double varPrior = fit [0 ];
0 commit comments