num4regana 0.0.1-java → 0.0.3-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 616a5ce4aa14748f31750e7b1c2e69ca9af26295e7a173a8454ff9b3bc83cbfe
4
- data.tar.gz: 74b2370bb501a69ef6546840d3ed0b61315e13ef35f584d5dbe3f2f38725cbb2
3
+ metadata.gz: e3b63549f4e9c8d1779a21f7247bae8c034c4066e9907b53c76281c84c18c114
4
+ data.tar.gz: 19bd91c1939a3055b03a5bf35b516e09f31360cdeb227baab648c5277f843de9
5
5
  SHA512:
6
- metadata.gz: 7133ab852f61abbcb0e93bbc1be555bcfe16dfab0751ff1026be21dc8a46d829b3d03ba94313a7a196a0f385b7dbe01a6cb16d4678337bb063662e3faac88b43
7
- data.tar.gz: a9851b753e8bf686a1a5bccc5dc392dc229822e51e47a3b1b82c8f1f76dbaf8629c04ecc825679aee250ff1225a7cf9ab6ed2f774f61e927f32fcb58ba83e7ee
6
+ metadata.gz: ed0417b5fd2374d6ce4b3253484f532c40675fdf920e0bd7f10ec65e1c14ff7d1cf8be8fc748d9c68fe173613e67c4bcfa97235c43dbd4a9a65533a7a5fa38f1
7
+ data.tar.gz: a630e9d0ea9b931542b439617f9b28b38f1b0ac6e857ae95538cb04da60407af0882bf2654ec444abcbe150109c5e40bfcaf372d8514f5f621eb9af23cdb13fc
data/CHANGELOG.md CHANGED
@@ -2,6 +2,18 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## [0.0.3] - 2024-09-05
6
+
7
+ ### add
8
+ - add GLM
9
+ logistic regression analystis
10
+ poisson regression analystis
11
+
12
+ ## [0.0.2] - 2024-08-08
13
+
14
+ ### fix
15
+ - add function of equal variances in OLSMultRegAnaLib
16
+
5
17
  ## [0.0.1] - 2024-06-27
6
18
 
7
19
  ### Fixed
