Maria Dulce Subida
2011-Feb-16 10:04 UTC
[R] add regression equation to panel.lm - package psych
Dear all,
I created a scatterplot matrix with the scatterplot and linear
regression line in the upper panel, the density histogram in the
diagonal panel and the values of the Pearson coefficient, probability
and number of observations used, in the lower panel. Please see the
script above.
Now I would like to add the regression equation to the upper panel. I
thought it would be something simple, but I was not able to figure it
out. Any ideas, please?
Thanks a lot!
Dulce
________________
library(car)
library(psych)
panel.cor<- function(x, y, method="pearson", digits=2,...) {
points(x,y,type="n");
usr<- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1));
correl<- cor.test(x,y, method=method, use="complete.obs");
sum.row<- rowSums(cbind(x,y));
conta.n<- length(sum.row[!is.na(sum.row)]);
n=conta.n;
r=correl$estimate;
pval=correl$p.value;
color="black";
fontface=1;
if (pval<0.05) fontface=2;
txt<- format(r,digits=2)
pval<- format(pval,digits=2)
txt<- paste("r= ", txt, "\np=",pval,
"\nn=", n, sep="")
text(0.5, 0.5, txt,col=color, font=fontface)
}
panel.diag<-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="grey", ...)
d<- density(x,na.rm=TRUE,bw="nrd",adjust=1.2)
d$y<- d$y/max(d$y)
lines(d)
}
pairs (~ x + y + z + q, data=my_data, main = "my_data", na.action
"na.pass",
diag.panel=panel.diag, upper.panel=panel.lm, col.lm="grey",
lower.panel=panel.cor,
pch=20, cex=0.5, cex.labels=1, font.labels=2, lwd=0.5)