public static SimpleRegression createWeigthedLinearRegression( final double[] x, final double[] y, final double[] weigths) { double[] xW = new double[x.length]; double[] yW = new double[y.length]; int numZeroWeigths = 0; for (int i = 0; i < weigths.length; i++) { if (weigths[i] <= 0) { numZeroWeigths++; } } for (int i = 0; i < x.length; i++) { if (numZeroWeigths >= 0.4 * weigths.length) { // See: http://www.ncsu.edu/crsc/events/ugw07/Presentations/Crooks_Qiao/Crooks_Qiao_Alt_Presentation.pdf xW[i] = Math.sqrt(weigths[i]) * x[i]; yW[i] = Math.sqrt(weigths[i]) * y[i]; } else { xW[i] = x[i]; yW[i] = y[i]; } } return createLinearRegression(xW, yW); }
/** * Creates a a weighted linear regression. * @param x the independent variable * @param y the dependent variable * @param weights the weights to apply to x and y * @return */ private static SimpleRegression createWeightedLinearRegression( final double[] x, final double[] y, final double[] weights) { final double[] weightedX = new double[x.length]; final double[] weightedY = new double[y.length]; final long numZeroWeights = Arrays.stream(weights).filter(weight -> weight <= 0).count(); for (int i = 0; i < x.length; i++) { if (numZeroWeights >= 0.4 * weights.length) { // See: http://www.ncsu.edu/crsc/events/ugw07/Presentations/Crooks_Qiao/Crooks_Qiao_Alt_Presentation.pdf weightedX[i] = Math.sqrt(weights[i]) * x[i]; weightedY[i] = Math.sqrt(weights[i]) * y[i]; } else { weightedX[i] = x[i]; weightedY[i] = y[i]; } } return createLinearRegression(weightedX, weightedY); }
/** * Gets the correlation coefficients. * * @param data the data * @return the correlation coefficients */ protected List<Double> getCorrelationCoefficients(final double[][] data) { int n = data.length; int m = data[0].length; List<Double> correlationCoefficients = new LinkedList<Double>(); for (int i = 0; i < n; i++) { double[][] x = new double[n - 1][m]; int k = 0; for (int j = 0; j < n; j++) { if (j != i) { x[k++] = data[j]; } } // Transpose the matrix so that it fits the linear model double[][] xT = new Array2DRowRealMatrix(x).transpose().getData(); // RSquare is the "coefficient of determination" correlationCoefficients.add(MathUtil.createLinearRegression(xT, data[i]).calculateRSquared()); } return correlationCoefficients; }
/** * Gets the correlation coefficients. * * @param data the data * @return the correlation coefficients */ protected List<Double> getCorrelationCoefficients(final double[][] data) { int n = data.length; int m = data[0].length; List<Double> correlationCoefficients = new LinkedList<Double>(); for (int i = 0; i < n; i++) { double[][] x = new double[n - 1][m]; int k = 0; for (int j = 0; j < n; j++) { if (j != i) { x[k++] = data[j]; } } // Transpose the matrix so that it fits the linear model double[][] xT = new Array2DRowRealMatrix(x).transpose().getData(); // RSquare is the "coefficient of determination" correlationCoefficients.add(MathUtil.createLinearRegression(xT, data[i]).calculateRSquared()); } return correlationCoefficients; }
/** * Gets the correlation coefficients. * * @param data the data * @return the correlation coefficients */ protected List<Double> getCorrelationCoefficients(final double[][] data) { int n = data.length; int m = data[0].length; List<Double> correlationCoefficients = new LinkedList<Double>(); for (int i = 0; i < n; i++) { double[][] x = new double[n - 1][m]; int k = 0; for (int j = 0; j < n; j++) { if (j != i) { x[k++] = data[j]; } } // Transpose the matrix so that it fits the linear model double[][] xT = new Array2DRowRealMatrix(x).transpose().getData(); // RSquare is the "coefficient of determination" correlationCoefficients.add(MathUtil.createLinearRegression(xT, data[i]).calculateRSquared()); } return correlationCoefficients; }
/** * Gets the correlation coefficients. * * @param data the data * @return the correlation coefficients */ protected List<Double> getCorrelationCoefficients(final double[][] data) { final int rows = data.length; final int cols = data[0].length; final List<Double> correlationCoefficients = new LinkedList<>(); for (int i = 0; i < rows; i++) { final double[][] x = new double[rows - 1][cols]; int k = 0; for (int j = 0; j < rows; j++) { if (j != i) { x[k++] = data[j]; } } // Transpose the matrix so that it fits the linear model final double[][] xT = new Array2DRowRealMatrix(x).transpose().getData(); // RSquare is the "coefficient of determination" correlationCoefficients.add( MathUtil.createLinearRegression(xT, data[i]).calculateRSquared()); } return correlationCoefficients; }