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