我有一個矩陣R,即dat,我正在嘗試使用infotheo所有變陣列合之間的包創建一個資訊矩陣。這是代碼:
set.seed(1234)
m <- 10
n <- 5
dat <- round(matrix(runif(m * n), m, n))
library(infotheo)
a = combn(seq(ncol(dat)), 2, function(x)condinformation(dat[, x[1]], dat[,x[2]], method = 'emp'))
b <- structure(a, Size = ncol(dat), class = 'dist')
b <- as.matrix(b)
結果如下:
> b
1 2 3 4 5
1 0.000000000 0.004021743 0.06326870 0.19497599 0.004021743
2 0.004021743 0.000000000 0.03218930 0.01384429 0.291103166
3 0.063268705 0.032189301 0.00000000 0.01384429 0.013844294
4 0.194975994 0.013844294 0.01384429 0.00000000 0.032189301
5 0.004021743 0.291103166 0.01384429 0.03218930 0.000000000
我的問題是,雖然所有值都是正確的,即 ,condinformation(dat[, 1], dat[,2],method = 'emp')=0.004021743但對角線元素不正確。例如
> condinformation(dat[, 1], dat[,1],method = 'emp')
[1] 0.6108643
我在我的代碼中做錯了什么并得到這個結果?對于預期的結果,您有替代解決方案嗎?
uj5u.com熱心網友回復:
正如 det 所提到的,combn不會做 (1, 1)、(2, 2) 等。試試這個。
b <- matrix(0, n, n)
b[lower.tri(b, TRUE)] <- combn(seq(n 1), 2, function(x) condinformation(dat[, x[1]], dat[,x[2] - 1], method = 'emp'))
b[upper.tri(b)] <- b[lower.tri(b)]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/449698.html
上一篇:Python:將串列串列中的bs4.element.ResultSet元素更改為文本
下一篇:比較R中多個資料幀的唯一值
