Francesco Nutini
2011-May-20 09:17 UTC
[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]]
?summary produces r^2 in 2nd to last line, as in,> set.seed(12); a=rnorm(100); b = runif(100); c = factor(rep(c('No', > 'Yes'),50)); df = data.frame(a,b,c) > head(df)a b c 1 -1.4805676 0.9729927 No 2 1.5771695 0.2172974 Yes 3 -0.9567445 0.5205087 No 4 -0.9200052 0.8279428 Yes 5 -1.9976421 0.9641110 No 6 -0.2722960 0.6318801 Yes> mod = lm(a ~ b*c) > summary(mod)Call: lm(formula = a ~ b * c) Residuals: Min 1Q Median 3Q Max -1.8196 -0.4754 -0.0246 0.5585 2.0941 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 0.2293 0.2314 0.991 0.324 b -0.4226 0.3885 -1.088 0.280 cYes 0.1578 0.3202 0.493 0.623 b:cYes -0.5878 0.5621 -1.046 0.298 Residual standard error: 0.8455 on 96 degrees of freedom Multiple R-squared: 0.07385, Adjusted R-squared: 0.04491 F-statistic: 2.552 on 3 and 96 DF, p-value: 0.0601 ------------------------------------------ Robert W. Baer, Ph.D. Professor of Physiology Kirksville College of Osteopathic Medicine A. T. Still University of Health Sciences 800 W. Jefferson St. Kirksville, MO 63501 660-626-2322 FAX 660-626-2965 -------------------------------------------------- From: "Francesco Nutini" <nutini.francesco at gmail.com> Sent: Friday, May 20, 2011 4:17 AM To: "[R] help" <r-help at r-project.org> 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. >
Yes Dimitri that's what I mean! Something like this? for(i in levels(c)) { lm(a ~ b * c , data=mydataset)} And what about to see the output? Thanks!> Date: Fri, 20 May 2011 09:46:08 -0400 > Subject: Re: [R] [r] regression coefficient for different factors > From: dimitri.liakhovitski@gmail.com > To: nutini.francesco@gmail.com > CC: rbaer@atsu.edu; r-help@r-project.org > > Francesco, do you just want a separate regression for each level of > your factor c? > You could write a loop - looping through levels of c: > > for(i in levels(c)){ > select your data here and write a regression formula > } > > On Fri, May 20, 2011 at 9:39 AM, Francesco Nutini > <nutini.francesco@gmail.com> wrote: > > > > Thanks for your reply, > > > > ?summary produce a multiple r2. > > My dataset il similar to this one: > > > >> a b c > >> 1 -1.4805676 0.9729927 x > >> 2 1.5771695 0.2172974 x > >> 3 -0.9567445 0.5205087 x > >> 4 -0.9200052 0.8279428 z > >> 5 -1.9976421 0.9641110 z > >> 6 -0.2722960 0.6318801 y > > > > So, I would like to know the r2 for a~b for every factors levels. > > Off course I can made the regression separately for every factors, but my dataset have 68 factors... > > > > ---------- > > Francesco Nutini > > PhD student > > CNR-IREA (Institute for Electromagnetic Sensing of the Environment) > > Milano, Italy > > > > > From: rbaer@atsu.edu > >> To: nutini.francesco@gmail.com; r-help@r-project.org > >> Subject: Re: [R] [r] regression coefficient for different factors > >> Date: Fri, 20 May 2011 08:07:59 -0500 > >> > >> ?summary > >> > >> produces r^2 in 2nd to last line, as in, > >> > set.seed(12); a=rnorm(100); b = runif(100); c = factor(rep(c('No', > >> > 'Yes'),50)); df = data.frame(a,b,c) > >> > head(df) > >> a b c > >> 1 -1.4805676 0.9729927 No > >> 2 1.5771695 0.2172974 Yes > >> 3 -0.9567445 0.5205087 No > >> 4 -0.9200052 0.8279428 Yes > >> 5 -1.9976421 0.9641110 No > >> 6 -0.2722960 0.6318801 Yes > >> > mod = lm(a ~ b*c) > >> > summary(mod) > >> > >> Call: > >> lm(formula = a ~ b * c) > >> > >> Residuals: > >> Min 1Q Median 3Q Max > >> -1.8196 -0.4754 -0.0246 0.5585 2.0941 > >> > >> Coefficients: > >> Estimate Std. Error t value Pr(>|t|) > >> (Intercept) 0.2293 0.2314 0.991 0.324 > >> b -0.4226 0.3885 -1.088 0.280 > >> cYes 0.1578 0.3202 0.493 0.623 > >> b:cYes -0.5878 0.5621 -1.046 0.298 > >> > >> Residual standard error: 0.8455 on 96 degrees of freedom > >> Multiple R-squared: 0.07385, Adjusted R-squared: 0.04491 > >> F-statistic: 2.552 on 3 and 96 DF, p-value: 0.0601 > >> > >> ------------------------------------------ > >> Robert W. Baer, Ph.D. > >> Professor of Physiology > >> Kirksville College of Osteopathic Medicine > >> A. T. Still University of Health Sciences > >> 800 W. Jefferson St. > >> Kirksville, MO 63501 > >> 660-626-2322 > >> FAX 660-626-2965 > >> > >> > >> -------------------------------------------------- > >> From: "Francesco Nutini" <nutini.francesco@gmail.com> > >> Sent: Friday, May 20, 2011 4:17 AM > >> To: "[R] help" <r-help@r-project.org> > >> 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@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. > >> > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@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. > > > > > > -- > Dimitri Liakhovitski > Ninah Consulting > www.ninah.com[[alternative HTML version deleted]]
Dimitri Liakhovitski
2011-May-20 14:01 UTC
[R] [r] regression coefficient for different factors
First you have to create something (e.g., a list) that holds your output: mylist<-NULL Then you loop through the levels of c and run a regression of a onto b (no need to include c anymore because c will have zero variance within each level of c): for(i in levels(c)){ temp.data<-mydataset[mydataset$c %in% i] mylist[[i]]<-lm(a ~ b, data=temp.data) } Once you are done - you can write another loop (this time across all elements of mylist - that will have as many elements as there are levels in c) and extract the coefficients. Dimitri On Fri, May 20, 2011 at 9:57 AM, Francesco Nutini <nutini.francesco at gmail.com> wrote:> Yes Dimitri that's what I mean! > Something like this? > > for(i in levels(c)) { lm(a ~? b *? c , data=mydataset)} > > And what about to see the output? > > Thanks! > >> Date: Fri, 20 May 2011 09:46:08 -0400 >> Subject: Re: [R] [r] regression coefficient for different factors >> From: dimitri.liakhovitski at gmail.com >> To: nutini.francesco at gmail.com >> CC: rbaer at atsu.edu; r-help at r-project.org >> >> Francesco, do you just want a separate regression for each level of >> your factor c? >> You could write a loop - looping through levels of c: >> >> for(i in levels(c)){ >> select your data here and write a regression formula >> } >> >> On Fri, May 20, 2011 at 9:39 AM, Francesco Nutini >> <nutini.francesco at gmail.com> wrote: >> > >> > Thanks for your reply, >> > >> > ?summary produce a ?multiple r2. >> > My dataset il similar to this one: >> > >> >> ? ? ? ? ? ?a ? ? ? ? b ? c >> >> 1 -1.4805676 0.9729927 x >> >> 2 ?1.5771695 0.2172974 x >> >> 3 -0.9567445 0.5205087 x >> >> 4 -0.9200052 0.8279428 z >> >> 5 -1.9976421 0.9641110 z >> >> 6 -0.2722960 0.6318801 y >> > >> > So, I would like to know the r2 for a~b for every factors levels. >> > Off course I can made the regression separately for every factors, but >> > my dataset have 68 factors... >> > >> > ---------- >> > Francesco Nutini >> > PhD student >> > CNR-IREA (Institute for Electromagnetic Sensing of the Environment) >> > Milano, Italy >> > >> > ?> From: rbaer at atsu.edu >> >> To: nutini.francesco at gmail.com; r-help at r-project.org >> >> Subject: Re: [R] [r] regression coefficient for different factors >> >> Date: Fri, 20 May 2011 08:07:59 -0500 >> >> >> >> ?summary >> >> >> >> produces r^2 in 2nd to last line, as in, >> >> > set.seed(12); a=rnorm(100); b = runif(100); c = factor(rep(c('No', >> >> > 'Yes'),50)); df = data.frame(a,b,c) >> >> > head(df) >> >> ? ? ? ? ? ?a ? ? ? ? b ? c >> >> 1 -1.4805676 0.9729927 ?No >> >> 2 ?1.5771695 0.2172974 Yes >> >> 3 -0.9567445 0.5205087 ?No >> >> 4 -0.9200052 0.8279428 Yes >> >> 5 -1.9976421 0.9641110 ?No >> >> 6 -0.2722960 0.6318801 Yes >> >> > mod = lm(a ~ b*c) >> >> > summary(mod) >> >> >> >> Call: >> >> lm(formula = a ~ b * c) >> >> >> >> Residuals: >> >> ? ? Min ? ? ?1Q ?Median ? ? ?3Q ? ? Max >> >> -1.8196 -0.4754 -0.0246 ?0.5585 ?2.0941 >> >> >> >> Coefficients: >> >> ? ? ? ? ? ? Estimate Std. Error t value Pr(>|t|) >> >> (Intercept) ? 0.2293 ? ? 0.2314 ? 0.991 ? ?0.324 >> >> b ? ? ? ? ? ?-0.4226 ? ? 0.3885 ?-1.088 ? ?0.280 >> >> cYes ? ? ? ? ?0.1578 ? ? 0.3202 ? 0.493 ? ?0.623 >> >> b:cYes ? ? ? -0.5878 ? ? 0.5621 ?-1.046 ? ?0.298 >> >> >> >> Residual standard error: 0.8455 on 96 degrees of freedom >> >> Multiple R-squared: 0.07385, ?Adjusted R-squared: 0.04491 >> >> F-statistic: 2.552 on 3 and 96 DF, ?p-value: 0.0601 >> >> >> >> ------------------------------------------ >> >> Robert W. Baer, Ph.D. >> >> Professor of Physiology >> >> Kirksville College of Osteopathic Medicine >> >> A. T. Still University of Health Sciences >> >> 800 W. Jefferson St. >> >> Kirksville, MO 63501 >> >> 660-626-2322 >> >> FAX 660-626-2965 >> >> >> >> >> >> -------------------------------------------------- >> >> From: "Francesco Nutini" <nutini.francesco at gmail.com> >> >> Sent: Friday, May 20, 2011 4:17 AM >> >> To: "[R] help" <r-help at r-project.org> >> >> 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. >> >> > >> > >> > ? ? ? ?[[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. >> > >> >> >> >> -- >> Dimitri Liakhovitski >> Ninah Consulting >> www.ninah.com >-- Dimitri Liakhovitski Ninah Consulting www.ninah.com
Look at the lmList function in the nlme package, it does what I think you want. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Francesco Nutini > Sent: Friday, May 20, 2011 3:18 AM > 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.
Bill.Venables at csiro.au
2011-May-21 02:34 UTC
[R] [r] regression coefficient for different factors
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.