xts

Set frequency in xts object

孤街浪徒 提交于 2019-11-30 00:08:14
问题 I want to create an xts object in R, which I then want to decompose to seasonal and trend. > require(xts) > require(lubridate) > chicos$date <- ymd(chicos$date) > ctr.ts <- xts(chicos[, 7], order.by = chicos[, 8], frequency = 365) > plot(ctr.ts, main="Meaningful title") When I try to decompose it, I get this error message.. > plot(decompose(ctr.ts)) Error in decompose(ctr.ts) : time series has no or less than 2 periods My observations are daily, and span from 2014-12-01 to 2015-02-25. Did I

Dividing each row of an xts or zoo time series object by a fixed row

心已入冬 提交于 2019-11-29 23:55:23
问题 I am trying to divide an xts object which holds a number of time series (columns; with a common date column (index). I want to divide each column by its value at a specified date (say '2010-09-30'). This is so as to re-scale the entire object with values of 1 in each column at that date (a common re-basing task). Had it been an ordinary matrix, A , and the row I wanted to rebase to was say A[6,] , I could just do t(t(A)/A[6,]) and that works. But, trying to manipulate the xts object and its

R: Converting data frame (mixed factor and numeric) to XTS in R

半城伤御伤魂 提交于 2019-11-29 22:31:53
问题 When converting a data frame with mixed factor and numeric columns to an xts, all of my data gets converted to strings. This isn't a problem with the factors, but it's extremely annoying with the numerics. Is there a workaround? For example: > x marketTimestamp price id 1 2010-12-17 11:38:31.100 83.89 b-0 2 2010-12-17 11:38:31.100 83.88 b-1 3 2010-12-17 11:38:31.100 83.87 b-2 4 2010-12-17 11:38:31.300 83.91 o-0 5 2010-12-17 11:38:31.300 83.92 o-1 6 2010-12-17 11:38:31.300 83.93 o-2 > as.xts(x

Get the month from the week of the year

我与影子孤独终老i 提交于 2019-11-29 15:45:36
Let's say we have this: ex <- c('2012-41') This represent the week 41 from the year 2012. How would I get the month from this? Since a week can be between two months, I will be interested to get the month when that week started (here October). Not duplicate to How to extract Month from date in R (do not have a standard date format like %Y-%m-%d). The following will add the week-of-year to an input of year-week formatted strings and return a vector of dates as character. The lubridate package weeks() function will add the dates corresponding to the end of the relevant week. Note for example I

to.minutes using custom endpoints

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 14:59:27
问题 I am using intra-day data that starts at 9:50am and would like to convert it into 20 minute time intervals so the first period would be from 09:50 to 10:09:59 and the second time period would be from 10:10 to 10:29:59 etc. However to.minutes() from the xts package seems to fix it onto the hours and the has time bars at 09:59:59 and 10:19:59 etc...i.e. it is 10 minutes out... i know its probably not a regular request...but is there anyway of doing this so that it has the correct endpoints, i.e

Rolling regression xts object in R

你说的曾经没有我的故事 提交于 2019-11-29 12:10:01
I am attempting to perform a rolling 100 day regression on an xts object and return the t statistic of the slope coefficient for all dates. I have an xts object, prices: > tail(prices) DBC EEM EFA GLD HYG IEF IWM IYR MDY TLT 2012-11-02 27.14 41.60 53.69 162.60 92.41 107.62 81.19 64.50 179.99 122.26 2012-11-05 27.37 41.80 53.56 163.23 92.26 107.88 81.73 64.02 181.10 122.95 2012-11-06 27.86 42.13 54.07 166.30 92.40 107.39 82.34 64.16 182.69 121.79 2012-11-07 27.34 41.44 53.26 166.49 91.85 108.29 80.34 63.84 178.90 124.00 2012-11-08 27.38 40.92 52.78 167.99 91.55 108.77 79.21 63.19 176.37 125.84

subsetting in xts using a parameter holding dates

百般思念 提交于 2019-11-29 11:15:33
I am familiar with the xts subsetting abilities. However, I can't find an elegant way to subset a parameterized range of dates. something like this: times = c(as.POSIXct("2012-11-03 09:45:00 IST"), as.POSIXct("2012-11-05 09:45:00 IST")) #create an xts object: xts.obj = xts(c(1,2),order.by = times) #filter with these dates: start.date = as.POSIXct("2012-11-03") end.date = as.POSIXct("2012-11-04") #instead of xts["2012-11-03"/"2012-11-04"], do something like this: xts[start.date:end.date] Does anybody have any idea? Thanks! You could paste the start.date and end.date objects together, separating

ggplot2: highlight chart area

▼魔方 西西 提交于 2019-11-29 10:44:09
I am working with some time series data and would like to highlight chart area whenever certain conditions become true. For example: require(ggplot2) require(quantmod) initDate <- "1993-01-31" endDate <- "2012-08-10" symbols <- c("SPY") getSymbols(symbols, from=initDate, to=endDate, index.class=c("POSIXt","POSIXct")) spy<-SPY$SPY.Adjusted spy$sma<-SMA(spy$SPY.Adjusted,200) spy<-spy[-(1:199),] spy<-as.data.frame(spy) ggplot(spy,aes(x=index(spy),y=spy$SPY.Adjusted))+geom_line()+geom_line(aes(x=index(spy),y=spy$sma)) The above code plots the the data, but how can I highlight the section when ever

create an OHLC series from ticker data using R

て烟熏妆下的殇ゞ 提交于 2019-11-29 10:42:49
This seems like it should be a common thing, but all my searching comes up with half or unfinished answers. I have a set of data in a csv. But the data is set up so it is time, price, volume. To properly analyze my data I need it in OHLCV format: open, high, low, close, volume. Does anyone have an idea how to reformat into OHLCV? Here is a sample of a data set : time,price,volume, 7/18/10 0:09,0.04951,20, 7/18/10 4:43,0.05941,50.01, 7/18/10 18:48,0.0808,5, 7/18/10 22:44,0.08585,10, 7/18/10 23:00,0.08584,5, 7/18/10 23:00,0.08584,5, 7/19/10 4:53,0.0909,5, 7/19/10 17:24,0.09307,80, 7/19/10 18:03

Obtain date column from xts object [duplicate]

旧城冷巷雨未停 提交于 2019-11-29 10:36:55
This question already has an answer here: Access zoo or xts index 2 answers I used getSymbols to obtain stock data, and it returned something like this: > require(quantmod) > getSymbols(AAPL) > head(AAPL) AAPL.Open AAPL.High AAPL.Low AAPL.Close 2007-01-03 86.29 86.58 81.90 83.80 2007-01-04 84.05 85.95 83.82 85.66 2007-01-05 85.77 86.20 84.40 85.05 2007-01-08 85.96 86.53 85.28 85.47 2007-01-09 86.45 92.98 85.15 92.57 2007-01-10 94.75 97.80 93.45 97.00 > str(AAPL) An ‘xts’ object on 2007-01-03/2015-02-23 containing: Data: num [1:2049, 1:6] 86.3 84 85.8 86 86.5 ... - attr(*, "dimnames")=List of 2