ashz
2010-Sep-30 21:42 UTC
[R] Scatterplot matrix - Pearson linear correlation and Density Ellipse
Hi, I have modified a known script to generate a scatterplot matrix: panel.cor = function(x, y, digits=2, prefix="Rho=", cex.cor) { usr = par("usr"); on.exit(par(usr)) par(usr = c(0, 1, 0, 1)) r = abs(cor(x, y, use="pairwise.complete.obs", method = "pearson")) txt = format(c(r, 0.123456789), digits=digits)[1] txt = paste(prefix, txt, sep="") if(missing(cex.cor)) cex.cor = 0.8/strwidth(txt) text(0.5, 0.5, txt, cex = cex.cor) } pairs(ap[8:11], lower.panel=panel.smooth, upper.panel=panel.cor) My question is how I can change the lower.panel to show both the pearson linear correlation and Density Ellipse? Thanks a lot. As Hz -- View this message in context: http://r.789695.n4.nabble.com/Scatterplot-matrix-Pearson-linear-correlation-and-Density-Ellipse-tp2763552p2763552.html Sent from the R help mailing list archive at Nabble.com.
Peter Ehlers
2010-Oct-01 15:03 UTC
[R] Scatterplot matrix - Pearson linear correlation and Density Ellipse
On 2010-09-30 15:42, ashz wrote:> > Hi, > > I have modified a known script to generate a scatterplot matrix: > > panel.cor = function(x, y, digits=2, prefix="Rho=", cex.cor) > { > usr = par("usr"); on.exit(par(usr)) > par(usr = c(0, 1, 0, 1)) > r = abs(cor(x, y, use="pairwise.complete.obs", method = "pearson")) > txt = format(c(r, 0.123456789), digits=digits)[1] > txt = paste(prefix, txt, sep="") > if(missing(cex.cor)) cex.cor = 0.8/strwidth(txt) > text(0.5, 0.5, txt, cex = cex.cor) > } > > pairs(ap[8:11], lower.panel=panel.smooth, upper.panel=panel.cor) > > My question is how I can change the lower.panel to show both the pearson > linear correlation and Density Ellipse? >Have a look at pairs.panels() in pkg:psych. -Peter Ehlers> Thanks a lot. > As Hz >
ashz
2010-Oct-03 20:37 UTC
[R] Scatterplot matrix - Pearson linear correlation and Density Ellipse
Hi, I used the pairs.panels() in pkg:psych and it is helpful. It saves time. but if I use this line: pairs.panels(cfcap[8:11], scale = FALSE, lm=TRUE,ellipses=TRUE, digits = 2 ) The results are: - The upper.panel does not show the pearson r but the lm data. Furthermore, can I use the pairwise.complete.obs method for the upper.panel. Can it be fixed? - Can I remove the histograms? - Can I control the eliipse alpha? Thanks a lot. -- View this message in context: http://r.789695.n4.nabble.com/Scatterplot-matrix-Pearson-linear-correlation-and-Density-Ellipse-tp2763552p2953521.html Sent from the R help mailing list archive at Nabble.com.
William Revelle
2010-Oct-03 23:29 UTC
[R] Scatterplot matrix - Pearson linear correlation and Density Ellipse
Dear ashz, Unfortunately, much of you want is not possible with the current implementation of pairs.panels. Since pairs.panels is adapted from the help file of pairs, you might try pairs and then add in the panel functions that do what you want. That the lm option does it what it does met a need of mine for a demo of the difference of regression slopes of X on Y versus Y on X. Your request is very reasonable and I will implement it in the next revision. To get the pairwise complete rather than pairwise, you can preprocess your data file with na.omit e.g., cfc <- na.omit(cfcap[8:11]) pairs.panesl(cfc) At 1:37 PM -0700 10/3/10, ashz wrote:>Hi, > >I used the pairs.panels() in pkg:psych and it is helpful. It saves time. > >but if I use this line: >pairs.panels(cfcap[8:11], scale = FALSE, lm=TRUE,ellipses=TRUE, digits = 2 >) > >The results are: >- The upper.panel does not show the pearson r but the lm data. Furthermore, >can I use the pairwise.complete.obs method for the upper.panel. Can it be >fixed?>- Can I remove the histograms?Not as a call, but you change pairs.panels to draw just the densities by substituting "panel.hist.density" <- function(x,...) { usr <- par("usr"); on.exit(par(usr)) par(usr = c(usr[1:2], 0, 1.5) ) h <- hist(x, plot = FALSE) breaks <- h$breaks; nB <- length(breaks) y <- h$counts; y <- y/max(y) #rect(breaks[-nB], 0, breaks[-1], y,col=hist.col) # <--- comment this line out tryd <- try( d <- density(x,na.rm=TRUE,bw="nrd",adjust=1.2),silent=TRUE) if(class(tryd) != "try-error") { d$y <- d$y/max(d$y) lines(d)} } in place of the current panel.hist.density function.>- Can I control the eliipse alpha?Not yet. Good idea. Your requests are all very reasonable and will be added to my wish list of additions to pairs.panels. This will not happen for several weeks, however. Bill> >Thanks a lot. > >-- >View this message in context: >http://r.789695.n4.nabble.com/Scatterplot-matrix-Pearson-linear-correlation-and-Density-Ellipse-tp2763552p2953521.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.
ashz
2010-Oct-04 07:33 UTC
[R] Scatterplot matrix - Pearson linear correlation and Density Ellipse
Thanks a lot for the answer -- View this message in context: http://r.789695.n4.nabble.com/Scatterplot-matrix-Pearson-linear-correlation-and-Density-Ellipse-tp2763552p2953909.html Sent from the R help mailing list archive at Nabble.com.
ashz
2010-Oct-04 14:26 UTC
[R] Scatterplot matrix - Pearson linear correlation and Density Ellipse
Hi, strangely, when I run this script: panel.cor = function(x, y, digits=2, prefix="r=", cex.cor) { usr = par("usr"); on.exit(par(usr)) par(usr = c(0, 1, 0, 1)) r = abs(cor(x, y, use="pairwise.complete.obs", method = "pearson")) txt = format(c(r, 0.123456789), digits=digits)[1] txt = paste(prefix, txt, sep="") if(missing(cex.cor)) cex.cor = 0.8/strwidth(txt) text(0.5, 0.5, txt, cex = cex.cor) } pairs(cap[8:11], lower.panel=panel.lm, upper.panel=panel.cor, cex=2) I get linear regression and density ellipse, why? Is it possible to add also the data point? Thanks a lot -- View this message in context: http://r.789695.n4.nabble.com/Scatterplot-matrix-Pearson-linear-correlation-and-Density-Ellipse-tp2763552p2954416.html Sent from the R help mailing list archive at Nabble.com.