rolling autocorrelation function for a R dataframe -


i have r dataframe, , need compute autocorrelation every column of several rolling time windows. used following solution

myacf=function(x,lag){ return(acf(x, na.action=na.pass,lag.max=lag)[lag]) }  for(i in 2:dim(dfres)[1]){   print(i)   col=rollapply(as.numeric(dfres[,i]),width=oneday,fun=myacf,lag=oneday)  } 

where dfres matrix (i exclude first column since contains timestamps), , rollapply package zoo. obtain following error: error in plot.window(need finite 'ylim' values). anyway don't need plots, values of autocorrelation in chosen lag. can me?

with lapply can operate rollapply function on each column resulting in acf series selected lag value. use reduce combine results above step.

i have used dataset edhec performanceanalytics package demo. can change width parameter accordingly.

library("performanceanalytics")  #load test dataset data(edhec,package="performanceanalytics")  #select subset fewer columns edhec_sub = edhec[,1:5]  fn_lag_acf = function(lagvalue = x) {   #for width 1 year calculate acf input lagvalue each column  acflist = lapply(edhec_sub,function(x) {    ts = rollapply(x, width = 12,  fun = function(z) acf(z,na.action=na.pass,lag.max= lagvalue,plot=false)$acf[lagvalue],                 by.column = false, align = "right")  colnames(ts) = colnames(x)                return(ts)   })  #combine acf output columns above step acfmerge  = reduce(function(x,y) merge.xts(x,y), acflist)  return(acfmerge) }  #test lagvalue = 2 lag2df = fn_lag_acf(lagvalue = 2) 

output:

head(lag2df,15) #           convertible.arbitrage cta.global distressed.securities emerging.markets #1997-01-31                    na         na                    na               na #1997-02-28                    na         na                    na               na #1997-03-31                    na         na                    na               na #1997-04-30                    na         na                    na               na #1997-05-31                    na         na                    na               na #1997-06-30                    na         na                    na               na #1997-07-31                    na         na                    na               na #1997-08-31                    na         na                    na               na #1997-09-30                    na         na                    na               na #1997-10-31                    na         na                    na               na #1997-11-30                    na         na                    na               na #1997-12-31             0.5560540 -0.3010264            0.02908761        0.3305791 #1998-01-31             0.5055951 -0.4245876            0.04278214        0.1761287 #1998-02-28             0.5195872 -0.4298767            0.01375580        0.1605579 #1998-03-31             0.5070003 -0.4656213           -0.04519778        0.2061610 #           equity.market.neutral #1997-01-31                    na #1997-02-28                    na #1997-03-31                    na #1997-04-30                    na #1997-05-31                    na #1997-06-30                    na #1997-07-31                    na #1997-08-31                    na #1997-09-30                    na #1997-10-31                    na #1997-11-30                    na #1997-12-31           -0.11842164 #1998-01-31           -0.05986578 #1998-02-28           -0.09663855 #1998-03-31           -0.09680819 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -