title: Is there an equivalent function for anova.lm() in Java?
body: <p>I am comparing two linear models in R with Anova, and I would like to do the same thing in Java. To simplify it, I took the example code from <a href="https://stats.stackexchange.com/questions/48854/why-am-i-getting-different-intercept-values-in-r-and-java-for-simple-linear-regr">https://stats.stackexchange.com/questions/48854/why-am-i-getting-different-intercept-values-in-r-and-java-for-simple-linear-regr</a> and modified it a bit below. The models are <code>test_trait ~ geno_A + geno_B</code> and <code>test_trait ~ geno_A + geno_B + geno_A:geno_B</code>. The coefficients of the models implemented in R and Java are the same. In R I use <code>anova(fit, fit2)</code> where the fits are the results of lm and in Java I use <code>TestUtils.oneWayAnovaPValue</code> from <code>org.apache.commons.math3</code>.</p>
  
  <p>With R I get a pvalue of <code>0.797</code>, while with Java I get a pvalue of <code>0.817</code>, so this is not the right method, but I can not find how to do it correctly. Is there an equivalent of R's <code>anova.lm</code> in Java?</p>
  
  <p>The full code is below.</p>
  
  <p>R</p>
  
  <pre><code>test_trait &lt;- c( -0.48812477 , 0.33458213, -0.52754476, -0.79863471, -0.68544309, -0.12970239,  0.02355622, -0.31890850,0.34725819 , 0.08108851)
  geno_A &lt;- c(1, 0, 1, 2, 0, 0, 1, 0, 1, 0)
  geno_B &lt;- c(0, 0, 0, 1, 1, 0, 0, 0, 0, 0) 
  fit &lt;- lm(test_trait ~ geno_A+geno_B)
  fit2 &lt;- lm(test_trait ~ geno_A + geno_B + geno_A:geno_B)
  </code></pre>
  
  <p>which gives the coefficients</p>
  
  <pre><code>&gt; fit
  Call:
  lm(formula = test_trait ~ geno_A + geno_B)
  
  Coefficients:
  (Intercept)       geno_A       geno_B  
     -0.03233     -0.10479     -0.60492  
  
  &gt; fit2
  Call:
  lm(formula = test_trait ~ geno_A + geno_B + geno_A:geno_B)
  
  Coefficients:
    (Intercept)         geno_A         geno_B  geno_A:geno_B  
      -0.008235      -0.152979      -0.677208       0.096383  
  </code></pre>
  
  <p>And the Anova</p>
  
  <pre><code>&gt; anova(fit, fit2) # 0.797 
  Analysis of Variance Table
  
  Model 1: test_trait ~ geno_A + geno_B
  Model 2: test_trait ~ geno_A + geno_B + geno_A:geno_B
    Res.Df     RSS Df Sum of Sq      F Pr(&gt;F)
  1      7 0.77982                           
  2      6 0.77053  1 0.0092897 0.0723  0.797
  </code></pre>
  
  <p>Java </p>
  
  <pre><code>    double [] y =  {-0.48812477,  0.33458213,  
              -0.52754476, -0.79863471,
              -0.68544309, -0.12970239,
               0.02355622, -0.31890850,
               0.34725819,  0.08108851};
  double [][] x = {{1,0}, {0,0},
                   {1,0}, {2,1},
                   {0,1}, {0,0},
                   {1,0}, {0,0},
                   {1,0}, {0,0}};
  double [][] xb = {{1,0,0}, {0,0,0},
                    {1,0,0}, {2,1,2},
                    {0,1,0}, {0,0,0},
                    {1,0,0}, {0,0,0},
                    {1,0,0}, {0,0,0}};
  
  OLSMultipleLinearRegression regr = new OLSMultipleLinearRegression();
  regr.newSampleData(y, x);
  double[] beta = regr.estimateRegressionParameters();   
  
  System.out.printf("First model: y = int + genoA + genoB\n");
  System.out.printf("Intercept: %.3f\t", beta[0]);
  System.out.printf("beta1: %.3f\t", beta[1]);
  System.out.printf("beta2: %.3f\n\n", beta[2]);
  
  regr.newSampleData(y, xb);
  double[] betab = regr.estimateRegressionParameters();   
  
  System.out.printf("Second model: y = int + genoA + genoB + genoA:genoB\n");
  System.out.printf("Intercept: %.3f\t", betab[0]);
  System.out.printf("beta1: %.3f\t", betab[1]);
  System.out.printf("beta2: %.3f\t", betab[2]);
  System.out.printf("beta2: %.3f\n", betab[3]);
  </code></pre>
  
  <p>Which gives the same coefficients as in R</p>
  
  <pre><code>First model: y = int + genoA + genoB
  Intercept: -0.032   beta1: -0.105   beta2: -0.605
  
  Second model: y = int + genoA + genoB + genoA:genoB
  Intercept: -0.008   beta1: -0.153   beta2: -0.677   beta2: 0.096
  </code></pre>
  
  <p>But the Anova gives a different result</p>
  
  <pre><code>List classes = new ArrayList();
  classes.add(beta);
  classes.add(betab);
  double pvalue = TestUtils.oneWayAnovaPValue(classes);
  double fvalue = TestUtils.oneWayAnovaFValue(classes);
  System.out.println(pvalue); 
  System.out.println(fvalue); 
  
  0.8165390406874127
  0.05979444576790511
  </code></pre>