【问题标题】:perl regression without interceptperl 无截距回归
【发布时间】:2015-06-19 07:39:26
【问题描述】:

我正在尝试使用 Statistics::Regression 模块在 perl 中实现线性回归,而无需 INTERCEPT。如何在没有截距的情况下实现回归模型?我使用以下代码通过拦截得到正确的结果:

#!/usr/bin/perl
use strict;
use warnings;
use Statistics::Regression

my @row=();
my $reg=Statistics::Regression->new("sample regression",["const", "x"]);

open(my $f1, "<","ema_bid_44671_11536") or 
        die "cant open ema_bid_44671_11536";

while(my $line=<$f1>){
    my @row=split(",",$line);
    chomp($row[2]);
    chomp($row[1]);
    $reg->include($row[2],[ 1.0, $row[1]]); 
}

$reg->print();
close $f1;

但是当我在 while 循环中的最后一条语句中将 1.0 更改为 0.0 时(包括零常量),代码如下所示:

#!/usr/bin/perl
use strict;
use warnings;
use Statistics::Regression

my @row=();
my $reg=Statistics::Regression->new("sample regression",["const", "x"]);


open(my $f1, "<","ema_bid_44671_11536") or 
        die "cant open ema_bid_44671_11536";

while(my $line=<$f1>){
    my @row=split(",",$line);
    chomp($row[2]);
    chomp($row[1]);
    $reg->include($row[2],[ 0.0, $row[1]]); 
}

$reg->print();
close $f1;

它给了我错误:

regression_ema.pl::Statistics::Regression:standarderrors: I cannot compute the theta-covariance matrix for variable 1 0
 at /usr/local/share/perl/5.20.2/Statistics/Regression.pm line 619, <$f1> line 2472.
    Statistics::Regression::standarderrors(Statistics::Regression=HASH(0x23f41f0)) called at /usr/local/share/perl/5.20.2/Statistics/Regression.pm line 430
    Statistics::Regression::print(Statistics::Regression=HASH(0x23f41f0)) called at regression_ema.pl line 23

【问题讨论】:

    标签: perl regression linear-regression intercept


    【解决方案1】:

    文档说:

    请注意,如果需要,必须提供常量。

    这会让我相信,如果不需要,可以简单地省略常数项。但是,在这种情况下,模块会发出嘶哑的声音:

    t.pl::Statistics::Regression:new:没有至少两个变量就无法运行回归。

    我一直坚信,对于数值分析和统计计算,人们不应该相信善意但考虑不周的业余贡献。

    使用打算供统计学家使用的东西。 R 是一个明显的替代方案。

    【讨论】:

      猜你喜欢
      • 2018-11-21
      • 2018-10-30
      • 2020-04-12
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      • 2022-01-09
      相关资源
      最近更新 更多