/* scatterRegress applet takes pairs of numbers from a TEXT file, and computes R square and Linear regression Line. Other features include an active Mouse pointer to help identify the coordinates of a data point, shows/hides Regression line & r^2, and custom ranges of the x and y axes. Version 1.0 - 27 Oct 1999 Chris Thiel Next Version: Label x and Y axis */ 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.*; import java.io.*; import java.io.StreamTokenizer; import java.io.InputStream; import java.io.IOException; import java.net.URL; public class scatterRegressFile extends Applet{ int MAX=200; int MARGIN=30; int n=0; String dataFileName; String userInput=null; String s=null; String message=null; float x[] = new float[MAX]; float y[] = new float[MAX]; float xMean, yMean; float B0, B1, R_square,Var_x,Var_y; float xMin, yMin, xMax,yMax; int xM,yM,i=0; double w,h, xFactor, yFactor; boolean autoRange=true; boolean showLine=true; boolean showFormula=true; boolean mousePointer=true; String title; Font titleFont; FontMetrics titleFontMetrics; int titleHeight = 15; InputStream is; /* 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; w=(double)(size().width)-2*MARGIN; h=(double)(size().height)-2*MARGIN; // first see if axes' range will be based on data or not if (autoRange) { xMin = (999999); yMin = (999999); xMax = (-999999); yMax = (-999999); for (int i=0; ixMax) xMax=x[i]; if (y[i]yMax) yMax=y[i]; } } for(int i=0; i< n; i++) { Sx += x[i]; Sy += y[i]; } xFactor= (w-10.0)/(xMax-xMin); yFactor= (h-10.0)/(yMax-yMin); 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); } public void init() { i =0; String rs; dataFileName = getParameter("data"); if (dataFileName == null) dataFileName = "sampledata.txt"; rs = getParameter("n"); if (rs !=null) n = Integer.parseInt(rs); try { BufferedReader dis = new BufferedReader( new InputStreamReader(new URL(getDocumentBase(), dataFileName).openStream())); int i=0; try { while (i