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)