/* Simple Least Squares Applet 20-July-1997 This is the fourth in a series of simple statistics applets. Written in 1997 by Bryan Lewis Department of Mathematics and Computer Science Kent State University This software is free. Please feel free to use it and modify it. Needs the following objects: lsq.class the applet editor.class data editor object Matrix.class linear algebra routines ExampleData.class generates random examples specialFucntions.class F-distribution and others History: 11.August, 1997: Added quadratic, cubic models (requested) 21.December, 1997: Fixed string output of p-value 24.December, 1997: Incorporated data editor object 24.December, 1997: Added "nice" output monoString code 1.June, 1998: Added fourth degree polynomial fitting */ import java.awt.*; import java.lang.*; import java.applet.*; public class lsqFrame extends editor { Panel controlPanel = new Panel(); Button compute = new Button("Compute"); Button example = new Button("Example data"); Button plotData = new Button("Plot data"); public Choice model = new Choice(); Plot plot1 = new Plot(); String newln; double zero=0.00001; // precision for output display public lsqFrame(String title) { super(title); // Set line termination character newln = System.getProperty("line.separator"); // Set up the applet setBackground(Color.lightGray); plot1.resize(200,175); topPanel.add("East",plot1); plot1.repaint(); controlPanel.add(compute); controlPanel.add(example); controlPanel.add(plotData); model.addItem("Linear"); model.addItem("Quadratic"); model.addItem("Cubic"); model.addItem("Quartic"); controlPanel.add(model); add("South",controlPanel); resize(475,450); show(); } public boolean action(Event e, Object arg) { if (e.target == compute) { // calculate the result result.setText("Calculating..."); Matrix D = new Matrix(data.getText()); plot1.P = D; plot1.repaint(); result.setText(compute(D)); } else if (e.target == example) { // display some example data ExampleData ed = new ExampleData(); data.setText(ed.example); } else if (e.target == plotData) { // plot the data plot1.M = new Matrix("0;0"); plot1.P = new Matrix(data.getText()); plot1.repaint(); } return super.action(e, arg); } String compute(Matrix D){ // perform the least squares calculations int i, j, k; String result; // create a model matrix (normal equations) j = model.getSelectedIndex() + 2; Matrix A = new Matrix(D.rows, j, 1); for(k=1; k=0){result = result + " +";} result = result + " " + x.element[j][0] +"x^" + j; } result = result + "." + newln + "Residual 2-norm = " + monoString(ssr) + newln; if(x.rows == 2){ result = result + "Generalized correlation coefficient (r^2) = " + monoString(r2) + newln + newln + " Analysis of Variance" + newln + monoString("Source") + monoString("Sum squares") + monoString("Deg. Freedom") + monoString("Mean square")+monoString("F ratio")+monoString("P(X>F)")+ newln + monoString("Regression") + monoString(numeratorSum) + monoString(A.columns-1) + monoString(numeratorSum/(A.columns-1)) + monoString(F) + p + newln + monoString("Residual") + monoString(ssr) + monoString(b.rows-2) + monoString(ssr/(b.rows-2)) + newln ; } return result; } String monoString(String s) { // convert a generic string s to a fixed length one String sAdd = new String(s + " "); return sAdd.substring(0,14); } String monoString(double s) { // convert a generic double s to a "nice" fixed length string Double sD = new Double(s); String sAdd = new String(); if(s>zero){ sAdd = new String(sD.toString()); } else{ sAdd = "<0.00001"; } if(sAdd.length()>10){ sAdd = sAdd.substring(0,10); } sAdd = sAdd + " "; return sAdd.substring(0,14); } String monoString(int s) { // convert a generic integer s to a fixed length string Integer sD = new Integer(s); String sAdd = new String(sD.toString()); sAdd = sAdd + " "; return sAdd.substring(0,14); } }