gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:07:43 26 4
gpt4 key购买 nike

使用示例中的以下代码...我如何获得您会在 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 );
}

最佳答案

  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 );
}

这将为您提供 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);
}

这是改进后的代码。我在匹配

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

关于java - 如何从 OLSMultipleLinearRegression 获取 T-Stat 和 P-Value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33638170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com