perl (Statistics::PCA): Can't use string ("0") as an ARRAY ref while "strict refs"

StackOverflow https://stackoverflow.com/questions/15947997

  •  03-04-2022
  •  | 
  •  

Вопрос

I'm trying to use the Statistics::PCA package to conduct a PCA on some genetic variants. The package requires reading in a list of lists (if I understood it correctly, that can be an array of arrays, right?).

Below is part of my code where I create arrays for each individual (each individual is an actual human subject that gets an array filled with 0's and 1's, which represent the presence or absence of a genetic variant called an "SV"), and then push them in my array of arrays called @LoL_SVs:

my @LoL_SVs;

foreach (@individuals) {
    my $ind = $_;
    foreach (@all_SVs) {
        if ($SV{$ind}{$_} != 1) {
            push(@{$SVs{$ind}}, "0");
        }
        if ($SV{$ind}{$_} == 1) {
            push(@{$SVs{$ind}}, "1");
        }
    }
    push @LoL_SVs, [ @{$SVs{$ind}} ];
} 

I then try to load the data as described on the CPAN website of the module (see paragraph *load_data*):

use Statistics::PCA;

my $pca = Statistics::PCA->new;

$pca->load_data ( { format => 'table', data => @LoL_SVs, } ); 
# ^ this line is where it goes wrong

Unfortunately this doesn't work, and I get the error message:

Can't use string ("0") as an ARRAY ref while "strict refs" in use at /home/abdel/myperl/share/perl/5.10.1/Statistics/PCA.pm line 189.

Any idea what might go wrong here?

I hope the problem is well specified, otherwise please let me know if you need more info! Many thanks!

Это было полезно?

Решение

Your options in the load_data call include this:

data => @LoL_SVs

Whereas in the documentation it is described like this

data => [ \@Obs1, \@Obs2, \@Obs3, \@Obs4, \@Obs5 ], 

You try to load an array, but the module expects an array ref (possibly of array refs). So when you try to pass an empty array as the scalar argument, it evaluates to 0 (because it contains 0 elements). Similar to:

my @bar;
my $foo = @bar;   # @bar is empty, $foo is 0
print $foo->[0];  # Can't use string ("0") as ARRAY ref ...

Your problem may be resolved by passing the reference to @LoL_SVs:

data => \@LoL_SVs

As a side note, it is good that you are using

use strict;

And I also hope that you are using

use warnings;

Without strict and with variable names like @LoL_SVs it is very easy to make typos such as @Lol_Svs which leads to hard to detect bugs. Without using warnings, such bugs would be even harder to find.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top