create($X,$Y,$title); } } /* Create the class */ function create($X,$Y,$title="") { /* Check if we got a valid set of data*/ (($this->N = count($X)) > 1) or die("Not enough data, number of values : ".$this->N."n"); /* initialize values */ $this->MINX = (float) min($X); $this->MAXX= (float) max($X); /* Compute X Median */ # Sort values in array X $XX = $X; sort ($XX); if ($this->N%2 == 0){ $this->MEDX = (($XX[($this->N)/2])+($XX[-1+($this->N/2)]))/2; }else{ $this->MEDX = $XX[floor($this->N/2)]; } $this->NY = count($Y); $this->MINY = (float) min($Y); $this->MAXY= (float) max($Y); /* Compute Y Median */ # Sort values in array Y $YY = $Y; sort ($YY); if ($this->NY%2 == 0){ $this->MEDY = (($YY[($this->NY)/2])+($YY[-1+($this->NY/2)]))/2; }else{ $this->MEDY = $YY[floor($this->NY/2)]; } $this->setTitle($title); /* stats */ for ($i = 0; $i < $this->N ; $i++) { $this->SUMX += (float) $X[$i]; $this->SUMX2 += (float) pow($X[$i],2); $this->SUMXY += (float) $X[$i]* (float)$Y[$i]; } $this->AVGX = (float) $this->SUMX/ (float) $this->N; $this->STDVX = (float) sqrt(($this->SUMX2 - $this->N*pow($this->AVGX,2))/(float)($this->N - 1)); for ($i = 0; $i < $this->NY ; $i++) { $this->SUMY += (float) $Y[$i]; $this->SUMY2 += (float) pow($Y[$i],2); } $this->AVGY = (float) $this->SUMY/ (float)$this->NY; $this->STDVY = (float) sqrt(($this->SUMY2 - $this->NY*pow($this->AVGY,2))/(float)($this->NY - 1)); if ($this->NY == $this->N){ $this->DEV = (float) (($this->SUMX2 * $this->N)- ($this->SUMX * $this->SUMX)); $this->m = (float)(($this->SUMXY * $this->N)- ($this->SUMX * $this->SUMY))/$this->DEV; $this->b = (float)(($this->SUMX2 * $this->SUMY)- ($this->SUMXY * $this->SUMX))/$this->DEV; $this->r = (float) ($this->SUMXY -($this->N * $this->AVGX * $this->AVGY))/(($this->N-1)*$this->STDVX * $this->STDVY); $this->t = (float) $this->r * sqrt(($this->N -2)/(1- pow($this->r,2))); } /* make the STATS array */ $this->STATS = array ( N=>$this->N, NY=>$this->NY, MINX=>$this->MINX, MAXX=>$this->MAXX, SUMX=>$this->SUMX, MEDX=>$this->MEDX, AVGX=>$this->AVGX, STDVX=>$this->STDVX, MINY=>$this->MINY, MAXY=>$this->MAXY, SUMY=>$this->SUMY, MEDY=>$this->MEDY, AVGY=>$this->AVGY, STDVY=>$this->STDVY, m=>$this->m, b=>$this->b, r=>$this->r, t=>$this->t ); } /* sets the Title */ function setTitle($title) { $this->TITLE=$title; } /* send back STATS array */ function getStats() { return $this->STATS; } /* simple printStats */ function printStats() { $s = "Statistics for : ".$this->TITLE."n"; $s .= "Number of data in X axis: $this->N n"; $s .= sprintf("Min = %-2.4ftMax = %-2.4ftAvg = %-2.4ftMED = %-2.4ftStDev = %-2.4f n",$this->MINX,$this->MAXX,$this->AVGX,$this->MEDX, $this->STDVX ); $s .= "nNumber of data in Y axis: $this->NY n"; $s .= sprintf("Min = %-2.4ftMax = %-2.4ftAvg = %-2.4ftMED = %-2.4ftStDev = %-2.4f n",$this->MINY,$this->MAXY,$this->AVGY,$this->MEDY, $this->STDVY ); if($this->NY ==$this->N){ $s .= "nLinear regression line: n"; if ($this->b >=0){ $s .= "Y = $this->m X + $this->b with correlation (r)= $this->rn "t"test (df=$this->N-2) = $this->t"; }else{ $s .= "Y = $this->m X $this->b with correlation (r)= $this->rn "t"test (df=$this->N-2) = $this->t"; } } echo $s; } } /* end of Stat1 class */ ?>