I want to extract the numerical values of a xts object. Let's look at an example
data <- new.env()
starting.date <- as.Date("2006-01-01")
nlookback <- 20
getSymbols("UBS", env = data, src = "yahoo", from = starting.date)
Reg.curve <- rollapply(Cl(data$UBS), nlookback, mean, align="right")
The Reg.cuve is still a xts object but actually I'm just interested in the running means. How can I modify Reg.curve to get a numerical vector?
Use coredata:
reg.curve.num <- coredata(Reg.curve)
# or, if you want a vector:
reg.curve.num <- drop(coredata(Reg.curve))
To extract the numerical values of any xts, ts, or zoo object use:
as.numeric(Reg.curve)
Related
I am using R to pull financial data from Yahoo with Quantmod's getSymbols() function. I use a character vector, Tickers, as the first argument in getSymbols() and the function then creates xts objects of each symbol passed from the Tickers vector. I then would like to merge the various xts objects into one object to perform further analysis on. Rather than typing out each new object's name, I would like to reference back to the original Tickers vector and use its contents (a vector of strings) to reference the newly created objects/variables.
So far I've fiddled around with various combinations of the below functions, but have had no luck:
assign(Ticker, merge(Ticker))
eval(parse(text = Ticker) --- This seems promising but only returns the last object in the Ticker vector. So close yet so far.
get()
as.name / as.symbol
rlang::syms()
library(tidyverse)
library(quantmod)
# Symbol List
Tickers <- c("RY", "TD", "BNS", "BMO", "CM")
# From To
StartDate <- as.Date("2001-01-01", format = "%Y-%m-%d")
EndDate <- Sys.Date()
# Symbol Lookup Function
SymLookup <- function(ticker, from){
assign(ticker, getSymbols(ticker, from = StartDate, to = EndDate, auto.assign = FALSE)[,6], pos = 1)
}
# Retrieving price data from Yahoo Finance
for (i in seq_along(Tickers)) {
SymLookup(Tickers[i],StartDate)
}
### At this point we have 5 newly created xts objects which all have variable
### names corresponding to the 5 character strings in the Tickers vector
### i.e. RY is now an xts object from 2001-01-01 to today etc.
### Attempting to systematically merge XTS dataframes together
BtBB <- assign(BtBB_syms, merge(BtBB_syms))
# Doesn't work - no default value for "y" which is missing
BtBB <- merge(eval(parse(text = Tickers)))
# This works, but only creates a merged data.frame with
# the last instance of Tickers, CM
BtBB <- merge(as.name(Tickers))
# Cannot coerce class `"name"` to a data.frame
BtBB <- merge(rlang::syms(Tickers))
# Same error as first attempt with assign function
I am hoping to just create a merged xts object with n number of columns created from however many symbols I input into the initial Tickers vector.
Basically I'm trying to reference variables in the global environment by using a vector of strings (plural) that I created previously.
Thanks so much!
I have a CSV file with the format
ref_date;wings;airfoil;turbines
2015-03-31;123,22;22,77;99,0
2015-04-30;123,22;28,77;99,0
2015-05-31;123,22;22,177;02,0
2015-06-30;56,288;22,77;99,0
and I want to use the forecast package to predict the next values of this time series. The forecast package only accepts a ts object, but so far all my attempts to create one failed. I tried to
1) Use zoo package
df = read.zoo(data_file, sep=';', dec=',', format="%Y-%m-%d", header=T)
but the data is truncated at the decimal point.
2) Use the zoo package with xts
df = read.zoo(datafile, sep=';', dec=',', format="%Y-%m-%d", header=T)
df_ts = ts(df)
The dates are nowhere to be seen, the index is just a sequence of numbers, like
1 123.22 22.77 99
3) Use read.csv and ts
df = read.zoo(datafile, sep=';', dec=',', format="%Y-%m-%d", header=T)
df_ts = ts(df)
4) Try using xts
df = read.csv(data_file, sep=';', header=T, dec=',')
tt = as.xts(df[,-1],order.by = as.Date(as.character(df[,1]), format = "%Y-%m-%d"))
forecast(tt)
Error in `tsp<-`(`*tmp*`, value = tsp.y) :
invalid time series parameters specified
the result looses all information about the date, including the ref_date column, and now the forecast package gives nonsense as result.
What is the correct approach to create the object that the forecast library is waiting and can generate a forecast, maintaining the dates, including in the plots?
I have been wrestling CSV data into ZOO/XTS objects and sympathize -- painful.
Suggest using as_xts() in the tidyquant package
as_xts(read_csv(file),ref_date)
You may need to coerce the resulting coredata() in the XTS object back to numeric.
I have a vector of asset returns without dates in each row.
Is there a similar method as chart.CumReturns from package PerformanceAnalytics that does not require having to have a vector, dataframe etc. which is a time-based object (I do not have dates in rows).
If you want to keep all the functionality of chart.CumReturns and appearance of plots generated by the function, you may create fake dates, convert the vector to a format that chart.CumReturns accepts (e.g. xts or zoo), and then plot using chart.CumReturns with the fake x axis removed. It seems that chart.CumReturns does not handle order.by = index(x), thus you need a 'real' date.
library(PerformanceAnalytics)
library(xts)
# an example vector
vec <- coredata(edhec)[ , "Funds of Funds"]
# create fake dates, e.g.:
date <- seq(Sys.Date(), by = "1 month", length.out = length(vec))
# convert to xts (or zoo) object
xt <- xts(x = vec, order.by = date)
# plot without fake x axis
chart.CumReturns(xt, main = "Cumulative Returns", xaxis = FALSE)
I am relatively new to R. I am merging data contained in multiple csv files into a single zoo object.
Here is a snippet of the code in my for loop:
temp <- read.csv(filename, stringsAsFactors=F)
temp_dates <- as.Date(temp[,2])
temp <- zoo(temp[,17], temp_dates)
dataset <- temp[seq_specified_dates]
# merge data into output
if (length(output) == 0)
output <- dataset
else
output <- merge(output, dataset, all=FALSE)
When I run head() on the output zoo object, I notice bizarrely named column names like: 'dataset.output.output.output' etc. How can I assign more meaningful names to the merged columns. ?
Also, how do I reference a particular column in a zoo object?. For example if output was a dataframe, I could reference the 'Patient_A' column as output$Patient_A. How do I reference a specific column in a merged zoo object?
I think this would work regardless of the date being a zoo class, if you provide an example I may be able to fix the details, but all in all this should be a good starting point.
#1- Put your multiple csv files in one folder
setwd(your path)
listnames = list.files(pattern=".csv")
#2-use package plyr
library(plyr)
pp1 = ldply(listnames,read.csv,header=T) #put all the files in on data.frame
names(pp1)=c('name1','name2','name3',...)
pp1$date = zoo(pp1$date)
# Reshape data frame so it gets organized by date
pp2=reshape(pp1,timevar='name1',idvar='date',direction='wide')
read.zoo is able to read and merge multiple files. For example:
idx <- seq(as.Date('2012-01-01'), by = 'day', length = 30)
dat1<- data.frame(date = idx, x = rnorm(30))
dat2<- data.frame(date = idx, x = rnorm(30))
dat3<- data.frame(date = idx, x = rnorm(30))
write.table(dat1, file = 'ex1.csv')
write.table(dat2, file = 'ex2.csv')
write.table(dat3, file = 'ex3.csv')
datMerged <- read.zoo(c('ex1.csv', 'ex2.csv', 'ex3.csv'))
If you want to access a particular column you can use the $ method:
datMerged$ex1.csv
EDITED:
You can extract a time period with the window method:
window(datMerged, start='2012-01-28', end='2012-01-30')
The xts package includes more extraction methods:
library(xts)
datMergedx['2012-01-03']
datMergedx['2012-01-28/2012-01-30']
I have a dataframe with several columns:
state
county
year
Then x, y, and z, where x, y, and z are observations unique to the triplet listed above. I am looking for a sane way to store this in a time series and xts will not let me since there are multiple observations for each time index. I have looked at the hts package, but am having trouble figuring out how to get my data into it from the dataframe.
(Yes, I did post the same question on Quora, and was advised to bring it here!)
One option is to reshape your data so you have a column for every State-County combination. This allows you to construct an xts matrix :
require(reshape)
Opt1 <- as.data.frame(cast(Data, Date ~ county + State, value="Val"))
rownames(Opt1) <- Opt1$Date
Opt1$Date <- NULL
as.xts(Opt1)
Alternatively, you could work with a list of xts objects, each time making sure that you have the correct format as asked by xts. Same goes for any of the other timeseries packages. A possible solution would be :
Opt2 <-
with(Data,
by(Data,list(county,State,year),
function(x){
rownames(x) <- x$Date
x <- x["Val"]
as.xts(x)
}
)
)
Which would allow something like :
Opt2[["d","b","2012"]]
to select a specific time series. You can use all xts options on that. You can loop through the counties, states and years to construct plots like this one :
Code for plot :
counties <- dimnames(Opt2)[[1]]
states <- dimnames(Opt2)[[2]]
years <- dimnames(Opt2)[[3]]
op <- par(mfrow=c(3,6))
apply(
expand.grid(counties,states,years),1,
function(i){
plot(Opt2[[i[1],i[2],i[3]]],main=paste(i,collapse="-"))
invisible()
}
)
par(op)
Test-data :
Data <- data.frame( State = rep(letters[1:3],each=90),
county = rep(letters[4:6],90),
Date = rep(seq(as.Date("2011-01-01"),by="month",length.out=30),each=3),
Val = runif(270)
)
Data$year <- as.POSIXlt(Data$Date)$year + 1900