MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Efficient creation of regression design matrix

  • To: mathgroup at smc.vnet.net
  • Subject: [mg82306] Re: [mg82235] Efficient creation of regression design matrix
  • From: Sseziwa Mukasa <mukasa at jeol.com>
  • Date: Wed, 17 Oct 2007 04:04:14 -0400 (EDT)
  • References: <200710160720.DAA08572@smc.vnet.net>

On Oct 16, 2007, at 3:20 AM, Coleman, Mark wrote:

> Hi,
>
> I'm searching for an efficient bit of code to create a design  
> matrix of
> 1's and 0's computed from  categorical (non-numeric) variables,  
> suitable
> for use in regression problems. More precisely, imagine one has an  
> n x 1
> vector of k different non-numeric values. For argument sakes, let
> k={Red,Blue,Green,Yellow}. I would like to create an n x k matrix
> consisting of 1's and 0's, where a '1' appears in the row and column
> location corresponding to the presence of an element of k. For  
> example,
> say the original data is
>
> Red
> Blue
> Blue
> Yellow
> Red
> Green

In[22]:= designMatrix[v_,k_]:=Transpose[Replace[v,{#->1,Except[#]->0}, 
{1}]&/@k]
In[23]:= MatrixForm[designMatrix[{Red,Blue,Blue,Yellow,Red,Green}, 
{Red,Blue,Green,Yellow}]]
Out[23]//MatrixForm=
1	0	0	0
0	1	0	0
0	1	0	0
0	0	0	1
1	0	0	0
0	0	1	0

Regards,

Ssezi


  • Prev by Date: Re: ColorData etc.
  • Next by Date: Re: Releasing several Holds simultaneously
  • Previous by thread: Efficient creation of regression design matrix
  • Next by thread: Re: Efficient creation of regression design matrix