10

In R I want to do some regression on multivariate response on all predictors, for univariate response, I know the formula is like

y~., this is to use all predictors to regress y, what if now I face 100 response, I can not type 100 yi like y1+y2+y3...+y4~x, so how to use all predictors to regress multivariate response?

user974270
  • 627
  • 3
  • 8
  • 18
  • 4
    [This answer](http://stats.stackexchange.com/questions/11127/multivariate-multiple-regression-in-r/11132#11132) has an example. – caracal May 29 '12 at 21:11

2 Answers2

13

In R, the multivariate formula is to use cbind() for your Y variable. Thus, the formula would be:

model <- lm(cbind(y1, y2, y3, y4)~x)
gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79
  • Would it be possible to encapsulate the `cbind` method in `as.formula`? I would like to create a formula object that would make use of the `cbind` method. – Konrad Jan 11 '16 at 09:54
  • 1
    @Konrad, this seems to work: `heads <- c(50, 25); tails <- c(50, 75); form <- as.formula(cbind(heads, tails)~c(0, 1)); summary(glm(form, family=binomial))`. Is that what you had in mind? – gung - Reinstate Monica Jan 11 '16 at 17:40
  • Yes, this is what I was looking for. – Konrad Jan 11 '16 at 21:15
1

That's relatively easy if y is a matrix with 100 columns. In that case you do it the same way. For example:

lm(y ~ x)

will do a linear regression of y onto the columns of x.

gui11aume
  • 2,768
  • 3
  • 18
  • 23
  • This just fits a separate regression for each column of `y` which effectively assumes independence of the columns of $y$ (conditional on $x$) - is there a way to do general multivariate regression with non-independence association structures? I know you can do multivariate regression in `lme4` by doing univariate regression with correlated errors, but is there a package that will let you do general multivariate regression in a more intuitive way? – Macro May 29 '12 at 23:38