Multivariate Regression with different predictors for each variable in R
<p>I have a matrix <code>Y</code> of time series. The number of rows is the number of observations. I also have a matrix of predictors <code>X</code>. I want to regress columns of <code>Y</code> on predictors specific to these columns.
A logical matrix of 1's and 0's shows if the <code>i</code>-th variable should be regressed on the <code>j</code>-th predictor.</p>

<p>For instance:</p>

<pre><code>y1 &lt;- c(1, 2, 3, 5, 7, 3, 2, 1)
y2 &lt;- c(5, 1, 3, 1, 3, 4, 5, 3)
y3 &lt;- c(1, 3, 4, 5, 1, 2, 1, 2)
x1 &lt;- c(2, 5, 4, 2, 1, 3, 6, 7)
x2 &lt;- c(1, 5, 1, 3, 4, 2, 1, 3)

Y &lt;- cbind(y1, y2, y3)
X &lt;- cbind(x1, x2)

logical &lt;- matrix(c(1,1,0,1,0,1), 3, 2)
lm &lt;- lm(Y ~ ??? - 1)
</code></pre>

<p>Here, <code>y1</code> should be regressed on <code>x1</code> and <code>x2</code>, <code>y2</code> on <code>x1</code> only and <code>y3</code> on <code>x2</code> only.</p>

<p>I have tried to specify the model in the <code>lm</code> function but I have not been able to do so. In the <code>$coefficients</code> result, I would like "0"'s when a variable is not regressed on the <code>j</code>-th predictor.</p>

<p>Is it possible to do so without heavy dirty loops?</p>