Forecast with data series with quantmod and forecast package - r

I'm new to ts, and xts object.
When dealing with time series data, I encountered the problem
require(quantmod)
require(forecast)
ticker <- "^GSPC"
getSymbols(ticker, src="yahoo", to = "2013-12-31")
prices <- GSPC[,6] # First get data using package quantmod
# then forecasting using package forecast
prices.ts <- as.ts(prices)
prices.ets <- ets(prices.ts)
prices.fore <- forecast(prices.ets, h=10)
# then plot
plot(prices.fore, xaxt = "n")
My problems are :
1 . When I tried to save the GSPC with date in a csv file. I searched and tried this
write.zoo((GSPC, file = "GSPC.csv", sep = ",", qmethod = "double"))
The error message: Error: unexpected ',' in "write.zoo((GSPC," , I checked the syntax, it seems to be correct, and I tried other combinations. All failed with the similar error message.
also I tried index(GSPC) to get the date.
and then cbind(index(GSPC), GSPC[, 6]). It also failed..
Error message: Error in merge.xts(..., all = all, fill = fill, suffixes = suffixes) :
dims [product 1762] do not match the length of object [3524]
but when I checked the length
> length(GSPC[,6])
[1] 1762
> length(index(GSPC))
[1] 1762
2 . the plot is like this
there's no x-lab and y- lab. I tried the methods of accepted answer posted here, . but failed.
Especially, I don't get the purpose of the following code. It seems to change the appearance of the plot, but it doesn't change the appearance at all. I don't know whether I lose some points.
a = seq(as.Date("2011-11-01"), by="weeks", length=11)
axis(1, at = decimal_date(a), labels = format(a, "%Y %b %d"), cex.axis=0.6)
abline(v = decimal_date(a), col='grey', lwd=0.5)
Also, I want to plot from as.Date("2013-01-01").
Could you please give some suggestions?
Thanks a lot!

You have additional parenthesis. Use
write.zoo(GSPC, file = "GSPC.csv", sep = ",", qmethod = "double")
I don't know what you are trying to achieve with your index and cbind commands. index does not give the data. And if you want the 6th column of GSPC just use GSPC[,6].
It looks like you have some non-standard plotting dimensions. Start a new graphics window and you will reset them to defaults. But you won't get xlab and ylab unless you specify them explicitly. And you won't get an x-axis because you have set xaxt="n"
The questions about the last code block do not seem to relate to your data at all.

Related

R error with gap.plot: origin must be supplied

I am using the gap.plot function to make a graph that spans across a multi-year data-set. However, with the first series I am trying to add it's already giving me issues with respect to the origin:
Error in as.Date.numeric(e) : 'origin' must be supplied
I looked at the typical things to try to resolve this, and I think I am using the correct function to transform the timestamps. The dataset looks fine.
My code is as follows:
setwd("~/PhD/4 Field experiment/Harvest data")
##### data weergeven #####
BioAvg<-read.csv("Alex Copy of Biomass (average).csv", header=T, na="", stringsAsFactors=TRUE)
class(BioAvg$Time)
class(BioAvg$Harvest)
class(BioAvg$Time)<-"Date"
BioAvg$Time2<- as.Date(BioAvg$Time, origin="1970-01-01")
BioAvg2<-BioAvg[1:15,]
plot.new()
par(bty="n")
gap.plot(BioAvg2$Time, BioAvg2$X0EW.0P, gap=c(17850,18000, 18230,18300), gap.axis = "x",pch = 19, col = "gray55",
type="o", lwd=2, ylab = "Aboveground biomass (g)", xlab= "Time",
xtics = c(17600:17850,18000:18230,18300:18430), xticlab = c(May:Nov,Jun:Nov,Apr:Jun))
P.s. there are more graphs to be added later on in the code, otherwise gap.plot would not make sense indeed. But this is the minimum code needed to reproduce the error.
The CSV file with data:
https://drive.google.com/file/d/1IdfiSnEeSHWu4s52l0RCgqTahNe-3cNL/view
Many thanks for the advice

Plot two columns in R from csv file

I have just started to learn R and I have a problem with plotting some values read from a CSV file.
I have managed to load the csv file:
timeseries <- read.csv(file="R/scripts/timeseries.csv",head=FALSE,sep=",")
When checking the content of timeseries, I get the correct results (so far, so good):
1 2016-12-29T19:00:00Z 6
...
17497 2016-12-30T00:00:00Z 3
Now, I am trying to plot the values - the date should be on the x-axis and the values on the y-axis.
I found some SO questions about this topic: How to plot a multicolumn CSV file?. But I am unable to make it work following the instructions.
I tried:
matplot(timeseries[, 1], timeseries[, -1], type="1")
Also, I tried various barplot and matplot modifications but I usuassly get some exception like this one: Error in plot.window(...) : need finite 'xlim' values
Could someone suggest how to tackle this problem? Sorry for elementary question...
You need to make sure your dates have class Date.
dates <- c("2016-12-29T19:00:00Z", "2016-12-30T00:00:00Z")
values <- c(6,3)
df <- data.frame(dates, values)
df$dates <- as.Date(df$dates)
Then you could use ggplot2
library(ggplot2)
qplot(df$dates, df$values) + geom_line()
or even the default
plot(df$dates, df$values, type = "l")
or with lattice as in the question you referred to
library(lattice)
xyplot(df$values ~ df$dates, type = "l")

