[R] loop issues (r.squared)

Greg Snow Greg.Snow at intermountainmail.org
Thu Feb 8 22:12:14 CET 2007


The most straight forward way that I can think of is just:

> cor(my.mat)^2 # assuming my.mat is the matrix with your data in the
columns

That will give you all the R^2 values for regressing 1 column on 1
column (it is called R-squared for a reason).

If you want the R^2 values for regressing one column on all other
columns in the matrix, then a short-cut is:

> 1-1/diag(solve(cor(my.mat)))

Both should be much faster than looping, the 2nd may give problems in
trying to invert a very large matrix.

Hope this helps,

-- 
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 andy1983
> Sent: Thursday, February 08, 2007 1:29 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] loop issues (r.squared)
> 
> 
> I would like to compare every column in my matrix with every 
> other column and get the r-squared. I have been using the 
> following formula and loops:
> summary(lm(matrix[,x]~matrix[,y]))$r.squared
> where x and y are the looping column numbers
> 
> If I have 100 columns (10,000 iterations), the loops give me 
> results in a reasonable time.
> If I try 10,000 columns, the loops take forever even if there 
> is no formula inside. I am guessing I can vectorize my code 
> so that I could eliminate one or both loops. Unfortunately, I 
> can't figure out how to.
> 
> Any suggestions?
> 
> Thanks.
> --
> View this message in context: 
> http://www.nabble.com/loop-issues-%28r.squared%29-tf3195843.ht
> ml#a8873580
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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