2
$\begingroup$

I am trying to replicate some steps in Jonathon D. Brown's text "Linear Models in Matrix Form: A Hands-On Approach for the Behavioral Sciences" Brown, 2014, p.12. I am creating a "matrix of deviate scores" and I am bothered that I am using the Transpose command twice:

X = {{1, 2, 6}, {4, 8, -4}, {5, 6, 5}, {4, 2, 3}}
N[Transpose[Transpose[X] - Mean[X]]]

I realize this is artificial; but I am trying to "color within the lines" of the theme of "Matrix Form". I muddled about with code such as:

onesMat = ConstantArray[1, {4, 3}]
xmeans = N[Mean[X]]
Xmeans = Transpose[Transpose[onesMat]*xmeans]
X - Xmeans

However, the former code snippet is briefer. But, still, I seem to be artificially transposing the covariate matrix into a 3 row by 4 column matrix in order to subtract the means and then transposing again to regain the 4 x 3 covariate matrix structure. Is this just what it is? Or am I abusing Mathematics's syntax for matrices and vectors?

$\endgroup$
  • 3
    $\begingroup$ Does Standardize[X, Mean, 1 &] do what you want? This is a pretty common enough operation that they provided this function for the purpose. $\endgroup$ – J. M. will be back soon Mar 14 at 1:42
  • 2
    $\begingroup$ # - Mean[X] & /@ X // N or TranslationTransform[-Mean[X]] /@ X // N? $\endgroup$ – kglr Mar 14 at 1:49
  • $\begingroup$ Thank you both. @J M you have help me understand a feature of the Standardize command that I did not get previously and @kglr you helped remember a syntax I have used before (but neglected) and introduced me to a command (TranslationTransform) that I do not grok at all! $\endgroup$ – BeanSith Mar 14 at 3:27
  • 1
    $\begingroup$ @J.M. Usually, I would try something like X - ConstantArray[Mean[X], Length[X]] but Standardize seems to be about 30% faster. That is good to know! $\endgroup$ – Henrik Schumacher Mar 14 at 6:36
  • 1
    $\begingroup$ @BeanSith If you plan that to apply to large datasets, keep in mind to apply N first in order to avoid expensive exact precision computations. $\endgroup$ – Henrik Schumacher Mar 14 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.