[R] Speed up computing: looping through data?

Greg Snow Greg.Snow at intermountainmail.org
Tue Jul 17 20:23:21 CEST 2007

There are many options that can help, but which to use depends a lot on
what your research question is.  What are you really looking for in
these relationships?

Here are a couple of tools that may help.

If my.data is a matrix or data.frame with just the variables of interest
look at 1-1/diag(solve(cor(my.data))), it will be a vector of the
R-squared values from the set of regressions you asked about (each
variable regressed on all the others).

Look at ?cancor, cannonical correlation explores relationships between 2
sets of variables.

The lm function can take a matrix as the response variable:

> fit <- lm( cbind(Sepal.Length, Sepal.Width) ~
Petal.Length+Petal.Width, data=iris)
> fit
> summary(fit)

If you tell us more what you are trying to accomplish, we can be of more

Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at intermountainmail.org
(801) 408-8111

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Andy Weller
> Sent: Tuesday, July 17, 2007 11:33 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Speed up computing: looping through data?
> Dear all,
> Please excuse my ignorance, but I am having difficulty with 
> this, and am unable to find help on the website/Google.
> I have a series of explanatory variables that I am aiming to 
> get parsimony out of.
> For example, if I have 10 variables, a-j, I am initially 
> looking at the linear relationships amongst them:
> my.lm1 <- lm(a ~ b+c+d+e+f+g+h+i+j, data=my.data)
> summary(my.lm1)
> my.lm2 <- lm(b ~ a+c+d+e+f+g+h+i+j, data=my.data) etc
> Instead of repeatedly typing this in, is there a way to construct a
> (for) loop of some description to semi-automate this process?
> In addition, I have several response variables. Instead of 
> examining these individually against the explanatory 
> variables, is there a way to 'group' all response and 
> explanatory variables and then look at all the linear 
> relationship amongst all these variables? I have tried:
> my.group1 <- c(a,b,c,d,e,f,g,h,i,j)
> my.group2 <- c(k,l,m,n,o,p,q,r,s,t)
> my.lm3 <- lm(my.group1 ~ my.group2)
> to no avail.
> Thanks for your help, Andy
> ______________________________________________
> 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