[R] Linear model - coefficients
Weidong Gu
anopheles123 at gmail.com
Sun Jun 12 23:32:35 CEST 2011
this may work.
X<-data.frame(sapply(X,function(x) as.factor(x)))
reg3=lm(Y~.,data=X)
dummy.coef(reg3)
Weidong Gu
On Sun, Jun 12, 2011 at 4:55 PM, Robert Ruser <robert.ruser at gmail.com> wrote:
> Hi,
> but I want to get the coefficients for every variables from x1 to x5.
> (x1 was an example)
>
> Robert
>
> 2011/6/12 Jorge Ivan Velez <jorgeivanvelez at gmail.com>:
>> Hi Robert,
>>
>> Try this:
>> reg2 <- lm( Y ~ factor(x1) + factor(x2) + factor(x3) + factor(x4) +
>> factor(x5) - 1, data = X )
>> cof(ref2)
>> HTH,
>> Jorge
>>
>> On Sun, Jun 12, 2011 at 4:40 PM, Robert Ruser <> wrote:
>>>
>>> Prof. Ripley, thank you very much for the answer but wanted to get
>>> something else. There is an example and an explanation:
>>>
>>> options(contrasts=c("contr.sum","contr.poly")) # contr.sum uses ‘sum
>>> to zero contrasts’
>>> Y <- c(6,3,5,2,3,1,1,6,6,6,7,4,1,6,6,6,6,1)
>>> X <- structure(list(x1 = c(2L, 3L, 1L, 3L, 3L, 2L, 1L, 1L, 3L, 2L,
>>> 3L, 2L, 1L, 1L, 2L, 1L, 2L, 3L), x2 = c(3L, 3L, 2L, 3L, 1L, 3L,
>>> 2L, 3L, 2L, 1L, 2L, 2L, 3L, 1L, 2L, 1L, 1L, 1L), x3 = c(1L, 1L,
>>> 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L
>>> ), x4 = c(1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
>>> 1L, 2L, 2L, 1L, 2L), x5 = c(1L, 1L, 2L, 2L, 3L, 3L, 3L, 2L, 2L,
>>> 2L, 1L, 3L, 3L, 1L, 1L, 1L, 2L, 3L)), .Names = c("x1", "x2",
>>> "x3", "x4", "x5"), row.names = c(NA, 18L), class = "data.frame")
>>>
>>> reg <- lm( Y ~ factor(X$x1) + factor(X$x2) + factor(X$x3) +
>>> factor(X$x4) + factor(X$x5) )
>>> coef(reg)
>>>
>>> and e.g. I get two coefficients for variable x1 (3-levels variable)
>>> but I would like to get the third. Of course I can calculate a3=
>>> -(a1+a2) where a1 and a2 are coefficients of the variable x1.
>>>
>>> I hope that I manage to explain my problem.
>>>
>>> Robert
>>>
>>> 2011/6/12 Prof Brian Ripley <>:
>>> > ?dummy.coef
>>> >
>>> > (NB: 'R' does as you tell it, and if you ask for the default contrasts
>>> > you
>>> > get coefficients a2 and a3, not a1 and a2. So perhaps you did something
>>> > else and failed to tell us? And see the comment in ?dummy.coef about
>>> > treatment contrasts.)
>>> >
>>> >
>>> > On Sun, 12 Jun 2011, Robert Ruser wrote:
>>> >
>>> >> Dear R Users,
>>> >> Using lm() function with categorical variable R use contrasts. Let
>>> >> assume that I have one X independent variable with 3-levels. Because R
>>> >> estimate only 2 parameters ( e.g. a1, a2) the coef function returns
>>> >> only 2 estimators. Is there any function or trick to get another a3
>>> >> values. I know that using contrast sum (?contr.sum) I could compute a3
>>> >> = -(a1+a2). But I have many independent categorical variables and I'm
>>> >> looking for a fast solution.
>>> >>
>>> >> Robert
>>> >>
>>> >> ______________________________________________
>>> >> 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.
>>> >>
>>> >
>>> > --
>>> > Brian D. Ripley, ripley at stats.ox.ac.uk
>>> > Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
>>> > University of Oxford, Tel: +44 1865 272861 (self)
>>> > 1 South Parks Road, +44 1865 272866 (PA)
>>> > Oxford OX1 3TG, UK Fax: +44 1865 272595
>>> >
>>>
>>> ______________________________________________
>>> 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.
>>
>>
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list