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

Mik*_*ras 4 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);
  }
Run Code Online (Sandbox Code Playgroud)

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

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

Mik*_*ras 5

  int residualdf = regression.estimateResiduals().length-beta.length;
  for (int i=0; i < beta.length; i++){
     double tstat = beta[i] / regression.estimateRegressionParametersStandardErrors()[i];

     double pvalue = new TDistribution(residualdf).cumulativeProbability(-FastMath.abs(tstat))*2;

     System.out.println("p-value(" +i +") : " +pvalue );
  }
Run Code Online (Sandbox Code Playgroud)

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

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

      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);
  }
Run Code Online (Sandbox Code Playgroud)

这是改进的代码。我匹配

class RegressionCoefficient {
    private final RegressionCoefficientNames valueName;
    private final Double coefficient;
    private final Double standardError;
    private final Double tStat;
    private final Double pValue;
}
Run Code Online (Sandbox Code Playgroud)