@@ -0,0 +1,82 @@
1
+ public class LogitRegAna {
2
+ private static LogitRegAna regana = new LogitRegAna();
3
+ public static LogitRegAna getInstance() {
4
+ return regana;
5
+ }
6
+ public LineReg nonLineRegAna(double[] yi, double xij[][]) {
7
+ NonLineRegAna line = new NonLineRegAna();
8
+
9
+ return line.nonLineRegAna(yi, xij);
10
+ }
11
+ /*********************************/
12
+ /* interface define */
13
+ /*********************************/
14
+ /*********************************/
15
+ /* class define */
16
+ /*********************************/
17
+ public class LineReg {
18
+ private double a = 0.0;
19
+ private double[] b = null;
20
+ public LineReg(double[] b) {
21
+ this.a = b[0];
22
+ this.b = new double[b.length - 1];
23
+ for (int i = 0; i < this.b.length; i++) {
24
+ this.b[i] = b[i + 1];
25
+ }
26
+ }
27
+ public double getIntercept() {
28
+ return a;
29
+ }
30
+ public double[] getSlope() {
31
+ return b;
32
+ }
33
+ }
34
+ private class NonLineRegAna {
35
+ private final double eta = 0.001;
36
+ private final int num = 10000;
37
+ public LineReg nonLineRegAna(double[] yi, double[][] xij) {
38
+ double[] b = new double[1 + xij[0].length];
39
+
40
+ for(int i = 0; i < b.length; i++) {
41
+ b[i] = 0.0;
42
+ }
43
+ for (int i = 0; i < num; i++) {
44
+ b = grand_metod(yi, b, xij);
45
+ }
46
+ return new LineReg(b);
47
+ }
48
+ // q = b0 + b1 * x0
49
+ private double rereion(double[] b, double[] xi) {
50
+ double ret = b[0];
51
+
52
+ for(int i = 0; i < xi.length; i++) {
53
+ ret += b[i + 1] * xi[i];
54
+ }
55
+ return ret;
56
+ }
57
+ // p = 1 / (1 + exp( -q))
58
+ private double sigmoid(double q) {
59
+ return 1.0 / (1.0 + Math.exp(-1.0 * q));
60
+ }
61
+ private double[] grand_metod(double[] yi, double[] b, double[][] xij) {
62
+ double e0 = 0.0;
63
+ double[] en = new double[xij[0].length];
64
+
65
+ for(int i = 0; i < yi.length; i++) {
66
+ double q = rereion(b, xij[i]);
67
+ double p = sigmoid(q);
68
+
69
+ e0 += (yi[i] - p);
70
+ for(int j = 0; j < en.length; j++) {
71
+ en[j] += (yi[i] - p) * xij[i][j];
72
+ }
73
+ }
74
+ b[0] = b[0] + eta * e0;
75
+ for(int j = 0; j < en.length; j++) {
76
+ b[1 + j] += eta * en[j];
77
+ }
78
+ return b;
79
+ }
80
+ }
81
+ }
82
+
@@ -0,0 +1,205 @@
1
+ import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression;
2
+ import org.apache.commons.math3.stat.regression.GLSMultipleLinearRegression;
3
+ import org.apache.commons.math3.stat.correlation.Covariance;
4
+ import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
5
+ import java.util.Arrays;
6
+ import org.apache.commons.math3.distribution.ChiSquaredDistribution;
7
+
8
+ public class MultRegAna {
9
+ private final double A = 0.05;
10
+ private static MultRegAna regana = new MultRegAna();
11
+ public static MultRegAna getInstance() {
12
+ return regana;
13
+ }
14
+ public LineReg lineRegAna(double[] yi, double xij[][]) {
15
+ LineRegAna line = createLineRegAna(yi, xij);
16
+
17
+ return line.lineRegAna(yi, xij);
18
+ }
19
+ public double getR2(double[] yi, double xij[][]) {
20
+ LineRegAna line = createLineRegAna(yi, xij);
21
+
22
+ return line.getR2(yi, xij);
23
+ }
24
+ public double getAdjR2(double[] yi, double xij[][]) {
25
+ LineRegAna line = createLineRegAna(yi, xij);
26
+
27
+ return line.getAdjR2(yi, xij);
28
+ }
29
+ private LineRegAna createLineRegAna(double[] yi, double xij[][]) {
30
+ double[][] data = createData(yi, xij);
31
+
32
+ // 等分散性の検定
33
+ if (false == bartletTest(data)) { // 等分散性
34
+ return new OLSMultRegAna();
35
+ }
36
+ else { //
37
+ return new GLSMultRegAna(data);
38
+ }
39
+ }
40
+ private double[][] createData(double[] yi, double xij[][]) {
41
+ double[][] data = new double[yi.length][1 + xij[0].length];
42
+
43
+ for (int i = 0; i < yi.length; i++) {
44
+ data[i][0] = yi[i];
45
+ System.arraycopy(xij[i], 0, data[i], 1, xij[0].length);
46
+ }
47
+ return data;
48
+ }
49
+ private boolean bartletTest(double data[][]) {
50
+ OneWayAnovaTest anova = new BartletTest();
51
+ double statistic = anova.calcTestStatistic(data);
52
+
53
+ return anova.execute_test(statistic, A);
54
+ }
55
+ /*********************************/
56
+ /* interface define */
57
+ /*********************************/
58
+ private interface LineRegAna {
59
+ // 最小2乗法
60
+ LineReg lineRegAna(double[] yi, double xij[][]);
61
+ // 決定係数取得
62
+ double getR2(double[] yi, double xij[][]);
63
+ // 自由度調整済み決定係数
64
+ double getAdjR2(double[] yi, double xij[][]);
65
+ }
66
+ private interface OneWayAnovaTest {
67
+ double calcTestStatistic(double[][] xi);
68
+ boolean execute_test(double statistic, double a);
69
+ }
70
+ /*********************************/
71
+ /* class define */
72
+ /*********************************/
73
+ public class LineReg {
74
+ private double a = 0.0;
75
+ private double[] b = null;
76
+ public LineReg(double[] b) {
77
+ this.a = b[0];
78
+ this.b = new double[b.length - 1];
79
+ for (int i = 0; i < this.b.length; i++) {
80
+ this.b[i] = b[i + 1];
81
+ }
82
+ }
83
+ public double getIntercept() {
84
+ return a;
85
+ }
86
+ public double[] getSlope() {
87
+ return b;
88
+ }
89
+ }
90
+ // 等分散性検定
91
+ private class BartletTest implements OneWayAnovaTest {
92
+ private int n = 0;
93
+ public double calcTestStatistic(double[][] xi) {
94
+ n = xi.length;
95
+ double ln2L = logL(xi);
96
+
97
+ return calcB(ln2L, xi);
98
+ }
99
+ private double logL(double[][] xi) {
100
+ double[] si = new double[n];
101
+ DescriptiveStatistics stat = new DescriptiveStatistics();
102
+ double nisi2 = 0.0; // (Ni - 1)*si^2の合計
103
+ double nilogsi2 = 0.0; // (Ni - 1)*log(si^2)の合計
104
+ int sumN = 0;
105
+
106
+ for(int i = 0; i < n; i++) {
107
+ Arrays.stream(xi[i]).forEach(stat::addValue);
108
+ sumN += stat.getN();
109
+ si[i] = stat.getVariance();
110
+ nisi2 += (stat.getN() - 1) * si[i];
111
+ nilogsi2 += (stat.getN() - 1) * Math.log(si[i]);
112
+ stat.clear();
113
+ }
114
+ double sumNin = sumN - n;
115
+ return sumNin * (Math.log(nisi2 / sumNin) - nilogsi2 / sumNin);
116
+ }
117
+ private double calcB(double ln2L, double[][] xi) {
118
+ double invSumN = 0.0;
119
+ int sumN = 0;
120
+ DescriptiveStatistics stat = new DescriptiveStatistics();
121
+
122
+ for(int i = 0; i < n; i++) {
123
+ Arrays.stream(xi[i]).forEach(stat::addValue);
124
+ invSumN += 1.0 / (stat.getN() - 1.0);
125
+ sumN += stat.getN();
126
+ stat.clear();
127
+ }
128
+ double deno = 1 + 1.0 / (3 * (n - 1))
129
+ * (invSumN - 1.0 / (sumN - n));
130
+ return ln2L / deno;
131
+ }
132
+ public boolean execute_test(double statistic, double a) {
133
+ ChiSquaredDistribution chi2Dist = new ChiSquaredDistribution(n - 1);
134
+ double r_val = chi2Dist.inverseCumulativeProbability(1.0 - a);
135
+
136
+ return (r_val < statistic) ? true : false;
137
+ }
138
+ }
139
+
140
+ // 最小2乗法
141
+ private class OLSMultRegAna implements LineRegAna {
142
+ private OLSMultipleLinearRegression regression = null;
143
+ public OLSMultRegAna() {
144
+ regression = new OLSMultipleLinearRegression();
145
+ }
146
+ public LineReg lineRegAna(double[] yi, double xij[][]) {
147
+ regression.newSampleData(yi, xij);
148
+
149
+ double[] beta = regression.estimateRegressionParameters();
150
+
151
+ return new LineReg(beta);
152
+ }
153
+ // 決定係数取得
154
+ public double getR2(double[] yi, double xij[][]) {
155
+ regression.newSampleData(yi, xij);
156
+ return regression.calculateRSquared();
157
+ }
158
+ // 自由度調整済み決定係数
159
+ public double getAdjR2(double[] yi, double xij[][]) {
160
+ regression.newSampleData(yi, xij);
161
+ return regression.calculateAdjustedRSquared();
162
+ }
163
+
164
+ }
165
+ // 一般化最小2乗法
166
+ private class GLSMultRegAna implements LineRegAna {
167
+ private GLSMultipleLinearRegression regression = null;
168
+ private double[][] data = null;
169
+ public GLSMultRegAna(double data[][]) {
170
+ regression = new GLSMultipleLinearRegression();
171
+ this.data = data;
172
+ }
173
+ public LineReg lineRegAna(double[] yi, double xij[][]) {
174
+ double[][] omega = calcCovatrianceMatrix();
175
+ regression.newSampleData(yi, xij, omega);
176
+
177
+ double[] beta = regression.estimateRegressionParameters();
178
+ return new LineReg(beta);
179
+ }
180
+ // 決定係数取得
181
+ public double getR2(double[] yi, double xij[][]) {
182
+ return 0.0;
183
+ }
184
+ // 自由度調整済み決定係数
185
+ public double getAdjR2(double[] yi, double xij[][]) {
186
+ return 0.0;
187
+ }
188
+ private double[][] calcCovatrianceMatrix() {
189
+ Covariance corel = new Covariance();
190
+ double[][] omega = new double[data.length][data.length];
191
+
192
+ for(int i = 0; i < data.length; i++) {
193
+ for(int j = 0; j < data.length; j++) {
194
+ double[] xArray = data[i];
195
+ double[] yArray = data[j];
196
+
197
+ omega[i][j] = corel.covariance(xArray, yArray);
198
+ }
199
+ }
200
+ return omega;
201
+ }
202
+ }
203
+
204
+ }
205
+
@@ -0,0 +1,82 @@
1
+ public class PoissonRegAna {
2
+ private static PoissonRegAna regana = new PoissonRegAna();
3
+ public static PoissonRegAna getInstance() {
4
+ return regana;
5
+ }
6
+ public LineReg nonLineRegAna(double[] yi, double[][] xij) {
7
+ NonLineRegAna line = new NonLineRegAna();
8
+
9
+ return line.nonLineRegAna(yi, xij);
10
+ }
11
+ /*********************************/
12
+ /* interface define */
13
+ /*********************************/
14
+ /*********************************/
15
+ /* class define */
16
+ /*********************************/
17
+ public class LineReg {
18
+ private double a = 0.0;
19
+ private double[] b = null;
20
+ public LineReg(double[] b) {
21
+ this.a = b[0];
22
+ this.b = new double[b.length - 1];
23
+ for (int i = 0; i < this.b.length; i++) {
24
+ this.b[i] = b[i + 1];
25
+ }
26
+ }
27
+ public double getIntercept() {
28
+ return a;
29
+ }
30
+ public double[] getSlope() {
31
+ return b;
32
+ }
33
+ }
34
+ private class NonLineRegAna {
35
+ private final double eta = 0.005;
36
+ private final int num = 1000;
37
+ public LineReg nonLineRegAna(double[] yi, double[][] xij) {
38
+ double[] b = new double[1 + xij[0].length];
39
+
40
+ for(int i = 0; i < b.length; i++) {
41
+ b[i] = 0.0;
42
+ }
43
+ for (int i = 0; i < num; i++) {
44
+ b = grand_metod(yi, b, xij);
45
+ }
46
+
47
+ return new LineReg(b);
48
+ }
49
+ // q = b0 + b1 * x0
50
+ private double rereion(double[] b, double[] xi) {
51
+ double ret = b[0];
52
+
53
+ for(int i = 0; i < xi.length; i++) {
54
+ ret += b[i + 1] * xi[i];
55
+ }
56
+ return ret;
57
+ }
58
+ private double linkFunc(double q) {
59
+ return Math.exp(q);
60
+ }
61
+ private double[] grand_metod(double[] yi, double[] b, double[][] xij) {
62
+ double e0 = 0.0;
63
+ double[] en = new double[xij[0].length];
64
+
65
+ for(int i = 0; i < yi.length; i++) {
66
+ double q = rereion(b, xij[i]);
67
+ double p = linkFunc(q);
68
+
69
+ e0 += (yi[i] - p);
70
+ for(int j = 0; j < en.length; j++) {
71
+ en[j] += (yi[i] - p) * xij[i][j];
72
+ }
73
+ }
74
+ b[0] += eta * e0;
75
+ for(int j = 0; j < en.length; j++) {
76
+ b[1 + j] += eta * en[j];
77
+ }
78
+ return b;
79
+ }
80
+ }
81
+ }
82
+
@@ -0,0 +1,100 @@
1
+ require 'java'
2
+ require 'num4regana.jar'
3
+ require 'commons-math3-3.6.1.jar'
4
+
5
+ java_import 'LogitRegAna'
6
+ java_import 'PoissonRegAna'
7
+
8
+ # 一般化線形回帰分析
9
+ # (Apache commoms math3使用)
10
+ module Num4GLMRegAnaLib
11
+ # (2項)ロジスティック回帰分析
12
+ class LogitRegAnaLib
13
+ def initialize
14
+ @multana = LogitRegAna.getInstance()
15
+ end
16
+ # (2項)ロジスティック回帰分析
17
+ #
18
+ # @overload non_line_reg_ana(yi, xij)
19
+ # @param [Array] yi yの値(double[])
20
+ # @param [Array] xij xの値(double[][])
21
+ # @return [Hash] (intercept:定数項 slope:回帰係数)
22
+ # @example
23
+ # glsyi = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
24
+ # glsxij = [
25
+ # [95],
26
+ # [90],
27
+ # [85],
28
+ # [80],
29
+ # [80],
30
+ # [75],
31
+ # [70],
32
+ # [70],
33
+ # [65],
34
+ # [50],
35
+ # [60],
36
+ # [55],
37
+ # [45],
38
+ # [65],
39
+ # [40],
40
+ # [35],
41
+ # [55],
42
+ # [50],
43
+ # [50],
44
+ # [45],
45
+ # ]
46
+ # regana = Num4RegAnaLib::LogitRegAnaLib.new
47
+ # regana.non_line_reg_ana(glsyi, glsxij)
48
+ # =>
49
+ # {
50
+ # "intercept": -17.81, # 定数項
51
+ # "slope": [0.16], # 回帰係数
52
+ # }
53
+ def non_line_reg_ana(yi, xij)
54
+ multRet = @multana.nonLineRegAna(yi.to_java(Java::double), xij.to_java(Java::double[]))
55
+ retRb = {
56
+ "intercept": multRet.getIntercept(), # 定数項
57
+ "slope": multRet.getSlope().to_a, # 回帰係数
58
+ }
59
+ return retRb
60
+ end
61
+ end
62
+ # ポアソン回帰分析
63
+ class PoissonRegAnaLib
64
+ def initialize
65
+ @multana = PoissonRegAna.getInstance()
66
+ end
67
+ # ポアソン回帰分析
68
+ #
69
+ # @overload non_line_reg_ana(yi, xij)
70
+ # @param [Array] yi yの値(double[])
71
+ # @param [Array] xij xの値(double[][])
72
+ # @return [Hash] (intercept:定数項 slope:回帰係数)
73
+ # @example
74
+ # glsyi = [4, 10, 7, 14]
75
+ # glsxij = [
76
+ # [1],
77
+ # [2],
78
+ # [3],
79
+ # [4],
80
+ # ]
81
+ # regana = Num4RegAnaLib::PoissonRegAnaLib.new
82
+ # regana.non_line_reg_ana(glsyi, glsxij)
83
+ # =>
84
+ # {
85
+ # "intercept": 1.3138, # 定数項
86
+ # "slope": [0.3173], # 回帰係数
87
+ # }
88
+ def non_line_reg_ana(yi, xij)
89
+ multRet = @multana.nonLineRegAna(yi.to_java(Java::double), xij.to_java(Java::double[]))
90
+ retRb = {
91
+ "intercept": multRet.getIntercept(), # 定数項
92
+ "slope": multRet.getSlope().to_a, # 回帰係数
93
+ }
94
+ return retRb
95
+ end
96
+ end
97
+ end
98
+
99
+
100
+
@@ -0,0 +1,165 @@
1
+ require 'java'
2
+ require 'num4regana.jar'
3
+ require 'commons-math3-3.6.1.jar'
4
+
5
+ java_import 'SmplRegAna'
6
+ java_import 'MultRegAna'
7
+ # 線形回帰分析
8
+ # (Apache commoms math3使用)
9
+ module Num4LineRegAnaLib
10
+ # 単回帰分析
11
+ class SmplRegAnaLib
12
+ def initialize
13
+ @regana = SmplRegAna.getInstance()
14
+ end
15
+ # 単回帰分析
16
+ #
17
+ # @overload line_reg_ana(yi, xi)
18
+ # @param [Array] yi yの値(double[])
19
+ # @param [Array] xi xの値(double[])
20
+ # @return [Hash] (intercept:定数項 slope:回帰係数)
21
+ # @example
22
+ # yi = [286, 851, 589, 389, 158, 1037, 463, 563, 372, 1020]
23
+ # xi = [107, 336, 233, 82, 61, 378, 129, 313, 142, 428]
24
+ # regana = Num4RegAnaLib::SmplRegAnaLib.new
25
+ # regana.line_reg_ana(yi, xi)
26
+ # =>
27
+ # {
28
+ # "intercept": 99.075, # 定数項
29
+ # "slope": 2.145, # 回帰係数
30
+ # }
31
+ def line_reg_ana(yi, xi)
32
+ ret = @regana.lineRegAna(yi.to_java(Java::double), xi.to_java(Java::double))
33
+ retRb = {
34
+ "intercept": ret.getIntercept(), # 定数項
35
+ "slope": ret.getSlope(), # 回帰係数
36
+ }
37
+ return retRb
38
+ end
39
+ # 決定係数
40
+ #
41
+ # @overload getr2(yi, xi)
42
+ # @param [Array] yi yの値(double[])
43
+ # @param [Array] xi xの値(double[])
44
+ # @return [double] 決定係数
45
+ # @example
46
+ # yi = [286, 851, 589, 389, 158, 1037, 463, 563, 372, 1020]
47
+ # xi = [107, 336, 233, 82, 61, 378, 129, 313, 142, 428]
48
+ # regana = Num4RegAnaLib::SmplRegAnaLib.new
49
+ # regana.getr2(yi, xi)
50
+ # => 0.893
51
+ def getr2(yi, xi)
52
+ return @regana.getR2(yi.to_java(Java::double), xi.to_java(Java::double))
53
+ end
54
+ # 相関係数
55
+ #
56
+ # @overload getr(yi, xi)
57
+ # @param [Array] yi yの値(double[])
58
+ # @param [Array] xi xの値(double[])
59
+ # @return [double] 決定係数
60
+ # @example
61
+ # yi = [286, 851, 589, 389, 158, 1037, 463, 563, 372, 1020]
62
+ # xi = [107, 336, 233, 82, 61, 378, 129, 313, 142, 428]
63
+ # regana = Num4RegAnaLib::SmplRegAnaLib.new
64
+ # regana.getr(yi, xi)
65
+ # => 0.945
66
+ def getr(yi, xi)
67
+ return @regana.getR(yi.to_java(Java::double), xi.to_java(Java::double))
68
+ end
69
+ end
70
+ # 重回帰分析(最小2乗法:等分散性checkあり)
71
+ class OLSMultRegAnaLib
72
+ def initialize
73
+ @multana = MultRegAna.getInstance()
74
+ end
75
+ # 重回帰分析
76
+ #
77
+ # @overload line_reg_ana(yi, xij)
78
+ # @param [Array] yi yの値(double[])
79
+ # @param [Array] xij xの値(double[][])
80
+ # @return [Hash] (intercept:定数項 slope:回帰係数)
81
+ # @example
82
+ # olsyi = [45, 38, 41, 34, 59, 47, 35, 43, 54, 52]
83
+ # olsxij = [
84
+ # [17.5, 30],
85
+ # [17.0, 25],
86
+ # [18.5, 20],
87
+ # [16.0, 30],
88
+ # [19.0, 45],
89
+ # [19.5, 35],
90
+ # [16.0, 25],
91
+ # [18.0, 35],
92
+ # [19.0, 35],
93
+ # [19.5, 40],
94
+ # ]
95
+ # regana = Num4RegAnaLib::OLSMultRegAnaLib.new
96
+ # regana.line_reg_ana(olsyi, olsxij)
97
+ # =>
98
+ # {
99
+ # "intercept": -34.71, # 定数項
100
+ # "slope": [3.47, 0.53], # 回帰係数
101
+ # }
102
+ def line_reg_ana(yi, xij)
103
+ multRet = @multana.lineRegAna(yi.to_java(Java::double), xij.to_java(Java::double[]))
104
+
105
+ retRb = {
106
+ "intercept": multRet.getIntercept(), # 定数項
107
+ "slope": multRet.getSlope().to_a, # 回帰係数
108
+ }
109
+ return retRb
110
+ end
111
+ # 決定係数
112
+ #
113
+ # @overload getr2(yi, xij)
114
+ # @param [Array] yi yの値(double[])
115
+ # @param [Array] xij xの値(double[][])
116
+ # @return [double] 決定係数
117
+ # @example
118
+ # olsyi = [45, 38, 41, 34, 59, 47, 35, 43, 54, 52]
119
+ # olsxij = [
120
+ # [17.5, 30],
121
+ # [17.0, 25],
122
+ # [18.5, 20],
123
+ # [16.0, 30],
124
+ # [19.0, 45],
125
+ # [19.5, 35],
126
+ # [16.0, 25],
127
+ # [18.0, 35],
128
+ # [19.0, 35],
129
+ # [19.5, 40],
130
+ # ]
131
+ # regana = Num4RegAnaLib::OLSMultRegAnaLib.new
132
+ # regana.getr2(yi, xi)
133
+ # => 0.858
134
+ def getr2(yi, xij)
135
+ return @multana.getR2(yi.to_java(Java::double), xij.to_java(Java::double[]))
136
+ end
137
+ # 自由度調整済み決定係数
138
+ #
139
+ # @overload getadjr2(yi, xij)
140
+ # @param [Array] yi yの値(double[])
141
+ # @param [Array] xij xの値(double[][])
142
+ # @return [double] 決定係数
143
+ # @example
144
+ # olsyi = [45, 38, 41, 34, 59, 47, 35, 43, 54, 52]
145
+ # olsxij = [
146
+ # [17.5, 30],
147
+ # [17.0, 25],
148
+ # [18.5, 20],
149
+ # [16.0, 30],
150
+ # [19.0, 45],
151
+ # [19.5, 35],
152
+ # [16.0, 25],
153
+ # [18.0, 35],
154
+ # [19.0, 35],
155
+ # [19.5, 40],
156
+ # ]
157
+ # regana = Num4RegAnaLib::OLSMultRegAnaLib.new
158
+ # regana.getadjr2(yi, xij)
159
+ # => 0.8176
160
+ def getadjr2(yi, xij)
161
+ return @multana.getAdjR2(yi.to_java(Java::double), xij.to_java(Java::double[]))
162
+ end
163
+ end
164
+ end
165
+
data/lib/num4regana.rb CHANGED
@@ -1,165 +1,9 @@
1
- require 'java'
2
- require 'num4regana.jar'
3
- require 'commons-math3-3.6.1.jar'
1
+ require_relative('num4lineregana')
2
+ require_relative('num4glmregana')
4
3
 
