【问题标题】:How do I extract the Correlation of fixed effects part of the lmer output如何提取 lmer 输出的固定效应部分的相关性
【发布时间】:2013-04-26 09:59:45
【问题描述】:

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

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

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)

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

编辑:

这是我根据答案创建的功能。

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)
}

您必须从here 下载 my.plotcorr 函数。 上面使用命令fixeff.plotcorr(mod) 的示例的结果图现在如下所示:

【问题讨论】:

    标签: r lme4


    【解决方案1】:

    如何使用内置

    cov2cor(vcov(mod))
    

    ?

    【讨论】:

    • 不错的一个!我不知道那个函数。
    【解决方案2】:

    我不知道直接方法。但这是解决方法。

     diag(diag(1/sqrt(vcov(mod)))) %*% vcov(mod) %*% diag(diag(1/sqrt(vcov(mod))))
    

    【讨论】:

    • +1 我认为不是解决方法,而是从 v-cov 矩阵的相关性的数学推导! :-)
    【解决方案3】:

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

    selectMethod( print , "mer" )
    

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

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

    so 被定义为你的对象的摘要,所以在你的情况下你应该只需要提取:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多