如何提取lmer输出的固定效果的相关性 [英] How do I extract the Correlation of fixed effects part of the lmer output

查看:226
本文介绍了如何提取lmer输出的固定效果的相关性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您具有一个包含许多因素和相互作用的多级模型时,固定效应矩阵的相关性大小可能变得非常大且不清楚.

When you have a multilevel model with lots of factors and interactions the size of the correlation of fixed effects matrix can become quite big and unclear.

我可以在打印方法中使用symbolic.cor=T参数来使摘要更清晰地打印,如下所示:

I can use the symbolic.cor=T parameter in the print method to make a clearer print of the summary like below:

ratbrain <-
within(read.delim("http://www-personal.umich.edu/~bwest/rat_brain.dat"),
{
treatment <- factor(treatment,
labels = c("Basal", "Carbachol"))
region <- factor(region,
labels = c("BST", "LS", "VDB"))
})

print(mod<-lmer(activate ~ region * treatment + (0 + treatment | animal),ratbrain),symbolic.cor=T)

这为大型矩阵绘制了一个更清晰的相关矩阵.尽管此示例的矩阵不是很大. 但是,如果我可以绘制相关性的热图,那就太好了.
如何提取固定效果的相关性,以便制作此热图?

This plots a somewhat clearer correlation matrix for large matrices. Allthough this example's matrix isn't so big. But it would be nice if I could plot a heatmap of the correlations.
How do I extract the correlation of fixed effects so I can make this heatmap?

这是我通过回答创建的功能.

Here's the function I created thanks to the answers.

fixeff.plotcorr<-function(mod,...)
{
  #require(GGally) # contains another correlation plot using ggplot2
  require(lme4)

  fixNames<-names(fixef(mod))

  # Simon O'Hanlon's answer:
  # so <- summary(mod)
  # df<-as.matrix(so@vcov@factors$correlation) for version lme4<1.0
  # df<-as.matrix(so$vcov@factors$correlation)  # lme4 >= 1.0

  df<-as.matrix(cov2cor(vcov(mod))) #Ben Bolker's solution

  rownames(df)<-fixNames
  colnames(df)<-abbreviate(fixNames, minlength = 11)

  colsc=c(rgb(241, 54, 23, maxColorValue=255), 'white', rgb(0, 61, 104, maxColorValue=255))
  colramp = colorRampPalette(colsc, space='Lab')
  colors = colramp(100)
  cols=colors[((df + 1)/2) * 100]
  # I'm using function my.plotcorr which you can download here:
  # http://hlplab.wordpress.com/2012/03/20/correlation-plot-matrices-using-the-ellipse-library/
  my.plotcorr(df, col=cols, diag='none', upper.panel="number", mar=c(0,0.1,0,0),...)

  # Another possibility is the corrplot package:
  # cols <- colorRampPalette(c("#67001F", "#B2182B", "#D6604D", "#F4A582", "#FDDBC7", 
  #                              "#FFFFFF", "#D1E5F0", "#92C5DE", "#4393C3", "#2166AC", "#053061"))
  # require(corrplot,quiet=T)
  # corrplot(df, type="upper", method="number", tl.pos='tl', tl.col='black', tl.cex=0.8, cl.pos='n', col=cols(50))
  # corrplot(df,add=TRUE,  method='ellipse', type='lower', tl.pos='n', tl.col='black', cl.pos='n', col=cols(50), diag=FALSE)
}

您必须从此处. 上面使用命令fixeff.plotcorr(mod)生成的示例的结果图现在看起来像这样:

You have to download the my.plotcorr function from here. The resulting plot of the example above using command fixeff.plotcorr(mod) now looks like this:

推荐答案

使用S4方法列表函数,我们可以返回在类"mer"的对象上调用print时分派的函数:

Using the S4 method listing function we can return the function which is dispatched when print is called on an object of class "mer":

selectMethod( print , "mer" )

查看返回的源代码,我们可以找到适合您的行:

Looking at the source code that is returned we can find the lines applicable to you:

if (correlation) {
            corF <- so@vcov@factors$correlation

so被定义为对象的摘要,因此在您的情况下,您只需要提取以下内容即可:

so is defined as the summary of your object, so in your case you should just need to extract:

so <- summary(mod)
so@vcov@factors$correlation

这篇关于如何提取lmer输出的固定效果的相关性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