[R] how to create this design matrix?

Charles C. Berry cberry at tajo.ucsd.edu
Thu Nov 16 06:16:32 CET 2006


On Wed, 15 Nov 2006, Michael wrote:

> How about I make a design matrix as follows:
>
> 1  d1,1  0   0     0   0     0   0   0
> 0    0   1   d1,2  0   0     0   0   0
> 0    0   0   0     1   d1,3  0   0   0
> ...
> ...
>
> 0    0    0  0     0   0  ...  ... 1  d1,12
>
>
> The above matrix will work for the 1st row of Y data;
>
> d1, 1 means the 1st column of the 1st row of data;
> d1, 12 means the 12th column of the 1st row of data.
>

And 'd1,1' might not equal 'd1,2' , etc ???

If so, this is decidedly different than what you described below where you 
say that there is a common regressor for all 12 regressands (viz. "they 
all regress to the 13th column, which is the predictor, i.e. the X.")

Here, you seem to have a different value of the regressor variable for 
each regressand.

John's suggestion was 'spot on' for the setup in your original posting.

>
> But now since I have N samples(rows) of Y data; how do I conceptually do a
> 3D design matrix?

As you have it above, if 'D' is an N by 12 matrix,

 	t(matrix(apply(D,1,diag),nr=12))

will produce the even numbered columns and

 	diag(12)[rep(1:12,N),]

will take care of the odd numbered columns. cbind() and the tips in 
this recent thread

 	http://article.gmane.org/gmane.comp.lang.r.general/73730

provide the needed tools to assemble the results in the form you specify 
above.

But I seriously doubt that this is what you need, so please don't use this 
in a regression without assurance from a statistician who understands your 
setup that the implied model is reasonable.

>
> On 11/15/06, John Fox <jfox at mcmaster.ca> wrote:
>>
>> Dear Michael,
>>
>>> -----Original Message-----
>>> From: Michael [mailto:comtech.usa at gmail.com]
>>> Sent: Wednesday, November 15, 2006 2:40 PM
>>> To: John Fox
>>> Cc: R-help at stat.math.ethz.ch
>>> Subject: Re: [R] how to create this design matrix?
>>>
>>> There are 12 response variables, columns 1 to 12 are response
>>> variables, i.e., these are y's, they all regress to the 13th
>>> column, which is the predictor, i.e. the X.
>>>
>>
>> Right.
>>
>>> Let's take column 1, call this Y1, and there are n rows(n
>>> samples) of it,
>>>
>>> I need Y1= b0_1 + b1_1* X + epsilon, where X is the 13th column
>>>
>>> Similarly, for column 1 to column 12, we do the above,
>>>
>>> Y12= b0_12 + b1_12 * X + epsilon, where Y12 is the 12th column,
>>>
>>> they all have different b0's and b1's.
>>
>> Right.
>>
>>> Totally there are 24 b0's and b1's.
>>>
>>
>> Yes.
>>
>>> I want a group regression, not separated regression...
>>>
>>
>> I'm not sure what you mean by a "group regression" rather than "separated
>> regressions." The multivarite linear regression that I suggested will give
>> you 12 slopes and 12 intercepts. They are exactly what you'd get from 12
>> individual least-squares regression of each Y on X, but the multivariate
>> regression can also give you, e.g., the covariances among all of the
>> coefficients (if you want them).
>>
>> John
>>
>>> Thanks
>>>
>>>
>>>
>>>
>>> On 11/15/06, John Fox < jfox at mcmaster.ca> wrote:
>>>
>>>       Dear Michael,
>>>
>>>       This looks like a multivariate simple regression --
>>> that is, 12 response
>>>       variables, one predictor. If the data are in the matrix
>>> X, then lm(X[,1:12]
>>>       ~ X[,13]) should do the trick.
>>>
>>>       I hope this helps,
>>>       John
>>>
>>>       --------------------------------
>>>       John Fox
>>>       Department of Sociology
>>>       McMaster University
>>>       Hamilton, Ontario
>>>       Canada L8S 4M4
>>>       905-525-9140x23604
>>>       http://socserv.mcmaster.ca/jfox
>>>       --------------------------------
>>>
>>>      > -----Original Message-----
>>>      > From: r-help-bounces at stat.math.ethz.ch
>>>      > [mailto: r-help-bounces at stat.math.ethz.ch
>>> <mailto:r-help-bounces at stat.math.ethz.ch> ] On Behalf Of Michael
>>>      > Sent: Wednesday, November 15, 2006 12:23 AM
>>>      > To: R-help at stat.math.ethz.ch
>>>      > Subject: [R] how to create this design matrix?
>>>      >
>>>      > Hi all,
>>>      >
>>>      > I have a multiple-linear regression problem.
>>>      >
>>>      > There are 13 columns of data, the whole data matrix is:  n x
>>>      > 13, where n is the number of samples.
>>>      >
>>>      > Now I want to regress  EACH of the first 12 columns onto the
>>>      > 13th column, with 2-parameter linear model  y_i = b0 + b1 *
>>>      > x_i, where i goes from 1 to n, and b0 is the intercept.
>>>      >
>>>      > How do I create a design matrix to do the 12-column
>>>      > regression collectively all at once using multiple
>>> linear regressions?
>>>      >
>>>      > Thanks a lot
>>>      >
>>>      >       [[alternative HTML version deleted]]
>>>      >
>>>      > ______________________________________________
>>>      > R-help at stat.math.ethz.ch mailing list
>>>      > https://stat.ethz.ch/mailman/listinfo/r-help
>>>      > PLEASE do read the posting guide
>>>      > http://www.R-project.org/posting-guide.html
>>> < http://www.R-project.org/posting-guide.html>
>>>      > and provide commented, minimal, self-contained,
>>> reproducible code.
>>>
>>>
>>>
>>>
>>>
>>
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

Charles C. Berry                        (858) 534-2098
                                          Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	         UC San Diego
http://biostat.ucsd.edu/~cberry/         La Jolla, San Diego 92093-0717



More information about the R-help mailing list