# margmean

Estimate marginal means

## Description

## Examples

### Compute Marginal Means Grouped by Two Factors

Load the sample data.

`load repeatedmeas`

The table `between`

includes the between-subject variables age, IQ, group, gender, and eight repeated measures `y1`

to `y8`

as responses. The table `within`

includes the within-subject variables `w1`

and `w2`

. This is simulated data.

Fit a repeated measures model, where the repeated measures `y1`

to `y8`

are the responses, and age, IQ, group, gender, and the group-gender interaction are the predictor variables. Also specify the within-subject design matrix.

rm = fitrm(between,'y1-y8 ~ Group*Gender + Age + IQ','WithinDesign',within);

Compute the marginal means grouped by the factors `Group`

and `Gender`

.

M = margmean(rm,{'Group' 'Gender'})

`M=`*6×6 table*
Group Gender Mean StdErr Lower Upper
_____ ______ _______ ______ ________ _______
A Female 15.946 5.6153 4.3009 27.592
A Male 8.0726 5.7236 -3.7973 19.943
B Female 11.758 5.7091 -0.08189 23.598
B Male 2.2858 5.6748 -9.483 14.055
C Female -8.6183 5.871 -20.794 3.5574
C Male -13.551 5.7283 -25.431 -1.6712

Display the description for table M.

M.Properties.Description

ans = 'Estimated marginal means Means computed with Age=13.7, IQ=98.2667'

### Compute Estimated Marginal Means and Confidence Intervals

Load the sample data.

`load fisheriris`

The column vector, `species`

, consists of iris flowers of three different species, setosa, versicolor, virginica. The double matrix `meas`

consists of four types of measurements on the flowers, the length and width of sepals and petals in centimeters, respectively.

Store the data in a table array.

t = table(species,meas(:,1),meas(:,2),meas(:,3),meas(:,4),... 'VariableNames',{'species','meas1','meas2','meas3','meas4'}); Meas = dataset([1 2 3 4]','VarNames',{'Measurements'});

Fit a repeated measures model, where the measurements are the responses and the species is the predictor variable.

rm = fitrm(t,'meas1-meas4~species','WithinDesign',Meas);

Compute the marginal means grouped by the factor species.

`margmean(rm,'species')`

`ans=`*3×5 table*
species Mean StdErr Lower Upper
______________ ______ ________ ______ ______
{'setosa' } 2.5355 0.042807 2.4509 2.6201
{'versicolor'} 3.573 0.042807 3.4884 3.6576
{'virginica' } 4.285 0.042807 4.2004 4.3696

`StdError`

field shows the standard errors of the estimated marginal means. The `Lower`

and `Upper`

fields show the lower and upper bounds for the 95% confidence intervals of the group marginal means, respectively. None of the confidence intervals overlap, which indicates that marginal means differ with species. You can also plot the estimated marginal means using the `plotprofile`

method.

Compute the 99% confidence intervals for the marginal means.

margmean(rm,'species','alpha',0.01)

`ans=`*3×5 table*
species Mean StdErr Lower Upper
______________ ______ ________ ______ ______
{'setosa' } 2.5355 0.042807 2.4238 2.6472
{'versicolor'} 3.573 0.042807 3.4613 3.6847
{'virginica' } 4.285 0.042807 4.1733 4.3967

## Input Arguments

`rm`

— Repeated measures model

`RepeatedMeasuresModel`

object

Repeated measures model, returned as a `RepeatedMeasuresModel`

object.

For properties and methods of this object, see `RepeatedMeasuresModel`

.

`vars`

— Variables for which to compute the marginal means

character vector | string scalar | string array | cell array of character vectors

Variables for which to compute the marginal means, specified as a character vector or string
scalar representing the name of a between or within-subjects factor in
`rm`

, or a string array or cell array of character
vectors representing the names of multiple variables. Each between-subjects
factor must be categorical.

For example, if you want to compute the marginal means for the variables Drug and Gender, then you can specify as follows.

**Example: **`{'Drug','Gender'}`

**Data Types: **`char`

| `string`

| `cell`

`alpha`

— Significance level

0.05 (default) | scalar value in the range of 0 to 1

Significance level of the confidence intervals for population marginal
means, specified as a scalar value in the range of 0 to 1. The confidence
level is 100*(1–`alpha`

)%.

For example, you can specify a 99% confidence level as follows.

**Example: **`'alpha',0.01`

**Data Types: **`double`

| `single`

## Output Arguments

`tbl`

— Estimated marginal means

table

Estimated marginal means, returned as a table. `tbl`

contains
one row for each combination of the groups of the variables you specify
in `vars`

, one column for each variable, and the
following columns.

Column name | Description |
---|---|

`Mean` | Estimated marginal means |

`StdErr` | Standard errors of the estimates |

`Lower` | Lower limit of a 95% confidence interval for the true population mean |

`Upper` | Upper limit of a 95% confidence interval for the true population mean |

## Version History

**Introduced in R2014a**

## See Also

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)