Apply linear regression using Mathnet library and VB.net


(duarte) #1

dear all, I would appreciate if someone could help me solving this problem:

I need to apply a simple linear regression analysis in visual studio using VB.net and I’m using the Math.Net library.

I created and initiated the vectors X and Y and them I call the regression function as such:

Dim regression_result = New MathNet.Numerics.LinearRegression.SimpleRegression(X,Y)

However, it gives an error in the statement “SimpleRegression” saying that: “Overload resolution failed because no ‘New’ is accessible”

Any thoughts for this problem?

Thanks


#2

SimpleRegression is a static class. You can’t create an instance. Instead, you can use the method Fit:

    Dim X() As Double = {10.0, 20.0, 30.0}
    Dim Y() As Double = {15.0, 20.0, 25.0}
    Dim regression_result = MathNet.Numerics.LinearRegression.SimpleRegression.Fit(X, Y)
    Console.WriteLine(regression_result.Item1)
    Console.WriteLine(regression_result.Item2)

https://dotnetfiddle.net/ogAT6j


(duarte) #3

thanks your reply,

I’ve tried that but now it says that Type ‘MathNet.Numerics.LinearRegression.SimpleRegression.Fit’ is not defined.

Do you think it can be a problem related to the package installation? I followed the process as described in the Math.net website and then, at the top of the scrip I have ‘Imports MathNet’ . Although when I do this import the program tells me something like ‘import not necessary’

Any thoughts?

Thanks


#4

Are you sure, that removed new before MathNet.Numerics.LinearRegression.SimpleRegression.Fit?
Insert your code to the https://dotnetfiddle.net, as in my message above.


(duarte) #5

It works now!

I copied the code exactly as you wrote, and it works. I guess I had the word ‘New’ and it wasn’t necessary.

Many thanks for your help!


(duarte) #6

Just one more question please:

I’m now trying to apply Multiple regression analysis. I’m trying with this piece of code:

Dim X(,) As Double = {{10.0, 20.0}, {30.0, 35.0}, {25.0, 54.0}}
Dim Y() As Double = {15.0, 20.0, 25.0}
Dim regression_result = MathNet.Numerics.LinearRegression.MultipleRegression.DirectMethod(X, Y, True)

But I’m getting an error in terms of arguments in the function “directmethod”

I think I’m having some problems interpreting the Math.net documentation for multiple regression.

Could you please tell me what I have wrong in this code?

Thanks


(duarte) #7

Just one more question please:

I’m now trying to apply Multiple regression analysis. I’m trying with this piece of code:

Dim X(,) As Double = {{10.0, 20.0}, {30.0, 35.0}, {25.0, 54.0}} Dim Y() As Double = {15.0, 20.0, 25.0} Dim regression_result = MathNet.Numerics.LinearRegression.MultipleRegression.DirectMethod(X, Y, True)

But I’m getting an error in terms of arguments in the function “directmethod”

I think I’m having some problems interpreting the Math.net documentation for multiple regression.

Could you please tell me what I have wrong in this code?

Thanks


#8

DirectMethod have no overload that works with two-dimensional rectangular array.
You can use “jagged array”:

Dim X As Double ()() = New Double(2)() {}
X(0) = New Double () {10.0, 20.0}
X(1) = New Double () {30.0, 35.0}
X(2) = New Double () {25.0, 54.0}

Dim Y() As Double = {15.0, 20.0, 25.0} 
Dim regression_result = MultipleRegression.DirectMethod(X, Y, True)

https://dotnetfiddle.net/GWy7Hy


(duarte) #9

Sorted.

Many Thanks!


#10

FoggyFinder,

I had some trouble understanding your example so I wrote up another that might help someone else looking at this post. This is a 2^3 full factorial experiment design (i.e. three variables with two levels each, -1 and 1) with one replicate, i.e. 8 observations.

THANK YOU for the Dim X As Double () () … code line! I have no idea what that does and would never have figured it out myself.

P.

        Console.WriteLine("A 2^3 full factorial experiment analyzed by MR, main effects only.")
        Dim X As Double()() = New Double(7)() {}       '7+1 = 8 is the number of observations
        X(0) = New Double() {-1, -1, -1}               'these are the predictor settings for the first observation, i.e. x1 = -1, x2 = -1, x3 = -1
        X(1) = New Double() {-1, -1, 1}
        X(2) = New Double() {-1, 1, -1}
        X(3) = New Double() {-1, 1, 1}
        X(4) = New Double() {1, -1, -1}
        X(5) = New Double() {1, -1, 1}
        X(6) = New Double() {1, 1, -1}
        X(7) = New Double() {1, 1, 1}       'last, 8th observation


        Dim Y() As Double = {10, 20, 110, 120, 20, 30, 140, 150}    'the 8 values of the response Y
        Dim MR_Coeffs = MathNet.Numerics.LinearRegression.MultipleRegression.DirectMethod(X, Y, True)
        Console.WriteLine("The MR coefficients are " & MR_Coeffs(0).ToString & ", " & MR_Coeffs(1).ToString & ", " & MR_Coeffs(2).ToString & ", " & MR_Coeffs(3).ToString)


(Bruno Voisin) #11

Hi, I am trying to model a 3D plane such as Y = a + bX1 + cX2 + dX1X2 + eX1^2 + fX2^2
I am also coding in VB.NET and I have used the ‘jagged array’ for the predictors as explained in this thread.
I see on the Regression page that other parameters (model functions) are fed through “d=>function” (lambda operator) in C#. I am trying a similar syntax in VB.NET (inline functions).
However in this short example on the Regression page I don’t see how to specify the input vector:
Call Fit.LinearMultiDim with the model (z:(x,y)↦p0+p1tanh(x)+p2ψ(xy)+p3xy), which will return an array with the best fitting 4 parameters p0,p1,p2,p3.
////////////
double[] p = Fit.LinearMultiDim(xy, z,
d => 1.0, // p01.0
d => Math.Tanh(d[0]), // p1
tanh(x)
d => SpecialFunctions.DiGamma(d[0]d[1]), // p2psi(xy)
d => Math.Pow(d[0], d[1])); // p3
x^y
////////////

Here below is my attempt (it woudn’t compile) :
’ Model Surface is Y = a + bX1 + cX2 + dX1X2 + eX1^2 + fX2^2
’ The following inline model functions are needed here to regress the 3D surface
Dim p0 = Function()
Return 1
End Function
Dim p1 = Function(x As Double)
Return x
End Function
Dim p2 = Function(x As Double, y As Double)
Return x * y
End Function
Dim p3 = Function(x As Double)
Return x ^ 2
End Function

    Dim regparams1() As Double = MathNet.Numerics.Fit.LinearMultiDim(predictors1, impvol,
        p0, p1(predictors1(0)), p1(predictors1(1)), p2(predictors1(0), predictors1(1)), p3(predictors1(0)), p3(predictors1(1)))

    Dim regparams2() As Double = MathNet.Numerics.Fit.LinearMultiDim(predictors2, impvol, False)

Thanks in advance !


(Peter Vanderwaart) #12

The programming is a bit simpler if you use MathNet.Numerics.LinearRegression. Assuming n data observations, build a nx6 matrix M where row i is

{1, X1[i], x2[i], X1[i]*X2[i], X1[i]^2, X2[i]^2}

Also build a vector Y of the Y[i] values.

Then solve:

double[] result = MultipleRegression.NormalEquations(M, Y, intercept: false);


(Bruno Voisin) #13

Thanks Peter. Simpler is always better !
Out of sheer curiosity, I’ll still try other methods later on as well.