public LinearModelWithSweep(ArrayList<ModelEffect> effects, double[] data) { int numberOfEffects = effects.size(); this.data = data; xtxmatrices = new DoubleMatrix2D[numberOfEffects][numberOfEffects]; xtymatrices = new DoubleMatrix2D[numberOfEffects][1]; for (int i = 0; i < numberOfEffects; i++) { ModelEffect me = effects.get(i); xtxmatrices[i][i] = me.getXTX(); xtymatrices[i][0] = DoubleFactory2D.dense.make(me.getXTy(data).toArray(), me.getNumberOfLevels()); for (int j = 0; j < i; j++) { xtxmatrices[i][j] = ModelEffect.getX1TX2(me, effects.get(j)); xtxmatrices[j][i] = xtxmatrices[i][j].viewDice(); } } sweep = new Sweep(); initialSweep(); }
public static DoubleMatrix2D[][] modelEffectsToxtx(ModelEffect[] mes) { int dim = mes.length; DoubleMatrix2D[][] xtx = new DoubleMatrix2D[dim][dim]; for (int i = 0; i < dim; i++) { xtx[i][i] = mes[i].getXTX(); for (int j = i + 1; j < dim; j++) { xtx[i][j] = ModelEffect.getX1TX2(mes[i], mes[j]); xtx[j][i] = xtx[i][j].viewDice(); } } return xtx; }
public void test() { ArrayList<String> A = new ArrayList<String>(); ArrayList<String> B = new ArrayList<String>(); ArrayList<String> C = new ArrayList<String>(); ArrayList<String> D = new ArrayList<String>(); ArrayList<Double> values = new ArrayList<Double>(); try { BufferedReader br = new BufferedReader(new FileReader("c:/temp/testdata.txt")); // br.readLine(); String line; while ((line = br.readLine()) != null) { String[] splitline = line.split("\t"); A.add(splitline[0]); B.add(splitline[1]); C.add(splitline[2]); D.add(splitline[3]); values.add(Double.parseDouble(splitline[4])); } br.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } data = new double[values.size()]; Iterator<Double> dit = values.iterator(); int count = 0; while (dit.hasNext()) { data[count++] = dit.next().doubleValue(); } double[] dblC = new double[C.size()]; Iterator<String> sit = C.iterator(); count = 0; while (sit.hasNext()) { dblC[count++] = Double.parseDouble(sit.next()); } double[] dblD = new double[D.size()]; sit = D.iterator(); count = 0; while (sit.hasNext()) { dblD[count++] = Double.parseDouble(sit.next()); } int[] meanLevel = new int[data.length]; ModelEffect memean = new ModelEffect(meanLevel); ModelEffect meA = new ModelEffect(ModelEffect.getIntegerLevels(A)); ModelEffect meB = new ModelEffect(ModelEffect.getIntegerLevels(B)); ModelEffect meC = new CovariateModelEffect(dblC); ModelEffect meD = new CovariateModelEffect(dblD); ModelEffect[] theEffects = new ModelEffect[] {memean, meA, meB, meC, meD}; xtxmatrices = modelEffectsToxtx(theEffects); xtymatrices = modelEffectsToxty(theEffects, data); initialSweep(); double cfm = effectSS.get(0); for (int i = 0; i < effectSS.size(); i++) { System.out.println(i + ", " + effectSS.get(i) + ", " + effectdf.get(i)); } System.out.println("model ss = " + (getModelSS() - cfm) + ", model df = " + (getModeldf() - 1)); System.out.println("error ss = " + getErrorSS() + ", error df = " + getErrordf()); double[] result = marginalEffectSSdf(1); System.out.println("marginal A ss = " + result[0] + ", df = " + result[1]); result = marginalEffectSSdf(2); System.out.println("marginal B ss = " + result[0] + ", df = " + result[1]); result = marginalEffectSSdf(3); System.out.println("marginal C ss = " + result[0] + ", df = " + result[1]); result = marginalEffectSSdf(4); System.out.println("marginal D ss = " + result[0] + ", df = " + result[1]); System.out.println("finished"); }