mosaic()-function of the vcd package: error in adding text in the cells

I have created a mosaic plot using mosaic function in the vcd package. Now I wish to add some annotations using labeling_cells. Unfortunately, I get an error. The problem might be that it is not the standard Titanic example...
library("grid"); library("vcd")
dataset <- read.table("http://bit.ly/1aJTI1C")
# prepare data for plot as a "Structured Contingency Table"
data1 <- structable(dauer ~ groesse + ort, dataset)
# basic plot
mosaic(data1,
# separate the two elements of the plot
split_vertical = c(T, T, F),
# put the names in the right places and adds boxes
labeling_args = list(tl_labels = TRUE,
tl_varnames = FALSE,
boxes = TRUE),
# grip remains open
pop=FALSE
)
# structure that matches plot, but it does not help
#match<-t(data1)
# try to add labels
labeling_cells(text = data1, clip = FALSE)(data1)
This results in:
# Error in ifelse(abbreviate_varnames, sapply(seq_along(dn), function(i) abbreviate(dn[i], :
# replacement has length zero
# In addition: Warning message:
# In rep(no, length.out = length(ans)) :
# 'x' is NULL so the result will be NULL
Another problem I have is that the boxes do not fit the labels. If you have a hint for that just let me know as well!
It's my first question here, so please excuse potential errors!
Thanks a lot!
Fixed upstream in vcd 1.4-4, but note that you can simply use
mosaic(data1, labeling = labeling_values)
Yes, this is quite confusing and ought to be fixed in labeling_cells(). For some reason the data in the labeling should be a regular table, not a structable. I'll raise this with David, the principal author of mosaic() and package maintainer.
If you know it it's easy to work around it, though:
labeling_cells(text = as.table(data1), clip = FALSE)(as.table(data1))

plotting DCC results with R

I have been running a dcc garch on R; the results is presented as matrix
I would like to extract the second column as a vector to plot, with date on the x-axis.
For the moment, if I define
DCCrho = dccresults$DCC[,2]
then head(DCCrho) yields this:
1 0.9256281
2 0.9256139
3 0.9245794
...
any help to redefine this as a simple vector of numerical values?
any other option to graph the results of dcc with date on the x-axis?
Thanks a lot!
While trying this
x <- cbind(DCCrho, com_30[,2])
head(x)
and this:
matplot(DCCrho ~ x[,2], x, xaxt = "n", type='l')
yields the following error message:
"Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x), :
invalid first argument"
Apparently it was a matter of length of the vector; the Date and the DCC results need to be vectors of same length.
One also needs to plot both date and DCCrho as shown below.
matplot(com_30$date, DCCrho, xaxt = "n", type='l')
axis(1, com_30$date, format(com_30$date, "%y"), cex.axis = .7)
I'm assuming that when you said, "I would like to extract the second row..." that you actually meant "column", because you did the following: dccresults$DCC[,2] Also, as pointed out by a previous comment, the code isn't reproducible, so it's difficult to propose and answer with certainty. However, I'll do my best.
You said you wanted DCCrho to be a "simple vector of numerical values". I'm assuming that this is largely a matter of the way that the values are displayed. Does DCCrho = as.vector(dccresults$DCC[,2]) look better?.
As for the error message, I think that it's b/c in matplot(x,y, ...), x can't be a formula. Try matplot(DCCrho, x[,2]).
If you just want to plot the DCCrho value across some index, you could try something like the following:
Y <- as.vector(dccresults$DCC[,2])
X <- seq_along(Y)
plot(X,Y)
Does that work? Aside from the arbitrary time index, what did you intend to reference as "time"? I don't see a part of the code that you supplied (e.g., a column in dccresults$DCC) that would be an obvious candidate for use as a "date".

R package xtsExtra issue to plot a multiple ts

My aim is to get 1 plot in which there is multiple times series with an auto legend to identify the series. In my CSV file I have 5 columns (agri, food, fuel, manu, ores) starting from January,1996.
library(xts)
library(xtsExtra)
RuChAgri <- read.csv("https://dl.dropbox.com/u/6421260/Forum/RuChAgri.csv", sep=";")
#transform csv data according to R ts
RuChAgri <- ts(RuChAgri, start = c(1996, 1), frequency = 1)
#try to get 1 plot with multiple ts with an auto legend
plot.xts(RuChAgri, screens = factor(1, 1), auto.legend = TRUE)
When I run last line I get the error:
Error in try.xts(x) :
Error in xts(x.mat, order.by = order.by, frequency = frequency(x),
.CLASS = "ts", : NROW(x) must match length(order.by)
Does someone know what is wrong with my code?
Your ts object isn't well-constructed. The series is monthly, so the frequency should be 12, not 1.
RuChAgri <- ts(RuChAgri, start=c(1996, 1), frequency=12)
Then you should convert it to an xts object and then call plot.xts by calling plot. You really shouldn't call plot.xts directly, even though it tries to convert the object you give it to an xts object...
x <- as.xts(RuChAgri)
plot(x, screens=factor(1, 1), auto.legend=TRUE)

Resources