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

0 Likes

#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

0 Likes

(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

0 Likes

#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.

0 Likes

(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!

0 Likes

(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

0 Likes

(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

0 Likes

#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

1 Like

(duarte) #9

Sorted.

Many Thanks!

0 Likes

#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)

0 Likes

(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 !

0 Likes

(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);

0 Likes

(Bruno Voisin) #13

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

0 Likes

(Bruno Voisin) #14

Hi,
I don’t quite understand what sometimes causes the error “Matrix must be positive definite”, and more generally what that message actually means.
My data set may be imperfect but the matrix looks OK (6 inputs, 1 output).
Thanks in advance.

0 Likes

(Peter Vanderwaart) #15

Under the covers, this probably means that a matrix inversion, or, pretty equivalently, solution of a system of linear equation, operation failed. Looking at it from a statistical point of view, it could be because the explanatory variables are not independent.

For example, if you are trying to find values x,y,z to model an equation like:

WaistSize = x * weight + y * height _ z * age

and your data for weight, height and age has an internal relationship such that:

u * weight + v * height = age

you may get that error.

0 Likes

(Bruno Voisin) #16

Thank you Peter. The error message was obscure to me and your explanation seems 100% correct. I indeed need to increase the size of my data set as one variable is almost constant in the current one. I suppose I should code for dimension reduction when that arises.

0 Likes