Gary Perlman
© 1986 Gary Perlman
All rights reserved. No part of this handbook may be reproduced or transmitted in any form or by any means without prior written permission from the author. Non-profit organizations may make copies provided such copies are not made for resale.
|STAT Handbook
Table of Contents
1. Introduction
1.1 Capabilities and Requirements
1.2 Design Philosophy
1.3 Table of |STAT Programs
1.4 Table of UNIX and MSDOS Utilities
2. Annotated Example
2.1 A Familiar Problem
2.2 Computing Final Scores
2.3 Summary of Final Scores
2.4 Predicting Final Exam Scores
2.5 Failures by Assistant and Gender
2.6 Effects of Assistant and Gender
3. Conventions
3.1 Command Line Interpreters
3.2 Command Formats
3.3 Program Options
3.4 File Inputs and Outputs
3.5 Input Formats
3.6 Limits and Error Messages
3.7 Manual Entries
4. Data Manipulation
4.1 Data Generation/Augmentation
4.2 Data Transformation
4.3 Data Formatting
4.4 Data Extraction
4.5 Data Validation
4.6 DM: Tutorial and Manual
5. Data Analysis
5.1 Table of Analysis Programs
5.2 stats: print summary statistics
5.3 desc: descriptions of a single distribution
5.4 ts: time series analysis and plots
5.5 oneway: one way analysis of variance
5.6 rankind: rank-order analysis of independent groups
5.7 pair: paired points analysis and plots
5.8 rankrel: rank-order analysis of related groups
5.9 regress: multiple correlation/regression
5.10 anova: multi-factor analysis of variance
5.11 contab: contingency tables and chi-square
5.12 dprime: d'/beta for signal detection data
5.13 CALC: Tutorial and Manual
6. Manual Entries The |STAT Handbook Data Analysis Programs on UNIX and MSDOS Gary Perlman Copyright 1986 Gary Perlman All rights reserved. No part of this handbook may be reproduced or transmitted in any form or by any means without prior written permission from the author. Non-profit organizations may make copies provided such copies are not made for resale.
|STAT Handbook Table of Contents Chapter 0: Preface Chapter 1: Introduction 1 Capabilities and Requirements ....................... 1-1 2 Design Philosophy ................................... 1-2 3 Table of |STAT Programs ............................. 1-3 4 Table of UNIX and MSDOS Utilities ................... 1-4 Chapter 2: Annotated Example 1 A Familiar Problem .................................. 2-1 2 Computing Final Scores .............................. 2-2 3 Summary of Final Scores ............................. 2-3 4 Predicting Final Exam Scores ........................ 2-4 5 Failures by Assistant and Gender .................... 2-6 6 Effects of Assistant and Gender ..................... 2-8 Chapter 3: Conventions 1 Command Line Interpreters ........................... 3-1 2 Command Formats ..................................... 3-2 3 Program Options ..................................... 3-3 4 File Inputs and Outputs ............................. 3-4 5 Input Formats ....................................... 3-5 6 Limits and Error Messages ........................... 3-6 7 Manual Entries ...................................... 3-7 Chapter 4: Data Manipulation 1 Data Generation/Augmentation ........................ 4-1 2 Data Transformation ................................. 4-3 3 Data Formatting ..................................... 4-5 4 Data Extraction ..................................... 4-8 5 Data Validation ..................................... 4-9 6 DM: Tutorial and Manual ............................. 4-10 Chapter 5: Data Analysis 1 Table of Analysis Programs .......................... 5-1 2 stats: print summary statistics ..................... 5-2 3 desc: descriptions of a single distribution ......... 5-3 4 ts: time series analysis and plots .................. 5-4 5 oneway: one way analysis of variance ................ 5-5 6 rankind: rank-order analysis of independent groups .. 5-6 7 pair: paired points analysis and plots .............. 5-7 8 rankrel: rank-order analysis of related groups ...... 5-8 9 regress: multiple correlation/regression ............ 5-9 10 anova: multi-factor analysis of variance ............ 5-10 11 contab: contingency tables and chi-square ........... 5-12 12 dprime: d'/beta for signal detection data ........... 5-13 13 CALC: Tutorial and Manual ........................... 5-14 Chapter 6: Manual Entries Chapter 0: Preface
Purpose and Intended Audience of the Handbook
This handbook is meant to be an introduction to the |STAT programs.
It is not written to teach students how to do data analysis,
although it has been used as a supplementary text in courses.
|STAT users should be familiar with using the hardware and utility
programs (e.g., a text editor) on their systems.
Comparison With Other Packages
|STAT has advantages and disadvantages
compared to other statistical packages.
|STAT is not a comprehensive package because it was developed
as needs arose.
So there are deficits in many areas of analysis:
no multivariate analysis other than regression,
and only simple graphics.
Independent of these limitations,
the programs are not designed for use with large data sets or large values;
the programs are usually adequate for data up to a few thousand points.
Also, |STAT is unsupported,
so if you have problems installing or using the programs,
you may be on your own.
Despite these limitations,
|STAT provides you with most analyses reported in research.
|STAT programs run on UNIX and MSDOS, operating systems
popular in educational and research institutions, government, and industry.
The liberal copyright of the programs
allows free copies to be made for multiple machines
provided the programs are not copied for material gain.
|STAT programs integrate easily with other programs,
and this makes it possible for new programs to be added later.
Distribution Conditions
CAREFULLY READ THE FOLLOWING CONDITIONS. IF YOU DO NOT FIND THEM
ACCEPTABLE, YOU SHOULD NOT USE |STAT.
|STAT IS PROVIDED "AS IS," WITHOUT ANY EXPRESS OR IMPLIED WARRANTY. THE USER ASSUMES ALL RISKS OF USING |STAT. THERE IS NO CLAIM OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |STAT MAY NOT BE SUITED TO YOUR NEEDS. |STAT MAY NOT RUN ON YOUR PARTICULAR HARDWARE OR SOFTWARE CONFIGURATION. THE AVAILABILITY OF AND PROGRAMS IN |STAT MAY CHANGE WITHOUT NOTICE. NEITHER MANUFACTURER NOR DISTRIBUTOR BEAR RESPONSIBILITY FOR ANY MISHAP OR ECONOMIC LOSS RESULTING THEREFROM OF THE USE OF |STAT EVEN IF THE PROGRAMS PROVE TO BE DEFECTIVE. |STAT IS NOT INTENDED FOR CONSUMER USE.
CASUAL USE BY USERS NOT TRAINED IN STATISTICS, OR BY USERS NOT SUPERVISED BY PERSONS TRAINED IN STATISTICS, MUST BE AVOIDED. USERS MUST BE TRAINED AT THEIR OWN EXPENSE TO LEARN TO USE THE PROGRAMS. DATA ANALYSIS PROGRAMS MAKE MANY ASSUMPTIONS ABOUT DATA, THESE ASSUMPTIONS AFFECT THE VALIDITY OF CONCLUSIONS MADE BASED ON THE PROGRAMS. REFERENCES TO SOME APPROPRIATE STATISTICAL SOURCES ARE MADE IN THE |STAT HANDBOOK AND IN THE MANUAL ENTRIES FOR SPECIFIC PROGRAMS. |STAT PROGRAMS HAVE NOT BEEN VALIDATED FOR LARGE DATASETS, HIGHLY VARIABLE DATA, NOR VERY LARGE NUMBERS.
You may make copies of any tangible forms of |STAT programs, provided that there is no material gain involved, and provided that the information in this notice accompanies every copy. You may not copy printed documentation unless such duplication is for non- profit educational purposes. You may not provide |STAT as an inducement to buy your software or hardware or any products or services. You may distribute copies of |STAT, provided that mass distribution (such as electronic bulletin boards or anonymous ftp) is not used. You may not modify the source code for any purposes other than getting the programs to work on your system. Any costs in compiling or porting |STAT to your system are your's alone, and not any other parties. You may not distribute any modified source code or documentation to users at any sites other than your own.
References
|STAT is a small statistical package I have developed on the UNIX operating system (Ritchie & Thompson, 1974) at the University of California San Diego and at the Wang Institute of Graduate Studies. Over twenty programs allow the manipulation and analysis of data and are complemented by this documentation and manual entries for each program. The package has been distributed to hundreds of UNIX sites and the portability of the package, written in C (Kernighan & Ritchie, 1979), was demonstrated when it was ported from UNIX to MSDOS at Cornell University on an IBM PC using the Lattice C compiler. This handbook is designed to be a tutorial introduction and reference for the most popular parts of release 5.3 of |STAT (January, 1987) and updates through February, 1987. Full reference information on the programs is found in the online manual entries and in the online options help available with most of the programs.
Dataset Sizes
|STAT programs have mostly been run on small datasets,
the kind obtained in controlled psychological experiments,
not the large sets obtained in surveys or physical experiments.
The programs' performances on datasets with more than about 10,000
points is not known, and the programs should not be used for them.
System Requirements
The programs run on almost any version of UNIX.
They are compatible with UNIX systems dating back to Version 6 UNIX
(circa 1975).
On MSDOS, the programs run on versions 2.X through 3.X.
MSDOS versions earlier than 2.0 may not support the pipes often used
with |STAT programs,
and MSDOS version 4.0 formats are not compatible.
Space requirements for MSDOS are about 1 megabyte of disk space,
and at least 96 kilobytes of main memory.
Hard disk storage is preferred, but not mandatory.
2 Design Philosophy 1-
|STAT programs promote a particular style of data analysis. The package is interactive and programmable. Data analysis is typically not a single action but an iterative process in which a goal of understanding some data is approached. Many tools are used to provide several analyses of data, and based on the feedback provided by one analysis, new analyses are suggested.
The design philosophy of |STAT is easy to summarize. |STAT consists of several separate programs that can be used apart or together. The programs are called and combined at the command level, and common analyses can be saved in files using UNIX shell scripts or MSDOS batch files.
Understanding the design philosophy behind |STAT programs makes it easier to use them. |STAT programs are designed to be tools, used with each other, and with standard UNIX and MSDOS tools. This is possible because the programs make few assumptions about file formats used by other programs. Most of the programs read their inputs from the standard input (what is typed at the keyboard, unless redirected from a file), and all write to the standard output (what appears on the screen, unless saved to a file or sent to another program). The data formats are readable by people, with fields (columns) on lines separated by white space (blank spaces or tabs). Data are line-oriented, so they can be operated on by many programs. An example of a filter program on UNIX and MSDOS that can be used with the |STAT programs is the sort utility, which puts lines in numerical or alphabetical order. The following command sorts the lines in the file input and saves the result in the file sorted.
sort < input > sorted The < symbol causes sort to read from input and the > causes sort to write to the file sorted. Because sort exists on UNIX and MSDOS, it is not necessary to duplicate its function in |STAT, which does not duplicate existing tools. (In all following examples, this font will be used to show text (e.g., commands and program names) that would be seen by people using the programs.User efficiency is supported over program efficiency. That does not mean the programs are slow, but ease-of-use is not sacrificed to save computer time. Input formats are simple and readable by people. There is extensive checking to protect against invalid analyses. Output formats of analysis programs are designed to be easy to understand. Data manipulation programs are designed to produce uncluttered output that is ready for input to other programs.
On UNIX and MSDOS, a filter is a program that reads from the standard input, also called stdin (the keyboard, unless redirected from a file) and writes to the standard output, also called stdout (the screen, unless redirected to a file). Most |STAT programs are filters. They are small programs that can be used alone, or with other programs. |STAT users typically keep their data in a master data file. With data manipulation programs, extractions from the master data file are transformed into a format suitable for input to an analysis program. The original data do not change, but copies are made for transformations and analysis. Thus, an analysis consists of an extraction of data, optional transformations, and some analysis. Pictorially, this can be shown as:
data | extract | transform | format | analysis | results where a copy a subset of the data has been extracted, transformed, reformatted, and analyzed by chaining several programs. Data manipulation functions, sometimes built into analysis programs in other packages, are distinct programs in |STAT. The use of pipelines, signaled with the pipe symbol, |, is the reason for the name |STAT. 3 Table of |STAT Programs 1-|STAT programs are divided into two categories. There are programs for data manipulation: data generation, transformation, formatting, extraction, and validation. And there are programs for data analysis: summary statistics, inferential statistics, and data plots. The data manipulation programs can be used for tasks outside of statistics.
Data Manipulation Programs
abut join data files beside each other colex column extraction/formatting dm conditional data extraction/transformation dsort multiple key data sorting filter linex line extraction maketrix create matrix format file from free-format input perm permute line order randomly, numerically, alphabetically probdist probability distribution functions ranksort convert data to ranks repeat repeat strings or lines in files reverse reverse lines, columns, or characters series generate an additive series of numbers transpose transpose matrix format input validata verify data file consistencyData Analysis Programs
anova multi-factor analysis of variance calc interactive algebraic modeling calculator contab contingency tables and chi-square desc descriptions, histograms, frequency tables dprime signal detection d' and beta calculations features display features of items oneway one-way anova/t-test with error-bar plots pair paired data statistics, regression, scatterplots rankind rank order analysis for independent conditions rankrel rank order analysis for related conditions regress multiple linear regression and correlation stats simple summary statistics ts time series analysis and plots 4 Table of UNIX and MSDOS Utilities 1-The UNIX and MSDOS environments are similar, at least as far as |STAT is concerned, but many command names differ. The following table shows the pairing of UNIX names with their MSDOS equivalents. UNIX MSDOS Purpose cat type print files to stdout cd,pwd cd change/print working directory cp copy copy files diff comp compare and list file differences echo echo print text to standard output grep find search for pattern in files ls dir list files in directory mkdir mkdir create a new directory more more paginate text on screen mv rename move/rename files print print print files on printer rm del,erase remove/delete files rmdir rmdir remove an empty directory sort sort sort lines in files shell-script batch-file programming language $1,$2 %1,%2 variables /dev/tty con terminal keyboard/screen /dev/null nul empty file, infinite sink Chapter 2: Annotated Example A concrete example with several |STAT programs is worked in detail. The example shows the style of analysis in |STAT. New users of |STAT should not try to understand all the details in the examples. Details about all the programs can be found in on-line manual entries and more examples of program use appear in following chapters. Explanations about features common to all |STAT programs can be found in the next chapter. 1 A Familiar Problem .................................. 2-
To show the |STAT style of interactive data analysis, I will work through a concrete example. The example is based on a familiar problem: grades in a course based on two midterm exams and a final exam. Scores on exams will be broken down by student gender (male or female) and by the lab section taught by one of two teaching assistants: John or Jane. Assume the following data are in the file exam.dat. Each line in the file includes a student identification number, the student's section's teaching assistant, the student's gender, and the scores (out of 100) on the two midterm exams and the final.
S- 1 john male 56 42 58 S-2 john male 96 90 91 S- 3 john male 70 59 65 S-4 john male 82 75 78 S- 5 john male 85 90 92 S-6 john male 69 60 65 S- 7 john female 82 78 60 S-8 john female 84 81 82 S- 9 john female 89 80 68 S-10 john female 90 93 91 S- 11 jane male 42 46 65 S-12 jane male 28 15 34 S- 13 jane male 49 68 75 S-14 jane male 36 30 48 S- 15 jane male 58 58 62 S-16 jane male 72 70 84 S- 17 jane female 65 61 70 S-18 jane female 68 75 71 S- 19 jane female 62 50 55 S-20 jane female 71 72 87We are interested in computing final grades based on the exam scores, and comparing the performances of males versus females, and of the different teaching assistants. The following analyses can be tried by typing in the above file and running the commands in the examples. Minor variations on the example commands will help show how the programs work. 2 Computing Final Scores ..... 2-
Computing final scores is easy with the data manipulation program dm. Assume that the first midterm is worth 20 percent, the second 30 percent, and the final exam, 50 percent. The following command tells dm to repeat each input line with INPUT, and then print the weighted sum of columns 4, 5, and 6, treated as numbers. To print numbers, dm uses an x before the column number. The input to dm is read from the file exam.dat and the result is saved in the file scores.dat. Once all the original data and the final scores are in scores.dat, only that file will be used in following analyses.
dm INPUT ".2*x4 + .3*x5 + .5*x6" < exam.dat > scores.dat The standard input is redirected from the file exam.dat with the < on the command line. Similarly, the standard output, which would ordinarily go to the screen, is redirected to the file scores.dat with the > on the command line. The second expression for dm is in quotes. This allows the insertion of spaces to make the expression more readable, and to make sure that any special characters (e.g., * is special to UNIX shells) are hidden from the command line interpreter. The output from the above command, saved in the file scores.dat, would begin with the following.S- 1 john male 56 42 58 52.8 S- 2 john male 96 90 91 91.7 S- 3 john male 70 59 65 64.2 S- 4 john male 82 75 78 77.9 S- 5 john male 85 90 92 90 S- 6 john male 69 60 65 64.3 etc. This could be sorted by final grade by reversing the columns and sending the output to the standard UNIX or MSDOS sort utility program using the ``pipe'' symbol |.reverse -f < scores.dat | sort The above command would produce the following output.27.1 34 15 28 male jane S-12 40.2 48 30 36 male jane S-14 52.8 58 42 56 male john S-1 54.7 65 46 42 male jane S-11 54.9 55 50 62 female jane S-19 ... 79.3 87 72 71 female jane S-20 82.1 82 81 84 female john S-8 90 92 90 85 male john S-5 91.4 91 93 90 female john S-10 91.7 91 90 96 male john S-2 To restore the order of the fields, reverse could be called again. Another way, more efficient, would be to use the dsort filter to sort based on column 7:dsort 7 < scores.dat 3 Summary of Final Scores .... 2- desc prints summary statistics, histograms, and frequency tables. The following command takes the final scores (the weighted average from the previous section).dm s7 < scores.dat Summary order statistics are printed with the -o option and the distribution is tested against the passing grade of 75 with the -t 75 option. desc makes a histogram (the -h option) with 10 point intervals (the -i 10 option) starting at a minimum value of 0 (the -m 0 option).dm s7 < scores.dat | desc -o -t 75 -h -i 10 -m 0---------- -------------------------------------------------- Under Range In Range Over Range Missing Sum 0 20 0 0 1359.200 ---------- -------------------------------------------------- Mean Median Midpoint Geometric Harmonic 67.960 68.750 59.400 65.564 62.529 ---------- -------------------------------------------------- SD Quart Dev Range SE mean 16.707 10.575 64.600 3.736 ---------- -------------------------------------------------- Minimum Quartile 1 Quartile 2 Quartile 3 Maximum 27.100 57.450 68.750 78.600 91.700 ---------- -------------------------------------------------- Skew SD Skew Kurtosis SD Kurt -0.586 0.548 2.844 1.095 ---------- -------------------------------------------------- Null Mean t prob (t) F prob (F) 75.000 -1.884 0.075 3.551 0.075 ---------- -------------------------------------------------- Midpt Freq 5.000 0 15.000 0 25.000 1 * 35.000 0 45.000 1 * 55.000 4 **** 65.000 5 ***** 75.000 5 ***** 85.000 2 ** 95.000 2 ** 4 Predicting Final Exam Scores ...... 2-The next analysis predicts final exam scores with those of the two midterm exams. The regress program assumes its input has the predicted variable in column 1 and the predictors in following columns. dm can extract the columns in the correct order from the file scores.dat. The command for dm looks like this.
dm x6 x4 x5 < scores.dat The output from dm looks like this.58 56 42 91 96 90 65 70 59 78 82 75 92 85 90 65 69 60 60 82 78 etc. This is the correct format for input for regress, which is given mnemonic names for the columns. The -e option tells regress to save the regression equation in the file regress.eqn for a later analysis.dm x6 x4 x5 < scores.dat | regress -e final midterm1 midterm2 The output from regress includes summary statistics for all the variables, a correlation matrix (e.g., the correlation of midterm1 and midterm2 is .9190), the regression equation relating the predicted variable, and the significance test of the multiple correlation coefficient. The squared multiple correlation coefficient of 0.7996 shows a strong relationship between midterm exams and the final.Analysis for 20 cases of 3 variables: Variable final midterm1 midterm2 Min 34.0000 28.0000 15.0000 Max 92.0000 96.0000 93.0000 Sum 1401.0000 1354.0000 1293.0000 Mean 70.0500 67.7000 64.6500 SD 15.3502 18.6720 20.4303 Correlation Matrix: final 1.0000 midterm1 0.7586 1.0000 midterm2 0.8838 0.9190 1.0000 Variable final midterm1 midterm2 Regression Equation for final: final = -0.2835 midterm1 + 0.9022 midterm2 + 30.9177 Significance test for prediction of final Mult-R R-Squared SEest F(2,17) prob (F) 0.8942 0.7996 7.2640 33.9228 0.0000Predicted Plot
We can look at the predictions from the regression analysis. From the analysis above, the file regress.eqn contains a regression equation for dm.s1 (x2 * -0.283512...) + (x3 * 0.902182...) + 30.9177... Extra precision is used in regress.eqn, compared to the equation in the output from regress to allow more accurate calculations. These two expressions, one on each line, are the obtained and predicted final exam scores, respectively. To plot these against each other, we duplicate the input used to regress, and process regress's output with dm, reading its expressions from the expression file regress.eqn that follows the letter E. The result is passed through a pipe to the paired data analysis program pair with the plotting option -p, options to control the height and width of the plot, the -h and -w options, and -x and -y options to label the plot.dm x6 x4 x5 < scores.dat | dm Eregress.eqn | pair -p -h 10 -w 30 -x final -y predicted |------------------------------|89.3045 | 3| | 1 1 | | 1 1 11 1 1 | | | | 1 2 1 |predicted | 1 1 | | 1 | | 1 | | | |1 | |------------------------------|36.5121 34.000 92.000 final r= 0.894Residual Plot
To plot the residuals (deviations) from prediction, you can run the data through another pass of dm to subtract the predicted scores from the obtained. Note that r must be zero.dm x6 x4 x5 < scores.dat | dm Eregress.eqn | dm x2 x1-x2 | pair -p -h 10 -w 30 -x predicted -y residuals |------------------------------|11.2546 | 11 | | 1 | | 1 1 1 1 1| | 1 1 1 1| |1 1 1 |residuals | 1 1 | | 1 | | 1 | | | | 1 | |------------------------------|-18.0399 36.512 89.304 predicted r= 0.000 5 Failures by Assistant and Gender .. 2-Now suppose the passing grade in the course is 75. To see how many people of each sex in the two sections passed, we can use the contab program to print contingency tables. First dm extracts the columns containing teaching assistant, gender, and the final grade (the weighted average computed earlier). Rather than include the final grade, a label indicating pass or fail is added, as appropriate.
dm s2 s3 "if x7 >= 75 then 'pass' else 'fail'" 1 < scores.dat The huge third expression says ``if the final grade is greater than or equal to 75, then insert the string pass, else insert the string fail.'' Such expressions can be placed in files rather than be typed on the command line, and usually dm is used for simpler expressions. The fourth expression is the constant 1 used to tell contab that there was one replication for each combination of factor levels. Part of the output from dm follows.john male fail 1 john male pass 1 john male fail 1 ... jane female fail 1 jane female fail 1 jane female pass 1 This is used as input to contab, which is given mnemonic factor names.dm s2 s3 "if x7 >= 75 then 'pass' else 'fail'" 1 < scores.dat | contab assistant gender success count Parts of the output from this command follow. First, there is a summary of the input, which contained three factors, each with 2 levels, and a sum of observation counts.FACTOR: assistant gender success count LEVELS: 2 2 2 20 The first contingency table does not provide new information. It shows that both Jane's section and John's section had 6 male and 4 female students.SOURCE: assistant gender male female Totals john 6 4 10 jane 6 4 10 Totals 12 8 20 The second contingency table tells us that 12 of 20 students failed the course--4 in John's section and 8 in Jane's. A significance test follows, and the warning about small expected frequencies suggests that the chi-square test for independence might be invalid. No matter, the Fisher exact test applies because we are dealing with a 2x2 table and total frequencies less than 100. It does not show a significant association of factors (ie. Jane's section did not do significantly better than John's).SOURCE: assistant success fail pass Totals john 4 6 10 jane 8 2 10 Totals 12 8 20Analysis for assistant x success: NOTE: Yates' correction for continuity applied WARNING: 2 of 4 cells had expected frequencies < 5 chisq 1.875000 df 1 p 0.170904 Fisher Exact One-Tailed Probability 0.084901 Fisher Exact Two-Tailed Probability 0.169802 phi Coefficient == Cramer's V 0.306186 Contingency Coefficient 0.292770 The third contingency table shows that 8 male students and 4 female students failed the course.SOURCE: gender success fail pass Totals male 8 4 12 female 4 4 8 Totals 12 8 20 The final table, the three- way interaction, shows all the effects listed above, but no significance test is computed by contab. Some hints about the reason for the poorer performance of Jane's section follow from the next section's analysis of variance.SOURCE: assistant gender success assistan gender success john male fail 3 john male pass 3 john female fail 1 john female pass 3 jane male fail 5 jane male pass 1 jane female fail 3 jane female pass 1 6 Effects of Assistant and Gender ... 2-We now want to compare the performance of the two teaching assistants and of male versus female students. We are interested to see how an assistant's students progress through the term. anova, the analysis of variance program, is the program to analyze these data, but we have to get the data into the correct format for input to anova. anova assumes that there is only one datum per line, preceded by the levels of factors under which it was obtained. This is unlike the format of scores.dat, which has the three exam scores after the student number, teaching assistant name, and gender. Several transformations are needed to get the data in the correct format. As an example, the data for student 1:
S-1 john male 56 42 58 must be transformed to:S- 1 john male m1 56 S-1 john male m2 42 S- 1 john male final 58 This is made up of three replications of the labels with new labels, m1, m2, and final, for the exams inserted. First, dm extracts and inserts the desired information. The result is a 15 column output, one for each expression. Note that on UNIX, it is necessary to quote the quotes of the labels for the exam names. To insert the newlines, so that each datum is on one line, the program maketrix reformats the input to anova into 5 columns. Finally, mnemonic labels for factor names are given to anova.dm s1 s2 s3 "'m1'" s4 ... s1 s2 s3 "'m2'" s5 ... s1 s2 s3 "'final'" s6 < scores.dat | maketrix 5 | anova student assistant gender exam score Only parts of the output are shown below. First, John's students did better than Jane's students (F(1,16)=8.311, p=.011).john 76.7000 jane 58.2333 Female students scored better than males, although the effect is not statistically significant (F(1,16)=3.102, p=.097).male 62.8611 female 74.3750 There was no interaction between these two factors (F(1,16)=.289), but there were some interactions between section assistant and gender and the different exam grades. If we look at the interaction of section assistant and exam, we get a better picture of the performances of John and Jane.SOURCE: assistant exam assista exam N MEAN SD SE john m1 10 80.3000 11.9355 3.7743 john m2 10 74.8000 16.3761 5.1786 john final 10 75.0000 13.4247 4.2453 jane m1 10 55.1000 15.5167 4.9068 jane m2 10 54.5000 19.5973 6.1972 jane final 10 65.1000 16.2101 5.1261 This is the first full cell-means table shown. It contains the names of factors and levels, cell counts, means, standard deviations, and standard errors. The results show that John's students started higher than Jane's (80.3 versus 55.1), and that over the term, Jane's students improved while John's got worse. The significance test for the interaction looks like this.SOURCE SS df MS F p ================================================= ae 610.4333 2 305.2167 9.502 0.001 *** es/ag 1027.8889 32 32.1215 A Scheffe confidence interval around the difference between two means of this interaction can be found with the following formula.sqrt (df1 * critf * MSerror * 2 / N) df1 is the degrees of freedom numerator, critf is the critical F-ratio given the degrees of freedom and confidence level desired, MSerror is the mean-square error for the overall F-test, and N is the number of scores going into each cell. The critical F ratio for a 95% confidence interval based on 2 and 32 degrees of freedom can be found with the probdist program.probdist crit F 2 32 .05 3.294537 Then, the calculator program calc can be used interactively to substitute the values.CALC: sqrt (2 * 3.294537 * 32.1215 * 2 / 10) sqrt(((((2 * 3.29454) * 32.1215) * 2) / 10)) = 6.50617 Any difference of two means in this interaction greater than 6.5 is significant at the .05 level.There was a similar pattern of males versus females on the three exams. Males started out lower than females, and males improved slightly while females stayed about the same.
SOURCE: gender exam gender exam N MEAN SD SE male m1 12 61.9167 20.7822 5.9993 male m2 12 58.5833 22.5931 6.5221 male final 12 68.0833 17.1329 4.9459 female m1 8 76.3750 11.1475 3.9413 female m2 8 73.7500 13.1557 4.6512 female final 8 73.0000 12.7167 4.4960 After the cell means in the output from anova is a summary of the design, followed by an F-table, parts of which were seen above.FACTOR: student assistant gender exam score LEVELS: 20 2 2 3 60 TYPE : RANDOM BETWEEN BETWEEN WITHIN DATAThe results of the analysis show that John's section did better than Jane's. That must be qualified because it seems that Jane's students may not have been as good as John's. To Jane's credit, her students improved more than John's during the term. Chapter 3: Conventions Features common to all the |STAT programs are covered. This information makes it easier to learn about new |STAT programs, and serves as a reference for experienced users. 1 Command Line Interpreters ........................... 3-
|STAT analyses consist of a series of commands, each on a single line, hence the name command line. Commands are typed by users into a command line interpreter, itself a program that runs the commands typed in. On MSDOS, there is no special name given to the command line interpreter. On UNIX, the command line interpreters are called shells, and there are several of them. Users are expected to know the conventions of their command line interpreters. Some of the examples in this handbook and in the manual entries will not work because of differences in how command lines are formatted. Minor modifications to the examples are sometimes needed.
Some command line interpreters support in-line editing, which is useful when running |STAT analyses because data analysis is an iterative process in which minor changes in analyses, and hence commands, are common.
Special Characters
Command line interpreters have special characters to perform special tasks. On both MSDOS and UNIX, there are special characters for file input, output, and pipe redirection:<redirect standard input from the following file >redirect standard output to the following file redirect standard output to the following command UNIX and MSDOS both have patterns (sometimes called ``wildcards'') to match file names. For example, *.c matches all files that end with a c suffix. Also, the ? can be used in patterns to match any one character. An important difference between UNIX and MSDOS command line interpreters is that on UNIX, the pattern matching is part of the shell, and so is available to every program, while on MSDOS, it is part of only some programs.It is sometimes necessary to quote the special meaning of special characters so that they are not seen by the command line interpreter. For example, an expression for dm might contain the symbols * for multiplication or < for comparison. Both these characters are special to UNIX shells, while only < is special to MSDOS. The blank space and tab characters are special on both UNIX and MSDOS, and are used to separate command line arguments. Special characters can be quoted by enclosing command line arguments in double quotes. For example, dm expressions may contain special characters, and strings may contain spaces.
dm "if x1 > 10 then 'Large number on line:' else SKIP" INLINE 2 Command Formats ..................................... 3-|STAT programs are run on UNIX and MSDOS by typing the name of the program, program options, and program operands (e.g., expressions or file names). Program names, options, and operands, are separated or delimited by blank space. On UNIX, program names are lower case, while on the case-insensitive MSDOS, they are always upper case, although users can type the names in lower case. Program options and operands can be complex, so it is sometimes useful to insert spaces into an option value or an operand, either to modify the output or to make the command line more readable. This is done by quoting (with double quotes) the parts that should be kept together.
Simple Commands
A simple command consists of a program name, program options delimited with minus signs, and program operands, such as file or variable names. Here are some examples:dm x1+x2 x3/x4 calc model regress -p age height weight desc -h -i 1 -m 0 -cfp series 1 100 .5 probdist random normal 100Pipelines of Commands
A pipeline of commands is a series of simple commands joined by the pipe symbol, |. In a pipeline, the output from one simple command is the input to the next command in the pipeline. The following pipeline creates a series of numbers from 1 to 100, transforms it by using the dm logarithm function, and then makes a histogram of the result.series 1 100 | dm logx1 | desc -h The following pipeline abuts three files beside one another, and passes the result to the regress program, which prints their correlation matrix.abut age height weight | regress -r age height weight Note that the operands to abut are file names, while those for regress are variable names, which could be different if desired. If they were always supposed to be the same, then this constraint could be encoded in a shell script or batch file.Batch Files and Shell Scripts
Because the |STAT programs work well together, and because most data analysis is routine, it is often advantageous to save a series of commands in a file for later analyses. Both UNIX and MSDOS support this, MSDOS with batch files and UNIX with shell scripts. Batch files and shell scripts also support variables, some set by command line calls and some set inside the command file. They provide |STAT with a simple but effective programming facility. 3 Program Options ..................................... 3-Program options allow the user to control how a program works by requesting custom or extra analysis. Without options, |STAT programs provide the simplest or most common behavior by default. Program options conform to the standard UNIX option parsing convention (Hemenway & Armitage, 1984) by using the getopt option parser. In this standard, all program options are single characters preceded by a minus sign. For example, -a and -X are both options. All program options must precede operands (such as file names, variable names, or expressions). Some options require values, and these should follow the option. For example, the pair plotting function allows setting the height of the plot with the -h option: -h 30 would set the plot height to 30 lines. There should be a space between an option and its value. Options that do not take values (logical options) can be grouped or ``bundled'' to save typing. For example, the descriptive statistics program, desc, has options for requesting a histogram, a table of frequencies, and a table of proportions. These can be requested with the bundle of options: -hfp instead of the longer: -h -f -p.
There are some special conventions used with the getopt option parser. A double dash, --, by itself signals the end of the options, which can be useful when the first operand begins with - and it would be misinterpreted as an option. For programs that take files as operands (e.g., abut, calc), a solitary - means to read from the standard input, which can be useful to insert the output of a pipeline in a set of files. For example, the abut program can read several files with the standard input inserted with the following command line.
series 1 20 | abut file1 file2 - file3 The output would be four columns, the third of which would be the series 1 to 20.The same options can usually be specified more than once on a command line. For logical options (those that turn on or off a feature), repetition usually has no effect. For options that take values, such as the width of a plot, respecifying an option resets it to a new value. Exceptions to these rules for specific options are mentioned in program manual entries.
Table of Option Rules -x options are single letters preceded by minus -h 30 option values must follow the option after a space -nve logical options can be bundled -- signals the end of the options - insert standard input to operands of file-reading programStandard Options
All |STAT programs using the standard option parser, getopt, have standard options to get information online. The information reported by the program is always accurate, while the printed documentation may not be up to date, or may not apply to the particular version (e.g., limits on MSDOS may be smaller than on UNIX).-L prints a list of program limits -O prints a summary of program options -V prints version information 4File Inputs and Outputs 3-Most of the |STAT programs are filters. That means they read from the standard input and write to the standard output. By default, the standard input is the keyboard, and the standard output is the screen. The standard input and output can independently be ``redirected'' using the special characters: <, to redirect the standard input from an immediately following file name, >, to redirect the standard output to a file. Also, the pipe character |, can connect the output from one program to the input to another. (Some of these features are not available on early versions of MSDOS (before version 2.0).) The following command says for the anova program to read from the file anova.in.
anova < anova.in The output would go to the screen, by default. The following command saves the above output to the file anova.out.anova < anova.in > anova.out Never do this:anova < data > data # Never Do This! Never make the input file the same as the output file, or you will lose the file; the output file is created (and zeroed) by the command line interpreter before the input file is read. Temporary files should be used instead. Here is an example of output redirection to save 50 random normal numbers.probdist random normal 50 > numbers In English, this is read: ``A random sample of 50 numbers is created and saved in the file numbers. This file of numbers could be used as input to the descriptive statistics program, desc. The intermediate file, numbers, could be avoided by using a pipeline.probdist random normal 50 | desc To save the result of the above analysis in a file called results, output redirection would be used.probdist random normal 50 | desc > resultsAlthough pipes are supported on MSDOS, they are not efficient and they require that there is enough space for temporary files to hold the contents of the pipes (temporary files with names like PIPE%1.$$$). This can make input and output redirection without pipes a better choice for speed, especially in command scripts, called ``batch files'' on MSDOS.
Keyboard Input
If a program is expecting input from the keyboard (ie. the standard input has not been redirected from a file or pipe), a prompt will be printed on the screen. Often, input from the keyboard is a mistake; most people do not type directly into an analysis program but prepare a file with their preferred editor and use that file as input.prompt: desc desc: reading input from terminal: user types input, followed by end of file: ^D on UNIX, ^Z on MSDOS In all examples of keyboard input, the sequence ^X will be used for control characters like control-x (hold down the CTRL key and type the letter x). On UNIX, end of input from the keyboard is signaled by typing ^D. MSDOS users type ^Z. 5Input Formats 3-|STAT programs have simple input formats. Program input is read until the end of file, EOF, is found. End of file in disk files is done by the system; no special marking characters are needed nor allowed.
Input fields (visibly distinguishable words) are separated by whitespace (blank spaces, tabs, newlines). For most programs, fields in lines with embedded spaces can be enclosed by single or double quotes. Most |STAT analysis programs ignore blank input lines used to improve the human-readability of the data. However, blank lines are meaningful to some data manipulation programs, so when there are unexpected results, it is often instructive to run a file through validata.
Suggestion: Staged Analysis
It is usually a good idea to build a complex command, such as a pipeline, in stages. At each stage, a quick visual inspection of the output catches most errors you might make.Data Types
|STAT programs recognize several types of data: label and variable names, numbers (integers and real numbers), and some programs can deal with missing values, denoted by NA. Label and variable names begin with an alphabetic character (a-z or A-Z), and can be followed by any number of alphanumerics (a-z, A-Z, 0-9) and underscores. There are three types of numbers: integers, real numbers with a decimal point, and numbers in exponential scientific notation. Integers are positive or negative numbers with no decimal point, or if they have a decimal point, they have no non-zero digits after the decimal point. Exponential notation numbers are numbers of the form xxx.yyyEzz. They may have digits before an optional decimal point or after it, and the number after the E or e is a power of ten multiplier. For example, 1.2e-6 is 1.2 times the inverse of one million.Caveat: Appearances Can Be Deceiving
Inputs that look like they line up might not appear so to |STAT programs. For example, the following data might appear to have four columns, but have a variable number. Also, the columns that look like they line up to a person, do not line up to |STAT programs.a bcd e fg h ij Here is how |STAT programs see this input:a bcd e fg h ij This difference could be found with the validata utility program, which would report for both formats above:validata: Variable number of columns at line 2 Col N NA alnum alpha int float other type min max 1 3 0 3 3 0 0 0 alnum 0 0 2 3 0 3 3 0 0 0 alnum 0 0 3 3 0 3 3 0 0 0 alnum 0 0 4 1 0 1 1 0 0 0 alnum 0 0 6Limits and Error Messages 3-There is a system-dependent limit on the count of characters in an input line: on small systems, 512 characters, and on large ones, 1024. Many programs use dynamic memory allocation so the memory available on a machine will determine the size of data sets that can be analyzed. Integer overflow is not checked, so numbers like data counts are limited on 16 bit machines to 32767; in practice, this has not presented problems. All calculations are done with double precision floating point numbers, but overflow (exceeding the maximum allowed double precision number, about 10 to the 38th power) and underflow (loss of precision of a tiny non-zero result being rounded to 0.0) are not checked. Program specific limits can be found in most programs with the -L option. The programs are not robust when used on highly variable data (differences of several orders of magnitude), very large numbers, or large datasets (more than 10,000 values).
All error and warning messages (1) identify the program detecting the problem (useful when pipelines or command scripts are used), (2) print diagnostic information, (3) sound a bell, and for errors, (4) cause an exit. All error and warning messages are printed on the diagnostic output (that is stderr for C lovers), so they will be seen even if the standard output is redirected to a file. All |STAT programs exit with a non-zero exit status on error and a zero exit status after a successful run.
Common Error Messages
Some errors and messages are common to several programs. They are explained below. Other messages should be self- explanatory.Not enough (or no) input data There were no data points read, or not enough to make sense Too many xxxx's; at most N allowed Too many of something were in the input (e.g., columns or variables) Cannot open 'file' The named file could not be opened for reading No storage space left for xxxx The program has run out of dynamic memory for internal storage 'string' (description) is not a number The described object whose input value was 'string' was non-numerical N operand(s) ignored on command line Operands (e.g., files) on the command line are ignored by this program VALUE is an illegal value for the TYPE The provided value was out of the legal range for the given type Ragged input file The program expects a uniform number of input columns 7Manual Entries 3-|STAT manual entries contain detailed information about each of the programs. They describe the effects of all the options.
On-Line Manuals
On UNIX systems, the manual entries for |STAT programs are available online with the manstat program. UNIX system administrators might prefer to install the |STAT manuals in a public place, so they might be available with the standard UNIX man program. On MSDOS systems, manual entries might be available online with a batch file that types pre-formatted manuals. The following will print the online manual for the anova program.manstat anova Most programs print a summary of their options with the -O option. The following will print a summary of the options available with the desc descriptive statistics program.desc -OUNIX Manual Conventions
UNIX manual entries are often considered cryptic, especially for new users. It helps to know the conventions used in writing manual entries. In the following table, the contents of the different manual entry sections are summarized.ALGORITHMS sources or descriptions of algorithms BUGS limitations or known deficiencies in the program DESCRIPTION details about the workings of the program, and information about operands EXAMPLES examples of command lines showing expected use of the program FILES files used by the program (e.g., temporary files) LIMITS limits built into the program should be determined with the -L option NAME the name and purpose of the program OPTIONS detailed information about command line options (see the -O option) SYNOPSIS a short summary of the option/operand syntax for the program (items enclosed in square brackets are optional) Chapter 4: Data Manipulation All data manipulation programs are introduced, showing some of their options. Full documentation is in the manual entries. |STAT data manipulation tools allow users to generate, transform, format, extract, and validate data. dm, the data manipulator, is the most important tool for use with other |STAT programs. A detailed manual for dm is the last section of this chapter. There are several classes of data manipulation programs. Generation programs produce more data than their inputs by repeating data, numbering data, or by creating new data. Transformation programs allow algebraic conversion of data. Formatting programs change the shape or order of the data. Extraction programs produce subsets of datasets. Validation programs check the consistency, data types, and ranges of data. 1 Data Generation/Augmentation ........................ 4-repeat: repeat a string or file
repeat can repeat strings or lines in a file as many times as requested. It helps generate labels for datasets, or feed a program like dm that needs input to produce output. The following will repeat the file data 10 times.repeat -n 10 data The following will repeat its input series of 20 numbers 15 times.series 1 20 | repeat -n 15 Strings can be repeated using the echo command. The following will repeat the string hello 100 times.echo hello | repeat -n 100series: generate a linear series
series generates a linear series of numbers between two values. By default, its values change by units, but this can be modified. The following produces a series of 10 numbers, 1 to 10, one per line.series 1 10 The following produces the same series, but in reverse order; the start of the series can be greater than the end.series 10 1 Non-integral series can be created by supplying an optional increment.series 0 1 .1 produces the series:0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1 except that each value is on its own line. The output from series can be transformed with dm to produce other than linear series. Here is an exponential series:series 1 10 | dm "exp(x1)"probdist: generate random numbers
probdist can generate random numbers for several probability distributions. The following will generate 100 random numbers from the uniform distribution (between 0 and 1).probdist random uniform 100 This can be transformed using dm to get random numbers with other ranges. The following will produce 100 random integers uniformly distributed between 10 and 29.probdist random uniform 100 | dm "floor(x1*20+10)" The following generates numbers from a one- trial binomial distribution with probability 0.5.probdist random uniform 100 | dm "if x1 > .5 then 1 else 0" probdist also has a binomial distribution built in, so the following would be equivalent to the previous example:probdist rand binomial 1 1/2 100 The random number generator can be seeded. The following will seed the random number generator with 143 and generate 100 normally distributed z values.probdist -s 143 random normal 100 The seeding option is useful when a random sequence must be repeated. The random normal numbers have a mean of 0 and a standard deviation of 1, so dm can help create different random normal distributions. The following samples a normal distribution with mean 100 and standard deviation 15.probdist random normal 100 | dm "x1*15+100"abut: number lines, recycle files
abut can number input lines in files using the -n option, or cycle through input files as many times as is necessary to match the length of longer files. The latter case is common in creating input files for programs like anova and contab, which have input data tagged with regular patterns of labels.File1 File2 Data large easy 12 small easy 23 hard 34 hard 45 56 67 78 89 For the above input file configuration, the commandabut -nc File1 File2 Data would produce the following by recycling the smaller files.1 large easy 12 2 small easy 23 3 large hard 34 4 small hard 45 5 large easy 56 6 small easy 67 7 large hard 78 8 small hard 89dm: number lines
dm can number its input lines with its special variables INLINE, which always contains the input line number, and INPUT, which always contains the current input line.dm INLINE INPUT < data 2 Data Transformation 4-dm: conditional algebraic combinations of columns
dm can produce algebraic combinations of columns. The following command reads from data and produces the ratio of columns 2 and 1 with column 3 added on.dm x2/x1+x3 < data Transformations can be based on conditions. For example, if x1, the value in column 1, in the above example is 0, then dm will exit after producing an error message like:dm: division by zero. input line 12 expr[1]. To avoid this problem, the following will do the division only if x1 is non-zero.dm "if x1 then x2/x1+x3 else 0" < dataprobdist: probability/statistic conversion
probdist can convert probabilities to distribution statistics and vice versa as seen in tables at the end of most statistics textbooks. Many distributions are supported, including: the normal z, binomial, chi-square, F, and t. The following will print the two-tailed probability of an obtained t statistic of 2.5 with 20 degrees of freedom.probdist prob t 20 2.5 0.021234 Similarly, the following will print the two-tailed probability of an F ratio of 6.25 with 1 and 20 degrees of freedom.probdist prob F 1 20 6.25 0.021234 These results are the same because of the relationship between the t and F distributions.The following prints the critical value (also called the quantile) in the chi-square distribution with 5 degrees of freedom to obtain a significance level of .05.
probdist crit chisq 5 .05 11.070498 Both probabilities and critical values in the normal z distribution use the lower one tail -oo to +oo distribution, so the z value that produces the .05 level is obtained with the following.probdist crit z .05 -1.644854 The critical value for the 99th percentile is found with the following.probdist crit z .99 2.326348 Binomial distribution critical values are treated differently than the other continuous distributions. For the binomial distribution based on five trials, and a probability of success of one half, The critical value for a one-tailed test at the .05 level is:probdist crit binomial 5 1/2 .05 5 even though the probability of 5 successes is proportionally much less than .05:probdist prob binomial 5 1/2 5 0.031250 This is because the binomial distribution is discrete. Not only are critical values conservative, sometimes there may be no possible value; there is no way to get a less probable event than five out of five successes:probdist crit binomial 5 1/2 .01 6 Here, probdist is returning an impossible value (one with zero probability).ranksort: convert data to ranks
ranksort can rank order data from numerical data columns. For the input:1 95 4.3 2 113 5.2 3 89 4.5 4 100 5.0 5 89 4.5 ranksort would produce:1 3 1 2 5 5 3 1.5 2.5 4 4 4 5 1.5 2.5 The ties in the second and third columns get the average rank of the values for which they are tied. Once data are ranksorted, further ranksorting has no effect. With rank orders within columns, rank order statistics (e.g., Spearman rank order correlation, average group rank) can be computed by parametric programs like pair or regress. 3 Data Formatting .... 4-maketrix: form a matrix format file
maketrix reads its data, one whitespace separated string at a time from its free format input, and produces a multicolumn output.series 1 20 | maketrix 5 The above produces a five column output.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20perm: permute lines
perm, with no options, randomizes its input lines. It can randomize output from programs like series.series 1 20 | perm A subset of this permutation is a sample without replacement. The following is a sample of size 10 from the file data.perm < data | dm "if INLINE <= 10 then INPUT else EXIT" perm can be supplied a seed for its random number generator, to replicate a random permutation.series 1 20 | perm -s 5762 | maketrix 5 The above produces (with my system's random number generator):18 7 10 13 2 14 11 19 15 20 1 3 9 6 16 8 17 12 5 4perm can also put its lines in alphabetical or numerical order. For example, the output from the previous example could be put into ascending order (according to the first number on each line) with:
series 1 20 | perm -s 5762 | maketrix 5 | perm -n This produces:1 3 9 6 16 8 17 12 5 4 14 11 19 15 20 18 7 10 13 2dsort: sort data lines by multiple keys
The last example of the perm filter showed how lines can be ordered according to the numerical value in the first column. dsort can sort lines based on numerical or alphabetical values in any column. For example, the following command sorts the previous example matrix in ascending order of the values in the third column.series 1 20 | perm -s 5762 | maketrix 5 | dsort -n 3 This produces:1 3 9 6 16 18 7 10 13 2 8 17 12 5 4 14 11 19 15 20 If there were ties in a column, dsort could sort by additional key columns.transpose: transpose matrix format file
transpose flips rows and columns in its input. For the input:1 2 3 4 5 6 7 8 9 10 11 12 transpose produces:1 5 9 2 6 10 3 7 11 4 8 12 The input to transpose does not have to be regular, nor does it have to be numerical.one two three four five six seven eight nine ten eleven For the above input, transpose produces the following.one four six seven nine two five eight ten three eleven Note that with regular inputs, the transposition of a transposition yields the original. This is not necessarily so with data as in the above input and output. The above output piped through another pass of transpose produces a result different from the original input.one two three four five eleven six eight seven ten ninereverse: reverse lines, columns, characters
reverse can reverse the lines, fields, or characters in its input. It can provide easier access to the last lines in a file, or the last columns on lines. To get the last 10 lines in a file, we can reverse the file, get the first 10 lines, and then reverse those 10 lines.reverse < data | dm "if INLINE GT 10 then EXIT else INPUT" | reverse To get the last two columns in a file is easier.reverse -f < data | dm s2 s1 Here, dm is used for column extraction, and rather than call reverse a second time, what were the last two columns before reversal are listed in the opposite order.colex: reorder columns, reformat columns
colex is a column extraction program that shares some of the functionality of dm and reverse. colex is faster and has a simpler syntax than dm and has data formatting capabilities. Suppose a matrix dataset with 10 columns is created with the following.series 1 50 | maketrix 10 colex can extract the last five columns followed by the first five with the command:series 1 50 | maketrix 10 | colex 6-10 1 2 3 4 5 Either ranges of columns or single columns can be given. The above command produces:6 7 8 9 10 12345 16 17 18 19 20 1112131415 26 27 28 29 30 2122232425 36 37 38 39 40 3132333435 46 47 48 49 50 4142434445Note in the previous example how the numbers line up on the left, rather than the customary format to line up the unit digits. This is because colex puts tabs between columns, and it is not a problem because |STAT programs read data in free-format. colex can print its columns in several numerical formats as well as the default string format. The numerical formatting can round values to some number of decimal places (like zero, for whole numbers). The option: -F 4i would tell colex to format all the columns as integers, each four spaces wide, and the -t option would tell colex to not place a tab between columns. The format of columns can be assigned to individual columns by placing the format before each range of columns. For example, the following variation on the previous command would print columns 6-10 in a money format with two digits after the decimal place, and columns 1-5 as integers four wide.
series 1 50 | maketrix 10 | colex -t 6.2n6-10 4i1-56.00 7.00 8.00 9.00 10.00 1 2 3 4 5 16.00 17.00 18.00 19.00 20.00 11 12 13 14 15 26.00 27.00 28.00 29.00 30.00 21 22 23 24 25 36.00 37.00 38.00 39.00 40.00 31 32 33 34 35 46.00 47.00 48.00 49.00 50.00 41 42 43 44 45dm: reorder columns
dm, like colex, can reorder columns. However, it does not allow the specification of ranges of columns. The above example of colex could be done with dm with similar results.series 1 50 | maketrix 10 | dm s6 s7 s8 s9 s10 s1 s2 s3 s4 s5abut: paste corresponding lines from files
abut can join data in separate files beside one another. In the usual case, abut takes N files with K lines and produces 1 file with K lines. Suppose the files height and weight contain the respective heights and weights of the same people. Each line in each file contains one height or weight. These could be plotted with the plotting option on the pair program with the following command.abut height weight | pair -p 4 Data Extraction .... 4-dm: conditional data extraction
dm can extract subsets of its input, either by columns or by lines. To extract columns of data, each extracted column is specified with the number of the column preceded by the letter s. The following extracts columns 8, 2, and 11, in that order.dm s8 s2 s11 dm can extract lines of data by using its built-in line skipping expression SKIP. The following will extract lines 50 to 100.dm "if INLINE >= 50 & INLINE <= 100 then INPUT else SKIP" It is more awkward than column extraction, but the latter is common.colex: quick column extraction
colex can extract individual columns, or ranges of columns. For column extraction, it is easier to use and faster than dm. The following extracts, in order, columns 8, 2, and 11.colex 8 2 11linex: line extraction
linex can extract individual lines (by number), or ranges of lines. The following extracts, in order, lines 8, 2, and 11.linex 8 2 11 To extract lines 50 to 100, you could type:linex 50-100 or you could even extract them in reverse order:linex 100-50 5 Data Validation .... 4-validata: data validation
validata will report for its input the number of columns, data-types of columns, and for columns with numerical values, the maxima and minima. validata reports any inconsistencies in the number of columns in its input. Floating point numbers can be entered in scientific notation. For the input:1 2 3 4 5 6 7 2E2 end 5 1e-3 validata's output is:validata: Variable number of columns at line 4 Col N NA alnum alpha int float other type min max 1 4 0 4 0 4 4 0 int 1 7 2 4 0 3 0 2 4 0 float 0.001 200 3 3 0 3 1 2 2 0 alnum 3 6dm: conditional data validation
dm can find exceptional cases in its input. A simple case is non-numerical input, which can be checked with dm's number function.dm "if !number(s1) then 'bad input on line' else SKIP" INLINE dm can check for specific values, ranges of values, or specific relations of values. The following prints all lines in data with the string bad in them.dm "if 'bad' C INPUT then INPUT else SKIP" The input line number could be prepended.dm INLINE "if 'bad' C INPUT then INPUT else SKIP" This is possible because dm will produce no output for skipped lines, regardless of expression order. The following prints all lines where column 3 is greater than column 2.dm "if x3 > x2 then INPUT else SKIP" dm can print lengths of strings and check for numerical fields:dm len(s1) number(s1) will print the length of column 1 strings, and report if they are numerical (0 for non-numbers, 1 for integers, 2 for real numbers, 3 for exponential scientific notation numbers). 6 DM: Tutorial and Manual ...... 4-dm is a data manipulating program with many operators for manipulating columnated files of numbers and strings. dm helps avoid writing little BASIC or C programs every time some transformation to a file of data is wanted. To use dm, a list of expressions is entered, and for each line of data, dm prints the result of evaluating each expression.
Introductory Examples. Usually, the input to dm is a file of lines, each with the same number of fields. Put another way, dm's input is a file with some set number of columns.
Column Extraction: dm can be used to extract columns. If data is the name of a file of five columns, then the following will extract the 3rd string followed by the 1st, followed by the 4th, and print them to the standard output.
dm s3 s1 s4 < data Thus dm is useful for putting data in a correct format for input to many programs, notably the |STAT data analysis programs. Warning: If a column is missing (e.g., you access column 3 and there is no third column in the input), then the value of the access will be taken from the previous input line. This feature must be considered if there are blank lines in the input; it may be best to remove blank lines, with dm or some other filter program.Simple Expressions: In the preceding example, columns were accessed by typing the letter s (for string) followed by a column number. The numerical value of a column can be accessed by typing x followed by a column number. This is useful to form simple expressions based on columns. Suppose data is a file of four numerical columns, and that the task is to print the sum of the first two columns followed by the difference of the second two. The easiest way to do this is with:
dm x1+x2 x3-x4 < data Almost all arithmetic operations are available and expressions can be of arbitrary complexity. Care must be taken because many of the symbols used by dm (such as * for multiplication) have special meaning when used in UNIX (though not MSDOS). Problems can be avoided by putting expressions in quotes. For example, the following will print the sum of the squares of the first two columns followed by the square of the third, a simple Pythagorean program.dm "x1*x1+x2*x2" 'x3*x3' < dataLine Extraction Based on Conditions: dm allows printing values that depend on conditions. The dm call
dm "if x1 >= 100 then INPUT else NEXT" < data will print only those lines that have first columns with values greater than or equal to 100. The variable INPUT refers to the whole input line. The special variable NEXT instructs dm to stop processing on the current line and go to the next.Data Types
String Data. To access or print a column in a file, the string variable, s, is provided. si (the letter s followed by a column number, such as 5) refers to the ith column of the input, treated as a string. The most simple example is to use an si as the only part of an expression.dm s2 s3 s1 will print the second, third and first columns of the input. One special string is called INPUT, and is the current input line of data. String constants in expressions are delimited by single or double quotes. For example:"I am a string"Numerical Data. Constant numbers like 123 or 14.6 can be used alone or with other expressions. Two general numerical variables are available To refer to the input columns, there is xi and for the result of evaluated expressions, there is yi. xi refers to the ith column of the input, treated as a number. xi is the result of converting si to a number. If si contains non-numerical characters, xi may have strange values. A common use of the xi is in algebraic expressions.
dm x1+x2 x1/x2 will print out two columns, first the sum of the first two input columns, then their ratio.The value of a previously evaluated expression can be accessed to avoid evaluating the same sub-expression more than once. yi refers to the numerical value of the ith expression. Instead of writing:
dm x1+x2+x3 (x1+x2+x3)/3 the following would be more efficient:dm x1+x2+x3 y1/3 y1 is the value of the first expression, x1+x2+x3. String values of expressions are unfortunately inaccessible.Indexing numerical variables is usually done by putting the index after x or y, but if value of the index is to depend on the input, such as when there are a variable number of columns, and only the last column is of interest, the index value will depend on the number of columns. If a computed index is desired for x or y the index should be an expression in square brackets following x or y. For example, x[N] is the value of the last column of the input. N is a special variable equal to the number of columns in INPUT. There is the option to use x1 or x[1] but x1 will execute faster so computed indexes should not be used unless necessary.