2016年1月26日火曜日

rJava : プログラミング(11) Steel-Dwass法(non-parametoric, 等分散性制限無し)

まずBatlettで等分散性を検定、今回は帰無仮説が棄却され等分散性が成立しないので、Steel-Dwass法(non-parametoric, 等分散性制限無し)を利用した。

Rでやってみると
$ R
> source("http://aoki2.si.gunma-u.ac.jp/R/src/Steel-Dwass.R")
> data <- br="" c="">+ 0.1875, 1.3333333333333, 1.5384615384615, 1.9763313609467, 0.76748582230624, 1.6859504132231, 1.1142659279778, 1.1358024691358, 0.80555555555556, 3.2448979591837, 3.1675338189386, 3.1466836734694, 2.8512110726644, 2.0, 2.0826446280992,
+ 0.484375, 0.13888888888889, 2.2366863905325, 1.905325443787, 0.69187145557656, 1.6528925619835, 0.92243767313019, 1.1111111111111, 1.2222222222222, 3.1683673469388, 4.1727367325702, 5.0956632653061, 2.4636678200692, 1.7777777777778, 2.7272727272727,
+ 0.234375, 0.13888888888889, 1.6213017751479, 0.071005917159763, 0.5179584120983, 1.6461776859504, 0.82271468144044, 1.1358024691358, 1.3333333333333, 3.6734693877551, 4.1727367325702, 5.6772959183673, 3.4048442906574, 2.9876543209877, 2.9752066115702,
+ 0.0, 0.0, 1.5621301775148, 10.236686390533, 0.80151228733459, 1.5599173553719, 1.1004155124654, 1.7777777777778, 2.4722222222222, 4.9183673469388, 5.6357960457856, 6.6058673469388, 2.8512110726644, 3.1358024691358, 1.8842975206612)
> group <- 15="" br="" c="" rep="">> Steel.Dwass(data, group)
             t         p
1:2 0.02073903 0.9999968
1:3 0.02074365 0.9999968
1:4 0.70528407 0.8950434
2:3 0.18669284 0.9976931
2:4 0.66379678 0.9106851
3:4 0.64298158 0.9180231

となった。有意性は確認できない。rjavaでやると
$ vi JRITest13.java
$ javac JRITest13.java
$ java -Djava.library.path=/usr/lib64/R/library/rJava/jri/ JRITest13

[VECTOR ([REAL* (16.294575129341144)], [REAL* (3.0)], [REAL* (9.866990036363195E-4)], [STRING "score by group"], [STRING "Bartlett test of homogeneity of variances"])]
[REAL* (0.020739033894608506, 0.020743649235467825, 0.7052840740059061, 0.18669284311921042, 0.6637967755349704, 0.6429815765773006, 0.9999967966772032, 0.9999967945383197, 0.8950433896624197, 0.9976931476869926, 0.9106851171476555, 0.9180230907044584)]
となる。


import org.rosuda.JRI.REXP;
import org.rosuda.JRI.Rengine;
import java.sql.*;
import java.util.Arrays;

public class JRITest13 {

        public static void main(String[] args)
        {
                MySQL mysql = new MySQL();
                Rengine re = new Rengine( new String[]{"--no-save"}, false, null );
                ResultSet rs = mysql.selectOperators();

                int N = 15;
                double awareness [] = new double[N];
                double decision [] = new double[N];
                double communication [] = new double[N];
                double leadership [] = new double[N];
                int index = 0;
                try {
                        while(rs.next()){
                                awareness[index] = rs.getDouble("s_awareness");
                                decision[index] = rs.getDouble("s_decision");
                                communication[index] = rs.getDouble("s_communication");
                                leadership[index] = rs.getDouble("s_leadership");
                                index ++;
                        }
                } catch (SQLException e) {
                        e.printStackTrace();
                }
                //System.out.println(Arrays.toString(result));
                double score[] = new double[awareness.length*4];
                int index2 = 0;
                for(int i = 0 ; i< awareness.length; i++) score[index2++] = awareness[i];
                for(int i = 0 ; i< decision.length; i++) score[index2++] = decision[i];
                for(int i = 0 ; i< communication.length; i++) score[index2++] = communication[i];
                for(int i = 0 ; i< leadership.length; i++) score[index2++] = leadership[i];

                System.out.println(Arrays.toString(score));

                StringBuffer buf = new StringBuffer();
                buf.append("score<-c(");
                for(int i = 0; i < score.length; i++){
                        if(i == score.length -1)
                                buf.append(score[i]+")");
                        else
                                buf.append(score[i]+",");
                }
                String c_value = buf.toString();
                re.eval(c_value);

                REXP x = re.eval("summary(score)");
                System.out.println(x);
                double [] ary = x.asDoubleArray();
                for (int i = 0; i < ary.length; i++) {
                        System.out.println(ary[i]);
                }

                //Bartlett Test
                re.eval("group=factor(rep(c('awareness', 'decision', 'communication', 'leadership'), c(15, 15, 15, 15)))");
                REXP x1 = re.eval("bartlett.test(score ~ group)");
                System.out.println(x1);

                //Steel.Dwass Test
                re.eval("source('http://aoki2.si.gunma-u.ac.jp/R/src/Steel-Dwass.R')");
                re.eval("group <- rep(1:4, c(15, 15, 15, 15))");
                REXP x2 = re.eval("Steel.Dwass(score,group)");
                System.out.println(x2);

                re.end();
        }
}


参考
http://www.shiga-med.ac.jp/~koyama/stat/com-ph.html
http://aoki2.si.gunma-u.ac.jp/R/Steel-Dwass.html

0 件のコメント:

コメントを投稿