[R-SIG-Mac] xts timeseries merge

Ken Beath ken at kjbeath.com.au
Sat Nov 14 08:04:52 CET 2009


Please send this to the r-help mailing list, as it is a general R question.

Ken

On 14/11/2009, at 11:27 AM, Jan Daniel Semrau wrote:

> Hi,
> 
> I try to calculate the correlation between macroeconomic data from  
> FRED vs Market Data
> However, since the timeseries are not in synch, the correlation fails.
> 
> USPBS =get(getSymbols("USPBS", src="FRED" ))
> USPBS = USPBS['1983-1-1::']
> monDMANEMP = Cl(to.monthly(USPBS))
> 
>> length(monDMANEMP)
> [1] 312
> 
>> head(monDMANEMP)
>          USPBS.Close
> Oct 1983        8192
> Nov 1983        8231
> Dec 1983        8265
> Jan 1984        8309
> Feb 1984        8344
> Mar 1984        8382
> 
> symbol = getSymbols("^DJA",from = "1983-01-01",to=Sys.Date())
> market = Cl(get(symbol))
> monMarket = Cl(to.monthly(market))
> 
>> length(monMarket)
> [1] 314
>> head(monMarket)
>          market.Close
> Oct 1983       494.18
> Nov 1983       510.77
> Dec 1983       502.94
> Jan 1984       483.46
> Feb 1984       454.97
> Mar 1984       456.65
> 
> 
> Now I try to calculate the correlation
> 
>> cor(monMarket, monDMANEMP)
> Error in cor.default(monMarket, monDMANEMP) : incompatible dimensions
> 
> #This makes sense since my "Market" data is two data points longer.
> #However if I try to merge them to get them aligned on one time axis,  
> I get this
> 
>> head(merge.xts(monMarket, monDMANEMP))
>          market.Close USPBS.Close
> Oct 1983       494.18          NA
> Nov 1983           NA        8192
> Nov 1983       510.77          NA
> Dec 1983           NA        8231
> Dec 1983       502.94          NA
> Jan 1984           NA        8265
> 
> #If I try cbind the same happens
>> head(cbind(monMarket, monDMANEMP))
>          market.Close USPBS.Close
> Oct 1983       494.18          NA
> Nov 1983           NA        8192
> Nov 1983       510.77          NA
> Dec 1983           NA        8231
> Dec 1983       502.94          NA
> Jan 1984           NA        8265
> 
> #If I then continue to align both of the timeseries on one standard axis
> 
> l= (round(Sys.Date()-as.Date("01JAN1983", "%d%b%Y")))
> timeline = to.monthly(xts(1:l, as.Date("01JAN1983", "%d%b%Y")+1:l))
> alignbar = xts(, index(timeline))
> 
> alMonMarket = merge.xts(alignbar, monMarket)
> almonDMANEMP = merge.xts(alignbar, monDMANEMP)
> x = merge.xts(alMonMarket, almonDMANEMP)
> 
>> head(x)
>          market.Close USPBS.Close
> Jan 1983           NA          NA
> Feb 1983           NA          NA
> Mar 1983           NA          NA
> Apr 1983           NA          NA
> May 1983           NA          NA
> Jun 1983           NA          NA
> 
> Any pointer towards the error is highly appreciated
> 
> Jan
> 	[[alternative HTML version deleted]]
> 
> _______________________________________________
> R-SIG-Mac mailing list
> R-SIG-Mac at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-mac
>



More information about the R-SIG-Mac mailing list