Plotting Subset Time Interval R - r

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'])

Related

Trying to change time labels in R

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()

How do I plot multiple lines on the same graph?

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.

Plot Timeseries ts object in R

How to plot ts object. month in x axis and monthly.returns in y axis for each year in same graph. please find the code that i am using.
stock<-"^GSPC"
getSymbols(stock,from = "2000-01-01",to = Sys.Date())
GSPC_pr<-monthlyReturn(GSPC)
GSPC_pr<-ts(GSPC_pr,frequency=12, start=c(2000,1))
Presumably you're using the quantmod package. You can plot it like this:
plot(as.xts(GSPC_pr), major.format = "%Y-%m")

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)

Making a 3D surface from time series data in R

I have a large data set which I would like to make a 3D surface from. I would like the x-axis to be the date, the y-axis to be the time (24h) and the z-axis (height) to be a value I have ($). I am a beginner with R, so the simpler the better!
http://www.quantmod.com/examples/chartSeries3d/ has a nice example, but the code is way to complicated for my skill level!
Any help would be much appreciated - anything I have researched so far needs to have the data sorted, which is not suitable I think.
Several options present themselves, persp() and wireframe(), the latter in package lattice.
First some dummy data:
set.seed(3)
dat <- data.frame(Dates = rep(seq(Sys.Date(), Sys.Date() + 9, by = 1),
each = 24),
Times = rep(0:23, times = 10),
Value = rep(c(0:12,11:1), times = 10) + rnorm(240))
persp() needs the data as the x and y grid locations and a matrix z of observations.
new.dates <- with(dat, sort(unique(Dates)))
new.times <- with(dat, sort(unique(Times)))
new.values <- with(dat, matrix(Value, nrow = 10, ncol = 24, byrow = TRUE))
and can be plotted using:
persp(new.dates, new.times, new.values, ticktype = "detailed", r = 10,
theta = 35, scale = FALSE)
The facets can be coloured using the col argument. You could do a lot worse than study the code for chartSeries3d0() at the page you linked to. Most of the code is just drawing proper axes as neither persp() nor wireframe() handle Date objects easily.
As for wireframe(), we
require(lattice)
wireframe(Value ~ as.numeric(Dates) + Times, data = dat, drape = TRUE)
You'll need to do a bit or work to sort out the axis labelling as wireframe() doesn't work with objects of class "Date" at the moment (hence the cast as numeric).

Resources