1

I want to run an ANOVA test in PHP. The library I am using for this task is markrogoyski/math-php (https://github.com/markrogoyski/math-php#statistics---anova). And the method is shown below.

use MathPHP\Statistics\ANOVA;

// One-way ANOVA
$sample1 = [1, 2, 3];
$sample2 = [3, 4, 5];
$sample3 = [5, 6, 7];
   ⋮            ⋮

$anova = ANOVA::oneWay($sample1, $sample2, $sample3);
print_r($anova);

The data for the test were retrieved from the database in the following format. Each sub-array is one data set in the ANOVA set (like $sample1, $sample2, etc.). The problem is the number of sub-array is not fixed depending on the input. So, I add them all into a big array during the query process.

Array
(
[0] => Array
    (
        [0] => 14.60
        [1] => 15.94
    )

[1] => Array
    (
        [0] => 16.12
        [1] => 15.30
        [2] => 9.24
    )

[2] => Array
    (
        [0] => 6.80
        [1] => 15.78
    )
)

The problem is that the ANOVA::oneWay method only accepts comma delimited arrays. When I simply pass the above array to the function,

$anova = ANOVA::oneWay($TTFF_anova);

I got the following error.

Uncaught MathPHP\Exception\BadDataException: Must have at least three samples

I believe I need to pass a group of arrays to that function.

I have searched for possible solutions online. This post described a similar problem in Python (Running scipy's oneway anova in a script). The solution is

scipy.stats.f_oneway(*archive.values())

I hope to ask if there is a similar solution in PHP.

Similarly, I have tried

$anova = ANOVA::oneWay(array_values($TTFF_anova));

But it doesn't work.

I also tried to use call_user_func_array() function in PHP

$anova = call_user_func_array('ANOVA::oneWay', $TTFF_anova);

I have got an error as

Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'ANOVA' not found

Please help me to solve this problem. Thank you very much.

Romann
  • 89
  • 2
  • 7

1 Answers1

2

As of PHP 5.6 use argument unpacking with the splat operator ...:

$TTFF_anova = array($sample1, $sample2, $sample3);
$anova = ANOVA::oneWay(...$TTFF_anova);

call_user_func_array should work with the namespace or use __NAMESPACE__:

$anova = call_user_func_array('MathPHP\Statistics\ANOVA\ANOVA::oneWay', $TTFF_anova);
$anova = call_user_func_array(array('MathPHP\Statistics\ANOVA\ANOVA', 'oneWay'), $TTFF_anova);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • you should include the namespace for these: `$anova = call_user_func_array('ANOVA::oneWay', $TTFF_anova); $anova = call_user_func_array(array('ANOVA', 'oneWay'), $TTFF_anova);`, else they will return error since ANOVA is an imported class. – elegisandi Aug 04 '17 at 04:12
  • @AbraCadaver Thank you very much. The splat operator ... works great! – Romann Aug 04 '17 at 04:12
  • The other two call_user_func_array() methods do need the namespace. – Romann Aug 04 '17 at 04:19