后续:从lme4中的VarCorr对象提取名称并将其粘贴为列名称 [英] Follow-up: Extracting names from a VarCorr object in lme4 and pasting it as column names

查看:63
本文介绍了后续:从lme4中的VarCorr对象提取名称并将其粘贴为列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在跟踪这个好答案.下面的函数 foo ,采用 VarCorr(fit)输出的 Name 列,并使其成为 summary(rePCA(fit))调用.

I'm following up on this great answer. Function foo below, takes the Name column of VarCorr(fit) output and makes them the column names for summary(rePCA(fit)) call.

当我们输入 fm1 fm2 时,效果很好,但是我想知道为什么 fm3 失败了吗?有解决办法吗?

It works fine when we input fm1, fm2, but I wonder why it fails for fm3? Is there a fix?

library(lme4) 
dat <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/sng.csv')
fm1 <- lmer(diameter ~ 1 + (1|plate) + (1|sample), Penicillin) 
fm2 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
fm3 <- lmer(y ~ A * B * C + (A + B | group) + (C|group), data = dat)


foo <- function(fit) {
  
  obj <- summary(rePCA(fit))
  model <- VarCorr(fit)
  
  Map(function(x, z) {
    colnames(x$importance) <- paste(z, unique(sapply(model, colnames)), sep = '_')
    x
  }, obj, names(obj))
}

#EXAMPLE OF USE:
foo(fm1) ###     OK !
foo(fm2) ###     OK !
foo(fm3) ###     :-( Error in dimnames(x) <- dn

推荐答案

obj model 的长度不同时,该函数将失败.这是一种使其可用于 fm3 的黑客.

The function fails when length of obj and model is different. Here is a hack to make it work for fm3.

foo <- function(fit) {
  
  obj <- summary(rePCA(fit))
  model <- VarCorr(fit)
  if(length(obj) == length(model)) {
   obj <- Map(function(x, z) {
    colnames(x$importance) <- paste(z, unique(sapply(model, colnames)), sep = '_')
    x
  }, obj, names(obj))
  }
  else if(length(obj) == 1) {
    colnames(obj[[1]]$importance) <- unlist(mapply(paste, names(model), sapply(model, colnames), MoreArgs = list(sep = '_')))
  }
  return(obj)
}

这将返回以下输出:

foo(fm1) 

#$plate
#Importance of components:
#                       plate_(Intercept)
#Standard deviation                  1.54
#Proportion of Variance              1.00
#Cumulative Proportion               1.00

#$sample
#Importance of components:
#                       sample_(Intercept)
#Standard deviation                   3.51
#Proportion of Variance               1.00
#Cumulative Proportion                1.00

foo(fm2) 
#$Subject
#Importance of components:
#                       Subject_(Intercept) Subject_Days
#Standard deviation                   0.967       0.2309
#Proportion of Variance               0.946       0.0539
#Cumulative Proportion                0.946       1.0000

foo(fm3) 
#$group
#Importance of components:
#                       group_(Intercept) group_A group_B group.1_(Intercept) group.1_C
#Standard deviation                 1.418   1.291  0.5129              0.4542 0.0000497
#Proportion of Variance             0.485   0.402  0.0634              0.0498 0.0000000
#Cumulative Proportion              0.485   0.887  0.9502              1.0000 1.0000000

这篇关于后续:从lme4中的VarCorr对象提取名称并将其粘贴为列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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