1
$\begingroup$

The following R codes produce one graph for time series, while the next chunks of codes produce 8 graphs. Why?

births <- scan("http://robjhyndman.com/tsdldata/data/nybirths.dat") birthstimeseries <- ts(births, frequency=12, start=c(1946,1)) birthstimeseries plot.ts(birthstimeseries) 

The 2nd chunks of codes:

library(Matrix) i <- c(1, 3, 8, 4, 2, 7, 6, 9, 1, 4, 10, 5, 11, 2, 12) j <- c(2, 5, 3, 8, 6, 2, 4, 2, 4, 5, 2, 7, 3, 2, 1) x <- rpois(15, 5) M4 <- sparseMatrix(i, j, x = x) M4_regular_matrix <- as(M4, "matrix") timeseries <- ts(M4_regular_matrix) print(timeseries) plot.ts(timeseries) 

Also, in the first graph, how is the plot drawn? Is it, for the year 1946, it connects all the points from Jan to Dec, and the last point of 1946 is connected to the first point of 1947, and so on?

$\endgroup$

1 Answer 1

2
$\begingroup$

ts represents data which has been sampled at equispaced points in time, indicated by frequency

The first example is a single timeseries sampled $12$ times a year (i.e. monthly) and gives a plot based on using $1946$ for January 1946, $1946.083$ for February 1946, adding $\frac1{12}$ for each month up to $1959.917$ for December 1959. Each point is connected to the next. Adding a couple of vertical lines to your code shows the starting point is $1946$ and the ending point is just before $1960$:

abline(v=1946, col= "red") abline(v=1960, col= "red") 

timeseries1

The second example is $8$ different timeseries (columns in the matrix), which plot.ts shows separately. Since you generate these using rpois without setting a seed, the values will change each time you run it, but you will get something like this:

8 timeseries

If you wanted the second plot to look like the first you could do something like this (it starts by default at $1$ and goes up in steps of $\frac1{12}$)

timeseries3 <- ts(as.vector(M4_regular_matrix), frequency=12) plot.ts(timeseries3) 

to see

8 timeseries as one

$\endgroup$
2
  • $\begingroup$ How can I make the 2nd one a single time series? $\endgroup$ Commented Aug 5, 2023 at 14:15
  • $\begingroup$ @user149054 I have added a third chart $\endgroup$ Commented Aug 5, 2023 at 14:41

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.