Library for Chi square test for independence on contingency table?

I have a 2*2 contingency table of the kind

[[13,20],[9,15]]

I would like to calculate a Chi square test of independence and have looked into various libraries (ruby-statistics, statsample, distribution) for a test that will calculate expected values on its own and calculate p-value, degrees of freedom etc. Only statsample seems to deliver a function for this, but the library creates an error in the prawn dependency when running. Does anyone have a pointer to a Ruby library that provides this?

Hello Oliver,

One option would be to handle this by using the Ruby gem called rinruby. Rinruby can help you interface with R (statistical software) from Ruby. You can pass the dataset to R, do necessary modifications, run the test and get the result back.

Below is an example of how you can do this:

require 'rinruby'

my = RinRuby.new(echo: false)

matrix = [[13,20],[9,15]]

my.assign "matrix", matrix

my.eval "result = chisq.test(matrix)"

pvalue = my.pull "result$p.value"

puts pvalue

In the example above, you would replace “matrix” with the data you want to analyze.

I hope this helps. If you have any more questions, don’t hesitate to ask!

1 Like