Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A useful function for getting p values out of the correlation matrix is <code>rcorr</code> from <code>Hmisc</code>. Using it, I got this:</p> <p><img src="https://i.stack.imgur.com/Sgevc.png" alt="enter image description here"></p> <p>In each cell of the correlation matrix, there is a pair of numbers: The upper one represents the coefficient of correlation (as does the color gradient of the cell), while the lower one represents the p value. Is this what you wanted? <em>(See the bottom of the answer for improved response, whereby I convert p values into stars...)</em></p> <p><strong>I proceeded as follows:</strong></p> <p>Since your p values would be VERY small in this data frame, I have used <code>jitter</code> and stripped the amount of observations so as to decrease the statistical significance. The reason for that is that very low p values would be very hard to read in a correlation matrix of this type. Consequently, the result does not make much sense from a statistical point of view but it demonstrates nicely how the significance levels can be added to the matrix.</p> <p>First, jitter it and limit the number of observations:</p> <pre><code>mydf=df mydf[,2:5] = sapply(mydf[,2:5],jitter,amount=20) mydf=mydf[c(1:5,20:24,39:43,58:62),] </code></pre> <p>Then calculate r coefficient and p values:</p> <pre><code>library(Hmisc) # calculate r c = rcorr(as.matrix(mydf[sapply(mydf,is.numeric)]))$r # calculate p values p = rcorr(as.matrix(mydf[sapply(mydf,is.numeric)]))$P </code></pre> <p>Make a plot based on both those values:</p> <pre><code>plots &lt;- dlply(mydf, .(Method), function (x1) { ggplot(data.frame(subset(melt(rcorr(as.matrix(x1[sapply(x1,is.numeric)]))$r)[lower.tri(c),],Var1 != Var2), pvalue=subset(melt(rcorr(as.matrix(x1[sapply(x1,is.numeric)]))$P)[lower.tri(p),],Var1 != Var2)$value), aes(x=Var1,y=Var2,fill=value)) + geom_tile(aes(fill = value),colour = "white") + geom_text(aes(label = sprintf("%1.2f",value)), vjust = 0) + geom_text(aes(label = sprintf("%1.2f",pvalue)), vjust = 1) + theme_bw() + scale_fill_gradient2(name="R^2",midpoint=0.25,low = "blue", high = "red") + xlab(NULL) + ylab(NULL) + theme(axis.text.x=element_blank(), axis.text.y=element_blank(), axis.ticks=element_blank(), panel.border=element_blank()) + ggtitle(x1$Method) + theme(plot.title = element_text(lineheight=1,face="bold")) + geom_text(data = subset(melt(rcorr(as.matrix(x1[sapply(x1,is.numeric)]))$r),Var1==Var2), aes(label=Var1),vjust=1 ) }) </code></pre> <p>Display plot.</p> <pre><code>grid.arrange(plots$Single_ROI + theme(legend.position='none'), plots$Simple_2_ROI + theme(legend.position='none'), plots$WIG_Method + theme(legend.position='none'), plots$WIG_drawn_bg + theme(legend.position='none'), ncol=2, nrow=2) </code></pre> <p><strong>Stars instead of p values:</strong></p> <p>Modify data frame (I leave a few more observations this time):</p> <pre><code>library(Hmisc) library(car) mydf=df set.seed(12345) mydf[,2:5] = sapply(mydf[,2:5],jitter,amount=15) mydf=mydf[c(1:10,20:29,39:48,58:67),] </code></pre> <p>Calculate r, p values and recode p values into stars inside the plot function:</p> <pre><code># calculate r c = rcorr(as.matrix(mydf[sapply(mydf,is.numeric)]))$r # calculate p values p = rcorr(as.matrix(mydf[sapply(mydf,is.numeric)]))$P plots &lt;- dlply(mydf, .(Method), function (x1) { ggplot(data.frame(subset(melt(rcorr(as.matrix(x1[sapply(x1,is.numeric)]))$r)[lower.tri(c),],Var1 != Var2), pvalue=Recode(subset(melt(rcorr(as.matrix(x1[sapply(x1,is.numeric)]))$P)[lower.tri(p),],Var1 != Var2)$value , "lo:0.01 = '***'; 0.01:0.05 = '*'; else = ' ';")), aes(x=Var1,y=Var2,fill=value)) + geom_tile(aes(fill = value),colour = "white") + geom_text(aes(label = sprintf("%1.2f",value)), vjust = 0) + geom_text(aes(label = pvalue), vjust = 1) + theme_bw() + scale_fill_gradient2(name="R^2",midpoint=0.25,low = "blue", high = "red") + xlab(NULL) + ylab(NULL) + theme(axis.text.x=element_blank(), axis.text.y=element_blank(), axis.ticks=element_blank(), panel.border=element_blank()) + ggtitle(x1$Method) + theme(plot.title = element_text(lineheight=1,face="bold")) + geom_text(data = subset(melt(rcorr(as.matrix(x1[sapply(x1,is.numeric)]))$r),Var1==Var2), aes(label=Var1),vjust=1 ) }) </code></pre> <p>Display plot.</p> <pre><code>grid.arrange(plots$Single_ROI + theme(legend.position='none'), plots$Simple_2_ROI + theme(legend.position='none'), plots$WIG_Method + theme(legend.position='none'), plots$WIG_drawn_bg + theme(legend.position='none'), ncol=2, nrow=2) </code></pre> <p><img src="https://i.stack.imgur.com/OdnsO.png" alt="enter image description here"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload