【问题标题】:R language: Computer Covariance matrices of a 3-d array dataR 语言:3-d 数组数据的计算机协方差矩阵
【发布时间】:2021-02-07 20:41:29
【问题描述】:

如何在 iris 数据集(3-d 数组数据集)上使用 r 中的 apply() 函数来计算变量萼片长度、萼片宽度、花瓣长度和花瓣宽度的协方差矩阵 Setosa、Versicolor 和 Virginica 三种物种中的每一种,都表示为 3-d 数组。

这是数据集的前 6 行

, , Setosa
      Sepal L. Sepal W. Petal L. Petal W.
[1,]      5.1      3.5      1.4      0.2
[2,]      4.9      3.0      1.4      0.2
[3,]      4.7      3.2      1.3      0.2
[4,]      4.6      3.1      1.5      0.2
[5,]      5.0      3.6      1.4      0.2
[6,]      5.4      3.9      1.7      0.4

, , Versicolor
      Sepal L. Sepal W. Petal L. Petal W.
[1,]      7.0      3.2      4.7      1.4
[2,]      6.4      3.2      4.5      1.5
[3,]      6.9      3.1      4.9      1.5
[4,]      5.5      2.3      4.0      1.3
[5,]      6.5      2.8      4.6      1.5
[6,]      5.7      2.8      4.5      1.3

, , Virginica
      Sepal L. Sepal W. Petal L. Petal W.
[1,]      6.3      3.3      6.0      2.5
[2,]      5.8      2.7      5.1      1.9
[3,]      7.1      3.0      5.9      2.1
[4,]      6.3      2.9      5.6      1.8
[5,]      6.5      3.0      5.8      2.2
[6,]      7.6      3.0      6.6      2.1

【问题讨论】:

  • 在 R 中,iris 对象是一个 data.frame,而不是一个数组。确实存在 iris3 对象,它是一个 3d 数组。

标签: r apply covariance-matrix


【解决方案1】:

这可以通过遍历最后一个维度的顺序来实现,因为lapply 默认会遍历所有项目:

lapply(1:dim(arr)[3], function(i) cov(arr[,,i]))
[[1]]
             Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length   0.12424898 0.099216327  0.016355102 0.010330612
Sepal.Width    0.09921633 0.143689796  0.011697959 0.009297959
Petal.Length   0.01635510 0.011697959  0.030159184 0.006069388
Petal.Width    0.01033061 0.009297959  0.006069388 0.011106122

[[2]]
             Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length   0.26643265  0.08518367   0.18289796  0.05577959
Sepal.Width    0.08518367  0.09846939   0.08265306  0.04120408
Petal.Length   0.18289796  0.08265306   0.22081633  0.07310204
Petal.Width    0.05577959  0.04120408   0.07310204  0.03910612

[[3]]
             Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length   0.40434286  0.09376327   0.30328980  0.04909388
Sepal.Width    0.09376327  0.10400408   0.07137959  0.04762857
Petal.Length   0.30328980  0.07137959   0.30458776  0.04882449
Petal.Width    0.04909388  0.04762857   0.04882449  0.07543265

如果您希望输出为 3-d 数组

sapply(1:dim(arr)[3], function(i) cov(arr[,,i]), simplify="array") -> covar
, , 1

             Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length   0.12424898 0.099216327  0.016355102 0.010330612
Sepal.Width    0.09921633 0.143689796  0.011697959 0.009297959
Petal.Length   0.01635510 0.011697959  0.030159184 0.006069388
Petal.Width    0.01033061 0.009297959  0.006069388 0.011106122

, , 2

             Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length   0.26643265  0.08518367   0.18289796  0.05577959
Sepal.Width    0.08518367  0.09846939   0.08265306  0.04120408
Petal.Length   0.18289796  0.08265306   0.22081633  0.07310204
Petal.Width    0.05577959  0.04120408   0.07310204  0.03910612

, , 3

             Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length   0.40434286  0.09376327   0.30328980  0.04909388
Sepal.Width    0.09376327  0.10400408   0.07137959  0.04762857
Petal.Length   0.30328980  0.07137959   0.30458776  0.04882449
Petal.Width    0.04909388  0.04762857   0.04882449  0.07543265
# if you want to add names
dimnames(covar)[3] <- dimnames(arr)[3]

或关注@G.Grothendieck 的评论

array(apply(arr, 3, var), dim(arr)[c(2, 2, 3)], dimnames(arr)[c(2, 2, 3)])

数据

simplify2array(by(iris[-5], iris$Species, as.matrix)) -> arr

【讨论】:

  • @G.Grothendieck 谢谢我调整了它以解决暗名
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-13
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
  • 2011-05-23
  • 1970-01-01
相关资源
最近更新 更多