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

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

问题描述

下面,我想知道是否可能有一种方法可以从 vc1 vc2

中提取列 Name Groups code>并将它们分别粘贴为对象 AA BB 的列名称.

例如,对于 MODEL 1 (如下),我期望的 AA 输出为:

  plate_(Intercept)#:名称&vc1中的组"列标准偏差1.54方差比例1.00累计比例1.00sample_(Intercept)#:名称&vc1中的组"列标准偏差3.513方差比例1.000累计比例1.000 

这在R中是否可以实现(可能是作为一个函数)?

 库(lme4)##模型1:fm1<-lmer(直径〜1 +(1 |板)+(1 |样品),青霉素)(vc1<-VarCorr(fm1))AA<-摘要(rePCA(fm1))##模型2:fm2<-lmer(反应〜天+(天|主题),睡眠研究)(vc2<-VarCorr(fm2))BB<-摘要(rePCA(fm2)) 

解决方案

我们可以编写一个函数:

  return_names<-function(obj,model){地图(函数(x,z){colnames(x $ importance)<-paste(z,unique(sapply(model,colnames)),sep ='_')X},obj,names(obj))}return_names(AA,vc1)#$板#组件的重要性:#plate_(拦截)#标准偏差1.54#方差比例1.00#累计比例1.00#$ sample#组件的重要性:#sample_(拦截)#标准偏差3.513#方差比例1.000#累计比例1.000return_names(BB,vc2)#$主题#组件的重要性:#Subject_(Intercept)Subject_Days#标准偏差0.9669 0.23088#方差比例0.9460 0.05395#累计比例0.9460 1.00000 

Below I was wondering if there might be a way to extract the columns Name and Groups from vc1 and vc2 and respectively paste them as the column names for objects AA, BB.

For example, for MODEL 1 (below), my expected output of AA will be:

                    plate_(Intercept) #: Name & Groups column from `vc1`
Standard deviation     1.54
Proportion of Variance 1.00
Cumulative Proportion  1.00

                   sample_(Intercept) #: Name & Groups column from `vc1`
Standard deviation     3.513
Proportion of Variance 1.000
Cumulative Proportion  1.000

Is this achievable in R (possibly as a function)?

library(lme4)

## MODEL 1:
fm1 <- lmer(diameter ~ 1 + (1|plate) + (1|sample), Penicillin)

(vc1 <- VarCorr(fm1))

AA <- summary(rePCA(fm1))

## MODEL 2:
fm2 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)

(vc2 <- VarCorr(fm2))

BB <- summary(rePCA(fm2))

解决方案

We can write a function :

return_names <- function(obj, model) {

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

}

return_names(AA, vc1)
#$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.513
#Proportion of Variance              1.000
#Cumulative Proportion               1.000

return_names(BB, vc2)
#$Subject
#Importance of components:
#                       Subject_(Intercept) Subject_Days
#Standard deviation                  0.9669      0.23088
#Proportion of Variance              0.9460      0.05395
#Cumulative Proportion               0.9460      1.00000

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

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