[R] Loop over regression results

David L Carlson dcarlson at tamu.edu
Mon Feb 16 16:17:13 CET 2015


Or for the slopes and t-values:

> do.call(rbind, lapply(mod, function(x) summary(x)[["coefficients"]][2,]))
            Estimate Std. Error  t value     Pr(>|t|)
setosa     0.8371922  0.5049134 1.658091 1.038211e-01
versicolor 1.0536478  0.1712595 6.152348 1.466661e-07
virginica  0.6314052  0.1428938 4.418702 5.647610e-05

David C

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of David L Carlson
Sent: Monday, February 16, 2015 8:52 AM
To: Ronald Kölpin; r-help at r-project.org
Subject: Re: [R] Loop over regression results

In R you would want to combine the results into a list. This could be done when you create the regressions or afterwards. To repeat your example using a list:

data(iris)
taxon <- levels(iris$Species)
mod <- lapply(taxon, function (x) lm(Sepal.Width ~ Petal.Width, 
	data=iris, subset=Species==x))
names(mod) <- taxon
lapply(mod, summary)
coeffs <- do.call(rbind, lapply(mod, coef, "[1"))
coeffs
#             (Intercept) Petal.Width
# setosa        3.222051   0.8371922
# versicolor    1.372863   1.0536478
# virginica     1.694773   0.6314052

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352




-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ronald Kölpin
Sent: Monday, February 16, 2015 7:37 AM
To: r-help at r-project.org
Subject: [R] Loop over regression results

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dear all,

I have a problem when trying to present the results of several
regression. Say I have run several regressions on a dataset and saved
the different results (as in the mini example below). I then want to
loop over the regression results in order so save certain values to a
matrix (in order to put them into a paper or presentation).

Aside from the question of how to access certain information stored by
lm() (or printed by summary()) I can't seem to so loop over lm()
objects -- no matter whether they are stored in a vector or a list.
They are always evaluated immediately when called. I tried quote() or
substitute() but that didn't work either as "Objects of type 'symbol'
cannot be indexed."

In Stata I would simply do something like

forvalues k = 1/3 {
 quietly estimates restore mod`k'
// [...]
}

and I am looking for the R equivalent of that syntax.

Kind regard and thanks

RK


attach(iris)
mod1 <- lm(Sepal.Width ~ Petal.Width, data=iris, subset=Species=="setosa")
mod2 <- lm(Sepal.Width ~ Petal.Width, data=iris,
subset=Species=="versicolor")
mod3 <- lm(Sepal.Width ~ Petal.Width, data=iris,
subset=Species=="virginica")

summary(mod1); summary(mod2); summary(mod3)

mat <- matrix(data=NA, nrow=3, ncol=5,
              dimnames=list(1:3, c("Model", "Intercept", "p(T > |T|)",
"Slope", "R^2")))

mods <- c(mod1, mod2, mod3)

for(k in 1:3)
{
    mod <- mods[k]
    mat[2,k] <- as.numeric(coef(mod))[1]
    mat[3,k] <- as.numeric(coef(mod))[1]
}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQEcBAEBAgAGBQJU4fJnAAoJEKdHe5EUSrVeafwIALerOj+rsZTnbSKOUX6vYpr4
Uqsx0X2g+IgJw0KLdyqnlDmOut4wW6sWExtVgiugo/bkN8g5rDotGAl06d0UYRQV
17aLQqQjI6EGXKV9swwlm2DBphtXCIYUCXnDWUoG4Y2wC/4hDnaLbZ9yJFF1GSjn
+aN/PFf1mPPZLvF1NgMmzLdszP76VYzEgcOcEUfbmB7RU/2WEBLeBYJ8+FD1utPJ
cnh03rSc/0dgvphP8FO47Nj7mbqqhKL76a9oQqJSJiZJoCFCGiDIIgzq7vwGWc4T
9apwC/R3ahciB18yYOSMq7ZkVdQ+OpsqDTodnnIIUZjrVIcn9AI+GE0eq1VdLSE=
=x+gM
-----END PGP SIGNATURE-----

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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 -- To UNSUBSCRIBE and more, see
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