Apache Commons Math Linear Regression Question

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Apache Commons Math Linear Regression Question

Oscar Bastidas
Hello,

Would someone please tell me how I am supposed to acquire the actual
numeric coefficients for a linear regression using the
PolynomialCurveFitter class?

Below is my code (it's supposed to be a simple example) and when I print
out the variable that stores the coefficients, ("coeff" in the below
example) what is printed to the command line is a strange code:
*[D@1610302.*

This happens regardless of the order of the polynomial I request.

Would someone please help me with this?  Thanks.

*********************** CODE BELOW ******************************

import java.util.*;
import org.apache.commons.math3.fitting.PolynomialCurveFitter;
import org.apache.commons.math3.fitting.*;

public class MathTester
{
        public static void main(String[] args) {
        // Testing linear regression
        final WeightedObservedPoints obs = new WeightedObservedPoints();

                // Acquiring data points
                obs.add(-1.00, 2.021170);
                obs.add(-0.99, 2.221135);
                obs.add(-0.98, 2.099852);

               // Instantiate 2nd degree polynomial fitting
               final PolynomialCurveFitter fitter =
PolynomialCurveFitter.create(2);

                // Retrieve fitted coefficients of the fitted data
                final double[] coeff = fitter.fit(obs.toList());

               // Printing out coefficients
               System.out.println("The coefficients are: " + coeff);

       } // END 'main'
} // END class

************************************************************

Oscar

Oscar Bastidas, Ph.D.
Postdoctoral Research Associate
University of Minnesota
Reply | Threaded
Open this post in threaded view
|

RE: Apache Commons Math Linear Regression Question

John.E.Gregg-2
Just wrap with Arrays.toString():

System.out.println("The coefficients are: " + Arrays.toString(coeff));

You'll get this:

The coefficients are: [-1568.1599299988338, -3176.4210999976494, -1606.2399999988156]



> -----Original Message-----
> From: Oscar Bastidas <[hidden email]>
> Sent: Wednesday, January 08, 2020 2:12 PM
> To: [hidden email]
> Subject: Apache Commons Math Linear Regression Question
>
> Hello,
>
> Would someone please tell me how I am supposed to acquire the actual
> numeric coefficients for a linear regression using the PolynomialCurveFitter
> class?
>
> Below is my code (it's supposed to be a simple example) and when I print out
> the variable that stores the coefficients, ("coeff" in the below
> example) what is printed to the command line is a strange code:
> *[D@1610302.*
>
> This happens regardless of the order of the polynomial I request.
>
> Would someone please help me with this?  Thanks.
>
> *********************** CODE BELOW
> ******************************
>
> import java.util.*;
> import org.apache.commons.math3.fitting.PolynomialCurveFitter;
> import org.apache.commons.math3.fitting.*;
>
> public class MathTester
> {
>         public static void main(String[] args) {
>         // Testing linear regression
>         final WeightedObservedPoints obs = new WeightedObservedPoints();
>
>                 // Acquiring data points
>                 obs.add(-1.00, 2.021170);
>                 obs.add(-0.99, 2.221135);
>                 obs.add(-0.98, 2.099852);
>
>                // Instantiate 2nd degree polynomial fitting
>                final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(2);
>
>                 // Retrieve fitted coefficients of the fitted data
>                 final double[] coeff = fitter.fit(obs.toList());
>
>                // Printing out coefficients
>                System.out.println("The coefficients are: " + coeff);
>
>        } // END 'main'
> } // END class
>
> **********************************************************
> **
>
> Oscar
>
> Oscar Bastidas, Ph.D.
> Postdoctoral Research Associate
> University of Minnesota

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Apache Commons Math Linear Regression Question

Oscar Bastidas
In reply to this post by Oscar Bastidas
Please disregard my previous question!  I figured out what I was missing.

Oscar

Oscar Bastidas, Ph.D.
Postdoctoral Research Associate
University of Minnesota

On Wed, Jan 8, 2020, 2:12 PM Oscar Bastidas <[hidden email]> wrote:

> Hello,
>
> Would someone please tell me how I am supposed to acquire the actual
> numeric coefficients for a linear regression using the
> PolynomialCurveFitter class?
>
> Below is my code (it's supposed to be a simple example) and when I print
> out the variable that stores the coefficients, ("coeff" in the below
> example) what is printed to the command line is a strange code:
> *[D@1610302.*
>
> This happens regardless of the order of the polynomial I request.
>
> Would someone please help me with this?  Thanks.
>
> *********************** CODE BELOW ******************************
>
> import java.util.*;
> import org.apache.commons.math3.fitting.PolynomialCurveFitter;
> import org.apache.commons.math3.fitting.*;
>
> public class MathTester
> {
>         public static void main(String[] args) {
>         // Testing linear regression
>         final WeightedObservedPoints obs = new WeightedObservedPoints();
>
>                 // Acquiring data points
>                 obs.add(-1.00, 2.021170);
>                 obs.add(-0.99, 2.221135);
>                 obs.add(-0.98, 2.099852);
>
>                // Instantiate 2nd degree polynomial fitting
>                final PolynomialCurveFitter fitter =
> PolynomialCurveFitter.create(2);
>
>                 // Retrieve fitted coefficients of the fitted data
>                 final double[] coeff = fitter.fit(obs.toList());
>
>                // Printing out coefficients
>                System.out.println("The coefficients are: " + coeff);
>
>        } // END 'main'
> } // END class
>
> ************************************************************
>
> Oscar
>
> Oscar Bastidas, Ph.D.
> Postdoctoral Research Associate
> University of Minnesota
>