# Re: [R] [r] regression coefficient for different factors

From: Francesco Nutini <nutini.francesco_at_gmail.com>
Date: Fri, 20 May 2011 17:45:10 +0200

I'm running these input:

mylist<-NULL #in order to hold my input
for(i in levels(mydataset\$c)) { temp.data<-mydataset [mydataset\$c %in% i]  mylist[[i]]<- lm(temp.data\$a ~ temp.data\$b , data=temp.data) }

That's the erros returns
Error in `[.data.frame`(mydataset, niger\$site %in% i) : "undefined columns selected"

> Date: Fri, 20 May 2011 10:01:39 -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
>
> 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
> >> 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:
> >> >
> >> >
> >> > ?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)
> >> >> 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
> >> >> > 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
> >> > 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

[[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. Received on Fri 20 May 2011 - 23:06:03 GMT

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

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 Fri 20 May 2011 - 23:30:08 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.