# Change Dir

• 随笔 - 222
• 文章 - 0
• 评论 - 182
• 引用 - 0

• 积分 - 407698
• 排名 - 133

## Commons Math学习笔记——聚类和回归

1/**
2 *
3 */

4package algorithm.math;
5
6import org.apache.commons.math.stat.regression.OLSMultipleLinearRegression;
7import org.apache.commons.math.stat.regression.SimpleRegression;
8
9/**
10 * @author Jia Yu
11 * @date   2010-12-6
12 */

13public class RegressionTest {
14
15    /**
16     * @param args
17     */

18    public static void main(String[] args) {
19        // TODO Auto-generated method stub
20        regression();
21        System.out.println("-------------------------------------");
22        simple();
23    }

24
25    private static void simple() {
26        // TODO Auto-generated method stub
27        double[][] data = 0.10.2 }{338.8337.4 }{118.1118.2 }
28                {888.0884.6 }{9.210.1 }{228.1226.5 }{668.5666.3 }{998.5996.3 }
29                {449.1448.6 }{778.9777.0 }{559.2558.2 }{0.30.4 }{0.10.6 }{778.1775.5 }
30                {668.8666.9 }{339.3338.0 }{448.9447.5 }{10.811.6 }{557.7556.0 }
31                {228.3228.1 }{998.0995.8 }{888.8887.6 }{119.6120.2 }{0.30.3 }
32                {0.60.3 }{557.6556.8 }{339.3339.1 }{888.0887.2 }{998.5999.0 }
33                {778.9779.0 }{10.211.1 }{117.6118.3 }{228.9229.2 }{668.4669.1 }
34                {449.2448.9 }{0.20.5 }
35        }
;
36        SimpleRegression regression = new SimpleRegression();
37        for (int i = 0; i < data.length; i++{
39        }

40        System.out.println("slope is "+regression.getSlope());
41        System.out.println("slope std err is "+regression.getSlopeStdErr());
42        System.out.println("number of observations is "+regression.getN());
43        System.out.println("intercept is "+regression.getIntercept());
44        System.out.println("std err intercept is "+regression.getInterceptStdErr());
45        System.out.println("r-square is "+regression.getRSquare());
46        System.out.println("SSR is "+regression.getRegressionSumSquares());
47        System.out.println("MSE is "+regression.getMeanSquareError());
48        System.out.println("SSE is "+regression.getSumSquaredErrors());
49        System.out.println("predict(0) is "+regression.predict(0));
50        System.out.println("predict(1) is "+regression.predict(1));
51    }

52
53    private static void regression() {
54        // TODO Auto-generated method stub
55        double[] y;
56        double[][] x;
57        y = new double[]{11.012.013.014.015.016.0};
58        x = new double[6][];
59        x[0= new double[]{1.000000};
60        x[1= new double[]{1.02.00000};
61        x[2= new double[]{1.003.0000};
62        x[3= new double[]{1.0004.000};
63        x[4= new double[]{1.00005.00};
64        x[5= new double[]{1.000006.0};
65
66        OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
67        regression.newSampleData(y, x);
68
69        double[] betaHat = regression.estimateRegressionParameters();
70        System.out.println("Estimates the regression parameters b:");
71        print(betaHat);
72        double[] residuals = regression.estimateResiduals();
73        System.out.println("Estimates the residuals, ie u = y - X*b:");
74        print(residuals);
75        double vary = regression.estimateRegressandVariance();
76        System.out.println("Returns the variance of the regressand Var(y):");
77        System.out.println(vary);
79        System.out.println("Returns the standard errors of the regression parameters:");
80        print(erros);
81        double[][] varb = regression.estimateRegressionParametersVariance();
82    }

83
84    private static void print(double[] v) {
85        // TODO Auto-generated method stub
86        for(int i=0;i<v.length;i++){
87            System.out.print(v[i]+ " ");
88        }

89        System.out.println();
90    }

91
92}

93

Estimates the regression parameters b:
11.000000000000004 0.4999999999999988 0.6666666666666657 0.7499999999999993 0.7999999999999993 0.8333333333333329
Estimates the residuals, ie u = y - X*b:
-3.552713678800501E-15 -1.7763568394002505E-15 0.0 0.0 0.0 0.0
Returns the variance of the regressand Var(y):
Infinity
Returns the standard errors of the regression parameters:
Infinity Infinity Infinity Infinity Infinity Infinity
-------------------------------------
slope is 1.0021168180204547
slope std err is 4.297968481840198E-4
number of observations is 36
intercept is -0.26232307377414243
std err intercept is 0.2328182342925303
r-square is 0.9999937458837121
SSR is 4255954.132323695
MSE is 0.7828646625720841
SSE is 26.61739852745086
predict(0) is -0.26232307377414243
predict(1) is 0.7397937442463123