Call R from JAVA to get Chi-squared statistic and p-value

前端 未结 6 857
滥情空心
滥情空心 2020-12-19 23:49

I have two 4*4 matrices in JAVA, where one matrix holds observed counts and the other expected counts.

I need an automated way to calculate the p-value from the chi

6条回答
  •  忘掉有多难
    2020-12-20 00:20

    1) Outputting my matrices from JAVA into .csv files

    Use any of CSV libraies, I would recommend http://opencsv.sourceforge.net/

    2) Uploading the .csv files into R 3) Calling the chisq.test on the .csv files into R

    2 & 3 a pretty the same, You better create parametrized script to be run in R.

    obs<-read.csv(args[1])
    exp<-read.csv(args[2])
    chisq.test(obs,exp)
    

    So you can run

    RScript your_script.r path_to_csv1 path_to_csv2, 
    

    and use unique names for the csv files for example:

    UUID.randomUUID().toString().replace("-","")
    

    And then you use

    Runtime.getRuntime().exec(command, environments, dataDir);
    

    4) Returning the outputted p-value back into JAVA? You can only read the output of R if you are using getRuntime().exec() to invoke R.

    I would also recommend to take a look at Apache's Statistics Lib & How to calculate PValue from ChiSquare. Maybe you can live without R at all :)

提交回复
热议问题