基于 R 中的 apply 函数创建矩阵

发布于 2025-01-19 09:16:23 字数 1416 浏览 0 评论 0原文

我在 R 中有一个像这样的矩阵

dat <- matrix(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
                0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
                0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1), ncol = 4)

,我想计算互信息的局部变量 (lv)。对于一对列,我可以使用以下代码来查找 lv

dat <- dat[,c(1,2)]
#install.packages("rinform")
library(rinform)
library(devtools)
#install_github("ELIFE-ASU/rinform")
#install_github("ELIFE-ASU/rinform", ref = "dev")
library(rinform)
mutual_info(dat)
mi <- mutual_info(dat, local = T)
mi <- as.matrix(mi)

结果是:

> mi
            [,1]
 [1,] -1.0000000
 [2,] -1.0000000
 [3,]  0.2223924
 [4,]  0.2223924
 [5,]  0.2223924
 [6,]  0.2223924
 [7,]  0.2223924
 [8,]  0.2223924
 [9,]  0.2223924
[10,]  0.2223924
[11,]  0.2223924
[12,]  0.2223924
[13,]  0.2223924
[14,]  0.2223924
[15,]  0.2223924
[16,]  0.2223924
[17,]  1.5849625
[18,]  1.5849625
[19,]  1.5849625
[20,] -1.5849625

如何创建一个矩阵,其中每列将具有每对的 mi 值?尝试使用 apply 函数但无法使其工作。输出结果必须是一个包含所有成对列组合的20x6矩阵,即上面的dat[,c(1,2)]的mi过程,dat[,c(1,3)], dat[,c(1,4)], dat[,c(2,3)] >, dat[,c(2,4)]dat[,c(3,4)]。有人可以帮助我吗?

I have a matrix in R like this

dat <- matrix(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
                0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
                0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1), ncol = 4)

and i want to calculate the local variant (lv) of mutual information. For a pair of columns I can use the following code to find the lv

dat <- dat[,c(1,2)]
#install.packages("rinform")
library(rinform)
library(devtools)
#install_github("ELIFE-ASU/rinform")
#install_github("ELIFE-ASU/rinform", ref = "dev")
library(rinform)
mutual_info(dat)
mi <- mutual_info(dat, local = T)
mi <- as.matrix(mi)

which results in:

> mi
            [,1]
 [1,] -1.0000000
 [2,] -1.0000000
 [3,]  0.2223924
 [4,]  0.2223924
 [5,]  0.2223924
 [6,]  0.2223924
 [7,]  0.2223924
 [8,]  0.2223924
 [9,]  0.2223924
[10,]  0.2223924
[11,]  0.2223924
[12,]  0.2223924
[13,]  0.2223924
[14,]  0.2223924
[15,]  0.2223924
[16,]  0.2223924
[17,]  1.5849625
[18,]  1.5849625
[19,]  1.5849625
[20,] -1.5849625

How can I create a matrix that each column will have the mi values of each pair? Tried to use an apply function but couldn't make it work. The output result must be a 20x6 matrix with all pair-wise column combinations, i.e., the above mi procedure for dat[,c(1,2)], dat[,c(1,3)], dat[,c(1,4)], dat[,c(2,3)], dat[,c(2,4)], dat[,c(3,4)]. Can someone help me?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澜川若宁 2025-01-26 09:16:23

我无法验证这种方法,因为我不知道rinform软件包。您可以尝试

apply(combn(1:4, 2), 2, function(i) mutual_info(dat[, i], locale = TRUE))
  • combn(1:4,2)1:4中创建两个元素的矩阵。
  • 我们将其用作应用函数的输入来生成结果。

命名

如果将新数据保存到矩阵中,

new_data <- apply(combn(1:4, 2), 2, function(i) mutual_info(dat[, i], locale = TRUE))

则可以使用相同的方法命名列:

colnames(new_data) <- apply(combn(1:4, 2), 2, function(i) paste0("V", i, collapse = ""))

I can't verify this approach since I don't know the rinform package. You could try

apply(combn(1:4, 2), 2, function(i) mutual_info(dat[, i], locale = TRUE))
  • combn(1:4, 2) creates a matrix of all possible of two elements from 1:4.
  • We use this as an input for the apply function to generate the result.

Naming

If you save the new data into a matrix

new_data <- apply(combn(1:4, 2), 2, function(i) mutual_info(dat[, i], locale = TRUE))

you could name the columns using the same approach:

colnames(new_data) <- apply(combn(1:4, 2), 2, function(i) paste0("V", i, collapse = ""))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文