简体   繁体   English

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

[英]How do I get T-Stat and P-Value from OLSMultipleLinearRegression

With the following code taken from examples... How do I get the p-value and t-stat that you would find in output such as Excel? 使用以下代码示例...如何获得在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);
  }

After posting this question I solved the t-stat part: 发布此问题后,我解决了t-stat部分:

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

This will give you the p-values. 这将为您提供p值。 It's not optimized in anyway but the values match excel perfectly. 它无论如何都没有进行优化,但价值匹配完美无缺。

I've updated my code to the below to address comments.. It matches 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);
  }

Here's improved code. 这是改进的代码。 I am matching 我匹配

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM