从函数到函数调用值的问题

时间:2016-02-05 19:29:43

标签: r function call

我在连接两个功能时遇到问题。我想将GARCH函数的结果调用到LogLik函数中,这不起作用。有人可以帮忙吗? 这不是完整的代码,只是结束:

 #the basic part of this code is taken from http://www.r-bloggers.com/garch-estimation-using-maximum-likelihood/ on 2016-02-05

library(quantmod)
library(fBasics)
library(rmgarch)
library(fGarch)
library(parallel)
library(ccgarch)
library(mgarch) #from https://github.com/vst/mgarch on 2016-01-25
library(tsDyn)
library(ggplot2)
library(mvnmle)
library(rootSolve)
library(matrixcalc)

#load data, time series closing prices, 10 year sample
#DAX 30
getSymbols('^GDAXI', src='yahoo', return.class='ts',from="2005-01-01", to="2015-01-31")
GDAXI.DE=GDAXI[ , "GDAXI.Close"]
#S&P 500
getSymbols('^GSPC', src='yahoo', return.class='ts',from="2005-01-01", to="2015-01-31")
GSPC=GSPC[ , "GSPC.Close"]
#Credit Suisse Commodity Return Strat I
getSymbols('CRSOX', src='yahoo', return.class='ts',from="2005-01-01", to="2015-01-31")
CRSOX=CRSOX[ , "CRSOX.Close"]
#iShares MSCI Emerging Markets
getSymbols('EEM', src='yahoo', return.class='ts',from="2005-01-01", to="2015-01-31")
EEM=EEM[ , "EEM.Close"]

#calculating log returns of the time series, adjusting length to 2536 observations each
log_r1=diff(log(GDAXI.DE[39:2575]))
log_r2=diff(log(GSPC))
log_r3=diff(log(CRSOX))
log_r4=diff(log(EEM))

#calculating covariance matric
#constant
r_t=data.frame(log_r1,log_r2,log_r3,log_r4)   #moving timeseries connected to data frame
Sig=cov(r_t)                             #is.positive.definite(Sig)=True
#time variant
r_t1=matrix(r_t,length(r_t$log_r1),4) #moving timeseries connected to data frame
d=dim(r_t1)                       #extracting dimension from r_t to create       
Sigma and needed sequences j, k
Sigma_v=matrix(0,(d[1])*4,4)
j = seq(from=1, to=((d[1])*4)-3, by=4)
k = seq(from=4, to=(d[1])*4, by=4)
for(i in 2:(d[1]))               #loop for calculating time varying covariance matrices
{
Sigma_v[j[i-1]:k[i-1] ,1:4]=cov(r_t[(1:i), 1:4])
}
Sigma_past=Sigma_v[10057:10060, 1:4]  #is.positive.definite(Sigma_past)=True

#multivariate Garch(1,1) model
GARCH=function(theta, r_t)
{
#opening the vector of estimates and name it
theta=matrix(rep(0),36,1)
theta[1]->omega1
theta[2]->omega2
theta[3]->omega3
theta[4]->omega4
theta[5]->alpha11
theta[6]->alpha12
theta[7]->alpha13
theta[8]->alpha14
theta[9]->alpha21
theta[10]->alpha22
theta[11]->alpha23
theta[12]->alpha24
theta[13]->alpha31
theta[14]->alpha32
theta[15]->alpha33
theta[16]->alpha34
theta[17]->alpha41
theta[18]->alpha42
theta[19]->alpha43
theta[20]->alpha44
theta[21]->beta11
theta[22]->beta12
theta[23]->beta13
theta[24]->beta14
theta[25]->beta21
theta[26]->beta22
theta[27]->beta23
theta[28]->beta24
theta[29]->beta31
theta[30]->beta32
theta[31]->beta33
theta[32]->beta34
theta[33]->beta41
theta[34]->beta42
theta[35]->beta43
theta[36]->beta44
#calculating error terms and squared errors for GARCH 
e1=matrix(rep(0),length(r_t$log_r1),1)
e2=matrix(rep(0),length(r_t$log_r1),1)
e3=matrix(rep(0),length(r_t$log_r1),1)
e4=matrix(rep(0),length(r_t$log_r1),1)
ee=matrix(rep(0),(length(r_t$log_r1)*4),4)
j = seq(from=1, to=((length(r_t$log_r1))*4)-3, by=4)
k = seq(from=4, to=((length(r_t$log_r1))*4), by=4)
for(i in 1:length(r_t$log_r1))
{
 e1[i]=r_t$log_r1[i]-mean(r_t$log_r1)
 e2[i]=r_t$log_r2[i]-mean(r_t$log_r2)
 e3[i]=r_t$log_r3[i]-mean(r_t$log_r3)
 e4[i]=r_t$log_r4[i]-mean(r_t$log_r4)
 ee[(j[i]:k[i]),]=t(cbind(e1[i],e2[i],e3[i],e4[i]))%*%(cbind(e1[i],e2[i],e3[i],e4[i])) #matrix of squared residuals
 }
return(ee)
#initial values
H_t=matrix(rep(0),length(r_t$log_r1)*4,4)
H_t[1:4,]=Sigma_v[1:4,] #initial covariance matrix
omega=matrix(theta[1:4],4,1)
alpha=matrix(theta[5:20],4,4)
beta=matrix(theta[21:36],4,4)
#sequence to perform the loop
l= seq(from=1, to=((length(r_t$log_r1))*4)-3, by=4)
m= seq(from=4, to=(length(r_t$log_r1))*4, by=4)

#calculating covariance matrix of GARCH 
for(i in 2:length(r_t$log_r1))
{
 H_t[(l[i]:m[i]),]=as.vector(omega)+alpha%*%ee[(j[i-1]:k[i-1]),]+beta%*%H_t[(j[i-1]:k[i-1]),]
}
return(H_t)
data.frame(ee, H_t)
}

#log mulitvariate normal dsitribution, log-likelihood function
LogLik=function(theta, r_t)
{

问题是:我似乎无法在这里调用GARCH函数和/或使用$来调用结果。 ee和H_t是矩阵nx4。

input=GARCH(theta, r_t)
error=input$ee
Sigma=input$H_t
d=4 #combining 4 distributions
l= seq(from=1, to=((length(r_t$log_r1))*4)-3, by=4)
m= seq(from=4, to=(length(r_t$log_r1))*4, by=4)
for(i in 1:length(r_t$log_r1))
{
 LLik=-(d/2)*log(2*pi)-(d/2)*log(det(Sigma[(l[i]:m[i]),]))-(d/2)*error[(l[i]:m[i]),]%*%inv(Sigma[(l[i]:m[i]),]) #log-likelihoodfunction, multivariate
 }
return(LLik)
}  

estimates=nlm(LogLik, p=rep(1,36), hessian = TRUE, r_t=r_t) #optimization process

任何想法?

1 个答案:

答案 0 :(得分:0)

变量theta未在代码中的任何位置定义。这就是GARCH(theta, r_t)无效的原因。您需要先创建theta变量,然后再将其作为参数传递给GARCH函数。

顺便提一下,您的LogLik函数没有thetar_t的默认值以及您的最后一行调用LogLik而没有将任何变量作为参数传递。

此外,要调用函数,必须使用括号:LogLik()如果没有参数,或者您希望函数使用默认值(它当前没有)。

考虑阅读有关R函数的一些教程,例如this onethis onethis one,以便对函数进行一些练习。