Skip to content

Commit 049fbe0

Browse files
committed
demo OLS
1 parent a8c95bb commit 049fbe0

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

lib/PDL/Demos/Stats.pm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ $data = qsort random 10, 5; # 10 obs on 5 variables
6565
$data->plot_scores( $r{eigenvector}, {win=>$w} );
6666
|],
6767

68+
[act => q{
69+
# Suppose this is a person's ratings for top 10 box office movies
70+
# ascending sorted by box office
71+
$y = pdl '[1 1 2 2 2 2 4 4 5 5]';
72+
$x = cat sequence(10), sequence(10)**2; # IV with linear and quadratic component
73+
# We do an ordinary least squares (OLS), or multiple linear regression,
74+
# to get the underlying linear model. Here we also plot how far the real
75+
# data was from our calculated model.
76+
%m = $y->ols( $x, { plot=>1, win=>$w } );
77+
print "$_\t@{[$m{$_} =~ /^\n*(.*?)\n*\z/s]}\n" for sort keys %m;
78+
}],
79+
6880
[act => q{
6981
$y = pdl '[1 1 2 2 3 3 3 3 4 5 5 5]'; # suppose this is ratings for 12 apples
7082
$a = pdl '[1 2 3 1 2 3 1 2 3 1 2 3]'; # IV for types of apple

lib/PDL/Stats/GLM.pd

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,7 +2048,9 @@ Plots residuals against predicted values.
20482048

20492049
Usage:
20502050

2051-
$y->plot_residuals( $y_pred, { dev=>'/png' } );
2051+
use PDL::Graphics::Simple;
2052+
$w = pgswin();
2053+
$y->plot_residuals( $y_pred, { win=>$w } );
20522054

20532055
=for options
20542056

@@ -2076,7 +2078,7 @@ sub PDL::plot_residuals {
20762078
);
20772079
if ($opt) { $opt{uc $_} = $opt->{$_} for keys %$opt; }
20782080
my $residuals = $y - $y_pred;
2079-
my $win = $opt{WIN} || PDL::Graphics::Simple::pgswin(size=>[$opt{SIZE}, $opt{SIZE}]);
2081+
my $win = $opt{WIN} || PDL::Graphics::Simple::pgswin(size=>[@opt{qw(SIZE SIZE)}, 'px']);
20802082
$win->plot(
20812083
with=>'points', style=>$opt{COLOR}, $y_pred, $residuals,
20822084
with=>'lines', style=>$opt{COLOR}, pdl($y_pred->minmax), pdl(0,0), # 0-line
@@ -2130,7 +2132,7 @@ sub PDL::plot_scores {
21302132
# transformed normed values
21312133
my $scores = sumover($eigvec->slice(':',$i) * $z->transpose->dummy(1))->transpose;
21322134
$z = $z->slice(':',$i)->sever;
2133-
my $win = $opt{WIN} || PDL::Graphics::Simple::pgswin(size=>[$opt{SIZE}, $opt{SIZE}]);
2135+
my $win = $opt{WIN} || PDL::Graphics::Simple::pgswin(size=>[@opt{qw(SIZE SIZE)}, 'px']);
21342136
$win->plot(
21352137
with=>'points', style=>$opt{COLOR}[0], ke=>'original', $z->slice(',(0)'), $z->slice(',(1)'),
21362138
with=>'points', style=>$opt{COLOR}[1], ke=>'transformed', $scores->slice(',(0)'), $scores->slice(',(1)'),

0 commit comments

Comments
 (0)