Statistics: grpstats

### Function Reference: `grpstats`

statistics: mean = grpstats (x)
statistics: mean = grpstats (x, group)
statistics: [a, b, …] = grpstats (x, group, whichstats)
statistics: [a, b, …] = grpstats (x, group, whichstats, alpha)

Summary statistics by group. `grpstats` computes groupwise summary statistics, for data in a matrix x. `grpstats` treats NaNs as missing values, and removes them.

`means = grpstats (x, group)`, when X is a matrix of observations, returns the means of each column of x by group. group is a grouping variable defined as a categorical variable, numeric, string array, or cell array of strings. group can be [] or omitted to compute the mean of the entire sample without grouping.

`[a, b, …] = grpstats (x, group, whichstats)`, for a numeric matrix X, returns the statistics specified by whichstats, as separate arrays a, b, …. whichstats can be a single function name, or a cell array containing multiple function names. The number of output arguments must match the number of function names in whichstats. Names in whichstats can be chosen from among the following:

| "mean" | mean
| "median" | median
| "sem" | standard error of the mean
| "std" | standard deviation
| "var" | variance
| "min" | minimum value
| "max" | maximum value
| "range" | maximum - minimum
| "numel" | count, or number of elements
| "meanci" | 95% confidence interval for the mean
| "predci" | 95% prediction interval for a new observation
| "gname" | group name

`[…] = grpstats (x, group, whichstats, alpha)` specifies the confidence level as 100(1-ALPHA)% for the "meanci" and "predci" options. Default value for alpha is 0.05.

Examples:

     load carsmall;
     [m,p,g] = grpstats (Weight, Model_Year, {"mean", "predci", "gname"})
     n = length(m);
     errorbar((1:n)',m,p(:,2)-m)
     set (gca, "xtick", 1:n, "xticklabel", g);
     title ("95% prediction intervals for mean weight by year")

See also: grp2idx

Source Code: grpstats

### Example: 1

     load carsmall;
     [m,p,g] = grpstats (Weight, Model_Year, {"mean", "predci", "gname"})
     n = length(m);
     errorbar((1:n)',m,p(:,2)-m);
     set (gca, "xtick", 1:n, "xticklabel", g);
     title ("95% prediction intervals for mean weight by year");

    m =

       3441.3
       3078.7
       2453.5

    p =

       1847.1   5035.6
       1457.0   4700.4
       1754.1   3153.0

    g =
    {
      [1,1] = 70
      [2,1] = 76
      [3,1] = 82
    }

### Example: 2

     load carsmall;
     [m,p,g] = grpstats ([Acceleration,Weight/1000],Cylinders, ...
                         {"mean", "meanci", "gname"}, 0.05)
     [c,r] = size (m);
     errorbar((1:c)'.*ones(c,r),m,p(:,[(1:r)])-m);
     set (gca, "xtick", 1:c, "xticklabel", g);
     title ("95% prediction intervals for mean weight by year");

    m =

       11.6406    3.9703
       16.6706    2.3726
       16.4765    3.1255

    p =

       11.1762    3.8899   12.1050    4.0506
       16.1385    2.2998   17.2027    2.4454
       16.1622    3.0518   16.7907    3.1992

    g =
    {
      [1,1] = 8
      [2,1] = 4
      [3,1] = 6
    }