1. Home
2. Calculations and manipulation

# Calculations and manipulation

The directive `CALCULATE` allows arithmetic calculations on the values of any numeric data structure; logical tests can also be done on numerical and textual values. Functions and operators are available for a very wide range of calculations on matrices and tables. Another general directive is `EQUATE`, which allows values to be copied from one set of data structures to another; the structures must store values of the same mode (for example, numbers or text), but need not be of the same type.

Structure values can be deleted to save space within Genstat; attributes can also be deleted so that the structure can be redefined, for example as another type. Contents of data structures can be compared, to see if they contain the same distinct items, or whether the distinct values in one structure are a subset of those in another. You can also find all the locations where a number, identifier or string occurs within a data structure.

    `CALCULATE` performs arithmetic and logical calculations allows values and attributes of data structures to be deleted copies values between sets of data structures compares the sets of values in two data structures finds locations of an identifier within a pointer, or a string within a factor or text, or a number within any numerical data structure

There are several general directives for manipulating vectors (variates, factors or texts). Units of vectors can be sorted into systematic order or into random order. Boolean arithmetic can be performed on their contents, or you can form all the ways of partitioning them into subsets. A “restriction” can be associated with a vector, so that subsequent statements operate on only a subset of its units. A default length and labelling can be defined for vectors formed later in the job. Facilities for specific types of vector allow interpolation of values for variates, monotonic regression, calculation of regression quantiles, generation of factor values, and concatenation, editing and searching of text.

    `SORT` sorts units of vectors into alphabetic or numerical order of an index vector, or forms a factor from a variate or text performs Boolean set calculations on the contents of vectors and pointers runs through all ways of allocating a set of objects to subsets defines a “restriction” on the units of a vector defines default length or labelling for vectors defined subsequently in the job calculates variates of interpolated values forms regression quantiles fits an increasing monotonic regression forms a factor (or grouping variable) from a variate or text, together with the set of distinct values that occur concatenates together lines of text vectors line editor for units of text vectors breaks a text structure into individual words forms a text structure by appending or concatenating values of scalars, variates, texts, factors or pointers; allows the case of letters to be changed or values to truncated and reversed finds a subtext within a text structure converts textual characters to and from their corresponding integer codes locates strings within the lines of a text structure replaces a subtext within a text structure

Another general directive allows you to run many algorithms from the Numerical Algorithms Group Library, for example to build mathematical models.

    `NAG` calls an algorithm from the NAG Library

Other facilities for vectors are provided by the procedures in the Genstat Procedure Library, including:

    `APPEND` appends a list of vectors of compatible types permutes the levels and labels of a factor forms a factor to indicate observations with identical values of a set of variates, texts or factors represents a factor by factorial combinations of a set of factors redefines the levels and labels of a factor to exclude those that are unused merges levels of factors forms a factor with a level for every combination of other factors sorts the levels of a factor according to an index vector redefines a list of factors so that they have the same levels or labels redefines a factor so that its levels and labels are unique forms variates and classifying factors containing within-group summaries to use e.g. in a between-group analysis checks sets of factors to remove any that define duplicate classifications forms a pointer of factors representing a multiple-response forms multiple-response factors from free-response data expands vectors onto a regular two-dimensional grid forms vectors with the restricted subset of a list of vectors puts a matrix into row canonical, or reduced row echelon, form forms a single string from a list of strings in a text forms a text structure from a variate redefines a variate or text so that its values are unique forms factors to define terms representing the effects of one factor within another factor forms a string listing the identifiers of a set of data structures generates pseudo-random numbers from probability distributions generates multinomial pseudo-random numbers generates multivariate normal pseudo-random numbers joins or merges two sets of vectors together, based on classifying keys replaces missing values in a vector with the previous non-missing value calculates orthogonal polynomials calculates quantiles of the values in a variate produces ranks, from the values in a variate, allowing for ties reshapes a data set with classifying factors for rows and columns, into a reorganized data set with new identifying factors samples from a set of units, possibly stratified by factors constructs stratified random samples combines several data sets by “stacking” the corresponding vectors standardizes columns of a data matrix to have mean 0 and variance 1 forms vectors containing subsets of the values in other vectors pads strings of a text structure with extra characters so that their lengths are equal forms a text containing a progression of strings splits a text into individual texts, at positions on each line marked by separator character(s) splits vectors into individual vectors according to levels of a factor equates values across a set of data structures performs linear and inverse linear interpolation between variates replaces values of vectors and pointers

