0

In the documentation for Statistics::Regression, the instructions to add an observation is: $reg->include( 2.0, [ 1.0, 3.0, -1.0 ] );

Then, to just run the regression on your own data:

my @obs;
observations are like: %oneobs= %{$obs[1]};
...
foreach my $obshashptr (@obs) { $reg->include( $_[1], $_[3] ); }

I'm confused by %oneobs= %{$obs[1]}: what does this notation mean? Further, what is foreach my $obshashptr (@obs) { $reg->include( $_[1], $_[3] ); } doing? Could someone help by showing me what these two statement are doing in more than one line?

It seems that $_[1] is the observation and $_[3] contains the predictor variables. So, $_[3] is an array..

Sam Weisenthal
  • 2,791
  • 9
  • 28
  • 66

2 Answers2

2

%{ } is a hash dereference; what's inside it is a reference to a hash, and the contents of that hash are being assigned to %oneobs.

See http://perlmonks.org/?node=References+quick+reference

ysth
  • 96,171
  • 6
  • 121
  • 214
  • so `@obs` is an array of hash of arrays? Is my example structure correct: `my %hash1 = (1.0=>[1.0,2.0,1.0]);my %hash2 = (1.0=>[1.0,1.0,1.0]);my @obs = (\%hash1,\%hash2);`? – Sam Weisenthal Jul 16 '14 at 04:52
  • that would be an array of hashes of arrays, yes – ysth Jul 16 '14 at 05:05
1

my @obs; is an array which contains hash reference at each index.

run_regression("bivariate regression",  $obshashptr->{someY}, [ "const", "someX" ] );

This calls the run_regression subroutine and passes 3 arguments.

Now check the subroutine.

sub run_regression {
  my $reg = Statistics::Regression->new( $_[0], $_[2] );
  foreach my $obshashptr (@obs) { $reg->include( $_[1], $_[3] ); }
  $reg->print();
}

The subroutine takes the arguments in @_ array (see What is the meaning of @_ in Perl?). Then you create $reg object of Statistics::Regression and you pass 2 arguments to new method. $_[0] means the first element of @_ array which in this case is bivariate regression. $_[2] means the third argument.

Then foreach line loops through the array and assigns value at array's index to $obshashptr and then for each iteration of array you are calling the include method of Statistics::Regression and you are passing 2 arguments to it. First one is the value at second index of @_ and second argument is value at 4th index of @_.

http://i.imgur.com/cCMv1OT.png

Community
  • 1
  • 1
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
  • what is the value at the 4th index of @_? It seems that it's meant to be the array of predictor variables, but isn't it referring to the 4th argument given to `run_regression`? Otherwise `@_` changes inside the `foreach`, in which case it's referring to what? – Sam Weisenthal Jul 16 '14 at 04:42