title: Call R from JAVA to get Chi-squared statistic and p-value
body: <p>I have two 4*4 matrices in JAVA, where one matrix holds observed counts and the other expected counts. </p>
  
  <p>I need an automated way to calculate the p-value from the chi-square statistic between these two matrices; however, JAVA has no such function as far as I am aware.</p>
  
  <p>I can calculate the chi-square and its p-value by reading the two matrices into R as .csv file formats, and then using the chisq.test function as follows:</p>
  
  <pre><code>obs&lt;-read.csv("obs.csv")
  exp&lt;-read.csv("exp.csv")
  chisq.test(obs,exp)
  </code></pre>
  
  <p>where the format of the .csv files would as follows:</p>
  
  <pre><code>A, C, G, T
  A, 197.136, 124.32, 63.492, 59.052
  C, 124.32, 78.4, 40.04, 37.24
  G, 63.492, 40.04, 20.449, 19.019
  T, 59.052, 37.24, 19.019, 17.689
  </code></pre>
  
  <p>Given these commands, R will give an output of the format:</p>
  
  <pre><code>X-squared = 20.6236, df = 9, p-value = 0.01443
  </code></pre>
  
  <p>which includes the p-value I was looking for.</p>
  
  <p>Does anyone know of an efficient way to automate the process of:</p>
  
  <p>1) Outputting my matrices from JAVA into .csv files
  2) Uploading the .csv files into R
  3) Calling the chisq.test on the .csv files into R
  4) Returning the outputted p-value back into JAVA?</p>
  
  <p>Thanks for any help....</p>