There are several procedures for calculating or fitting splines, and for manipulating series of observations of a theoretical curve.

    `SPLINE` calculates a set of basis functions for M-, B- or I-splines calculates design matrices to fit a natural polynomial or trignometric L-spline as a linear mixed model calculates natural cubic spline basis functions (for use e.g. in `REML`) calculates design matrices to fit a penalized spline as a linear mixed model calculates design matrices to fit a P-spline as a linear mixed model calculates design matrices to fit a radial-spline surface as a linear mixed model calculates design matrices to fit a tensor-spline surface as a linear mixed model forms an optimal warping to align an observed series of observations with a standard series estimates a baseline for a series of numbers whose minimum value is drifting finds the locations of peaks in an observed series

Directives are available for eigenvalue, QR and singular-value decompositions of matrices, and to form the values of SSPM structures.

    `FLRV` calculates latent roots and vectors (that is, eigenvalues and eigenvectors) calculates QR decompositions of matrices calculates singular-value decompositions of matrices calculates values for SSPM structures (sums of squares and products, means, etc.)

Procedures in the Library for operating on matrices include:

    `FCORRELATION` forms the correlation matrix for a list of variates calculates partial correlations for a list of variates forms Hadamard matrices forms a projection matrix for a set of model terms forms summation, or relationship, matrices for model terms forms the variance-covariance matrix for a list of variates calculates the generalized inverse of a matrix finds the linear relations associated with matrix singularities forms integer powers of a square matrix calculates a positive semi-definite approximation of a non-positive semi-definite symmetric matrix copies values and row/column labels from a matrix to variates and texts

Tables can be formed containing summaries of values in variates: totals, minimum and maximum values, quantiles, numbers of missing and non-missing values, means and variances. Manipulations of multi-way structures include the ability to add various types of marginal summaries to tables, and to combine “slices” of tables, of matrices or of variates.

    `TABULATE` forms tables of summaries of the values of a variate calculates or deletes margins of tables combines or omits “slices” of tables, matrices or variates

Procedures in the Library for operating on tables include:

    `BACKTRANSFORM` calculates back-transformed means with approximate standard errors and confidence intervals gives robust identification of multiple outliers in 2-way tables tabulates data classified by multiple-response factors expresses the body of a table as percentages of one of its margins bootstraps data from random surveys performs generalized calibration of survey data fits generalized linear models to survey data modifies survey weights adjusting to ensure that their overall sum weights remains unchanged constructs stratified random samples analyses stratified random surveys by expansion or ratio raising tabulates data from random surveys, including multistage surveys and surveys with unequal probabilities of selection forms survey weights inserts the contents of a sub-table into a table forms summary tables of modes of values sorts tables so their margins are in ascending or descending order combines several tables into a single table expresses tables as percentages of control cells forms a variate and set of classifying factors from a table

Directives are available for adding and removing branches of trees, and to assist in the construction and use of trees.

    `BASSESS` assesses potential splits for regression and classification trees cuts a tree at a defined node, discarding nodes and information below it identifies specimens using a tree extends a tree by joining another tree to a terminal node adds new branches to a node of a tree

There are also procedures for displaying and pruning trees. These are provide basic utilities for tree-based analysis, and are used by the existing procedures for classification trees, identification keys and regression trees (`BCLASSIFICATION`, `BKEY` and `BREGRESSION`).

    `BCONSTRUCT` constructs a tree plots a tree displays a tree prunes a tree using minimal cost complexity

Formulae and expressions can be interpreted, revised or constructed automatically from the contents of pointers.

    `FARGUMENTS` forms lists of arguments involved in an expression forms classification sets for the terms in a formula or breaks a formula up into separate formulae (one for each term) modifies a formula or an expression to operate on a different set of data structures forms a model formula using structures supplied in a pointer

Values can be assigned to dummies and pointers.

    `ASSIGN` sets values of dummies and pointers

Aspects of the “environment” of the current job can be modified, such as whether or not Genstat starts output from a statistical analysis at the top of a new page, or whether it should pause during interactive output. New defaults can be set for options and parameters. Details of the environmental settings can be copied into Genstat data structures. Attributes of data structures can also be accessed.

    `SET` sets details of the “environment” of a Genstat job sets or modifies defaults of options of Genstat directives or procedures sets or modifies defaults of parameters of Genstat directives or procedures gets details of the “environment” of a Genstat job accesses attributes of data structures forms the name of a structure according to its `IPRINT` attribute

There are also various specialist mathematical facilities.

 `BPCONVERT` converts bit patterns between integers, pointers of set bits and textual descriptions forms the Pareto optimal set of non-dominated groups forms addition and multiplication tables for a Galois finite field converts integers between base 10 and other bases forms all possible permutations of the integers 1…n decomposes a positive integer into its constituent prime powers

And there are games.

 `BINGO ` can be used to set up and then play a game of bingo `FRUITMACHINE ` runs a fruit machine using pop-up menus and Genstat graphics `LIFE ` plays John Conway’s Game of Life `NOUGHTSANDCROSSES ` plays a game of noughts and crosses

Updated on February 7, 2023