Residuals multiple linear regression OLS; partial correlation

How can I get the residuals of a multiple linear regression with OLS?

In Java (Apache.Common.Math3) there is the function OLS.estimateResiduals() after the input OLS.newSampleData with a one dimensional array, the variable nobs and nvars). I would like to use a similar function in C#.

For a partial correlation I would like to get the residues of
regression 1 - y values and x1 values and
regression 2 - y values and x2 values and
using pearson to do a correlaton between the two residual arrays.

Each multiple regression has a one dimensional x array, not a two dimensional x array.

How can I achive that with

The description from

public void newSampleData(double[] data, int nobs, int nvars)

Loads model x and y sample data from a flat input array, overriding any previous sample.

Assumes that rows are concatenated with y values first in each row. For example, an input data array containing the sequence of values (1, 2, 3, 4, 5, 6, 7, 8, 9) with nobs = 3 and nvars = 2 creates a regression dataset with two independent variables, as below:

y x[0] x[1]

1 2 3
4 5 6
7 8 9

I guess I can use a simple linear regression, calculating each y value for each x value by using the slope and the intercept with the formula y = ax+b and doing a subtraction between each y value of the regression and each y value? Sorry, I am not a mathematician.

You can use the Multiple Regression function, regressing on a constant and your one variable as in the example below. The Pearson Correlation is the fancy name for the usual formula. The Matrix and Vector features are described at a link on the right side at

        Random Rnd = new Random();
        Matrix<double> X = Matrix<double>.Build.Dense(20, 2);
        Vector Y = new DenseVector(20);

        // fill with test data
        for (int i = 0; i < 20; i++)
            double d = i;
            X[i, 0] = 1;
            X[i, 1] = d + 10.0 * Rnd.NextDouble();
            Y[i] = 5 + d + Math.Cos(d) + 10.0 * Rnd.NextDouble();

        Vector<double> R = MultipleRegression.NormalEquations(X, Y);

        //Caclulate estimates
        Vector<double> E = X * R;

        // Calculate residuals
        Vector<double> Res = Y - E;

        // Calculate correlation between Y and E
        double Cor = Correlation.Pearson(Y, E);