5
- java_import 'SmplRegAna'
6
- java_import 'OLSMultRegAna'
7
- # 回帰分析
8
- # (Apache commoms math3使用)
9
4
  module Num4RegAnaLib
10
- # 単回帰分析
11
- class SmplRegAnaLib
12
- def initialize
13
- @regana = SmplRegAna.getInstance()
14
- end
15
- # 単回帰分析
16
- #
17
- # @overload line_reg_ana(yi, xi)
18
- # @param [Array] yi yの値(double[])
19
- # @param [Array] xi xの値(double[])
20
- # @return [Hash] (intercept:定数項 slope:回帰係数)
21
- # @example
22
- # yi = [286, 851, 589, 389, 158, 1037, 463, 563, 372, 1020]
23
- # xi = [107, 336, 233, 82, 61, 378, 129, 313, 142, 428]
24
- # regana = Num4RegAnaLib::SmplRegAnaLib.new
25
- # regana.line_reg_ana(yi, xi)
26
- # =>
27
- # {
28
- # "intercept": 99.075, # 定数項
29
- # "slope": 2.145, # 回帰係数
30
- # }
31
- def line_reg_ana(yi, xi)
32
- ret = @regana.lineRegAna(yi.to_java(Java::double), xi.to_java(Java::double))
33
- retRb = {
34
- "intercept": ret.getIntercept(), # 定数項
35
- "slope": ret.getSlope(), # 回帰係数
36
- }
37
- return retRb
38
- end
39
- # 決定係数
40
- #
41
- # @overload getr2(yi, xi)
42
- # @param [Array] yi yの値(double[])
43
- # @param [Array] xi xの値(double[])
44
- # @return [double] 決定係数
45
- # @example
46
- # yi = [286, 851, 589, 389, 158, 1037, 463, 563, 372, 1020]
47
- # xi = [107, 336, 233, 82, 61, 378, 129, 313, 142, 428]
48
- # regana = Num4RegAnaLib::SmplRegAnaLib.new
49
- # regana.getr2(yi, xi)
50
- # => 0.893
51
- def getr2(yi, xi)
52
- return @regana.getR2(yi.to_java(Java::double), xi.to_java(Java::double))
53
- end
54
- # 相関係数
55
- #
56
- # @overload getr(yi, xi)
57
- # @param [Array] yi yの値(double[])
58
- # @param [Array] xi xの値(double[])
59
- # @return [double] 決定係数
60
- # @example
61
- # yi = [286, 851, 589, 389, 158, 1037, 463, 563, 372, 1020]
62
- # xi = [107, 336, 233, 82, 61, 378, 129, 313, 142, 428]
63
- # regana = Num4RegAnaLib::SmplRegAnaLib.new
64
- # regana.getr(yi, xi)
65
- # => 0.945
66
- def getr(yi, xi)
67
- return @regana.getR(yi.to_java(Java::double), xi.to_java(Java::double))
68
- end
69
- end
70
- # 重回帰分析(最小2乗法)
71
- class OLSMultRegAnaLib
72
- def initialize
73
- @regana = OLSMultRegAna.getInstance()
74
- end
75
- # 重回帰分析
76
- #
77
- # @overload line_reg_ana(yi, xij)
78
- # @param [Array] yi yの値(double[])
79
- # @param [Array] xij xの値(double[][])
80
- # @return [Hash] (intercept:定数項 slope:回帰係数)
81
- # @example
82
- # olsyi = [45, 38, 41, 34, 59, 47, 35, 43, 54, 52]
83
- # olsxij = [
84
- # [17.5, 30],
85
- # [17.0, 25],
86
- # [18.5, 20],
87
- # [16.0, 30],
88
- # [19.0, 45],
89
- # [19.5, 35],
90
- # [16.0, 25],
91
- # [18.0, 35],
92
- # [19.0, 35],
93
- # [19.5, 40],
94
- # ]
95
- # regana = Num4RegAnaLib::OLSMultRegAnaLib.new
96
- # regana.line_reg_ana(olsyi, olsxij)
97
- # =>
98
- # {
99
- # "intercept": -34.71, # 定数項
100
- # "slope": [3.47, 0.53], # 回帰係数
101
- # }
102
- def line_reg_ana(yi, xij)
103
- ret = @regana.lineRegAna(yi.to_java(Java::double), xij.to_java(Java::double[]))
104
- retRb = {
105
- "intercept": ret.getIntercept(), # 定数項
106
- "slope": ret.getSlope().to_a, # 回帰係数
107
- }
108
- return retRb
109
- end
110
- # 決定係数
111
- #
112
- # @overload getr2(yi, xij)
113
- # @param [Array] yi yの値(double[])
114
- # @param [Array] xij xの値(double[][])
115
- # @return [double] 決定係数
116
- # @example
117
- # olsyi = [45, 38, 41, 34, 59, 47, 35, 43, 54, 52]
118
- # olsxij = [
119
- # [17.5, 30],
120
- # [17.0, 25],
121
- # [18.5, 20],
122
- # [16.0, 30],
123
- # [19.0, 45],
124
- # [19.5, 35],
125
- # [16.0, 25],
126
- # [18.0, 35],
127
- # [19.0, 35],
128
- # [19.5, 40],
129
- # ]
130
- # regana = Num4RegAnaLib::OLSMultRegAnaLib.new
131
- # regana.getr2(yi, xi)
132
- # => 0.858
133
- def getr2(yi, xij)
134
- return @regana.getR2(yi.to_java(Java::double), xij.to_java(Java::double[]))
135
- end
136
- # 自由度調整済み決定係数
137
- #
138
- # @overload getadjr2(yi, xij)
139
- # @param [Array] yi yの値(double[])
140
- # @param [Array] xij xの値(double[][])
141
- # @return [double] 決定係数
142
- # @example
143
- # olsyi = [45, 38, 41, 34, 59, 47, 35, 43, 54, 52]
144
- # olsxij = [
145
- # [17.5, 30],
146
- # [17.0, 25],
147
- # [18.5, 20],
148
- # [16.0, 30],
149
- # [19.0, 45],
150
- # [19.5, 35],
151
- # [16.0, 25],
152
- # [18.0, 35],
153
- # [19.0, 35],
154
- # [19.5, 40],
155
- # ]
156
- # regana = Num4RegAnaLib::OLSMultRegAnaLib.new
157
- # regana.getadjr2(yi, xij)
158
- # => 0.8176
159
- def getadjr2(yi, xij)
160
- return @regana.getAdjR2(yi.to_java(Java::double), xij.to_java(Java::double[]))
161
- end
162
-
163
- end
5
+ include Num4LineRegAnaLib
6
+ include Num4GLMRegAnaLib
164
7
  end
