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 and coefficient of determination (R2) for every possible combination. Any tip/idea/library/script how do to so. 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.
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 equationThe 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/
Hi, Thanks, the cor() works. Regarding the simple linear regression equation (mainly, the slope parameter) and r2. I think I was not writing it well. I need to do it just for the columns. If I have a, b, c, d columns I wish to compute the relation of there data, e.g., between a-b, a-c, a-d, b-a, b-c, b-d, etc. I hope it is clear now and an help will be great. Thanks. -- View this message in context: http://r.789695.n4.nabble.com/Linear-regression-equation-and-coefficient-matrix-tp2329804p2329948.html Sent from the R help mailing list archive at Nabble.com.
Well you already have the correlation matrix, so you can just work from there. One of the profound equations in statistics is that r^2=r^2, meaning that to get r2 (as you call it, assuming you mean the coefficient of determination) just square the elements of the correlation matrix. To get regression slopes, one equation is just b=s_y/s_x * r, so use the var function to get variances, diag to pull out just the diagonal, sqrt to get the standard deviations and then either matrix multiplication or the sweep function to multiply (divide) the correlations by the appropriate standard deviations and you will have a matrix of slopes. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of ashz > Sent: Wednesday, August 18, 2010 8:43 AM > To: r-help at r-project.org > Subject: Re: [R] Linear regression equation and coefficient matrix > > > Hi, > > Thanks, the cor() works. > > Regarding the simple linear regression equation (mainly, the slope > parameter) and r2. I think I was not writing it well. I need to do it > just > for the columns. If I have a, b, c, d columns I wish to compute the > relation > of there data, e.g., between a-b, a-c, a-d, b-a, b-c, b-d, etc. > > I hope it is clear now and an help will be great. > > Thanks. > > -- > View this message in context: http://r.789695.n4.nabble.com/Linear- > regression-equation-and-coefficient-matrix-tp2329804p2329948.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.
Dear Greg, Thanks for the tip. As I am new in R can you please provide me a script how do to so. It will help my learning process. Thanks, Asher -- View this message in context: http://r.789695.n4.nabble.com/Linear-regression-equation-and-coefficient-matrix-tp2329804p2330867.html Sent from the R help mailing list archive at Nabble.com.
Here is a quick example: c1 <- cor(iris[,-5]) s1 <- sqrt(diag(var(iris[,-5]))) betas <- diag( s1 ) %*% c1 %*% diag( 1/s1 ) # now compare: coef( lm( Sepal.Length ~ Sepal.Width, data=iris ) )[2] betas[1,2] But if you cannot work that out on your own, then you really should review linear algebra and linear model theory. Also when dealing with us "absent minded professor" types (I now have adjunct status at 2 universities, can I rationalize being twice as absent minded now?) it is best to include some context in your post (quote some of the previous conversation) to help us remember which of many discussions we have been part of recently you are asking about. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of ashz > Sent: Thursday, August 19, 2010 3:02 AM > To: r-help at r-project.org > Subject: Re: [R] Linear regression equation and coefficient matrix > > > Dear Greg, > > Thanks for the tip. As I am new in R can you please provide me a script > how > do to so. It will help my learning process. > > Thanks, > Asher > -- > View this message in context: http://r.789695.n4.nabble.com/Linear- > regression-equation-and-coefficient-matrix-tp2329804p2330867.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.
Reasonably Related Threads
- Coefficient of determination for non-linear equations system (nlsystemfit)
- how to solve a power series linear coefficient equation
- Standard error of coefficient in linear regression
- Matrix Plot and linear regression
- Scatter plots, linear regression in ggplot2