这是我的代码:
gross_ind00 <- ts(gross_ind00, frequency=12, start=c(2000,5), end=c(2005,12)) # CPI indices as time series object
grossCPI_00 <- window(gross_ind00, start=c(2000,5), end=c(2001,11)) %*% bfs_wei01
grossCPI_01 <- window(gross_ind00, start=c(2001,12), end=c(2002,11)) %*% bfs_wei02
grossCPI_02 <- window(gross_ind00, start=c(2002,12), end=c(2003,11)) %*% bfs_wei03
grossCPI_03 <- window(gross_ind00, start=c(2003,12), end=c(2004,11)) %*% bfs_wei04
grossCPI_04 <- window(gross_ind00, start=c(2004,12), end=c(2005,11)) %*% bfs_wei05
grossCPI_00_04 <- rbind(grossCPI_00, grossCPI_01, grossCPI_02, grossCPI_03, grossCPI_04)
你可以帮助我在循环中制定它吗?因为会有比上面更多的计算。并且有一种聪明的方法可以使用rbind()
而无需键入所有变量并输入类似:grossCPI_00:grossCPI_04
的内容?
感谢您的帮助
修改
> grossCPI_01
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
Dec 2001 104.9665 103.4361 101.8134 99.7197 101.7446 107.5090 99.6738 98.7074 105.2485 85.1300 90.1479 102.0818
Jan 2002 105.8917 101.4038 101.8614 100.6512 102.4002 105.7810 100.1525 98.7763 107.8663 84.4599 97.5025 102.0818
Feb 2002 105.0981 103.9143 101.6033 100.3654 101.9308 107.5860 99.3037 98.8399 107.6946 86.3123 96.6006 102.0818
Mar 2002 105.5574 104.0253 101.9319 99.0505 102.1732 107.1950 99.4989 98.8939 108.3773 87.3760 97.6724 105.6082
Apr 2002 106.3276 103.8253 101.9319 100.6256 101.8731 107.6447 99.5027 99.2162 109.6664 91.9696 97.5324 105.6082
May 2002 106.6286 103.2401 101.5830 100.6036 102.3719 106.2351 99.6822 99.2288 109.9690 93.0864 91.0591 105.6082
Jun 2002 106.2936 103.7086 101.3151 99.5129 102.5284 106.3006 100.4572 97.5428 108.5862 95.1738 96.5508 105.5038
Jul 2002 106.7085 103.6397 100.3745 100.6200 102.6327 107.4643 99.5623 99.1128 110.4853 94.4184 96.0726 105.5038
Aug 2002 106.5437 103.7663 101.5227 100.5942 102.7949 107.4828 98.7577 99.1182 109.0161 87.2811 94.8701 105.5038
Sep 2002 107.0305 103.4038 100.1747 100.5645 102.7173 106.2626 100.2282 99.1922 109.0022 85.3988 96.1315 105.2658
Oct 2002 106.5857 103.6162 99.4436 100.6370 102.5899 107.3943 98.8139 98.9612 108.7265 82.8484 95.6088 105.2658
Nov 2002 106.4154 103.5076 101.3215 97.1657 102.5914 107.1845 98.0560 97.8102 110.8636 81.5943 97.2104 105.2658
[,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23]
Dec 2001 105.1867 100.5958 102.3753 101.2844 97.1591 99.2814 102.1979 99.9278 103.7216 98.3511 100.6884
Jan 2002 108.1107 101.1786 103.9114 101.2844 89.7376 98.5510 98.8642 99.1857 103.7216 99.5307 100.6884
Feb 2002 108.2704 97.7893 103.7419 101.2844 89.7376 98.5510 98.8642 99.1857 103.9693 97.2546 100.6884
Mar 2002 108.2179 101.4996 104.4166 102.7570 89.7376 98.5510 98.8642 99.1857 103.9693 97.4405 100.7333
Apr 2002 108.2138 101.5872 104.3795 102.7570 98.2723 99.7893 101.1562 100.3661 103.9693 100.0432 100.7333
May 2002 107.8086 101.9348 103.1930 102.7570 98.2723 99.7893 101.1562 100.3661 104.1273 98.9859 100.7333
Jun 2002 107.7915 98.1612 104.4929 102.3941 98.2723 99.7893 101.1562 100.3661 104.1273 97.3056 100.9370
Jul 2002 106.1154 102.3521 104.4085 102.3941 90.3767 91.5404 99.6054 98.7413 104.1273 97.4783 100.9370
Aug 2002 107.8245 100.0784 104.1411 102.3941 90.3767 91.5404 99.6054 98.7413 104.2599 97.6829 100.9370
Sep 2002 107.8366 104.3089 104.4100 103.5287 90.3767 91.5404 99.6054 98.7413 104.2599 100.0216 100.8746
Oct 2002 107.8669 104.3829 104.3029 103.5287 99.0611 100.7646 102.9385 100.6700 104.2599 100.3649 100.8746
Nov 2002 104.9207 104.4966 104.6439 103.5287 99.0611 100.7646 102.9385 100.6700 104.2757 97.6168 100.8746
> grossCPI_02
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
Dec 2002 106.6044 103.1283 101.1823 100.4916 102.6032 105.3560 98.6483 99.1127 105.0623 86.5832 89.5190 105.2499
Jan 2003 107.5218 98.2756 100.8939 99.0749 102.8919 105.6646 99.9622 99.4528 105.3201 86.1198 96.3271 105.2499
Feb 2003 106.4162 102.7641 99.2874 99.4870 102.7216 107.4768 98.2725 99.5960 106.5410 87.0821 96.5550 105.2499
Mar 2003 106.8106 103.5342 100.1826 93.9405 102.0828 107.1432 98.9547 100.0666 107.1084 89.7789 96.8088 105.0399
Apr 2003 107.4836 102.7221 100.1019 99.3108 102.6645 107.2733 99.1892 100.2306 108.9895 92.4775 91.2679 105.0399
May 2003 107.5729 102.1431 100.3554 96.4541 102.6620 107.2511 99.1769 100.1562 108.4113 93.0043 95.9989 105.0399
Jun 2003 108.4867 102.7251 100.3031 94.7790 102.5488 106.8081 99.0674 99.3307 106.4398 93.7393 96.1834 105.3662
Jul 2003 108.7554 102.2346 100.3070 95.3694 102.3184 106.8202 98.2513 100.3056 92.2648 97.8009 97.1591 105.3662
Aug 2003 108.4638 103.0729 99.0946 96.9491 102.7773 106.9161 98.8583 100.2918 109.1960 95.0525 95.2308 105.3662
Sep 2003 107.4662 103.1500 99.0786 97.6044 102.9359 105.0846 99.3730 100.3653 108.7968 88.6643 95.5587 105.5445
Oct 2003 107.5281 103.2310 100.1712 94.8571 103.0989 106.8530 99.2727 100.3706 107.1306 88.1945 96.9795 105.5445
Nov 2003 108.7486 101.6296 100.0896 96.6006 103.2181 106.7691 99.8913 100.2603 109.4507 87.4985 91.8802 105.5445
[,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23]
Dec 2002 107.5236 104.5921 103.6836 101.3709 99.0611 100.7646 102.9385 100.6700 104.2757 98.3814 100.6350
Jan 2003 107.5045 99.6215 105.1748 101.3709 91.1114 98.3001 101.1790 96.2117 104.2757 101.8136 100.6350
Feb 2003 107.4993 104.7981 105.1514 101.3709 91.1114 98.3001 101.1790 96.2117 104.2573 100.8644 100.6350
Mar 2003 106.5980 105.2527 105.4720 103.5786 91.1114 98.3001 101.1790 96.2117 104.2573 106.2332 101.3015
Apr 2003 106.5720 106.5178 104.2995 103.5786 96.0893 103.5013 103.2663 99.1538 104.2573 99.9010 101.3015
May 2003 107.1855 106.5007 105.3680 103.5786 96.0893 103.5013 103.2663 99.1538 104.2632 96.7683 101.3015
Jun 2003 107.1865 106.5488 105.2877 103.8823 96.0893 103.5013 103.2663 99.1538 104.2632 96.2260 101.5796
Jul 2003 107.2727 102.4202 105.1842 103.8823 88.1028 101.5373 100.7837 96.6361 104.2632 97.2961 101.5796
Aug 2003 107.3353 106.6072 105.0816 103.8823 88.1028 101.5373 100.7837 96.6361 104.5899 98.8071 101.5796
Sep 2003 106.6309 106.0360 105.1103 103.4566 88.1028 101.5373 100.7837 96.6361 104.5899 99.4594 101.6452
Oct 2003 106.6141 106.7187 105.3818 103.4566 94.6828 104.7330 103.6390 101.4173 104.5899 99.6677 101.6452
Nov 2003 106.4101 106.1527 104.3122 103.4566 94.6828 104.7330 103.6390 101.4173 104.6186 98.3999 101.6452
> bfs_wei02
[1] 0.072879964 0.004212255 0.010755822 0.004159271 0.022730283 0.004291732 0.003761888 0.016266193 0.004609638
[10] 0.002225342 0.004344716 0.001404085 0.001006703 0.009590166 0.025935836 0.029856677 0.022544838 0.003258537
[19] 0.009007338 0.002755186 0.532307203 0.091000609 0.121095716
> bfs_wei03
[1] 0.074217907 0.004126214 0.010167206 0.004234088 0.023193096 0.004395901 0.003667745 0.016855448 0.004584682
[10] 0.002535059 0.004719525 0.001429342 0.001051780 0.009466019 0.026779935 0.029908306 0.024083064 0.003667745
[19] 0.008549083 0.002292341 0.530501618 0.088484358 0.121089536
我想将grossCPI01矩阵与bfs_wei02矩阵相乘,等等..
答案 0 :(得分:0)
我们在开始时提供一些测试数据(将来请在您的问题中提供)和:
bfs_wei
g
mapply
将列表s
的第i个组件与列表bfs_wei
的第i个组件合并为所有i以使用%*%
通常,我们会用两个参数的函数替换%*%
,这两个参数执行所需的任何计算。
# test data
bfs_wei01 <- 1:19
bfs_wei02 <- bfs_wei03 <- bfs_wei04 <- bfs_wei05 <- 1:12
tt <- ts(1:68, freq = 12, start = c(2000, 5))
bfs_wei <- mget(ls(pattern = "bfs_wei.."))
g <- cumsum(cycle(tt) == 12 & time(tt) > 2001)
s <- head(split(tt, g), -1)
names(s) <- 2001:2005
mapply("%*%", s, bfs_wei)