165
8
 
9
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: num4regana
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: java
6
6
  authors:
7
7
  - siranovel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-27 00:00:00.000000000 Z
11
+ date: 2024-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -61,9 +61,13 @@ files:
61
61
  - Gemfile
62
62
  - LICENSE
63
63
  - Rakefile
64
- - ext/num4regana/OLSMultRegAna.java
64
+ - ext/num4regana/LogitRegAna.java
65
+ - ext/num4regana/MultRegAna.java
66
+ - ext/num4regana/PoissonRegAna.java
65
67
  - ext/num4regana/SmplRegAna.java
66
68
  - lib/commons-math3-3.6.1.jar
69
+ - lib/num4glmregana.rb
70
+ - lib/num4lineregana.rb
67
71
  - lib/num4regana.rb
68
72
  homepage: http://github.com/siranovel/num4regana
69
73
  licenses:
@@ -1,75 +0,0 @@
1
- import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression;
2
-
3
- public class OLSMultRegAna {
4
- private static OLSMultRegAna regana = new OLSMultRegAna();
5
- public static OLSMultRegAna getInstance() {
6
- return regana;
7
- }
8
- public LineReg lineRegAna(double[] yi, double xij[][]) {
9
- LineRegAna line = new LineRegAna();
10
-
11
- return line.lineRegAna(yi, xij);
12
- }
13
- public double getR2(double[] yi, double xij[][]) {
14
- LineRegAna line = new LineRegAna();
15
-
16
- return line.getR2(yi, xij);
17
- }
18
- public double getAdjR2(double[] yi, double xij[][]) {
19
- LineRegAna line = new LineRegAna();
20
-
21
- return line.getAdjR2(yi, xij);
22
- }
23
- /*********************************/
24
- /* interface define */
25
- /*********************************/
26
- /*********************************/
27
- /* class define */
28
- /*********************************/
29
- public class LineReg {
30
- private double a = 0.0;
31
- private double[] b = null;
32
- public LineReg(double[] b) {
33
- this.a = b[0];
34
- this.b = new double[b.length - 1];
35
- for (int i = 0; i < this.b.length; i++) {
36
- this.b[i] = b[i + 1];
37
- }
38
- }
39
- public double getIntercept() {
40
- return a;
41
- }
42
- public double[] getSlope() {
43
- return b;
44
- }
45
- }
46
- private class LineRegAna {
47
- // 最小2乗法
48
- public LineReg lineRegAna(double[] yi, double xij[][]) {
49
- OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
50
-
51
- regression.newSampleData(yi, xij);
52
-
53
- double[] beta = regression.estimateRegressionParameters();
54
-
55
- LineReg ret = new LineReg(beta);
56
- return ret;
57
- }
58
- // 決定係数取得
59
- public double getR2(double[] yi, double xij[][]) {
60
- OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
61
-
62
- regression.newSampleData(yi, xij);
63
- return regression.calculateRSquared();
64
- }
65
- // 自由度調整済み決定係数
66
- public double getAdjR2(double[] yi, double xij[][]) {
67
- OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
68
-
69
- regression.newSampleData(yi, xij);
70
- return regression.calculateAdjustedRSquared();
71
- }
72
-
73
- }
74
- }
75
-