[R] [r] regression coefficient for different factors

Dennis Murphy djmuser at gmail.com
Sat May 21 05:20:59 CEST 2011


Here's another option using the plyr package:

library(plyr)
ddply(dat, 'f', function(d) coef(lm(A ~ B, data = d)))

Dennis

On Fri, May 20, 2011 at 7:34 PM,  <Bill.Venables at csiro.au> wrote:
> You have received suggestions about this already, but you may want to consider something like this as an alternative:
>
>> require(english)
>> lev <- as.character(as.english(0:9))
>> dat <- data.frame(f = factor(sample(lev, 500,
> +                   rep=TRUE), levels = lev),
> +                   B = rnorm(500))
>> dat <- within(dat, A <- 2 + 3*B + rnorm(B))
>>
>> ### method 1: using a loop
>> coefs <- sapply(levels(dat$f),
> +                 function(x) coef(lm(A ~ B, dat,
> +                                     subset = f == x)))
>> t(coefs)
>      (Intercept)        B
> zero     1.967234 2.795218
> one      1.864298 3.048861
> two      1.978757 2.893950
> three    2.035777 2.796963
> four     2.092047 2.826677
> five     2.263936 3.229843
> six      1.740911 3.114069
> seven    1.975918 3.090971
> eight    2.064802 3.048225
> nine     2.030697 3.059960
>>
>> ### Greg Snow's suggeston - use lmList
>> require(nlme)
>> coef(lmList(A ~ B | f, dat))
>      (Intercept)        B
> zero     1.967234 2.795218
> one      1.864298 3.048861
> two      1.978757 2.893950
> three    2.035777 2.796963
> four     2.092047 2.826677
> five     2.263936 3.229843
> six      1.740911 3.114069
> seven    1.975918 3.090971
> eight    2.064802 3.048225
> nine     2.030697 3.059960
>>
>
> Bill Venables
> ________________________________________
> From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of Francesco Nutini [nutini.francesco at gmail.com]
> Sent: 20 May 2011 19:17
> To: [R] help
> Subject: [R] [r] regression coefficient for different factors
>
> Dear R-helpers,
>
> In my dataset I have two continuous variable (A and B) and one factor.
> I'm investigating the regression between the two variables usign the command
> lm(A ~ B, ...)
> but now I want to know the regression coefficient (r2) of A vs. B for every factors.
> I know that I can obtain this information with excel, but the factor have 68 levels...maybe [r] have a useful command.
>
> Thanks,
>
> Francesco Nutini
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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