[R] Loop with string variable AND customizable "summary" output
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Jan 29 20:30:07 CET 2007
Note that the nlme solution seems to give the same coefficients
but appears to use a single error term rather than one error
term per level of the conditioning variable and that would change various
other statistics relative to the other solutions should that matter.
> summary(lmList(uptake ~ conc | Treatment, CO2))
Call:
Model: uptake ~ conc | Treatment
Data: CO2
Coefficients:
(Intercept)
Estimate Std. Error t value Pr(>|t|)
nonchilled 22.01916 2.46416 8.935769 1.174616e-13
chilled 16.98142 2.46416 6.891361 1.146556e-09
conc
Estimate Std. Error t value Pr(>|t|)
nonchilled 0.01982458 0.004692544 4.224699 6.292679e-05
chilled 0.01563659 0.004692544 3.332221 1.306259e-03
Residual standard error: 8.945667 on 80 degrees of freedom
On 1/29/07, Marc Schwartz <marc_schwartz at comcast.net> wrote:
> Or, to throw yet another couple of possibilities into the mix:
>
> lapply(split(YourDF, YourDF$country),
> function(x) summary(lm(y ~ x, data = x))
>
> and:
>
> library(nlme)
> summary(lmList(y ~ x | country, YourDF))
>
>
> See ?split and help(lmList, package = nlme)
>
> HTH,
>
> Marc Schwartz
>
> On Mon, 2007-01-29 at 09:03 -0800, Bert Gunter wrote:
> > Prior answers are certainly correct, but this is where lists and lapply
> > shine:
> >
> > result<-lapply(list(UK,USA),function(z)summary(lm(y~x,data=z)))
> >
> > As in (nearly) all else, simplicity is a virtue.
> >
> > If you prefer to keep the data sources as a character vector,dataNames,
> >
> > result<-lapply(dataNames,function(z)summary(lm(y~x,data=get(z))))
> >
> > should work.
> >
> > Note: both of these are untested for the general case where they might be
> > used within a function and may not find the right z unless you pay attention
> > to scope, especially in the get() construction.
> >
> >
> > Bert Gunter
> > Genentech Nonclinical Statistics
> > South San Francisco, CA 94404
> >
> >
> > -----Original Message-----
> > From: r-help-bounces at stat.math.ethz.ch
> > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of C.Rosa at lse.ac.uk
> > Sent: Monday, January 29, 2007 8:23 AM
> > To: liuwensui at gmail.com; bcarvalh at jhsph.edu; Roger.Bivand at nhh.no
> > Cc: r-help at stat.math.ethz.ch
> > Subject: Re: [R] Loop with string variable AND customizable "summary" output
> >
> > Dear All,
> > Thank you very much for your help!
> > Carlo
> >
> > -----Original Message-----
> > From: Wensui Liu [mailto:liuwensui at gmail.com]
> > Sent: Mon 29/01/2007 15:39
> > To: Rosa,C
> > Cc: r-help at stat.math.ethz.ch
> > Subject: Re: [R] Loop with string variable AND customizable "summary" output
> >
> > Carlo,
> >
> > try something like:
> >
> > for (i in c("UK","USA"))
> > {
> > summ<-summary(lm(y ~ x), subset = (country = i))
> > assign(paste('output', i, sep = ''), summ);
> > }
> >
> > (note: it is untested, sorry).
> >
> > On 1/29/07, C.Rosa at lse.ac.uk <C.Rosa at lse.ac.uk> wrote:
> > > Dear All,
> > >
> > > I am using R for my research and I have two questions about it:
> > >
> > > 1) is it possible to create a loop using a string, instead of a numeric
> > vector? I have in mind a specific problem:
> > >
> > > Suppose you have 2 countries: UK, and USA, one dependent (y) and one
> > independent variable (y) for each country (vale a dire: yUK, xUK, yUSA,
> > xUSA) and you want to run automatically the following regressions:
> > >
> > >
> > >
> > > for (i in c("UK","USA"))
> > >
> > > output{i}<-summary(lm(y{i} ~ x{i}))
> > >
> > >
> > >
> > > In other words, at the end I would like to have two objects as output:
> > "outputUK" and "outputUSA", which contain respectively the results of the
> > first and second regression (yUK on xUK and yUSA on xUSA).
> > >
> > >
> > >
> > > 2) in STATA there is a very nice code ("outreg") to display nicely (and as
> > the user wants to) your regression results.
> > >
> > > Is there anything similar in R / R contributed packages? More precisely, I
> > am thinking of something that is close in spirit to "summary" but it is also
> > customizable. For example, suppose you want different Signif. codes: 0
> > '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 or a different format display
> > (i.e. without "t value" column) implemented automatically (without manually
> > editing it every time).
> > >
> > > In alternative, if I was able to see it, I could modify the source code of
> > the function "summary", but I am not able to see its (line by line) code.
> > Any idea?
> > >
> > > Or may be a customizable regression output already exists?
> > >
> > > Thanks really a lot!
> > >
> > > Carlo
> > >
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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