function boxplot (args) ********************************* * * *!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!* * DEFAULT OPTIONS * *!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!* * * ********************************* * This is a list of default options * Note: Most of these options can be changed Individually when calling this script * [] denote argument precusors and values to use when calling this script. Variables without [] cannot must be set in the default section below. * * Boxplot reads all data arrays with the precursor -d. e.g., 'boxplot -d 'array * boxsize=0.1 ;*[-boxsize / -box] controls the size of the box plot data points. parea='1.5 10 0.75 7.5' ;*[-parea / -pgarea] controls size of boxplot: left-boundary, right-boundary, lower-boundary, upper-boundary ax_vert=1 ;*[-ax / -axis] Determines whether or not the data is plotted against the y-axis or the x-axis ax_col=1 ;*[-axcol] Determines the color of the axes linecol=1 ;*[-lcol / -linecol / -lc] Determines the color of the box plot data symcol=2 ;*[-symcol / -symcolor / -sc] Determines the color of the outlier symbols symsize=0.1 ;*[-symsize / -ssize] Determines the size of the outlier symbols symtype=1 ;*[-symtype / -stype] Determines the symbol of the outlier symbols cap=0 ;*[-cap] Determines whether or not to include a capping line at the ends of the 10th and 90th percentile range range='' ;*[-range] Range of data on the y-axis, default is set to 15% of the max and min data points ticks=10 ;*[-ticks] Number of tick marks on the data axis yticklen=0.02 ;*Length of y-axis tick marks (in percentage of total plot) xticklen=0.02 ;*Length of x-axis tick marks (in percentage of total plot) xlab=1 ;*[-ylab] turns on/off/custom x-tick labels ylab=1 ;*[-xlab] turns on/off/custom x-tick labels *********************************************** * *This Section Controls Custom labels and Titles *These variables MUST BE SET HERE! * *********************************************** * Each tick is separated by a space * yticklabel='' ;*y-tick labels, defaults to numbers if number of ticks > number of tick labels xticklabel='NY Oklahoma' ;*z-tick labels, defaults to numbers if number of ticks > number of tick labels title='' ;*Prompts user to enter title if -title is included in the script arguments. xtitle='' ;*Prompts user to enter xtitle if -xtitle is included in the script arguments. ytitle='' ;*Prompts user to enter ytitle if -ytitle is included in the script arguments. * *********************************************** *********************************************** enclose=0 ;*[-enclose] *Encloses a box around the axes-otherwise just plots bottom and left axes include_title=0 ;*[-title] Choses to include title: turned on if "-title" is included in the script arguments include_xlab=0 ;*[-xtitle] Choses to include xtitle: turned on if "-xtitle" is included in the script arguments include_ylab=0 ;*[-ytitle] Choses to include ytitle: turned on if "-ytitle" is included in the script arguments tsize=0.2 ;*[-tsize / -title_size] Title Fontsize tcol=1 ;*[-tcol / -titlecol / -tc] Title color **Additional call options: * -default : Prints out current default options to console * -help : Brings up Brief help page on how to use this script. ********************************* * * *!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!* * END DEFAULT OPTIONS * *!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!* * * ********************************* *---------------------------------------* *******DO NOT EDIT BELOW THIS LINE******* *---------------------------------------* *---------------------------------------* *******DO NOT EDIT BELOW THIS LINE******* *---------------------------------------* *---------------------------------------* *******DO NOT EDIT BELOW THIS LINE******* *---------------------------------------* *---------------------------------------* *******DO NOT EDIT BELOW THIS LINE******* *---------------------------------------* *---------------------------------------* *******DO NOT EDIT BELOW THIS LINE******* *---------------------------------------* *---------------------------------------* *******DO NOT EDIT BELOW THIS LINE******* *---------------------------------------* *---------------------------------------* *******DO NOT EDIT BELOW THIS LINE******* *---------------------------------------* ****SET ARRYS********* arrs=0 xticks=arrs check=1 a=1 while(check=1) line=subwrd(args,a) if(line='');break;endif if(line='-d') arrs=arrs+1 darr.arrs='' c=a+1 ck=1 while(ck=1) num=subwrd(args,c) chknum=valnum(num) if(chknum=0);a=c-2;ck=0;else;darr.arrs=darr.arrs' 'num;endif c=c+1 endwhile endif if(line='-range') r1=subwrd(args,a+1);r2=subwrd(args,a+2) r1=valnum(r2) r2=valnum(r2) if(r1=0 | r2=0) say "Your inputs for range are not numbers, the two values following -range, must be numbers corresponding to the max/min values of your axis" say "Resetting range to default." range='' else range=subwrd(args,a+1)' 'subwrd(args,a+2) endif endif if(line='-ax' | line='-axis') if(subwrd(args,a+1) != 'v' & subwrd(args,a+1) != 'vertical' & subwrd(args,a+1) != 'h' & subwrd(args,a+1) != 'horizontal') say 'You have not chosen an acceptable option for axis alignment, the options are: vertical(v) -default or horizontal(h)' ax_vert=1 else if(subwrd(args,a+1)='h' | subwrd(args,a+1)='horizontal') ax_vert=0 else ax_vert=1 endif endif endif if(line='-enclose') enclose=1 endif if(line='-cap') cap=1 endif if(line='-ticks') ticks=subwrd(args,a+1) if(valnum(ticks)!=1) say 'You have not chosen a valid option for the variable "ticks". You must choose an integer number. Resetting to default.' ticks=10 endif endif say "tan 1" if(line='-axcol') ax_col=subwrd(args,a+1) if(valnum(ax_col)!=1) say 'You have not chosen a valid option for the variable "ax_col". You must choose an integer number. Resetting to default." ax_col=1 endif endif if(line='-symcol' | line='-sc' | line='-scol') symcol=subwrd(args,a+1) if(valnum(symcol)!=1) say 'You have not chosen a valid option for the variable "symcol". You must choose an integer number. Resetting to default.' symcol=1 endif endif if(line='-linecol' | line='-lc' | line='-lcol') linecol=subwrd(args,a+1) if(valnum(linecol)!=1) say 'You have not chosen a valid option for the variable "linecol". You must choose an integer number. Resetting to default.' linecol=1 endif endif if(line='-stype' | line='-symtype') symtype=subwrd(args,a+1) if(valnum(symtype)!=1) say 'You have not chosen a valid option for the variable "symtype". You must choose an integer number between (0-11). Resetting to default.' symtype=1 endif endif if(line='-ssize' | line='-symsize') symsize=subwrd(args,a+1) if(valnum(symsize)=0) say 'You have not chosen a valid option for the variable "symsize".You must chose a number.' symsize=0.1 endif endif if(line='-tcol' | line='-tc' | line='-title_color') tcol=subwrd(args,a+1) if(valnum(tcol)!=1) say 'You have not chosen a valid option for the variable "tcol".You must chose an integer number.' tcol=1 endif endif if(line='-tsize' | line='-title_size') tsize=subwrd(args,a+1) if(valnum(tsize)=0) say 'You have not chosen a valid option for the variable "tcol".You must chose a number.' tsize=0.2 endif endif if(line='-boxsize' | line='-box') boxsize=subwrd(args,a+1) if(valnum(boxsize)=0) say 'You have not chosen a valid option for the variable "boxsize".You must chose a number.' boxsize=0.1 endif endif if(line='-parea' | line='-pgarea') p1=subwrd(args,a+1) p2=subwrd(args,a+2) p3=subwrd(args,a+3) p4=subwrd(args,a+4) if(valnum(p1)=0 | valnum(p2)=0 | valnum(p3)=0 | valnum(p4)=0) say 'You have not chosen a valid option for the variable "parea". The -parea option must be followed by 4 numbers corresponding to the lower left, lower right, upper left, and upper right corners of your plot.' parea='1.5 10 0.75 7.5' else parea=p1' 'p2' 'p3' 'p4 endif endif if(line='-xtitle') include_xlab=1 endif if(line='-ytitle') include_ylab=1 endif if(line='-title') include_title=1 endif if(line='-xlab') if(subwrd(args,a+1) != 'on' & subwrd(args,a+1) != 'On' & subwrd(args,a+1) != 'off' & subwrd(args,a+1) != 'Off' & subwrd(args,a+1) != 'custom' ) say 'You have not chosen a valid option for the variable "xlab". You must choose "on","off" or "custom". Default is on. Custom xlabels must be set in the file boxplot.gs' xlab=1 endif if(subwrd(args,a+1)='off' | subwrd(args,a+1)='Off') xlab=0 endif if(subwrd(args,a+1)='custom') xlab=2 endif endif if(line='-ylab') if(subwrd(args,a+1) != 'on' & subwrd(args,a+1) != 'On' & subwrd(args,a+1) != 'off' & subwrd(args,a+1) != 'Off' & subwrd(args,a+1) != 'custom' ) say 'You have not chosen a valid option for the variable "ylab". You must choose "on","off" or "custom". Default is on. Custom ylabels must be set in the file boxplot.gs' ylab=1 endif if(subwrd(args,a+1)='off' | subwrd(args,a+1)='Off') ylab=0 endif if(subwrd(args,a+1)='custom') ylab=2 endif endif if(line='-default') say "This option is not available in the current version. Please refer to the section at the top of boxplot.gs to see the default variables." endif if(line='-help') help() endif a=a+1 endwhile ********************** *SET DEFAULTS ********************** if(ax_vert=1) xrange=arrs ygap=(xrange-arrs*boxsize)/(arrs+1) box=2*boxsize*xrange/arrs else yrange=arrs xgap=(yrange-arrs*boxsize)/(arrs+1) box=2*boxsize*yrange/arrs endif a=1 max_max=-9999 min_min=9999 'set string 1 c 0.1 0' ******GATHER DATA AND SET PLOT SCALE******* ******************************************* ****** ******IN THIS CASE: SCALE IS PLOTTED TO FIT THE MAX DATA POINT IN ALL ARRAYS ******SCALE FITS MAX VALUE + 15% ****** ******************************************* while(a <=arrs) 'set line 1 1' data_array='' ck=1;c=1 while(ck=1) num=subwrd(darr.a,c) chknum=valnum(num) if(chknum=0);ck=0;else;data_array=data_array' 'num;endif c=c+1 endwhile size.a=size(data_array) sorted.a=sort(data_array) med.a=percentile(data_array,0.5) tnth.a=percentile(data_array,0.1) q1.a=percentile(data_array,0.25) q2.a=percentile(data_array,0.75) nith.a=percentile(data_array,0.9) min.a=min(data_array) max.a=max(data_array) if(max.a > max_max);max_max=max.a;endif if(min.a < min_min);min_min=min.a;endif a=a+1 endwhile if(ax_vert=1) if(range='') ymax=max_max+0.15*(max_max-min_min) ymin=min_min-0.15*(max_max-min_min) else ymax=subwrd(range,2) ymin=subwrd(range,1) endif yrange=ymax-ymin else if(range='') xmax=max_max+0.15*(max_max-min_min) xmin=min_min-0.15*(max_max-min_min) else xmax=subwrd(range,2) xmin=subwrd(range,1) endif xrange=xmax-xmin endif *********************************************** * ******START DATA PROCESSING AND PLOTTING!****** * *********************************************** a=1 while(a <=arrs) if(ax_vert=1) yq1=q1.a-ymin yq2=q2.a-ymin ytn=tnth.a-ymin ynt=nith.a-ymin ymed=med.a-ymin xmin=ygap*a+box*(a-2) xmax=ygap*a+box*(a-1) xmid=(xmax+xmin)/2 else xq1=q1.a-xmin xq2=q2.a-xmin xtn=tnth.a-xmin xnt=nith.a-xmin xmed=med.a-xmin ymin=xgap*a+box*(a-2) ymax=xgap*a+box*(a-1) ymid=(ymax+ymin)/2 endif *************************** ******SCALE TO PAGE******** *************************** dypg=(subwrd(parea,4)-subwrd(parea,3))/yrange dxpg=(subwrd(parea,2)-subwrd(parea,1))/xrange *************************** ********Draw Axis********** *************************** 'set line 'ax_col 'draw line 'subwrd(parea,1)' 'subwrd(parea,3)' 'subwrd(parea,1)' 'subwrd(parea,4) 'draw line 'subwrd(parea,1)' 'subwrd(parea,3)' 'subwrd(parea,2)' 'subwrd(parea,3) if(enclose=1) 'draw line 'subwrd(parea,2)' 'subwrd(parea,3)' 'subwrd(parea,2)' 'subwrd(parea,4) 'draw line 'subwrd(parea,1)' 'subwrd(parea,4)' 'subwrd(parea,2)' 'subwrd(parea,4) endif *************************** ********Box Plot ********** *************************** if(ax_vert=1) 'set line 'linecol 'draw rec 'xmin*dxpg+subwrd(parea,1)' 'yq1*dypg+subwrd(parea,3)' 'xmax*dxpg+subwrd(parea,1)' 'yq2*dypg+subwrd(parea,3) 'draw line 'xmid*dxpg+subwrd(parea,1)' 'yq2*dypg+subwrd(parea,3)' 'xmid*dxpg+subwrd(parea,1)' 'ynt*dypg+subwrd(parea,3) 'draw line 'xmid*dxpg+subwrd(parea,1)' 'ytn*dypg+subwrd(parea,3)' 'xmid*dxpg+subwrd(parea,1)' 'yq1*dypg+subwrd(parea,3) 'draw line 'xmin*dxpg+subwrd(parea,1)' 'ymed*dypg+subwrd(parea,3)' 'xmax*dxpg+subwrd(parea,1)' 'ymed*dypg+subwrd(parea,3) ***DRAW Line at ends of 10th and 90th percentile if(cap=1) 'draw line 'xmin*dxpg+0.25*(xmax-xmin)*dxpg+subwrd(parea,1)' 'ytn*dypg+subwrd(parea,3)' 'xmax*dxpg-0.25*(xmax-xmin)*dxpg+subwrd(parea,1)' 'ytn*dypg+subwrd(parea,3) 'draw line 'xmin*dxpg+0.25*(xmax-xmin)*dxpg+subwrd(parea,1)' 'ynt*dypg+subwrd(parea,3)' 'xmax*dxpg-0.25*(xmax-xmin)*dxpg+subwrd(parea,1)' 'ynt*dypg+subwrd(parea,3) endif c=1 while(c<=size.a) if(subwrd(sorted.a,c) subwrd(parea,3) & y*dypg+subwrd(parea,3)nith.a) y=subwrd(sorted.a,c)-ymin 'set line 'symcol if(y*dypg+subwrd(parea,3)>subwrd(parea,3) & y*dypg+subwrd(parea,3)subwrd(parea,1) & x*dxpg+subwrd(parea,1)nith.a) x=subwrd(sorted.a,c)-xmin 'set line 'symcol if(x*dxpg+subwrd(parea,1)>subwrd(parea,1) & x*dxpg+subwrd(parea,1)1) ytickval=ytickval+yrange/ticks ytickpg=ytickpg+(subwrd(parea,4)-subwrd(parea,3))/ticks endif if(subwrd(yticklabel,t) !='' & ylab=2) yticklab=subwrd(yticklabel,t) else yticklab=substr(ytickval,1,5) endif 'set line 'ax_col 'draw line 'subwrd(parea,1)' 'ytickpg' 'subwrd(parea,1)+xpgtick' 'ytickpg if(enclose=1);'draw line 'subwrd(parea,2)' 'ytickpg' 'subwrd(parea,2)-xpgtick' 'ytickpg;endif if(ylab>0) 'set string 'tcol' br 0.1 45' 'set strsiz 0.09' 'draw string 'subwrd(parea,1)-0.02*(subwrd(parea,2)-subwrd(parea,1))' 'ytickpg' 'yticklab 'set string 1 l 0.1 0' endif t=t+1 endwhile t=1 while(t<=arrs) xtickval=t xmin=ygap*t+box*(t-2) xmax=ygap*t+box*(t-1) xmid=(xmax-xmin)/2+xmin if(subwrd(xticklabel,t) !='' & xlab=2) xticklab=subwrd(xticklabel,t) else xticklab=substr(xtickval,1,5) endif 'set line 'ax_col 'draw line 'xmid*dxpg+subwrd(parea,1)' 'subwrd(parea,3)' 'xmid*dxpg+subwrd(parea,1)' 'subwrd(parea,3)+ypgtick if(xlab>0) 'set string 1 c 0.1' 'set strsiz 0.1' 'draw string 'xmid*dxpg+subwrd(parea,1)' 'subwrd(parea,3)-0.03*(subwrd(parea,4)-subwrd(parea,3))' 'xticklab endif t=t+1 endwhile else ;* HORIZONTAL AXIS! ytlen=yticklen*xrange xtickval=xmin xtickpg=subwrd(parea,1) xpgtick=yticklen*(subwrd(parea,2)-subwrd(parea,1)) ypgtick=xticklen*(subwrd(parea,4)-subwrd(parea,3)) t=1 while(t<=ticks+1) if(t>1) xtickval=xtickval+xrange/ticks xtickpg=xtickpg+(subwrd(parea,2)-subwrd(parea,1))/ticks endif if(subwrd(xticklabel,t) !='' & xlab=2) xticklab=subwrd(xticklabel,t) else xticklab=substr(xtickval,1,5) endif 'set line 'ax_col 'draw line 'xtickpg' 'subwrd(parea,3)' 'xtickpg' 'subwrd(parea,3)+ypgtick if(enclose=1);'draw line 'xtickpg' 'subwrd(parea,4)' 'xtickpg' 'subwrd(parea,4)-ypgtick;endif if(xlab>0) 'set string 1 c 0.1' 'set strsiz 0.1' 'draw string 'xtickpg' 'subwrd(parea,3)-0.03*(subwrd(parea,4)-subwrd(parea,3))' 'xticklab 'set string 1 l 0.1 0' endif t=t+1 endwhile t=1 while(t<=arrs) ytickval=t ymin=xgap*t+box*(t-2) ymax=xgap*t+box*(t-1) ymid=(ymax-ymin)/2+ymin if(subwrd(yticklabel,t) !='' & ylab=2) yticklab=subwrd(yticklabel,t) else yticklab=substr(ytickval,1,5) endif 'set line 'ax_col 'draw line 'subwrd(parea,1)' 'ymid*dypg+subwrd(parea,3)' 'subwrd(parea,1)+xpgtick' 'ymid*dypg+subwrd(parea,3) if(ylab>0) 'set string 'tcol' br 0.1 45' 'set strsiz 0.1' 'draw string 'subwrd(parea,1)-0.05*(subwrd(parea,2)-subwrd(parea,1))' 'ymid*dypg+subwrd(parea,3)' 'yticklab 'set string 1 l 0.1 0' endif t=t+1 endwhile endif if(include_title=1) center=(subwrd(parea,2)-subwrd(parea,1))/2+subwrd(parea,1) ypos=subwrd(parea,4)+tsize*2.25 'set strsiz 'tsize 'set string 'tcol' c 6' if(title!='') 'draw string 'center' 'ypos' 'title else say 'Please enter a title for this plot' pull title if(title='');title='You are right, titles are for hipsters';endif say 'Thank You, Drawing title!' 'draw string 'center' 'ypos' 'title endif endif if(include_xlab=1) center=(subwrd(parea,2)-subwrd(parea,1))/2+subwrd(parea,1) ypos=subwrd(parea,3)-tsize*2.5 'set strsiz 'tsize*0.75 'set string 'tcol' c 0.14' if(xtitle!='') 'draw string 'center' 'ypos' 'xtitle else say 'Please enter an x title for this plot' pull xtitle if(xtitle='');xtitle='Sure, who cares what is on the x-axis';endif say 'Thank You, Drawing x-title!' 'draw string 'center' 'ypos' 'xtitle endif endif if(include_ylab=1) center=(subwrd(parea,4)-subwrd(parea,3))/2+subwrd(parea,3) ypos=subwrd(parea,1)-tsize*5 'set strsiz 'tsize*0.75 'set string 'tcol' c 0.14 90' if(ytitle!='') 'draw string 'ypos' 'center' 'ytitle else say 'Please enter a y title for this plot' pull ytitle if(ytitle='');ytitle='Ugh, You are a jerk.';endif say 'Thank You, Drawing y-title!' 'draw string 'ypos' 'center' 'ytitle endif endif *************************************************** * *************************************************** * *************************************************** function help() say '--------------------------------------------------------' say '| |' say '| BOX PLOT v.1.0 |' say '| |' say '--------------------------------------------------------' say ' WELCOME TO BOX PLOT HELP PAGE ' say '' say 'The Scatter Plotter v1.0 was developed in July 2013 to ' say 'perform basic 2D plotting, including linear regression ' say 'in GrADS' say '' say 'The script is called in GrADS as such: ' say ' "boxplot -d array1 -d array2 ...-d arrayn" ' say '' say 'The number of arrays included in the plot is variable, ' say 'so long as each array is preceeded by "-d" ' say 'The arrays take this form: array="1 2 3 4 5" ' say '' say 'The box plots present data with a box surrounding the ' say 'IQR, with the quartiles determined by the equation ' say 'Q=p*(N-1)+1 [Same as MS-Excel ' say 'The line in in the plotted box is the median ' say 'Lines extend from the box to the 10th and 90th percentile' say '' say 'Symbols represent data points outside of this range. ' say 'Below is a full list of options to control the display ' say 'of the box plot. ' say '' say 'List of Options: ' say '--------------------- AXIS OPTIONS ---------------------' say ' -boxsize/-box :Set Size of the Box Plots ' say ' -range :Sets the data range for the data axis ' say ' -ticks :Number of tick marks on the data axis ' say ' -xlab :x-tick labels: on, off, custom* ' say ' -ylab :y-tick labels: on, off, custom* ' say ' -ax/-axis :determines data axis: horizontal/vertical' say ' -axcol :determines color of axes ' say '--------------------------------------------------------' say '' say '--------------------- PLOT OPTIONS ---------------------' say ' -symtype/-stype:Style of outlier Symbol (1-12)** ' say ' -symcol/-scol :Color of outlier Symbols ' say ' -symsiz/-ssize :Symbol Size of outlier ' say ' -parea :min/max x/y-page coordinates ' say ' -lcol/linecol :color of Line ' say ' -enclose :Complete box around plot ' say ' -cap :Caps off end of 10/90th percentiles ' say '--------------------------------------------------------' say '' say '--------------------- TITLE OPTIONS --------------------' say ' -title :Turn Title On **** ' say ' -xtitle :Turn x-title On **** ' say ' -ytitle :Turn y-title On **** ' say ' -tsize :Font size for Title ' say ' -tcol :Color of Title ' say '--------------------------------------------------------' say '' say '' say '-help :Brings up Help Page ' say '-default :Shows current default settings ' say '' say ' Press any key to see the remainder of the help page....' pull dummy say '* Custom array labels (e.g., dates) must be saved in the' say 'variables: xticklabel/yticklabel in the default options ' say 'at the top of this script. Be sure to match the array ' say 'values to the number of ticks. ' say 'e.g., xticklabel="lab1 lab2 lab3", xticks=3 ' say '' say '** Symbol Options' say ' 0 - none' say ' 1 - plus sign' say ' 2 - open circle' say ' 3 - closed circle' say ' 4 - open square' say ' 5 - closed square' say ' 6 - multiplication sign' say ' 7 - open diamond' say ' 8 - open triangle' say ' 9 - closed triangle' say '10 - open circle with vertical bar' say '11 - closed circle with vertical bar' say '12 - closed diamond (version 2.1+)' say '' say '*** Line style options' say '0 = no line (not applicable for regression line)' say '1 - solid' say '2 - long dash' say '3 - short dash' say '4 - long dash, short dash' say '5 - dotted' say '6 - dot dash' say '7 - dot dot dash' say '' say '**** Titles: If the titles are turned on and there is ' say 'no title set in the defaults, you will be prompted to ' say 'enter in a title. ' say '' say '' say '' say 'Box Plot v1.0 was developed in October 2013 ' say 'Please report any bugs to http://gradsaddict.blogspot.com/' say '' say '-------------------------------------------------------' say '| |' say '| |' say '-------------------------------------------------------' return **************************************************************** * * * *############### STATPACK v1.0 ############### * * * **************************************************************** * * * * * * **************************************************************** * * * STATPACK v1.0: GrADS-aholic October 2013 * ---------------------------------------- * * Description: A collection of functions designed to perform * basic statistical analysis on user input data arrays. * * User Instructions: * ------------------ * 1. Copy paste the contents of this file into the bottom your GrADS script. * 2. Arrays must take the format of a string, with array elements separated by spaces. * e.g., arr='1 2 3 4 5 6 7 8' * 3. List of included functions below. * * ------------------ * List of Functions: * ------------------ * mean(arr): Returns mean of input array "arr" * stdev(arr): Returns Standard Deviation of input array "arr" * sort(arr): Returns sorted array (lowest to highest) of array "arr" * rank(arr): Returns an array size equal to array "arr" ranging from 1 to size(arr) * percentile(arr,p): Returns the data percentile (p) of array "arr" * size(arr): Returns the number of elements in array "arr" * max(arr): Returns the maximum value in array "arr" * min(arr): Returns the minimum value in array "arr" * correlation(arr1,arr2): Returns the correlation coefficient "r" between "arr1" and "arr2" * regression(arr1,arr2): Returns the regression coefficient between "arr1" and "arr2" * ------------------ * ------------------ * * Example: Calculate the median of the array '9 7 8 6 5 4 3 2 1' using the percentile function. * ------------------ * data='9 7 8 6 5 4 3 2 1' * median=percentile(data,0.5) * * the value returned in variable "median" is equal to 5 in this example. * ------------------ **************************************************************** * * * FUNCTIONS * * * **************************************************************** function mean(arr) sum=0 ck=1;c=1 while(ck=1) num=subwrd(arr,c) chknum=valnum(num) if(chknum=0);ck=0;else;sum=sum+num;endif c=c+1 endwhile mean=sum/(c-2) return mean ****************** function stdev(arr) mean=mean(arr) sum_err_sq=0 ck=1;c=1 while(ck=1) num=subwrd(arr,c) chknum=valnum(num) if(chknum=0);ck=0;else sum_err_sq=sum_err_sq+math_pow(num-mean,2) endif c=c+1 endwhile stdev=sum_err_sq/(c-3) stdev=math_sqrt(stdev) return stdev ****************** function sort(arr) ck=1;c=1 while(ck=1) num=subwrd(arr,c) chknum=valnum(num) if(chknum=0);ck=0;endif c=c+1 endwhile size=c-2 ck=1 cstart=1 while(ck<=size) if(ck>1);arr=sorted_arr;endif sorted_arr='' c=cstart while(c<=size) n=c+1 val1=subwrd(arr,c) val2=subwrd(arr,c+1) if(val1 > val2) val.c=val2;val.n=val1 else val.c=val1;val.n=val2 endif c=c+2 endwhile c=size-1;n=size if(ck=1 & valnum(size/2)=1);val1=subwrd(arr,size-1);else;val1=val.c;endif val2=subwrd(arr,size) if(val1 > val2) val.c=val2;val.n=val1 else val.c=val1;val.n=val2 endif c=1 while(c<=size) sorted_arr=sorted_arr' 'val.c c=c+1 endwhile if(cstart=1);cstart=2;else;cstart=1;endif ck=ck+1 endwhile return sorted_arr ******************** function rank(arr) rank_arr='' c=1 ck=1 while(ck=1) num=subwrd(arr,c) chknum=valnum(num) if(chknum=0);ck=0;else;rank_arr=rank_arr' 'c;endif c=c+1 endwhile return rank_arr ******************** function percentile(arr,p) if(p > 1 | p < 0) say "Not a valid number, please choose a number between 0 and 1." else sorted=sort(arr) size=size(arr) q1=p*(size-1)+1 IR=math_int(q1) FR=q1-math_int(q1) if(q1=0);q1=1;endif if(q1=size);q1=subwrd(sorted,size);else q1=subwrd(sorted,math_int(q1))+(subwrd(sorted,math_int(q1)+1)-subwrd(sorted,math_int(q1)))*(q1-math_int(q1)) endif percentile=q1 return percentile endif ******************* function size(arr) ck=1;c=1 while(ck=1) num=subwrd(arr,c) chknum=valnum(num) if(chknum=0);ck=0;endif c=c+1 endwhile size=c-2 return size ******************** function max(arr) size=size(arr) sorted=sort(arr) max=subwrd(sorted,size) return max ******************** function min(arr) sorted=sort(arr) min=subwrd(sorted,1) return min ******************** function correlation(arr1,arr2) size1=size(arr1) size2=size(arr2) if(size1 != size2) say "Correlation Coefficient must be determined from two equal sized vectors." else xmean=mean(arr1) ymean=mean(arr2) t=1 denom=0 numer=0 ys=0 while(t<=size1) xnum=subwrd(arr1,t) ynum=subwrd(arr2,t) denom=(xnum-xmean)*(xnum-xmean)+denom numer=(xnum-xmean)*(ynum-ymean)+numer ys=(ynum-ymean)*(ynum-ymean)+ys t=t+1 endwhile r2=(numer*numer)/(denom*ys) r=math_sqrt(r2) return r endif ******************** function regression(arr1,arr2) size1=size(arr1) size2=size(arr2) if(size1 != size2) say "Regression Coefficient must be determined from two equal sized vectors." else xmean=mean(arr1) ymean=mean(arr2) t=1 denom=0 numer=0 ys=0 while(t<=size1) xnum=subwrd(arr1,t) ynum=subwrd(arr2,t) denom=(xnum-xmean)*(xnum-xmean)+denom numer=(xnum-xmean)*(ynum-ymean)+numer ys=(ynum-ymean)*(ynum-ymean)+ys t=t+1 endwhile reg=numer/denom return reg endif ******************** **************************************************************** * * * END FUNCTIONS * * * **************************************************************** **************************************************************** * * * *############### ############### * * * ****************************************************************