Advertisement
ZaynerTech

PCA Analysis Perl

Jan 17th, 2013
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.50 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #
  3. #
  4. # Messing around doing PCA analyses with Perl
  5. #
  6. #
  7.  
  8.  
  9.     use Statistics::PCA;
  10.  
  11.     # Create new Statistics::PCA object.
  12.     my $pca = Statistics::PCA->new;
  13.  
  14.     #                  Var1    Var2    Var3    Var4...
  15.     my @Obs1 = (qw/    2       4       3      1 /);
  16.     my @Obs2 = (qw/    5      10       6      2/);
  17.     my @Obs3 = (qw/    4       2       1      5/);
  18.     my @Obs4 = (qw/    3       6       7      3/);
  19.  #   my @Obs5 = (qw/    63      35      34     2/);
  20.  
  21.     # Load data. Data is loaded as a LIST-of-LISTS (LoL) pointed to by a named argument 'data'. Requires argument for format (see METHODS).
  22.     $pca->load_data ( { format => 'table', data => [ \@Obs1, \@Obs2, \@Obs3, \@Obs4], } ) ;
  23.  
  24.     # Perform the PCA analysis. Takes optional argument 'eigen' (see METHODS).
  25.     #$pca->pca( { eigen => 'C' } );
  26.     $pca->pca();
  27.  
  28.     @list = $pca->results('full');
  29.     for my $i (@list) {
  30.         print qq{\nPC rank: $i->[0]}
  31.               . qq{\nPC stdev $i->[1]}
  32.               . qq{\nPC proportion of variance $i->[2]}
  33.               . qq{\nPC cumulative variance $i->[3]}
  34.               . qq{\nPC eigenvalue $i->[4]}
  35.              
  36.         }
  37.  
  38.  @list = $pca->results('transformed');
  39.     print qq{\nThe transformed data for 'the' principal component (first PC): @{$list[0]}\n };
  40.     print qq{\nnThe transformed data for 'the' principal component (second PC): @{$list[1]}\n };
  41.  print qq{\nnThe transformed data for 'the' principal component (third PC): @{$list[2]}\n };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement