num4anova 0.0.4-java → 0.0.5-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ca0164a7092861bfca67f257129332375e90bfae762365194ced78ef75a293a7
4
- data.tar.gz: 16f17fb717e3d92c27c4c651e034704cd81609a3adc293555eac54f9209884be
3
+ metadata.gz: 7a089d52ead2c9726d9ca61db475f128925afddca7cbcf6f65f0da75e41f0b1b
4
+ data.tar.gz: 050e29e04bd0e8272da89d5aba231fe08294f50f090d261da7aa6822f5df5bc5
5
5
  SHA512:
6
- metadata.gz: e14efb373cde03fc5872f3dfb1d4186e5d2263b0b7597970d1629c562cfb6696ecf2cdbfc3325aa2d732e252241de093c156b7d9da21b51a8ea305f3e24e0e0f
7
- data.tar.gz: c9b7f310d00d060d1c6fee3e8e9f5c43fce812a65d51b192315c72a378de7e2bc775e1118346373a93047c1cd8880240d03d52309ec0d8f567481923f14e31b4
6
+ metadata.gz: f0cc84d4cebb416b04ed0360ef4cf72799aaebcf70104b6e2d2dc2bc471400d3ac313b71f5885fd0854ae8b3ae22dd66c753066233b1ed2848aabf586cee2c05
7
+ data.tar.gz: 88a74fb295e7ce0e0bdba405a14239a37fac4f3115bd29f358ce7b54fcc3b71a1533ff095e5aea7e2b141d867051eca703d179874f1ed349aef92575d8d0a186
data/CHANGELOG.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## [0.0.5] - 2024-02-01
6
+
7
+ ### add
8
+ - add function of twoway_anova.
9
+
5
10
  ## [0.0.4] - 2024-01-30
6
11
  ### add
7
12
  - add function of replicate_plot
@@ -58,7 +58,7 @@ public class OneWayLayout {
58
58
  OneWayAnovaTest oneway = new BartletTest();
59
59
 
60
60
  double statistic = oneway.calcTestStatistic(xi);
61
- return oneway.test(statistic, a);
61
+ return oneway.execute_test(statistic, a);
62
62
  }
63
63
  public void replicatePlot(String dname, Map<String, double[]> vals) {
64
64
  ChartPlot plot = new ReplicateChartPlot();
@@ -70,7 +70,7 @@ public class OneWayLayout {
70
70
  OneWayAnovaTest oneway = new ReplicateTest();
71
71
 
72
72
  double statistic = oneway.calcTestStatistic(xi);
73
- return oneway.test(statistic, a);
73
+ return oneway.execute_test(statistic, a);
74
74
  }
75
75
  /*********************************/
76
76
  /* interface define */
@@ -91,7 +91,7 @@ public class OneWayLayout {
91
91
  }
92
92
  private interface OneWayAnovaTest {
93
93
  double calcTestStatistic(double[][] xi);
94
- boolean test(double statistic, double a);
94
+ boolean execute_test(double statistic, double a);
95
95
  }
96
96
  /*********************************/
97
97
  /* class define */
@@ -251,7 +251,7 @@ public class OneWayLayout {
251
251
  * (invSumN - 1.0 / (sumN - n));
252
252
  return ln2L / deno;
253
253
  }
254
- public boolean test(double statistic, double a) {
254
+ public boolean execute_test(double statistic, double a) {
255
255
  ChiSquaredDistribution chi2Dist = new ChiSquaredDistribution(n - 1);
256
256
  double r_val = chi2Dist.inverseCumulativeProbability(1.0 - a);
257
257
 
@@ -373,7 +373,7 @@ public class OneWayLayout {
373
373
  sumSb1 = stat.getSumsq() / a;
374
374
  return sumSb1 - sumSb2 * sumSb2 / (a * b);
375
375
  }
376
- public boolean test(double statistic, double a) {
376
+ public boolean execute_test(double statistic, double a) {
377
377
  FDistribution fDist = new FDistribution(a1 - 1, (a1 - 1) * (b1 - 1));
378
378
  double f = fDist.inverseCumulativeProbability(1.0 - a);
379
379
 
@@ -0,0 +1,186 @@
1
+ import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
2
+ import org.apache.commons.math3.distribution.FDistribution;
3
+ import java.util.Arrays;
4
+ public class TwoWayLayout {
5
+ private static TwoWayLayout twoWay = new TwoWayLayout();
6
+ public static TwoWayLayout getInstance() {
7
+ return twoWay;
8
+ }
9
+ public boolean[] twowayAnova(double[][][] xij, double a) {
10
+ TwoWayAnovaTest twoway = new TwoWayAnova();
11
+
12
+ double[] statistic = twoway.calcTestStatistic(xij);
13
+ return twoway.execute_test(statistic, a);
14
+ }
15
+ /*********************************/
16
+ /* interface define */
17
+ /*********************************/
18
+ private interface TwoWayAnovaTest {
19
+ double[] calcTestStatistic(double[][][] xij);
20
+ boolean[] execute_test(double statistic[], double a);
21
+ }
22
+ /*********************************/
23
+ /* class define */
24
+ /*********************************/
25
+ private class TwoWayAnova implements TwoWayAnovaTest {
26
+ private int a = 0;
27
+ private int b = 0;
28
+ private int n = 0;
29
+ private int an = 0;
30
+ private int bn = 0;
31
+ private int abn = 0;
32
+ private int en = 0;
33
+ public double[] calcTestStatistic(double[][][] xij) {
34
+ double statistic[] = new double[3];
35
+ a = xij.length;
36
+ b = xij[0].length;
37
+ n = xij[0][0].length;
38
+ an = a- 1;
39
+ bn = b - 1;
40
+ abn = (a- 1) * (b - 1);
41
+ en = a * b * (n - 1);
42
+
43
+ double[][] meanXij = calcMeanXij(xij);
44
+ double[] meanAn = calcMeanAn(meanXij);
45
+ double[] meanBn = calcMeanBn(meanXij);
46
+ double meanABn = calcMeanABn(meanAn);
47
+
48
+ double allDrift = calcAllDrift(xij, meanABn); // 全変動
49
+ double anDrift = calcAnDrift(meanAn, meanABn); // 水準Ai間変動
50
+ double bnDrift = calcBnDrift(meanBn, meanABn); // 水準Bj間変動
51
+ // 交互作用の変動
52
+ double interaDrift = calcInteraDrift(meanXij, meanAn, meanBn, meanABn);
53
+ double benchDrift = calcBenchDrift(xij, meanXij); // 水準内変動
54
+ double va = b * n * anDrift / an;
55
+ double vb = a * n * bnDrift / bn;
56
+ double vab = n * interaDrift / abn;
57
+ double ve = benchDrift / en;
58
+
59
+ statistic[0] = va / ve;
60
+ statistic[1] = vb / ve;
61
+ statistic[2] = vab/ ve;
62
+ return statistic;
63
+ }
64
+ private double[][] calcMeanXij(double[][][] xij) {
65
+ DescriptiveStatistics stat = new DescriptiveStatistics();
66
+ double[][] meanXij = new double[a][b];
67
+
68
+ for(int i = 0; i < a; i++) {
69
+ for(int j = 0; j < b; j++) {
70
+ Arrays.stream(xij[i][j]).forEach(stat::addValue);
71
+ meanXij[i][j] = stat.getMean();
72
+ stat.clear();
73
+ }
74
+ }
75
+ return meanXij;
76
+ }
77
+ private double[] calcMeanAn(double[][] meanXij) {
78
+ double[] an = new double[a];
79
+ DescriptiveStatistics stat = new DescriptiveStatistics();
80
+
81
+ for(int i = 0; i < a; i++) {
82
+ double sumSa = 0.0;
83
+ for(int j = 0; j < b; j++) {
84
+ sumSa += meanXij[i][j];
85
+ }
86
+ an[i] = sumSa / b;
87
+ }
88
+ return an;
89
+ }
90
+ private double[] calcMeanBn(double[][] meanXij) {
91
+ double[] bn = new double[b];
92
+ double[] sumA = new double[b];
93
+
94
+ for(int i = 0; i < a; i++) {
95
+ for(int j = 0; j < b; j++) {
96
+ bn[j] += meanXij[i][j] / a;
97
+ }
98
+ }
99
+ return bn;
100
+ }
101
+ private double calcMeanABn(double[] meanAn) {
102
+ DescriptiveStatistics stat = new DescriptiveStatistics();
103
+
104
+ Arrays.stream(meanAn).forEach(stat::addValue);
105
+ return stat.getMean();
106
+ }
107
+ // 全変動
108
+ private double calcAllDrift(double[][][] xij, double meanABn) {
109
+ double sumDrift = 0.0;
110
+
111
+ for(int i = 0; i < a; i++) {
112
+ for(int j = 0; j < b; j++) {
113
+ for(int k = 0; k < xij[i][j].length; k++) {
114
+ double diffXijk = xij[i][j][k] - meanABn;
115
+ sumDrift += diffXijk * diffXijk;
116
+ }
117
+ }
118
+ }
119
+ return sumDrift;
120
+ }
121
+ // 水準Ai間変動
122
+ private double calcAnDrift(double[] meanAn, double meanABn) {
123
+ double sumDrift = 0.0;
124
+
125
+ for(int i =0; i < meanAn.length; i++) {
126
+ double diffXi = meanAn[i] - meanABn;
127
+
128
+ sumDrift += diffXi * diffXi;
129
+ }
130
+ return sumDrift;
131
+ }
132
+ // 水準Bj間変動
133
+ private double calcBnDrift(double[] meanBn, double meanABn) {
134
+ double sumDrift = 0.0;
135
+
136
+ for(int j = 0; j < meanBn.length; j++) {
137
+ double diffXj = meanBn[j] - meanABn;
138
+
139
+ sumDrift += diffXj * diffXj;
140
+ }
141
+ return sumDrift;
142
+ }
143
+ // 交互作用の変動
144
+ private double calcInteraDrift(double[][] meanXij, double[] meanAn, double[] meanBn, double meanABn) {
145
+ double sumDrift = 0.0;
146
+
147
+ for(int i = 0; i< a; i++) {
148
+ for(int j = 0; j < b; j++) {
149
+ double diffXj = meanXij[i][j] - meanAn[i] - meanBn[j] + meanABn;
150
+
151
+ sumDrift += diffXj * diffXj;
152
+ }
153
+ }
154
+ return sumDrift;
155
+ }
156
+ // 水準内変動
157
+ private double calcBenchDrift(double[][][] xij, double[][] meanXij) {
158
+ double sumDrift = 0.0;
159
+
160
+ for(int i = 0; i < a; i++) {
161
+ for(int j = 0; j < b; j++) {
162
+ for(int k = 0; k < xij[i][j].length; k++) {
163
+ double diffXj = xij[i][j][k] - meanXij[i][j];
164
+
165
+ sumDrift += diffXj * diffXj;
166
+ }
167
+ }
168
+ }
169
+ return sumDrift;
170
+ }
171
+ public boolean[] execute_test(double statistic[], double a) {
172
+ boolean[] ret = new boolean[3];
173
+
174
+ ret[0] = evaluation(new FDistribution(an, en), statistic[0], a);
175
+ ret[1] = evaluation(new FDistribution(bn, en), statistic[1], a);
176
+ ret[2] = evaluation(new FDistribution(abn, en), statistic[2], a);
177
+ return ret;
178
+ }
179
+ private boolean evaluation(FDistribution fDist, double statistic, double a) {
180
+ double r_val = fDist.inverseCumulativeProbability(1.0 - a);
181
+
182
+ return (statistic < r_val) ? false : true;
183
+ }
184
+ }
185
+ }
186
+
data/lib/num4anova.rb CHANGED
@@ -4,6 +4,7 @@ require 'jfreechart-1.5.4.jar'
4
4
  require 'commons-math3-3.6.1.jar'
5
5
 
6
6
  java_import 'OneWayLayout'
7
+ java_import 'TwoWayLayout'
7
8
  java_import 'java.util.HashMap'
8
9
  # 分散分析を行う
9
10
  # (Apache commoms math3使用)
@@ -144,11 +145,55 @@ module Num4AnovaLib
144
145
  # [28, 50, 22, 26, 29],
145
146
  # ]
146
147
  # oneWay = Num4AnovaLib::OneWayLayoutLib.new
147
- # oneWay.replicate_test("LDH", vals)
148
+ # oneWay.replicate_test(xi, 0.05)
148
149
  # => true
149
150
  def replicate_test(xi, a)
150
151
  return @oneWay.replicateTest(xi.to_java(Java::double[]), a)
151
152
  end
152
153
  end
154
+
155
+ # 二元配置の分散分析
156
+ class TwoWayLayoutLib
157
+ def initialize
158
+ @twoWay = TwoWayLayout.getInstance()
159
+ end
160
+ # 二元配置の分散分析
161
+ #
162
+ # @overload twoway_anova(xij, a)
163
+ # @param [array] xij データ(double[][][])
164
+ # @param [double] a 有意水準
165
+ # @return [Array] 検定結果(boolean[] true:棄却域内 false:棄却域外)
166
+ # @example
167
+ # xij = [
168
+ # [
169
+ # [13.2, 15.7, 11.9],
170
+ # [16.1, 15.7, 15.1],
171
+ # [9.1, 10.3, 8.2],
172
+ # ],
173
+ # [
174
+ # [22.8, 25.7, 18.5],
175
+ # [24.5, 21.2, 24.2],
176
+ # [11.9, 14.3, 13.7],
177
+ # ],
178
+ # [
179
+ # [21.8, 26.3, 32.1],
180
+ # [26.9, 31.3, 28.3],
181
+ # [15.1, 13.6, 16.2],
182
+ # ],
183
+ # [
184
+ # [25.7, 28.8, 29.5],
185
+ # [30.1, 33.8, 29.6],
186
+ # [15.2, 17.3, 14.8],
187
+ # ],
188
+ # ]
189
+ # twoWay = Num4AnovaLib::TwoWayLayoutLib.new
190
+ # twoWay.twoway_anova(xij, 0.05)
191
+ # =>
192
+ # [true, true, true]
193
+ def twoway_anova(xij, a)
194
+ ret = @twoWay.twowayAnova(xij.to_java(Java::double[][]), a)
195
+ return ret.to_a
196
+ end
197
+ end
153
198
  end
154
199
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: num4anova
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: java
6
6
  authors:
7
7
  - siranovel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-30 00:00:00.000000000 Z
11
+ date: 2024-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -63,6 +63,7 @@ files:
63
63
  - Rakefile
64
64
  - ext/num4anova/MultiComp.java
65
65
  - ext/num4anova/OneWayLayout.java
66
+ - ext/num4anova/TwoWayLayout.java
66
67
  - lib/commons-math3-3.6.1.jar
67
68
  - lib/dunnet.rb
68
69
  - lib/jcommon-1.0.23.jar