如何计算多维均值矩阵?

时间:2018-11-20 13:24:26

标签: r multidimensional-array apply mean correlation

我想一直计算动态条件相关(dcc)均值矩阵。 Rho是每天的矩阵。 但我想整天(不是每天)做一个平均矩阵

这是DCC-garch模型。

library(rugarch)
library(rmgarch)
library(xts)
stockdata<-read.zoo("C:\\Users\\Taehee Cha\\Desktop\\\\data\\EUCall.csv",
                header=TRUE,
                sep=",",
                format = "%Y-%m-%d", 
                nrow=4433)
stock_xts<-as.xts(stockdata,dateFormat='POSIXct')

DKOSPI200 <-diff(log(stock_xts$KOSPI200))*100
DDAX30 <-diff(log(stock_xts$DAX30))*100
DSNP500 <-diff(log(stock_xts$SNP500))*100
DFTSE100  <-diff(log(stock_xts$FTSE100))*100
...
data2<-data.frame(DKOSPI200,DDAX30,DSNP500,DFTSE100,DEUROSTOXX50,DFTSEMIB,DNIKKEI225,DSNPTSX,DCAC40,DJSE40,
  DRTS,DIPC,DBOVESPA,DTadawul,DMERV,DSENSEX,DJSX,DSSE,DXU100,DSNPASX,
  DATHEX,DAEX,DOBX,DSNPNZ50,DTAIEX,DOMXC20,DKLCI,DBEL20,DOMXS30,DSMI,
  DIBEX,DSTI,DADX,DISEQ,DATX,DTA35,DEGX30,DPX50,DIPSA,DQE,
  DIGBC,DSET,DKarachi100,DSNPBVL,DPSI20,DWIG20,DOMXH25,DPSEI,DBUX,DHANGSENG)

Univarate GARCH规范

gjrGARCH.spec <-ugarchspec(variance.model=list(model="gjrGARCH",garchOrder=c(1,1)),mean.model=list(armaOrder=c(1,0)))

多变量GARCH规范

dcc.gjrGARCH.spec=dccspec(uspec=multispec(replicate(50,gjrGARCH.spec)),
                      dccOrder=c(1,1),
                      distribution="mvnorm")       
OUTCOME=dccfit(dcc.gjrGARCH.spec, data=data2)
Fit<-fitted(OUTCOME)
Sigma<-sigma(OUTCOME)
Rho<-rcor(OUTCOME)

dim(Rho)
>50 50 4432

我尝试过,但是失败了。

dccmean<-apply(Rho, c(1:4432), mean)

错误消息:

