使矩阵对称,对角线上有单位

发布于 2025-01-18 17:48:12 字数 2754 浏览 0 评论 0原文

我的数据:

data = structure(list(st1 = c(7L, 7L, 9L, 5L, 9L, 9L, 6L, 7L, 7L, 7L, 
6L, 7L, 5L, 6L, 7L, 7L, 8L, 8L, 3L, 9L), st2 = c(2L, 1L, 1L, 
1L, 1L, 1L, 2L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 2L, 0L, 
1L), group = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), class = "data.frame", row.names = c(NA, 
-20L))

我使用nominalSymmetryTest 来比较列中的数据类别,但为此矩阵必须是对称的。

代码本身:

combination <- list(c(1, 2))

wilcox.fun <- function(dat) { 
  do.call(rbind, lapply(combination, function(x) {
    col1 <- dat[[x[1]]]
    col2 <- dat[[x[2]]]
      contingency.table.1 = data.frame(col1,col2)
                        contingency.table.1 = table(contingency.table.1[c(1,2)])
                        contingency.table.1 <- make_symmetric_matrix(contingency.table.1)
                        contingency.table.1[contingency.table.1==0|is.na(contingency.table.1)] <- 0
                        print(contingency.table.1)
                        test.1 <- nominalSymmetryTest(contingency.table.1,
                                                      digits     = 3,
                                                      MonteCarlo = TRUE,
                                                      ntrial     = 1000000)$Pairwise.symmetry.tests
                        test <- nominalSymmetryTest(contingency.table.1,
                                                    digits     = 3,
                                                    MonteCarlo = TRUE,
                                                    ntrial     = 1000000)$Global.test.for.symmetry
                        print(contingency.table.1)
                        print(test.1)
                    
    data.frame(Test = sprintf('Group %s by Group %s', x[1], x[2]), 
                
               p = test$Comparison)
  }))
}

result <- purrr::map_df(split(data, data$group), wilcox.fun, .id = 'Group')

使矩阵对称的函数

make_symmetric_matrix <- function(m) {
    nr <- nrow(m)
    nc <- ncol(m)
    if(nc > nr) {
        m <- rbind(m, matrix(1, nrow = nc - nr, nc = nc))
    } else if(nr > nc) {
        m <- cbind(m, matrix(1, nrow = nr, nc = nr - nc))
    }
    m
}

作为比较的结果,结果不正确

我需要 make_symmetry_matrix 函数将 1 放在对角线上,其余为 0。

现在它只是将到处 1 放置到对称矩阵

UP :

现在矩阵用函数 make_symmetry_matrix 来增强,看起来像这样

  0 1 2      
3 1 0 0 1 1 1
5 1 1 0 1 1 1
6 2 0 1 1 1 1
7 3 4 1 1 1 1
8 1 0 1 1 1 1
9 0 4 0 1 1 1

我希望 make_symmetry_matrix 函数来制作这样一个矩阵

  0 1 2      
3 1 0 0 0 0 0
5 1 1 0 0 0 0
6 2 0 1 0 0 0
7 3 4 1 1 0 0
8 1 0 1 0 1 0
9 0 4 0 0 0 1

我只在对角线上留下单位

My data:

data = structure(list(st1 = c(7L, 7L, 9L, 5L, 9L, 9L, 6L, 7L, 7L, 7L, 
6L, 7L, 5L, 6L, 7L, 7L, 8L, 8L, 3L, 9L), st2 = c(2L, 1L, 1L, 
1L, 1L, 1L, 2L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 2L, 0L, 
1L), group = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), class = "data.frame", row.names = c(NA, 
-20L))

I use nominalSymmetryTest in order to compare the categories of data in my columns, but for this the matrix must be symmetric.

the code itself:

combination <- list(c(1, 2))

wilcox.fun <- function(dat) { 
  do.call(rbind, lapply(combination, function(x) {
    col1 <- dat[[x[1]]]
    col2 <- dat[[x[2]]]
      contingency.table.1 = data.frame(col1,col2)
                        contingency.table.1 = table(contingency.table.1[c(1,2)])
                        contingency.table.1 <- make_symmetric_matrix(contingency.table.1)
                        contingency.table.1[contingency.table.1==0|is.na(contingency.table.1)] <- 0
                        print(contingency.table.1)
                        test.1 <- nominalSymmetryTest(contingency.table.1,
                                                      digits     = 3,
                                                      MonteCarlo = TRUE,
                                                      ntrial     = 1000000)$Pairwise.symmetry.tests
                        test <- nominalSymmetryTest(contingency.table.1,
                                                    digits     = 3,
                                                    MonteCarlo = TRUE,
                                                    ntrial     = 1000000)$Global.test.for.symmetry
                        print(contingency.table.1)
                        print(test.1)
                    
    data.frame(Test = sprintf('Group %s by Group %s', x[1], x[2]), 
                
               p = test$Comparison)
  }))
}

result <- purrr::map_df(split(data, data$group), wilcox.fun, .id = 'Group')

The function that makes the matrix symmetric

make_symmetric_matrix <- function(m) {
    nr <- nrow(m)
    nc <- ncol(m)
    if(nc > nr) {
        m <- rbind(m, matrix(1, nrow = nc - nr, nc = nc))
    } else if(nr > nc) {
        m <- cbind(m, matrix(1, nrow = nr, nc = nr - nc))
    }
    m
}

As a result of the comparison, the result is not correct

I need the make_symmetric_matrix function to place 1 on the diagonal and the rest 0.

Now it just places everywhere 1 to a symmetric matrix

UP:

now the matrix is augmented with a function make_symmetric_matrix it looks like this

  0 1 2      
3 1 0 0 1 1 1
5 1 1 0 1 1 1
6 2 0 1 1 1 1
7 3 4 1 1 1 1
8 1 0 1 1 1 1
9 0 4 0 1 1 1

I want the make_symmetric_matrix function to make such a matrix

  0 1 2      
3 1 0 0 0 0 0
5 1 1 0 0 0 0
6 2 0 1 0 0 0
7 3 4 1 1 0 0
8 1 0 1 0 1 0
9 0 4 0 0 0 1

I left units only on the diagonal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文