在 R 中创建包含表的列表
我在r
中有以下数据集,我想计算香农的熵。为了做到这一点,因为数据是连续的,我必须离散它们。 软件包的函数,可以计算$ x_1 $和$ x_2 $之间的熵
set.seed(1234)
data <- matrix(rnorm(150 * 11, mean = 0, sd = 1), 150, 11)
library(entropy)
dis <- discretize2d(data[,1],data[,2], numBins1 = 10, numBins2 = 10)
entropy(dis)
使用ivatize2d
熵
>包含所有iptize2d
data
之间的结果熵(DIS)
。有人可以帮我编码吗?
I have the following dataset in R
and I want to calculate Shannon's entropy. In order to do that since the data are continuous, I have to discretise them. Using the discretize2d
function of Entropy
package, the entropy between $X_1$ and $X_2$ can be calculated as follows:
set.seed(1234)
data <- matrix(rnorm(150 * 11, mean = 0, sd = 1), 150, 11)
library(entropy)
dis <- discretize2d(data[,1],data[,2], numBins1 = 10, numBins2 = 10)
entropy(dis)
I want to create a list
containing all the discretize2d
results between between the variables of data
so i can later just use entropy(dis$1.2)
and getting the same result as entropy(dis)
. Can someone help me code it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个全基R解决方案。我们使用
combn(x,m)
函数来生成x
的元素的所有组合,并带有sizem
。在这里,我们需要对m = 2
。这将创建2 x 55矩阵。然后将应用()
在该矩阵的列上迭代应用distize2d()
。apply()
的第二个参数为2,意思是在列上应用。我们还指定Simplify = false
,以便结果将留下列表,而不是被胁迫到数组。如果您需要像指定的元素的名称,例如
dis $ 1.2
,您可以执行此操作:最后,您还可以使用
lapply()
一次计算所有元素的熵:Here is an all-base R solution. We use the
combn(x, m)
function to generate all combinations of the elements ofx
with sizem
. Here we want pairs som = 2
. This creates a 2 by 55 matrix. Then useapply()
to iteratively applydiscretize2d()
over columns of that matrix. The second argument ofapply()
is 2, meaning to apply over columns. We also specifysimplify = FALSE
so that the result will stay a list rather than being coerced to an array.If you want names for the elements like you specified, such as
dis$1.2
, you can do this:Finally you could also calculate entropy for all elements at once with
lapply()
: