Maria Faleeva
Maria Faleeva

Reputation: 57

Using the ts() function in R

I am starting to work on my time series data analysis, and I have been told to use the ts() data function in R. However, my time series data doesnt follow a direct trend.

For example, the tutorials online explain how to create the time series if you have monthly, quarterly, or yearly trends. However, my recorded times are 0, 5, 11, 15, 20, 30, and 50.

I was wondering if anybody would have any advice on how to work around this? Many thanks :)

Upvotes: 1

Views: 1567

Answers (2)

G. Grothendieck
G. Grothendieck

Reputation: 269441

The key issue is that ts series are regularly spaced and yours is not but there are a number of approaches to get a regularly spaced ts class series:

  1. maintain the original representation as an irregularly spaced time series using the zoo package (which can directly represent such series) or after converting to zoo use as.ts on it to convert to a regularly spaced series with possibly many NAs.

  2. ignore the times and just use times of 1, 2, 3, ... instead. In that case the series is regularly spaced so we can use ts.

  3. discretize the data into regularly spaced intervals using the last value or average value in the interval. In that case the series is regularly spaced so we can use ts.

  4. Fit a spline to the data and pick off regularly spaced points from that giving a regularly spaced series that can be represented by ts.

If there is a natural periodicity to the series then you might need to modify how the series is represented as ts to reflect that in its frequency but as the question gave no information on this we will ignore that here.

Also depending on the precise application there might be packages that have application specific methods that could be used. See https://cran.r-project.org/web/views/

For example,

# create some test data
set.seed(123)
m <- matrix(rnorm(35), 7)
tt <- c(0, 5, 11, 15, 20, 30, 50)

# 1. zoo series
library(zoo)
z <- zoo(m, tt)

ts1 <- as.ts(z)  # make regularly spaced ts series but with many NAs

# 2. ignore times
ts2 <- ts(m)

# 3. discretize to regularly spaced grid - z is from #1
zd <- aggregate(z, 20 * ceiling(tt/20), tail, 1)
ts3 <- as.ts(zd)

# 4.  spline 
Ls <- apply(m, 2, spline, x = tt)   # list of splines, one per column
zs <- zoo(sapply(Ls, "[[", "y"), Ls[[1]]$x)  # as single zoo object
ts4 <- unname(as.ts(zs))

Upvotes: 2

Daniel_j_iii
Daniel_j_iii

Reputation: 3242

The below code uses a dataframe built into R named AirPassengers, while is a ts(), when mutate it to get it to plot with GGPlot2 nicely. I believe this dataset is monthly for 12 years(144 rows), but we get it as a time series.

library(ggplot2)
library(reshape2)

dff <- melt(AirPassengers)
ggplot(data = dff, aes(x = 1:length(dff[[1]]), y = dff$value)) + geom_line() + labs(x = "Time", y = "Value")

Please pay attention to

class(AirPassengers)
dff <- melt(AirPassengers)
class(dff)

Where the ts() becomes a data.frame.

enter image description here

Upvotes: 1

Related Questions