使矩阵对称,对角线上有单位
我的数据:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论