xts

Wrong week-ending date using 'to.weekly' function in 'xts' package

大兔子大兔子 提交于 2019-12-02 04:20:00
问题 I have a really odd issue... I am using the to.weekly and to.period function to convert a daily xts object to weekly data. In most instances, I get the week-ending date as a Friday ( day.of.week function will return 5) (e.g. "2010-01-08" , "2011-02-11" ), but there are a few cases where I get something other than Friday (Saturday/Sunday/Thursday/etc.) I have tried to.weekly and to.period(x, period = 'weeks') and both return the same problem. Why is this happening? Is there a work-around for

convert yyyymm on factor class to character class to be used with ChartSeries()

主宰稳场 提交于 2019-12-02 03:44:25
I read a CSV file using read.csv() command and I want to convert into xts and graph with chartSeries() . I changed into a matrix by doing: MyData <- as.matrix(MyData) When I convert to xts using MyData_xts <- xts(MyData[,-1], order.by=as.POSIXct(MyData[,1])) I get the following error message: Error in as.POSIXlt.character(as.character(x), ...) : character string is not in a standard unambiguous format The column that has my index is in the yyyymm format. I've read that that may be a problem, but I haven't been able to find a way around it. EDIT 1 The CSV read before converting to matrix looks

R - How can I change date format when I plot an xts & zoo object?

大兔子大兔子 提交于 2019-12-02 02:12:35
问题 I am wondering how I can change date format. The code I am working on is following: library(quantmod) getSymbols("AAPL") price_AAPL <- AAPL[,6] plot(price_AAPL, main = "The price of AAPL") This results I want to alter date format from "%m %d %Y" as shown in the graphic to "%b-%d-%Y" So I tried following after searching some tips: plot(price_AAPL, main = "The price of AAPL", xaxt="n") axis.Date(1, at=seq(head(index(price_AAPL),1), tail(index(price_AAPL),1), length.out=5), format="%b-%d-%Y",

Copy down last value over a daily period

ε祈祈猫儿з 提交于 2019-12-02 02:06:10
I have a multi-day XTS object, and I am trying to create an indicator that once true, remains true for the rest of the day. The approach I am trying (but its not working) is combining the na.locf function with the apply daily: output <- apply.daily(x, na.locf) Reproducible code: y <- as.xts(c(NA,NA,1,NA,NA,NA,NA,NA,NA),as.POSIXct(c( "2010-01-05 00:00:00", "2010-01-05 00:04:00", "2010-01-05 00:08:00", "2010-01-05 00:12:00", "2010-01-05 00:16:00", "2010-01-05 00:20:00", "2010-01-06 00:00:00", "2010-01-06 00:04:00", "2010-01-06 00:08:00"))) Desired output is to copy down the '1' for the rest of

Using rollapply function for VaR calculation using R

橙三吉。 提交于 2019-12-02 00:23:49
I did the following for calculating Value at Risk (VaR) over 20 period rolling window: require(PerformanceAnalytics); require(zoo) data(edhec) class(edhec) # [1] "xts" "zoo" class(edhec$CTAGlobal) # "NULL" var1<-rollapply(edhec,width=20,FUN=function(edhec) VaR(R=edhec,p=.95,method="modified"),by.column=TRUE) It produces the desired output, and then I tried the same on another data: data(managers) class(managers) # [1] "xts" "zoo" class(managers$HAM4) # [1] "xts" "zoo" var2<-rollapply(managers,width=20,FUN=function(managers) VaR(R=managers,p=.95,method="modified"),by.column=TRUE) But I am

R - How can I change date format when I plot an xts & zoo object?

*爱你&永不变心* 提交于 2019-12-02 00:04:21
I am wondering how I can change date format. The code I am working on is following: library(quantmod) getSymbols("AAPL") price_AAPL <- AAPL[,6] plot(price_AAPL, main = "The price of AAPL") This results I want to alter date format from "%m %d %Y" as shown in the graphic to "%b-%d-%Y" So I tried following after searching some tips: plot(price_AAPL, main = "The price of AAPL", xaxt="n") axis.Date(1, at=seq(head(index(price_AAPL),1), tail(index(price_AAPL),1), length.out=5), format="%b-%d-%Y", las=2) But this doesn't help, and doesn't even show any labeling on x-axis. I suppose that I might did

Why does xts shift a date one day back when creating an xts object from a data frame when TZ is not specified?

南楼画角 提交于 2019-12-01 22:11:43
问题 Let me start by saying that I took a look at ?xts , realised that this is a timezone related problem and seem to have resolved it, but I don't understand why it was happening. So: I have a simple data frame of price data. When I convert it to an xts object the first date of the xts object is a day earlier than the first date in the data frame. If I specify the time zone the dates match problem disappears. I thought at first it might be because xts() assumes that an order.by date without TZ

Remove y-axis label from plot of an xts object

六月ゝ 毕业季﹏ 提交于 2019-12-01 21:06:12
问题 Here is the code generating a plot of an xts object: require("quantmod") getSymbols("SPY") plot(Cl(SPY)) Which yields the following plot: Can you remove the y-axis values (the prices) from a plot of an xts object? Hint : passing yaxt='n' doesn't work . 回答1: Removing the y-axis is easy, but it also removes the x-axis. A couple options: 1) Easy -- use plot.zoo : plot.zoo(Cl(SPY), yaxt="n", ylab="") 2) Harder-ish -- take pieces from plot.xts : plot(Cl(SPY), axes=FALSE) axis(1, at=xy.coords(

Remove y-axis label from plot of an xts object

时光毁灭记忆、已成空白 提交于 2019-12-01 20:44:02
Here is the code generating a plot of an xts object: require("quantmod") getSymbols("SPY") plot(Cl(SPY)) Which yields the following plot: Can you remove the y-axis values (the prices) from a plot of an xts object? Hint : passing yaxt='n' doesn't work . Removing the y-axis is easy, but it also removes the x-axis. A couple options: 1) Easy -- use plot.zoo : plot.zoo(Cl(SPY), yaxt="n", ylab="") 2) Harder-ish -- take pieces from plot.xts : plot(Cl(SPY), axes=FALSE) axis(1, at=xy.coords(.index(SPY), SPY[, 1])$x[axTicksByTime(SPY)], label=names(axTicksByTime(SPY)), mgp = c(3, 2, 0)) 3) Customize-ish

Why does xts shift a date one day back when creating an xts object from a data frame when TZ is not specified?

你。 提交于 2019-12-01 20:40:59
Let me start by saying that I took a look at ?xts , realised that this is a timezone related problem and seem to have resolved it, but I don't understand why it was happening. So: I have a simple data frame of price data. When I convert it to an xts object the first date of the xts object is a day earlier than the first date in the data frame. If I specify the time zone the dates match problem disappears. I thought at first it might be because xts() assumes that an order.by date without TZ specified is UMT, and Sys.timezone() gives "JST" for me but I don't see why that would lead to a date