r - Problems wit calling values from function to function -


i have problems connecting 2 functions. want call results garch function loglik function, doesn't work. can help? not complete code end:

 #the basic part of code taken 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 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 time series, adjusting length 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 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 data frame d=dim(r_t1)                       #extracting dimension r_t create        sigma , 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 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 vector of estimates , name 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 , squared errors 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 perform 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) { 

here problem: seems cannot call garch function here and/or use $ call results. ee , h_t matrices 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 

any ideas?

the variable theta not defined anywhere in code. that's why garch(theta, r_t) isn't working. need create theta variable before pass garch function argument.

incidentally, loglik function has no default values theta or r_t , last line calls loglik without passing variables arguments.

further, invoke function, must use bracket notation: loglik() if there no arguments or if want function use default values (which doesn't have).

consider reading tutorials on r functions such this one, or this one, or this one practice functions.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -