commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From luc <...@spaceroots.org>
Subject [math] Re: Commons Math 3.0 Java Multiple Regression
Date Tue, 09 Oct 2012 09:02:17 GMT
Le 2012-10-09 10:43, joni8a a écrit :
> Hello,

Hi joni8a,

First of all, please notice that this mailin list is shared among 
several components
at Apache Commons project. So you should use a [math] marker in the 
subject of your
messages to help automatic filtering by subscribers to the list. I have 
added this marker
in this reply.

>
> I try to perform a multiple regression with help of the Commons Math 
> 3.0
> library for Java. I used this code and modified it so it fits for my 
> code.
> This is the sample code:
>  [code]
> OLSMultipleLinearRegression regression = new 
> OLSMultipleLinearRegression();
> double[] y = new double[]{11.0, 12.0, 13.0, 14.0, 15.0, 16.0};
> double[] x = new double[6][];
> x[0] = new double[]{0, 0, 0, 0, 0};
> x[1] = new double[]{2.0, 0, 0, 0, 0};
> x[2] = new double[]{0, 3.0, 0, 0, 0};
> x[3] = new double[]{0, 0, 4.0, 0, 0};
> x[4] = new double[]{0, 0, 0, 5.0, 0};
> x[5] = new double[]{0, 0, 0, 0, 6.0};
> regression.newSample(y, x);
> [/code]
>
> My code looks like this:
> [code]
>  String query = "SELECT "+yData+" FROM APP."+stock.stockName;
>                     System.out.println("QUERY:"+query);
>
>         PreparedStatement ps = 
> DBHelperClass.conn.prepareStatement(query,
>                 ResultSet.TYPE_SCROLL_INSENSITIVE,
> ResultSet.CONCUR_READ_ONLY);
>         ResultSet rs = 
> DBHelperClass.getSingelton().getDataFromDB(ps);
>
>         rs.last();
>         int sizeY = rs.getRow();
>         rs.beforeFirst();
>
>         double[] yDataMatrix = new double[sizeY];
>         for(int i=0;i<sizeY;i++)
>         {
>             rs.next();
>             yDataMatrix[i] = Double.parseDouble(rs.getString(1));
>         }
>
>         int xSize = xData.size();
>         double[][] xDataMatrix = new double[xSize][];
>
>         ps = null;
>         rs = null;
>         for(int i=0;i<xData.size();i++)
>         {
>             query = "SELECT "+xData.get(i)+" FROM 
> APP."+stock.stockName;
>             System.out.println("QUERY:"+query);
>             ps =DBHelperClass.conn.prepareStatement(query,
>                 ResultSet.TYPE_SCROLL_INSENSITIVE,
> ResultSet.CONCUR_READ_ONLY);
>             rs = DBHelperClass.getSingelton().getDataFromDB(ps);
>             rs.last();
>             int sizeX = rs.getRow();
>             rs.beforeFirst();
>
>             double[] xValue = new double[sizeX];
>             for(int x=0;x<sizeX;x++)
>             {
>                 rs.next();
>                 xValue[x] = Double.parseDouble(rs.getString(1));
>
>             }
>             xDataMatrix[i] = xValue;
>         }
>
>
>         System.out.println("Y DATA SIZE:"+yDataMatrix.length);
>         System.out.println("X DATA SIZE:"+xDataMatrix[0].length);
>
>         OLSMultipleLinearRegression regression = new
> OLSMultipleLinearRegression();
>         regression.newSampleData(yDataMatrix, xDataMatrix);
>         System.out.println("R
> Squared:"+regression.calculateResidualSumOfSquares());
>     }
> [/code]
> When I run the code, I get this error:
> [code]
> Exception in thread "AWT-EventQueue-0"
> org.apache.commons.math3.exception.DimensionMismatchException: 98 != 
> 2
> 	at
> 
> org.apache.commons.math3.stat.regression.AbstractMultipleLinearRegression.validateSampleData(AbstractMultipleLinearRegression.java:230)
> [/code]

I think you have xSize = 98 and ySize = 2. Both sizes should be equal. 
Perhaps your x matrix
is transposed? In addition to have xSize = ySize, note that the number 
of rows of the x matrix
must be greater than the number of columns otherwise you will get 
another error.

best regards,
Luc

>
> The error has to be around where I init and/or write to the double
> arrays....
>
>
>
>
> --
> View this message in context:
> 
> http://apache-commons.680414.n4.nabble.com/Commons-Math-3-0-Java-Multiple-Regression-tp4640491.html
> Sent from the Commons - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message