Error in if (d2 == 0L) { : missing value where TRUE/FALSE needed

请帮助我。...

> dput(head(data2))

structure(list(KOSPI200 = c(-0.64107155376405, -2.63428514277591, 
-3.02361881202007, -0.348129159251176, -0.8463497185911, 0.933495693605124
), DAX30 = c(-0.275158289647415, -2.31165589473132, 0.132288230298805, 
-0.916302412003311, -0.249954511741102, 1.5067409708557), SNP500 = c(0, 
-1.23978129442266, -2.37787312356224, 0.68553903957973, -1.45026278352685, 
-0.113477591886735), FTSE100 = c(-0.912643889420117, -1.27612498082748, 
-0.189622000351441, -0.0175554735125871, -1.40042215908522, 1.65029716900129
), EUROSTOXX50 = c(-0.693995680230763, -2.63873467285407, 0.0340923169277474, 
-1.10692637764913, -1.45688992650648, 0.90690109636018), FTSEMIB = c(-0.207307997135331, 
-2.20987523065315, 0.11929602916414, -0.591164923299203, -1.23334692081265, 
0.0674451667508791), NIKKEI225 = c(-0.172132904979705, -2.41825620786038, 
-0.541031623946608, 0.494946220458736, -2.43002026761108, 3.30050037277818
), SNPTSX = c(-0.228827286223954, -0.618158165738869, -1.163192751949, 
0.611234081963197, -1.07887096999253, 0.15982056970163), CAC40 = c(-0.996225333005363, 
-2.45923020374033, 0.558708030675348, -1.07932737047562, -1.18770661796788, 
0.946736097241008), JSE40 = c(-1.24801019778307, -0.660388491185593, 
-0.378077731086712, 1.12913917165454, -0.270357667812782, 0.925230160391344
), RTS = c(1.69252595201295, -2.47912051247043, -1.68278329224893, 
-0.628193976078784, -5.81832854668214, 2.33324601133127), IPC = c(0.410660821053099, 
-0.793221688816637, -1.46058560736098, -0.0995162816563422, 0.0582618177329408, 
-2.24304020680233), BOVESPA = c(-2.08399544316435, -0.0640501036023977, 
-1.00884481719454, 0, -2.45326225130107, 1.76775969319749), Tadawul = c(-0.0751612871260576, 
1.06988441714364, 0.467558379080923, 0.015629948658713, -0.479468927554549, 
0.115645625170213), MERV = c(-0.812834284218145, -3.48526114996641, 
-0.789176542098158, 0, -6.3347627637631, -2.25802417775345), 
    SENSEX = c(0.173466860804083, -0.357822108056638, -0.45387164272519, 
    1.1318914393021, 1.4301129114795, 2.24172363852979), JSX = c(0.464645515331874, 
    0.433624214120076, 0.382746462253802, 0.245017602482989, 
    0.417626097951018, 0.919117215344212), SSE = c(-0.9304947624722, 
    -0.512019911931727, -0.0399062163143782, 0.923519675548068, 
    -0.965701174169897, -0.149922817153048), XU100 = c(0.83997183599358, 
    -9.44220433432204, -1.19486088286003, -5.93037167728525, 
    -8.13019316543659, 4.39805147087551), SNPASX = c(-1.52481837941529, 
    -0.710076219124112, -0.355441763724151, 0.43889183264465, 
    0.632546988595983, 0.431277069387725), ATHEX = c(0.217251999604962, 
    -0.113093185749946, -4.08702446400842, -1.49759637062132, 
    -2.89449541976028, 2.84821759239637), AEX = c(-0.302724751872496, 
    -1.93350674725812, -0.334531789971226, -0.27844233175518, 
    -1.47467944242914, 0.59320521917563), OBX = c(-0.633345799178109, 
    -0.0872437271016935, -0.0686020794624653, -0.751459482064121, 
    -0.314782430663918, -0.138818800687446), SNPNZ50 = c(-0.589289243578772, 
    -0.212470628567818, -1.09525597597484, 1.56062961309287, 
    -0.594970797797778, 0.308439154148132), TAIEX = c(-0.634186259073921, 
    -0.0465155309724352, -1.06170058328026, -0.875355087660168, 
    -1.4930992055346, 1.85698166429091), OMXC20 = c(0.0415607672345608, 
    -0.892547743122307, 0.895744009882993, 0.00639222705407505, 
    -0.515886239547392, -0.744864387047794), KLCI = c(3.12710622798287, 
    1.55475618644338, 0.141888091655584, -0.2344629562125, -1.32130772048118, 
    -1.3324408754646), BEL20 = c(0.916239489510673, -0.838871120292239, 
    -0.349848101836958, 0.131119279926573, -0.220949965581685, 
    0.925072128183135), OMXS30 = c(-2.30694346428235, -3.59314374436401, 
    -0.132526843920378, -0.26558175544098, -0.700219536449254, 
    2.21356560948696), SMI = c(-0.923178651961543, -1.9888683347407, 
    0.190440901601185, -1.31581319814948, -1.24166996356809, 
    1.74938111396603), IBEX = c(-1.10228247337272, -3.30519689438287, 
    0.28541077977593, -0.719194686241131, -2.37890150982238, 
    -1.20979032592885), STI = c(-0.376825693193794, -1.56959227075815, 
    -0.209405887533087, 1.59189359203387, -0.998471722830718, 
    0.524545937488519), ADX = c(0, 0.234110707042312, -0.140988509294804, 
    -0.107833632972643, -0.00392341494084292, 0), ISEQ = c(-0.497210204416199, 
    -1.28827940816549, 1.05427460869301, 0.691923594974142, -1.2985333059083, 
    1.05885040639215), ATX = c(0.196176688098504, -0.397720264421864, 
    0.765032409498989, 0.634860192671294, -0.718109388684507, 
    0.288191756274081), TA35 = c(-0.467643907976623, -1.6933214269244, 
    0.638360404963567, 1.66873087937374, -1.03207784150818, 1.98476257832283
    ), EGX30 = c(-1.60808620054533, 0.486603179063838, 0.042060989053283, 
    -0.582335360174024, -1.05485347621963, 0.329916835288824), 
    PX50 = c(0, 0, -1.53717800478548, -0.567132576686191, -1.79645549752987, 
    0.100654261140143), IPSA = c(0.561439712306022, 0.849792667039129, 
    -0.327031029818503, -0.154168073702365, -0.737696755949457, 
    -0.476817106576277), QE = c(0.124155575561335, -0.299696872832733, 
    -0.087886339332055, 0.0366286953290995, 0.109805655043083, 
    0.0585137524009127), IGBC = c(0.151884596926966, -0.461362987666014, 
    1.12811837108895, 0.0931901841028981, 0.149517561508272, 
    0.312176099410877), SET = c(-0.475964725298805, 0, -0.126280143061397, 
    -0.243770852799319, -3.47030045650181, 0.752026997121558), 
    Karachi100 = c(-1.2595250361283, -1.09859841657665, -0.467295948347779, 
    -0.176282035250352, 1.06144685237481, 0.614077017080827), 
    SNPBVL = c(-0.0916272845215893, -0.488603970907153, -0.635609686024718, 
    0.72544080956467, -1.05989054433602, -0.290056131898186), 
    PSI20 = c(-0.498150623441695, -2.4995090145449, -0.916173928749231, 
    0.134620833204835, -1.96674118284665, 0.125363501017972), 
    WIG20 = c(-0.186154209522993, 2.29090171954054, -1.21234613135197, 
    -2.35741884661973, -1.39537944100319, 4.04050146384076), 
    OMXH25 = c(-0.734161177012371, -3.2781107280087, -0.274240630336653, 
    0.164265778301331, -2.88555610228327, 0.0196123710402674), 
    PSEI = c(1.16491407781849, -0.379401344663943, 0.604503127264966, 
    -0.551543468404248, -0.542372265371061, 0.668217806598204
    ), BUX = c(0.745432319945749, -0.407296683745706, 1.4847908668413, 
    -1.73656177766137, -1.70838647119353, 0.642365839539494), 
    HANGSENG = c(-1.58777648289217, 0, -2.40414908704771, 0.182801734695737, 
    -1.47377260414583, 1.05050572830176)), row.names = c("2001-07-05", 
"2001-07-06", "2001-07-09", "2001-07-10", "2001-07-11", "2001-07-12"
), class = "data.frame")

我认为解决我的问题会更有用。

dim(Rho)
>50 50 4432

> dput(head(Rho))
c(1, 0.28274756492168, 0.366762864615763, 0.287187938929745, 
0.279313675150249, 0.25099353597608)

> str(Rho)
 num [1:50, 1:50, 1:4432] 1 0.283 0.367 0.287 0.279 ...
 - attr(*, "dimnames")=List of 3
  ..$ : chr [1:50] "KOSPI200" "DAX30" "SNP500" "FTSE100" ...
  ..$ : chr [1:50] "KOSPI200" "DAX30" "SNP500" "FTSE100" ...
  ..$ : chr [1:4432] "2001-07-05" "2001-07-06" "2001-07-09" "2001-07-10" ...

我的问题

#, , "2001-07-05"

#       KOSPI200 DAX30 SNP500 FTSE100
#KOSPI200    1     5     9    13
#DAX30       2     6    10    14
#SNP500      3     7    11    15
#FTSE100     4     8    12    16

#, , "2001-07-06"

#       KOSPI200 DAX30 SNP500 FTSE100
#KOSPI200    1     5     9    13
#DAX30       2     6    10    14
#SNP500      3     7    11    15
#FTSE100     4     8    12    16

##----->I want to this ((ex)sum) 

#, , 

#       KOSPI200 DAX30 SNP500 FTSE100
#KOSPI200    2     10    18    26
#DAX30       4     12    20    28
#SNP500      6     14    22    30
#FTSE100     8     16    24    32

1 个答案:

答案 0 :(得分:1)

您的应用程序调用的MARGIN中存在代码错误。请勿使用c(1:4412),而应使用第一维和第二维的实际边距(因此c(1,2)

在您问题的最后一个示例中:

day1 <- matrix(1:16, 4, 4, dimnames = list(letters[1:4], LETTERS[1:4]))
day2 <- matrix(1:16, 4, 4, dimnames = list(letters[1:4], LETTERS[1:4]))

Rho <- abind::abind(day1=day1,day2=day2, along = 3)

apply(Rho, c(1,2), sum)
  A  B  C  D
a 2 10 18 26
b 4 12 20 28
c 6 14 22 30
d 8 16 24 32

提供所需的输出。