r - 乘矩阵 R
问题描述
我将如何乘法:
BNP.PA.Adjusted ACA.PA.Adjusted UG.PA.Adjusted
BNP.PA.Adjusted 0.010129967 0.009577789 0.007148473
ACA.PA.Adjusted 0.009577789 0.012127668 0.007340544
UG.PA.Adjusted 0.007148473 0.007340544 0.015503678
经过 :
c(0.3 , 0.2 , 0.5)
为了得到 :
BNP.PA.Adjusted ACA.PA.Adjusted UG.PA.Adjusted
BNP.PA.Adjusted 0.010129967x0.3x0.3 0.009577789x0.2x0.3 0.007148473x0.5x0.3
ACA.PA.Adjusted 0.009577789x0.3x0.2 0.012127668x0.2x0.2 0.007340544x0.5x0.2
UG.PA.Adjusted 0.007148473x0.3x0.5 0.007340544x0.2x0.5 0.015503678x0.5x0.5
我尝试使用%*%
:MaMatrix <- cov_m %*% Poids
但这只是
BNP.PA.Adjusted ACA.PA.Adjusted UG.PA.Adjusted
BNP.PA.Adjusted 0.010129967x0.3 0.009577789x0.2 0.007148473x0.5
ACA.PA.Adjusted 0.009577789x0.3 0.012127668x0.2 0.007340544x0.5
UG.PA.Adjusted 0.007148473x0.3 0.007340544x0.2 0.015503678x0.5
我错过了什么?
解决方案
outer
上一个选项vector
然后直接相乘
m1 <- outer(v1, v1)
或如@user20650 建议的那样 ( tcrossprod
)
m1 <- tcrossprod(v1)
cov_m * m1
-输出
# BNP.PA.Adjusted ACA.PA.Adjusted UG.PA.Adjusted
#BNP.PA.Adjusted 0.0009116970 0.0005746673 0.0010722709
#ACA.PA.Adjusted 0.0005746673 0.0004851067 0.0007340544
#UG.PA.Adjusted 0.0010722709 0.0007340544 0.0038759195
数据
v1 <- c(0.3, 0.2, 0.5)
cov_m <- structure(c(0.010129967, 0.009577789, 0.007148473, 0.009577789,
0.012127668, 0.007340544, 0.007148473, 0.007340544, 0.015503678
), .Dim = c(3L, 3L), .Dimnames = list(c("BNP.PA.Adjusted", "ACA.PA.Adjusted",
"UG.PA.Adjusted"), c("BNP.PA.Adjusted", "ACA.PA.Adjusted", "UG.PA.Adjusted"
)))
推荐阅读
- java - 插入时间戳时出现 ORA-01858 错误
- c# - 如何检查 Firestore 客户端是否离线并在 Unity 上更改为在线?
- arrays - 邮递员测试 - 验证数组中的值
- matlab - Matlab:获取表格中每个组的前/后 2 个排名观察值
- javascript - 在赛普拉斯测试规范中读取动态文件名
- node.js - 猫鼬在 findById 上给出错误无效类型
- sql-server - C#:如何单独查询 SQL Server?
- r - 在 R 中使用 ggplot 在绘图上绘制形状?
- smarty - 在 CS-Cart 中使用 Smarty 检查页面上是否存在网格类
- clean-architecture - 依赖关系向内显示是什么意思?