/* InteractiveCorr applet allows the user to select an ordered pair by clicking on a carteasean co-ord grid computes R square and least squares regression line Version 1.0 - 25 Aug 1999 Chris Thiel */ import java.lang.*; import java.awt.*; import java.applet.Applet; import java.awt.Graphics; import java.awt.Color; import java.awt.Event; import java.util.*; public class icorr extends Applet { int MAX=50; int n=0; String message=null; int x[] = new int[MAX]; int y[] = new int[MAX]; float xMean, yMean; float B0, B1, R_square,Var_x,Var_y; double R_Coef; int fudge =7; int i=0; /* Compute is the routine called to compute the Statistical Data */ public void compute(){ // Now calculate regression coeffecients float Sx = 0, Sy =0; float Sxx = 0, Sxy = 0, Syy = 0; for(int i=0; i< n; i++) { Sx += x[i]; Sy += y[i]; } xMean = Sx / n; yMean = Sy / n; for(int i=0; i< n; i++) { Sxx += (x[i]-xMean) * (x[i]-xMean); Sxy += (x[i]-xMean) * (y[i]-yMean); Syy += (y[i]-yMean) * (y[i]-yMean); } B1=Sxy/Sxx; B0 = yMean - B1 * xMean; R_square=(Sxy*Sxy)/(Sxx*Syy); R_Coef = (Sxy)/ Math.sqrt(Sxx*Syy); } public void clearAll() { n = 0; compute(); repaint(); } public void init() { n =0; repaint(); } public void paint( Graphics g ) { g.setColor(Color.black); for (i=0; i< n; i++){ g.fillOval(x[i]-2,size().height-y[i]-2, 4,4); } g.setColor(Color.magenta); g.drawLine(0, (int)(size().height-B0), size().width, (int)(size().height-B1*size().width-B0)); g.setColor(Color.black); g.drawString( "Number of Pairs:"+n, 25, 15 ); if (n == MAX-1) { g.setColor(Color.red); g.drawString( "WARNING: Maximum number of Pairs found", 5,size().height-48); g.drawString( "subsequent pairs ignored", 35,size().height-36); } g.drawString("Regression Line Equation:",25,30); g.setColor(Color.red); g.drawString("y = "+B1+" * x + "+B0,25,45); g.drawString("^",25,43); g.setColor(Color.black); g.drawString("r = "+R_square,25,60); g.drawString("2",30,57); g.setColor(Color.blue); g.drawString("r = "+R_Coef,25,75); g.setColor(Color.black); g.drawString("_",25,81); g.drawString("_",25,96); g.drawString("x = "+xMean,25, 90); g.drawString("y = "+yMean,25, 105); if (message != null) { g.drawString("Error in data: "+message, 10, size().height-24); g.drawString("Two numbers per line separated by space", 10, size().height-12); } }// of paint /* * Mouse methods */ public boolean mouseDown(java.awt.Event evt, int x0, int y0) { x[n]=x0; y[n]=size().height-y0; n++; if (n == MAX) { n = MAX-1; } compute(); repaint(); return true; } public boolean mouseMove(java.awt.Event evt, int x, int y) { y = size().height-y; getAppletContext().showStatus("Location of mouse: (" + x + ", " + y + ")"); return true; } public void mouseEnter() { repaint(); } public void mouseExit() { repaint(); } }//of applet