Created
February 1, 2022 00:49
-
-
Save Shiina18/844dcd880e5a377adc9880536f0d0563 to your computer and use it in GitHub Desktop.
Calculates the two-tailed p-values for the t-statistics for regression parameters.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.math3.distribution.TDistribution; | |
import org.apache.commons.math3.linear.RealMatrix; | |
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; | |
import org.apache.commons.math3.util.FastMath; | |
import java.util.Arrays; | |
public class MyOLSMultipleLinearRegression extends OLSMultipleLinearRegression { | |
/** | |
* <p>Calculates the two-tailed p-values for the t-statistics for regression parameters. | |
* </p> | |
* | |
* <p>Data for the model must have been successfully loaded using one of | |
* the {@code newSampleData} methods before invoking this method; otherwise | |
* a {@code NullPointerException} will be thrown.</p> | |
* | |
* @param beta The [p,1] array representing regression parameters, typically returned from invoking | |
* {@code estimateRegressionParameters} method | |
* @return The [n,1] array representing p-values | |
* @throws org.apache.commons.math3.linear.SingularMatrixException if the design matrix is singular | |
* @throws NullPointerException if the data for the model have not been loaded | |
*/ | |
public double[] calculateRegressionParametersPvalues(double[] beta) { | |
RealMatrix xMatrix = this.getX(); | |
int numSamples = xMatrix.getRowDimension(); | |
int numParameters = xMatrix.getColumnDimension(); | |
RealMatrix betaVarianceMatrix = this.calculateBetaVariance(); // (X'X)^{-1} | |
double regressionStandardError = this.estimateRegressionStandardError(); // sqrt{r'r/(n-p)} | |
double[] pValues = new double[numParameters]; | |
TDistribution tDist = new TDistribution(null, numSamples - numParameters); | |
for (int i=0; i<numParameters; i++) { | |
double tStats = (beta[i] / | |
(FastMath.sqrt(betaVarianceMatrix.getEntry(i, i)) * regressionStandardError)); | |
pValues[i] = 2 * (1 - tDist.cumulativeProbability(FastMath.abs(tStats))); | |
} | |
return pValues; | |
} | |
/** | |
* Calculates the two-tailed p-values for the t-statistics for regression parameters. Convenient method for | |
* {@link MyOLSMultipleLinearRegression#calculateRegressionParametersPvalues(double[])} if | |
* {@code estimateRegressionParameters} has not been invoked beforehand. | |
*/ | |
public double[] calculateRegressionParametersPvalues() { | |
double[] beta = this.estimateRegressionParameters(); | |
return calculateRegressionParametersPvalues(beta); | |
} | |
} | |
class Demo { | |
public static void main(String[] args) { | |
MyOLSMultipleLinearRegression regression = new MyOLSMultipleLinearRegression(); | |
regression.newSampleData(new double[]{0, 1, 2, 3}, new double[][]{{1, 2}, {3, -1}, {6, 5}, {9, 8}}); | |
double[] beta = regression.estimateRegressionParameters(); | |
System.out.println("regression parameters: " + Arrays.toString(beta)); | |
System.out.println("p-values: " + Arrays.toString(regression.calculateRegressionParametersPvalues(beta))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment