Extracting names from a VarCorr object in lme4 and pasting it as column names

╄→尐↘猪︶ㄣ 提交于 2021-02-08 06:33:09

问题


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

回答1:


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



回答2:


The summary of rePCA produces a list. You can iterate over the names of that list, draw the (unique) relevant labels from vc1/vc2, and assign those labels as colnames.

Note that the data frame inside each element of rePCA is accessed through the $importance attribute.

This can be wrapped as a function:

set_importance_colnames <- function(vc, pca_data) {
  for (name in names(pca_data)) {
    vc_df <- as.data.frame(vc)
    target <- vc_df[vc_df$grp == name, ]
    new_label <- unique(paste(target$grp, target$var1, sep = "_"))
    colnames(pca_data[[name]]$importance) <- new_label
  }
  return(pca_data)
}

Output for AA:

set_importance_colnames(vc1, AA)

$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

Output for BB:

set_importance_colnames(vc2, BB)
$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


来源:https://stackoverflow.com/questions/64095188/extracting-names-from-a-varcorr-object-in-lme4-and-pasting-it-as-column-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!