作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
使用示例中的以下代码...我如何获得您会在 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/
我是一名优秀的程序员,十分优秀!