Trajectory Analysis
Group trajectory differences: magnitude, orientation, and shape.
Design matrix
motco.stats.design.get_model_matrix(X, group_col, level_col, full=True)
Build a design (model) matrix for group × level factors.
Coding scheme
- Intercept (column of ones).
- Group main effects: one-hot with drop-first for groups (G-1 columns).
- Level main effects: one-hot with drop-first for levels (L-1 columns).
- If
full=True, include all interaction terms between group and level dummies: (G-1) × (L-1) columns.
The category order is deterministic: sorted by string representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
DataFrame containing |
required |
group_col
|
str
|
Name of the group column. |
required |
level_col
|
str
|
Name of the level/state column. |
required |
full
|
bool
|
Whether to include interaction terms. |
True
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Model matrix with intercept. |
Source code in src/motco/stats/design.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
motco.stats.design.build_ls_means(group_levels, level_levels, full=True)
Generate LS-mean rows for every group × level cell consistent with
get_model_matrix coding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_levels
|
Sequence[str]
|
Sorted group labels; first is baseline. |
required |
level_levels
|
Sequence[str]
|
Sorted level labels; first is baseline. |
required |
full
|
bool
|
Whether to include interaction terms. |
True
|
Returns:
| Type | Description |
|---|---|
ndarray
|
LS-mean design matrix with shape (G×L, 1 + (G-1) + (L-1) + I), where
I = (G-1)×(L-1) if |
Source code in src/motco/stats/design.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
motco.stats.design.center_matrix(dat, group_col, level_col, feature_cols=None)
Center feature columns by per-group means.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dat
|
DataFrame
|
Original, non-centered dataframe. |
required |
group_col
|
str
|
Column in |
required |
level_col
|
str
|
Column in |
required |
feature_cols
|
Sequence[str] | None
|
Feature columns to center. If None, all numeric columns except
|
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A copy of |
Source code in src/motco/stats/design.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
Estimation
motco.stats.trajectory.estimate_betas(X, Y)
Estimate the beta coefficients between an outcome matrix and a model matrix
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Union[DataFrame, ndarray]
|
Model matrix with intercept. |
required |
Y
|
Union[DataFrame, ndarray]
|
Outcome matrix. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
betas |
Union[DataFrame, ndarray]
|
Beta coefficients |
Source code in src/motco/stats/trajectory.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
motco.stats.trajectory.get_observed_vectors(X, Y, group_col, level_col, full=True)
Get LS-mean vectors for each group × level cell.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataFrame
|
DataFrame containing factors |
required |
Y
|
Union[DataFrame, ndarray]
|
Outcome matrix (n_samples × n_features). |
required |
group_col
|
str
|
Group column name in |
required |
level_col
|
str
|
Level/state column name in |
required |
full
|
bool
|
Whether to include interactions in the model. |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
LS means arranged with a MultiIndex (group, level). Columns follow |
Source code in src/motco/stats/trajectory.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
motco.stats.trajectory.estimate_difference(Y, model_matrix, LS_means, contrast)
Estimate parameters angle, delta, and shape given an outcome matrix, model matrix, and contrast to compare. This is a comparison of more than two states.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Y
|
Union[DataFrame, ndarray]
|
Outcome matrix. |
required |
model_matrix
|
Union[DataFrame, ndarray]
|
Model matrix with intercept. |
required |
LS_means
|
Union[DataFrame, ndarray]
|
Least-squares means to estimate. |
required |
contrast
|
list[list[int]]
|
Indices indicating the groups to compare based on LS means. Each list must contain the cohorts that belong to the same group. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
deltas |
ndarray
|
Symmetric matrix (n_groups x n_groups) with differences in magnitude. |
angles |
ndarray
|
Symmetric matrix (n_groups x n_groups) with differences in direction (degrees). |
shapes |
ndarray
|
Symmetric matrix (n_groups x n_groups) with shape distances. |
Notes
See [1]_ for more information on trajectory analysis.
References
.. [1] Adams, Dean C., and Michael L. Collyer. "A general framework for the analysis of phenotypic trajectories in evolutionary studies." Evolution: International Journal of Organic Evolution 63.5 (2009): 1143-1154. https://doi.org/10.1111/j.1558-5646.2009.00649.x
Source code in src/motco/stats/trajectory.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | |
motco.stats.trajectory.pair_difference(dat, group_col, level_col, groups=None, levels=None, feature_cols=None)
Estimate difference in direction (angle, degrees) and magnitude (delta) between two groups across two levels.
The change vector for a group is defined as level1 - level2 over the
selected feature columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dat
|
DataFrame
|
DataFrame containing features plus |
required |
group_col
|
str
|
Column with groups (between-subject factor). |
required |
level_col
|
str
|
Column with levels/states (within-subject factor). |
required |
groups
|
tuple[str, str] | None
|
Pair of group labels to compare. If None, infer and require exactly two. |
None
|
levels
|
tuple[str, str] | None
|
Pair of level labels to use for the change vector. If None, infer and require exactly two. |
None
|
feature_cols
|
Sequence[str] | None
|
Feature columns to use. If None, all numeric columns except |
None
|
Returns:
| Type | Description |
|---|---|
tuple[float, float]
|
(angle_degrees, delta_magnitude_difference) |
Notes
See [1]_ for more information on two-state comparisons.
References
.. [1] Collyer, Michael L., and Dean C. Adams. "Analysis of two‐state multivariate phenotypic change in ecological studies." Ecology 88.3 (2007): 683-692. https://doi.org/10.1890/06-0727
Source code in src/motco/stats/trajectory.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
Permutation test
motco.stats.permutation.RRPP(Y, model_full, model_reduced, LS_means, contrast, permutations=999, n_jobs=None, progress=True, seed=None)
Residual Randomization in a Permutation Procedure to evaluate linear models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Y
|
Union[DataFrame, ndarray]
|
Outcome matrix. |
required |
model_full
|
Union[DataFrame, ndarray]
|
Model matrix for full model, including intercept. |
required |
model_reduced
|
Union[DataFrame, ndarray]
|
Model matrix for reduced model, including intercept. |
required |
LS_means
|
Union[DataFrame, ndarray]
|
Least-squares means to estimate. |
required |
contrast
|
list[list[int]]
|
Indices indicating the groups to compare based on LS means. Each list must contain the cohorts that belong to the same group. |
required |
permutations
|
int
|
Number of permutations. |
999
|
n_jobs
|
Optional[int]
|
If provided and > 1, run permutations in parallel using multiple worker processes. Use -1 to use all available CPUs. When None or 1, runs single-threaded (backward-compatible default). |
None
|
seed
|
Optional[int]
|
Optional seed for reproducible residual randomization. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dist_delta |
list[float]
|
Distribution of deltas. |
dist_angle |
list[float]
|
Distribution of angles. |
Source code in src/motco/stats/permutation.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |