stat is a library which adds to LME advanced statistical functions.
The following statement makes available functions defined in stat:
use stat
Bootstrap estimate.
(stats, samples) = bootstrp(n, fun, D1, ...)
bootstrp(n,fun,D) picks random observations from the rows of matrix (or column vector) D to form n sets which have all the same size as D; then it applies function fun (a function name or reference or an inline function) to each set and returns the results in the columns of stats. Up to three different set of data can be provided.
bootstrp gives an idea of the robustness of the estimate with respect to the choice of the observations.
D = rand(1000, 1); bootstrp(5, @std, D) 0.2938 0.2878 0.2793 0.2859 0.2844
Geometric mean of a set of values.
m = geomean(A) m = geomean(A, dim)
geomean(A) gives the geometric mean of the columns of array A or of the row vector A. The dimension along which geomean proceeds may be specified with a second argument.
The geometric mean of vector v of length n
is defined as
geomean(1:10) 4.5287 mean(1:10) 5.5 exp(mean(log(1:10))) 4.5287
Harmonic mean of a set of values.
m = harmmean(A) m = harmmean(A, dim)
harmmean(A) gives the harmonic mean of the columns of array A or of the row vector A. The dimension along which harmmean proceeds may be specified with a second argument.
The inverse of the harmonic mean is the arithmetic mean of the inverse of the observations.
harmmean(1:10) 3.4142 mean(1:10) 5.5
Interquartile range.
m = iqr(A) m = iqr(A, dim)
iqr(A) gives the interquartile range of the columns of array A or of the row vector A. The dimension along which iqr proceeds may be specified with a second argument.
The interquartile range is the difference between the 75th percentile and the 25th percentile.
iqr(rand(1,1000)) 0.5158
Mean absolute deviation.
m = mad(A) m = mad(A, dim)
mad(A) gives the mean absolute deviation of the columns of array A or of the row vector A. The dimension along which mad proceeds may be specified with a second argument.
The mean absolute deviation is the mean of the absolute value of the deviation between each observation and the arithmetic mean.
mad(rand(1,1000)) 0.2446
Correlation coefficients after discarding NaNs.
S = nancorrcoef(X) S = nancorrcoef(X1, X2)
nancorrcoef(X) calculates the correlation coefficients of the columns of the m-by-n matrix X. NaN values are ignored. The result is a square n-by-n matrix whose diagonal is 1.
nancorrcoef(X1,X2) calculates the correlation coefficients of X1 and X2 and returns a 2-by-2 matrix, ignoring NaN values. It is equivalent to nancorrcoef([X1(:),X2(:)]).
nanmean, nanstd, nancov, corrcoef
Covariance after discarding NaNs.
M = nancov(data) M = nancov(data, 0) M = nancov(data, 1)
nancov(data) returns the best unbiased estimate m-by-m covariance matrix of the n-by-m matrix data for a normal distribution. NaN values are ignored. Each row of data is an observation where n quantities were measured. nancov(data,0) is the same as nancov(data).
nancov(data,1) returns the m-by-m covariance matrix of the n-by-m matrix data which contains the whole population; NaN values are ignored.
nanmean, nanstd, nancorrcoef, cov
Mean after discarding NaNs.
y = nanmean(A) y = nanmean(A, dim)
nanmean(v) returns the arithmetic mean of the elements of vector v. nanmean(A) returns a row vector whose elements are the means of the corresponding columns of array A. nanmean(A,dim) returns the mean of array A along dimension dim; the result is a row vector if dim is 1, or a column vector if dim is 2. In all cases, NaN values are ignored.
nanmean([1,2,nan;nan,6,7]) 1 4 7 nanmean([1,2,nan;nan,6,7],2) 1.5 6.5 nanmean([nan,nan]) nan
Median after discarding NaNs.
y = nanmedian(A) y = nanmedian(A, dim)
nanmedian(v) gives the median of vector v, i.e. the value x such that half of the elements of v are smaller and half of the elements are larger. NaN values are ignored.
nanmedian(A) gives a row vector which contains the median of the columns of A. With a second argument, nanmedian(A,dim) operates along dimension dim.
Sum after discarding NaNs.
y = nansum(A) y = nansum(A, dim)
nansum(v) returns the sum of the elements of vector v. NaN values are ignored. nansum(A) returns a row vector whose elements are the sums of the corresponding columns of array A. nansum(A,dim) returns the sum of array A along dimension dim; the result is a row vector if dim is 1, or a column vector if dim is 2.
Pairwise distance between observations.
d = pdist(M) d = pdist(M, metric) d = pdist(M, metric, p)
pdist calculates the distance between pairs of rows of the observation matrix M. The result is a column vector which contains the distances between rows i and j with i<j. It can be resized to a square matrix with squareform.
By default, the metric used to calculate the distance is the euclidean distance; but it can be specified with a second argument:
'euclid' | euclidean distance |
'seuclid' | standardized euclidean distance |
'mahal' | Mahalanobis distance |
'cityblock' | sum of absolute values |
'minkowski' | Minkowski metric with parameter p |
The standardized euclidean distance is the euclidean distance after each column of M has been divided by its standard deviation. The Minkowski metric is based on the p-norm of vector differences.
pdist((1:3)') 1 2 1 squareform(pdist((1:3)')) 0 1 2 1 0 1 2 1 0 squareform(pdist([1,2,6; 3,1,7;6,1,2])) 0 2.4495 6.4807 2.4495 0 5.831 6.4807 5.831 0
Percentile.
m = prctile(A, prc) m = prctile(A, prc, dim)
prctile(A,prc) gives the smallest values larger than prc percent of the elements of each column of array A or of the row vector A. The dimension along which prctile proceeds may be specified with a third argument.
prctile(rand(1,1000),90) 0.8966
Mean absolute deviation.
m = range(A) m = range(A, dim)
range(A) gives the differences between the maximum and minimum values of the columns of array A or of the row vector A. The dimension along which range proceeds may be specified with a second argument.
range(rand(1,100)) 0.9602
Resize the output of pdist to a square matrix.
D = squareform(d)
squareform(d) resize d, which should be the output of pdist, into a symmetric square matrix D, so that the distance between observations i and j is D(i,j).
Trimmed mean of a set of values.
m = trimmean(A, prc) m = trimmean(A, prc, dim)
trimmean(A,prc) gives the arithmetic mean of the columns of array A or of the row vector A once prc/2 percent of the values have been removed from each end. The dimension along which trimmean proceeds may be specified with a third argument.
trimmean is less sensitive to outliers than the regular arithmetic mean.
prctile, geomean, median, mean
Z score (normalized deviation).
Y = zscore(X) Y = zscore(X, dim)
zscore(X) normalizes the columns of array X or the row vector X by subtracting their mean and dividing by their standard deviation. The dimension along which zscore proceeds may be specified with a second argument.