列出矩阵时间总是相同的向量
list with matrices times always the same vector
我正在处理消费者价格指数 CPI,为了计算它,我必须将指数矩阵乘以相应的权重:
grossCPI77_10 <- grossIND1977 %*% weights1910/100
grossCPI82_10 <- grossIND1982 %*% weights1910/100
当然,我更希望有一个类似下面的代码:
grossIND1982 <- replicate(20, cbind(1:61))
grossIND1993 <- replicate(20, cbind(1:61))
weights1910_sc <- c(1:20)
grossIND_list <- mget(ls(pattern = "grossIND...."))
totalCPI <- mapply("*", grossIND_list, weights1910_sc)
问题是它给了我一个 1200x20 的矩阵。我期望正常矩阵 (61x20) 向量 (20x1) 乘法应该产生 20x1 向量?你能解释一下我做错了什么吗?谢谢
我不确定我是否理解你问题的所有方面(特别是关于什么应该是列,什么应该是行,以及应用叉积的顺序),但我会尝试至少涵盖一些方面。请参阅下面代码中的注释,以澄清您所做的以及您可能想要的内容。希望对您有所帮助,如果这就是您所需要的,请告诉我。
#instead of using mget, I recommend to use a list structure
#otherwise you might capture other variables with similar names
#that you do not want
INDlist <- sapply(c("1990", "1991"), function(x) {
#this is how to set up a matrix correctly, check `?matrix`
#I think your combination of cbind and rep did not give you what you wanted
matrix(rep(1:61, 20), nrow = 61)
}, USE.NAMES = TRUE, simplify = F)
weights <- list(c(1:20))
#the first argument of mapply needs to be a function, in this case of two variables
#the body of the function calculates the cross product
#you feed the arguments (both lists) in the following part of mapply
#I have repeated your weights, but you might assign different weights for each year
res <- mapply(function(x, y) {x %*% y}, INDlist, rep(weights, length(INDlist)))
dim(res)
#[1] 61 2
你的部分问题是你没有矩阵,而是 3D 数组,只有一个维度。另一个问题是 mapply 喜欢尝试将结果组合成一个矩阵,而且常量参数应该通过 MoreArgs 传递。但实际上,lapply.
更是如此
grossIND1982 <- replicate(20, cbind(1:61))[,1,]
grossIND1993 <- replicate(20, cbind(1:61))[,1,]
weights1910_sc <- c(1:20)
grossIND_list <- mget(ls(pattern = "grossIND...."))
totalCPI <- mapply("*", grossIND_list, MoreArgs=list(e2 = weights1910_sc), SIMPLIFY = FALSE)
totalCPI <- lapply(grossIND_list, "*", e2 = weights1910_sc)
我正在处理消费者价格指数 CPI,为了计算它,我必须将指数矩阵乘以相应的权重:
grossCPI77_10 <- grossIND1977 %*% weights1910/100
grossCPI82_10 <- grossIND1982 %*% weights1910/100
当然,我更希望有一个类似下面的代码:
grossIND1982 <- replicate(20, cbind(1:61))
grossIND1993 <- replicate(20, cbind(1:61))
weights1910_sc <- c(1:20)
grossIND_list <- mget(ls(pattern = "grossIND...."))
totalCPI <- mapply("*", grossIND_list, weights1910_sc)
问题是它给了我一个 1200x20 的矩阵。我期望正常矩阵 (61x20) 向量 (20x1) 乘法应该产生 20x1 向量?你能解释一下我做错了什么吗?谢谢
我不确定我是否理解你问题的所有方面(特别是关于什么应该是列,什么应该是行,以及应用叉积的顺序),但我会尝试至少涵盖一些方面。请参阅下面代码中的注释,以澄清您所做的以及您可能想要的内容。希望对您有所帮助,如果这就是您所需要的,请告诉我。
#instead of using mget, I recommend to use a list structure
#otherwise you might capture other variables with similar names
#that you do not want
INDlist <- sapply(c("1990", "1991"), function(x) {
#this is how to set up a matrix correctly, check `?matrix`
#I think your combination of cbind and rep did not give you what you wanted
matrix(rep(1:61, 20), nrow = 61)
}, USE.NAMES = TRUE, simplify = F)
weights <- list(c(1:20))
#the first argument of mapply needs to be a function, in this case of two variables
#the body of the function calculates the cross product
#you feed the arguments (both lists) in the following part of mapply
#I have repeated your weights, but you might assign different weights for each year
res <- mapply(function(x, y) {x %*% y}, INDlist, rep(weights, length(INDlist)))
dim(res)
#[1] 61 2
你的部分问题是你没有矩阵,而是 3D 数组,只有一个维度。另一个问题是 mapply 喜欢尝试将结果组合成一个矩阵,而且常量参数应该通过 MoreArgs 传递。但实际上,lapply.
更是如此grossIND1982 <- replicate(20, cbind(1:61))[,1,]
grossIND1993 <- replicate(20, cbind(1:61))[,1,]
weights1910_sc <- c(1:20)
grossIND_list <- mget(ls(pattern = "grossIND...."))
totalCPI <- mapply("*", grossIND_list, MoreArgs=list(e2 = weights1910_sc), SIMPLIFY = FALSE)
totalCPI <- lapply(grossIND_list, "*", e2 = weights1910_sc)