7
$\begingroup$

I know that LinearModelFit can generate a model of a single response variable as a function of a set in input variables. Does Mathematica provide support for matrix regression, i.e. multiple inputs, multiple response variables?

$\endgroup$
5
  • $\begingroup$ i.e. something equivalent to the function lm() in R $\endgroup$
    – JJM
    Jul 26, 2016 at 20:14
  • 4
    $\begingroup$ Could you show some data and a model as an example? $\endgroup$
    – MarcoB
    Jul 26, 2016 at 21:18
  • 2
    $\begingroup$ LinearModelFit[{m,v}] constructs a linear model from the design matrix m and response vector v. $\endgroup$
    – Sander
    Jul 27, 2016 at 2:05
  • $\begingroup$ LinearModelFit complains if v is not a list of real numbers, so if you mean is there a built-in function that does the type of multivariate regression discussed here, I think the answer is no. At least I couldn't find one. $\endgroup$
    – Michael E2
    May 18, 2018 at 21:41
  • 1
    $\begingroup$ For instance in R, one can analyze the linear relationship of several response variables to several predictor variables: E.g. mlm1 <- lm(cbind(TOT, AMI) ~ GEN + AMT + PR + DIAP + QRS, data = ami_data), from here, where the responses are TOT, AMI and the predictors are GEN, AMT, PR, DIAP, QRS. $\endgroup$
    – Michael E2
    May 18, 2018 at 22:03

1 Answer 1

5
$\begingroup$

While the terms multivariate and multiple are sometimes confused, in regression, those who make a distinction tend to call many outputs multivariate and many inputs multiple. LinearModelFit does univariate multiple regression, but not multivariate regression.

Here is the example from the U.Va. site I linked in a comment:

amiData = Import["http://static.lib.virginia.edu/statlab/materials/data/ami_data.DAT"]

Each row consists of values for the variables

{TOT, AMI, GEN, AMT, PR, DIAP, QRS}

The problem is to analyze the linear relationship between the response {TOT, AMI} and predictors {GEN, AMT, PR, DIAP, QRS}. The underlying model consists of two linear equations, which are the same whether doing univariate and multivariate analysis. Thus they can be obtained from separate calls to LinearModelFit:

(* individual univariate regressions *)
vars = {GEN, AMT, PR, DIAP, QRS};
totLM = LinearModelFit[amiData[[All, {3, 4, 5, 6, 7, 1}]], vars, vars]
amiLM = LinearModelFit[amiData[[All, {3, 4, 5, 6, 7, 2}]], vars, vars]

The coefficients of the linear model may be obtained from each of these linear models or in a multivariate way via LeastSquares[]:

{totLM["BestFitParameters"], amiLM["BestFitParameters"]} // Transpose
LeastSquares[  (* multivariate approach to the regression equation *)
 PadLeft[N@amiData[[All, {3, 4, 5, 6, 7}]], {Automatic, 6}, 1.],
 amiData[[All, {1, 2}]]
 ]
(*
{{-2879.48, -2728.71}, {675.651, 763.03}, {0.284851, 
  0.306373}, {10.2721, 8.8962}, {7.25117, 7.20556}, {7.59824, 4.98705}}

{{-2879.48, -2728.71}, {675.651, 763.03}, {0.284851, 
  0.306373}, {10.2721, 8.8962}, {7.25117, 7.20556}, {7.59824, 4.98705}}
*)

So far, so good. If that's all you want, then you can use separate calls to LinearModelFit to do independent univariate multiple regressions.

However, if you want to use Multivariate ANOVA (MANOVA) to compare variables across models, then I think there is no support for that. The ANOVA package does not seem to handle MANOVA nor does LinearModelFit. However, this Wikipedia article indicates that Mathematica can handle MANOVA. Perhaps they mean if you write the code yourself. This Community discussion is about the OP's question, but I don't think it really addresses MANOVA.

You can use RLink to call R. The car package contains the Manova command. I had to follow the protocol here to get Mathematica and R to play nicely together on my Mac. I was able to go through the U.Va. example, but the R output is translated back into Mathematica with lots of inconvenient RObject[..] wrappers. To parse it, I had to look simultaneously at R, which knows how to interpret the data structure and displays it in tables with meaningful headers. Somewhat of a headache. If I really needed to do this a lot, I'd write my own Mathematica functions.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.