commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From joni8a <jon...@yahoo.de>
Subject Commons Math 3.0 Java Multiple Regression
Date Tue, 09 Oct 2012 08:43:10 GMT
Hello,

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]

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


Mime
View raw message