1

I am quite a newbie in using the apache library for a simple linear regress. Task: I want to calculate the slope.

I have two linear lists, i.e., x-list and y-list. Y list values are the series of number. I am populating x-list by fetching value from a hashmap.

However, when I am trying to apply simpleRegression utility of apache library I am facing below difficulty:

Here is my code:

while(i< segmentI)
                {
                    xList.add(Double.parseDouble(timeStamp.get(i)));
                    yList.add(Double.parseDouble("1"));
                    i++;
                }
                for(int m=0; i< segmentI; i++)
                {
                        simpleRegression.addData(new double[][]{
                                {xList.get(m),yList.get(m)}
                        });

                }

Doubt: Is there any way can I create new double[][] before passing it to simpleRegression. Also, new double[][]-it is a matrix then what would be the value of [0,1], [0,2]... so on because we don't have anything like that in single ArrayList.

Anything in this regard will be helpful.

3
  • The Apache foundation supports lots of different projects. Which Apache library are you using? -- Y-list always contains a value of 1 -- Then the output of the linear regression will have slope 0 and intercept 1 Commented Aug 2, 2020 at 0:35
  • I mean to say for y, i.e., y2-y1 will always be 1. For the library I am using: commons-math3 Commented Aug 2, 2020 at 1:30
  • Create a 2-dimension array from two 1-D arrays: Java turning two arrays into one two-dimensional array. Then you can use the examples provided in the documentation for simple regression. Commented Aug 2, 2020 at 2:14

2 Answers 2

1

Is there any way can I create new double[][] before passing it to simpleRegression

Yes. For example

            double[][] data = new data[1][2];

            for(int m=0; m < segmentI; m++)
            {
                    data[0][0] = xList.get(m);
                    data[0][1] = yList.get(m);
                    simpleRegression.addData(data);
            }

You know there's also a method that you can call without an array?

simpleRegression.addData(xList.get(m), yList.get(m));
Sign up to request clarification or add additional context in comments.

Comments

0

This worked with below changes: double[][] pqr={ArrayUtils.toPrimitive(yList.toArray(new Double[0])),ArrayUtils.toPrimitive(xList.toArray(new Double[0]))}; simpleRegression.addData(pqr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.