Re: [R] Help with contrasts

From: Guelman, Leo <leo.guelman_at_rbc.com>
Date: Wed, 11 May 2011 16:06:57 -0400

Or alternatively (though very similar to Peter's idea) you can do

ci <- contrasts
formals(ci)$contrasts <- FALSE
dd <- data.frame(a = gl(3,4), b = gl(4,1,12)) mm <- model.matrix(~ a + b, dd, contrasts = list(a=ci(dd$a), b=ci(dd$b)))

Best,
Leo.

-----Original Message-----
From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On Behalf Of peter dalgaard
Sent: 2011, May, 11 7:31 AM
To: Lars Bishop
Cc: R-help_at_r-project.org Help
Subject: Re: [R] Help with contrasts

On May 11, 2011, at 12:51 , Lars Bishop wrote:

> Hi,
>
> I need to build a function to generate one column for each level of a
> factor in the model matrix created on an arbitrary formula (instead of

> using the available contrasts options such as contr.treatment,
contr.SAS, etc).
>
> My approach to this was first to use the built-in function for
> contr.treatment but changing the default value of the contrasts
> argument to FALSE (I named this function "contr.identity" and it shown

> at the bottom of the email for reference).
>
> So this function works fine,
>

>> contr.identity(4)

>
> 1 2 3 4
> 1 1 0 0 0
> 2 0 1 0 0
> 3 0 0 1 0
> 4 0 0 0 1
>
>> contr.treatment(4)

> 2 3 4
> 1 0 0 0
> 2 1 0 0
> 3 0 1 0
> 4 0 0 1
>
> However, when I try to create a model matrix using contr.identity
> specified in options(), it actually uses the contr.treatment option.
> Why is that? Any hint on how can I do this?

It's not actually using contr.treatment, it's just calling contr.identity with contrasts=TRUE...

I don't think there's a painless way to avoid this. The closest I can think of is

cI <- contr.treatment
formals(cI)$contrasts <- FALSE
dd <- data.frame(a = gl(3,4), b = gl(4,1,12)) model.matrix(~ C(a,cI,3) + C(b,cI,4), dd)

--
Peter Dalgaard
Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000
Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes_at_cbs.dk  Priv: PDalgd_at_gmail.com

______________________________________________
R-help_at_r-project.org 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.
_______________________________________________________________________

This e-mail may be privileged and/or confidential, and the sender does not waive
any related rights and obligations. Any distribution, use or copying of this e-mail or the information
it contains by other than an intended recipient is unauthorized.
If you received this e-mail in error, please advise me (by return e-mail or otherwise) immediately.

Ce courriel peut contenir des renseignements protégés et confidentiels.
L’expéditeur ne renonce pas aux droits et obligations qui s’y rapportent.
Toute diffusion, utilisation ou copie de ce courriel ou des renseignements qu’il contient
par une personne autre que le destinataire désigné est interdite.
Si vous recevez ce courriel par erreur, veuillez m’en aviser immédiatement, 
par retour de courriel ou par un autre moyen.
______________________________________________
R-help_at_r-project.org 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.
Received on Wed 11 May 2011 - 20:09:54 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 11 May 2011 - 20:40:07 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive