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)
Related
I'm posting this because i've been having a little problem with my code. What i want to do is to make a forecast of COVID cases in a province for the next 30 days using the AUTOARIMA script. Everything is ok, but when i plot the forecast model, the date labels appears in increments of 25% (IE: 2020.2, 2020.4, etc), but i want to label that axis with a YMD format. This is my code:
library(readxl)
library(ggplot2)
library(forecast)
data <- read_xlsx("C:/Users/XXXX/Documents/Casos ARIMA Ejemplo.xlsx")
provincia_1 <- ts(data$Provincia_1, frequency = 365, start = c(2020,64))
autoarima_provincia1 <- auto.arima(provincia_1)
forecast_provincia1 <- forecast(autoarima_provincia1, h = 30)
plot(forecast_provincia1, main = "Proyeccion Provincia 1", xlab = "Meses", ylab = "Casos Diarios")
When i plot the forecast, this is what appears (with the problem i've stated before on the dates label)
The database is here:
https://github.com/pgonzalezp/Casos-Covid-provincias
Try to create a data.frame having on one column your predictions and in the other the daily dates. Then plot it.
Introduce your start and ending date as seen below, then at "by" argument, please check documentation from this link:
https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/as.Date
df <- data.frame(
date=seq(as.Date("1999-01-01"), as.Date("2014-01-10"), by="6 mon"),
pred_val = forecast_provincia1
)
with(df, plot(date, pred_val ))
I got inspired from here:
R X-axis Date Labels using plot()
I am using the R. I am trying to use the "lines' command in ggplot2 to show the predicted values vs. the actual values for a statistical model (arima, time series). Yet, when I ran the code, I can only see a line of one color.
I simulated some data in R and then tried to make plots that show actual vs predicted:
#set seed
set.seed(123)
#load libraries
library(xts)
library(stats)
#create data
date_decision_made = seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")
date_decision_made <- format(as.Date(date_decision_made), "%Y/%m/%d")
property_damages_in_dollars <- rnorm(731,100,10)
final_data <- data.frame(date_decision_made, property_damages_in_dollars)
#aggregate
y.mon<-aggregate(property_damages_in_dollars~format(as.Date(date_decision_made),
format="%W-%y"),data=final_data, FUN=sum)
y.mon$week = y.mon$`format(as.Date(date_decision_made), format = "%W-%y")`
ts = ts(y.mon$property_damages_in_dollars, start = c(2014,1), frequency = 12)
#statistical model
fit = arima(ts, order = c(4, 1, 1))
Here were my attempts at plotting the graphs:
#first attempt at plotting (no second line?)
plot(fit$residuals, col="red")
lines(fitted(fit),col="blue")
#second attempt at plotting (no second line?)
par(mfrow = c(2,1),
oma = c(0,0,0,0),
mar = c(2,4,1,1))
plot(ts, main="as-is") # plot original sim
lines(fitted(fit), col = "red") # plot fitted values
legend("topleft", legend = c("original","fitted"), col = c("black","red"),lty = 1)
#third attempt (plot actual, predicted and 5 future values - here, the actual and future values show up, but not the predicted)
pred = predict(fit, n.ahead = 5)
ts.plot(ts, pred$pred, lty = c(1,3), col=c(5,2))
However, none of these seem to be working correctly. Could someone please tell me what I am doing wrong? (note: the computer I am using for my work does not have an internet connection or a usb port - it only has R with some preloaded packages. I do not have access to the forecast package.)
Thanks
Sources:
In R plot arima fitted model with the original series
R fitted ARIMA off by one timestep? pkg:Forecast
Plotting predicted values in ARIMA time series in R
You seem to be confusing a couple of things:
fitted usually does not work on an object of class arima. Usually, you can load the forecast package first and then use fitted.
But since you do not have acces to the forecast package you cannot use fitted(fit): it always returns NULL. I had problems with fitted
before.
You want to compare the actual series (x) to the fitted series (y), yet in your first attempt you work with the residuals (e = x - y)
You say you are using ggplot2 but actually you are not
So here is a small example on how to plot the actual series and the fitted series without ggplot.
set.seed(1)
x <- cumsum(rnorm(10))
y <- stats::arima(x, order = c(1, 0, 0))
plot(x, col = "red", type = "l")
lines(x - y$residuals, col = "blue")
I Hope this answer helps you get back on tracks.
I am a beginner in R TimeSeries analysis. I have a .txt file having comma separated values like below:
I have run the below commands:
dd <- read.zoo("data.txt", index.column = 1, sep = ",", format = "%Y-%m-%d",
header = TRUE, FUN = as.Date)
dd_xts <- as.xts(dd)
model <- arima(dd_xts, order=c(2, 0, 1))
Now, plotting the fitted values via:
plot(fitted.Arima(model))
which does not have the same values of Time as the initial xts object on X-axis, as shown below:
What is going wrong??
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.
Without using the zoo package, is there a way to use the plot(date,variable) command to restrict the time series for only a specific date range?
Reading through some prior posts, I have a few candidate:
with
which
subset
What is the best way to plot a subset time range of dataset?
Use window():
x = ts(cumsum(rnorm(40)), start = c(2013, 1), freq = 12)
x.sub = window(x, start = c(2014, 1), end = c(2014, 12))
plot(x)
lines(x.sub, col = 'red', lwd = 2)
Or just use xlim to control the x axis display:
plot(x, xlim = c(2014, 2014.5))
You can try xts package. It provides very easy way to subset the timeseries based on time ranges. In example below, X is a xts object. You can subset X by simple text yyyymmdd HH:MM:SS format.
require(xts)
X <- xts(1:100, order.by=Sys.Date()+1:100)
plot(X)
plot(X['201401'])
plot(X['201401/201402'])
plot(X['20140101/20140115'])