矩阵在R中不一致地删除列名称 [英] Matrix dropping the column names inconsistently in R

查看:80
本文介绍了矩阵在R中不一致地删除列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 foo 的小函数。当我使用 m0 运行它时,它会正确显示列名。但是当我将它与 m1 一起使用时, foo 会忽略所有列名。

I have a small function called foo. When I run it with m0, it correctly shows the column names. But when I use it with m1, foo omits all column names.


Is there a fix for that?

library(lme4)
library(Matrix)

sng <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/sng.csv')
m0 <- lmer(y ~ A * B * C + (A * B * C  | group), data = sng)
m1 <- lmer(y ~ A * B * C + (A * B * C  || group), data = sng)

foo <- function(fit){
vc <- VarCorr(fit)
as.matrix(Matrix::bdiag(vc))
}
# EXAMPLES OF USE:
foo(m0)   # SHOWS COLUMNAMES FINE :-)
foo(m1)   # OMITS COLUMNNAMES ALL :-(


推荐答案

尝试这种方法,它看起来像是输出矩阵和 VarCorr()中列表长度的问题。这里的代码:

Try this approach. It looks like an issue with the output matrix and the length of the list in VarCorr(). Here the code:

library(lme4)
library(Matrix)
#Data and models
sng <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/sng.csv')
m0 <- lmer(y ~ A * B * C + (A * B * C  | group), data = sng)
m1 <- lmer(y ~ A * B * C + (A * B * C  || group), data = sng)
#Function
foo <- function(fit){
  vc <- VarCorr(fit)
  if(length(vc)==1)
  {
    y <- as.matrix(Matrix::bdiag(vc))
  } else
  {
    z <- do.call(rbind,vc)
    y <- as.matrix(Matrix::bdiag(vc))
    dimnames(y)[[1]] <- rownames(z)
    dimnames(y)[[2]] <- rownames(z)
  }
  return(y)
}
#Apply
foo(m0)
foo(m1)

输出:

foo(m0)
               (Intercept)             A             B              C            A:B
(Intercept)  3.55516422891  0.2559261707 -0.0472493899  0.00321219209 -0.02924403667
A            0.25592617067  4.0133838788  0.0772138992 -0.03546219301 -0.22207717925
B           -0.04724938991  0.0772138992  6.4063596371 -1.16254872282 -0.42778740468
C            0.00321219209 -0.0354621930 -1.1625487228  4.78167840574 -0.05459620260
A:B         -0.02924403667 -0.2220771793 -0.4277874047 -0.05459620260  0.04574366496
A:C         -0.03399653992 -0.2008053942  0.3219439970 -0.42927661072 -0.00100945652
B:C         -0.02404787917  0.0212759619 -0.1454033407 -0.43345150872  0.02116923485
A:B:C        0.00586240974  0.0206112404  0.0140531649  0.04266309961 -0.00336982403
                       A:C            B:C           A:B:C
(Intercept) -0.03399653992 -0.02404787917  0.005862409744
A           -0.20080539424  0.02127596189  0.020611240433
B            0.32194399704 -0.14540334071  0.014053164881
C           -0.42927661072 -0.43345150872  0.042663099613
A:B         -0.00100945652  0.02116923485 -0.003369824029
A:C          0.05859594675  0.02996839148 -0.004072087992
B:C          0.02996839148  0.05038262309 -0.004808881907
A:B:C       -0.00407208799 -0.00480888191  0.000590970743

foo(m1)
            (Intercept)           A           B         C A:B            A:C          B:C A:B:C
(Intercept)  3.29966179 0.000000000 0.000000000 0.0000000   0 0.00000000e+00 0.0000000000     0
A            0.00000000 0.914972854 0.000000000 0.0000000   0 0.00000000e+00 0.0000000000     0
B            0.00000000 0.000000000 0.467647434 0.0000000   0 0.00000000e+00 0.0000000000     0
C            0.00000000 0.000000000 0.000000000 1.1369602   0 0.00000000e+00 0.0000000000     0
A:B          0.00000000 0.000000000 0.000000000 0.0000000   0 0.00000000e+00 0.0000000000     0
A:C          0.00000000 0.000000000 0.000000000 0.0000000   0 6.72771167e-08 0.0000000000     0
B:C          0.00000000 0.000000000 0.000000000 0.0000000   0 0.00000000e+00 0.0013880372     0
A:B:C        0.00000000 0.000000000 0.000000000 0.0000000   0 0.00000000e+00 0.0000000000     0

这篇关于矩阵在R中不一致地删除列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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