如何从OLSMultipleLinearRegression获取T-Stat和P-Value

时间:2015-11-10 19:38:38

标签: java linear-regression p-value

从示例中获取以下代码...如何获得在Excel等输出中找到的p值和t-stat?

  OLSMultipleLinearRegression regression2 = new OLSMultipleLinearRegression();
  double[] y = { 4, 8, 13, 18};
  double[][] x = {{ 1, 1, 1  },
                  { 1, 2, 4  },
                  { 1, 3, 9  },
                  { 1, 4, 16  }};

  regression2.newSampleData(y, x);
  regression2.setNoIntercept(true);
  double[] beta = regression2.estimateRegressionParameters();

  for (double d : beta) {
     System.out.println("D: " + d);
  }

发布此问题后,我解决了t-stat部分:

  for (int i=0; i < beta.length; i++){
     double tstat = beta[i] / regression.estimateRegressionParametersStandardErrors()[i];
     System.out.println("t-stats(" +i +") : " +tstat );
  }

1 个答案:

答案 0 :(得分:4)

      final double[] beta = regression.estimateRegressionParameters();
  final double[] standardErrors = regression.estimateRegressionParametersStandardErrors();
  final int residualdf = regression.estimateResiduals().length - beta.length;

  final TDistribution tdistribution = new TDistribution(residualdf);

  //calculate p-value and create coefficient
  final Map<RegressionCoefficientNames, RegressionCoefficient> coefficientMap = new HashMap<>(beta.length);
  for (int i = 0; i < beta.length; i++)
  {
     double tstat = beta[i] / standardErrors[i];
     double pvalue = tdistribution.cumulativeProbability(-FastMath.abs(tstat)) * 2;
     final RegressionCoefficient coefficient = new RegressionCoefficient(extensionModelType.getNameByIndex(i),
                                                                         beta[i],
                                                                         standardErrors[i],
                                                                         tstat,
                                                                         pvalue);

     coefficientMap.put(extensionModelType.getNameByIndex(i), coefficient);
  }

这将为您提供p值。它无论如何都没有优化,但价值匹配完美。

我已将我的代码更新到下面以解决评论..它与Excel匹配。

class RegressionCoefficient {
    private final RegressionCoefficientNames valueName;
    private final Double coefficient;
    private final Double standardError;
    private final Double tStat;
    private final Double pValue;
}

这是改进的代码。我匹配

$user_id        = $this->uri->segment(3);
$new_email_key  = $this->uri->segment(4);