[R] Linear regression equation and coefficient matrix

Joshua Wiley jwiley.psych at gmail.com
Wed Aug 18 17:01:58 CEST 2010


Hmm, after reading one of your other posts, I am thinking you may
*just* want all pairwise combinations. This worked for me:


# Create a sample data frame with 60 named columns
x <- data.frame(matrix(rnorm(1200), ncol = 60, dimnames = list(NULL,
paste("Col", 1:60, sep=''))))

# calculate the correlation matrix and the R^2
mycorrmatrix <- cor(x)
myr2s <- mycorrmatrix^2

###To get all the linear regressions

# get the column names
mynames <- colnames(x)

#Use sapply() and paste() to create formulae for all pairwise combinations
myformula <- sapply(mynames, function(x) {paste(x, "~", mynames)})

#Remove the diagonals since these regress a variable on itself
diag(myformula) <- NA
myformula <- myformula[which(!is.na(myformula))]

#check the structure of myformula
str(myformula)

#initialize a list to store all the models
mylms <- vector("list", 3540)

# calculate all the models
for(i in 1:3540) {
mylms[[i]] <- lm(formula = myformula[i], data = x)
}

# As an example
summary(mylms[[1]])



On Wed, Aug 18, 2010 at 7:21 AM, Joshua Wiley <jwiley.psych at gmail.com> wrote:
> On Wed, Aug 18, 2010 at 6:09 AM, ashz <ashz at walla.co.il> wrote:
>>
>> Hi,
>>
>> I have 20*60 data matrix (with some NAs) and I wish to perfom a  Pearson
>> correlation coefficient matrix as well as simple linear regression equation
>
> The correlation matrix can be readily obtained by calling cor() on the
> entire matrix.
>
>> and coefficient of determination (R2) for every possible combination. Any
>> tip/idea/library/script how do to so.
>
> So you have 60 variables, and you want every possible combination?  I
> may be mistaken, but isn't that 60! if you ignore any interactions?
> If so, this strikes me as a job for
>
> library(fortunes)
> fortune("hundreds")
>
>
>>
>> Thanks,
>> As hz
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/Linear-regression-equation-and-coefficient-matrix-tp2329804p2329804.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> --
> Joshua Wiley
> Ph.D. Student, Health Psychology
> University of California, Los Angeles
> http://www.joshuawiley.com/
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list