Creating Time Series in R with the ts() Function

rtip
timeseries
Author

Steven P. Sanderson II, MPH

Published

December 27, 2023

Introduction

Time series analysis is a powerful tool in the hands of a data scientist or analyst. It allows us to uncover patterns, trends, and insights hidden within temporal data. In this blog post, we’ll explore how to create a time series in R using the base R function ts().

The ts() Function

The ts() function in R is a fundamental tool for handling time series data. It takes four main arguments:

  1. data: A vector or matrix of time series values.
  2. start: The time of the first observation.
  3. end: The time of the last observation.
  4. frequency: The number of observations per unit of time.

Let’s dive into a practical example. Suppose we have a vector sales representing monthly sales data for a year. We can create a time series object using the ts() function as follows:

# Sample data
sales <- c(120, 150, 200, 180, 250, 300, 280, 320, 400, 350, 300, 380)

# Creating a time series
sales_ts <- ts(
  data = sales, 
  start = c(2022, 1), 
  end = c(2022, 12), 
  frequency = 12
  )

# Display the time series
print(sales_ts)
     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2022 120 150 200 180 250 300 280 320 400 350 300 380
plot(sales_ts)

This example creates a monthly time series for sales data throughout the year 2022.

Try it out!

Creating time series in R is a hands-on task. I encourage you to pick a dataset relevant to your interests or work and follow the same steps. You can use various time frequencies (e.g., daily, weekly, monthly) depending on your data.

Bonus: Using convert_to_ts() from TidyDensity

Now, let’s explore a bonus section discussing the convert_to_ts() function from TidyDensity. This function is designed to convert data in a data frame or tibble into a time series format. It works seamlessly with data generated from tidy_distribution functions.

convert_to_ts() Function Details

The convert_to_ts() function takes the following arguments:

  • .data: A data frame or tibble to be converted into a time series format.
  • .return_ts: A logical value indicating whether to return the time series data. Default is TRUE.
  • .pivot_longer: A logical value indicating whether to pivot the data into long format. Default is FALSE.

How It Works

  1. The function checks if the input is a data frame or tibble; otherwise, it raises an error.
  2. It verifies if the data comes from a tidy_distribution function; otherwise, it raises an error.
  3. The data is then converted into a time series format, grouping it by “sim_number” and transforming the “y” column into a time series.

Example Usage

library(TidyDensity)

# Assuming you have a tidy data frame 'tidy_data'
tidy_time_series <- convert_to_ts(.data = tidy_normal(), 
                                  .return_ts = TRUE, 
                                  .pivot_longer = FALSE)

# Display the result
head(tidy_time_series)
              y
[1,] -2.1617445
[2,]  0.7630891
[3,]  0.1951564
[4,]  1.0558584
[5,] -1.5169866
[6,] -1.4532770
plot(tidy_time_series)

multiple_simulations_series <- convert_to_ts(.data = tidy_normal(.num_sims = 10),
                                             .return_ts = TRUE, 
                                             .pivot_longer = TRUE)
head(multiple_simulations_series)
              1          2           3           4          5          6
[1,]  0.6591429  1.0850380 -1.41562870 -0.59330831 -0.2680326  0.6516654
[2,] -1.7456947 -0.5792555  0.95670979  0.35232047  1.3702818 -1.0709930
[3,]  0.2665711 -2.1701118  2.18141262  0.25480605  1.5762242 -0.8022482
[4,]  0.3128563  0.4328502  0.55082256  0.06628991  0.7984409  0.3048087
[5,]  0.6763225 -0.3997367 -0.09709908  1.13736623  1.0121689  0.3383476
[6,] -0.1086352  1.3522350 -1.00235321  0.14722832  1.3395307 -0.1026343
              7          8          9         10
[1,] -0.1113009  1.6959992 -1.1897814 -0.2290430
[2,] -0.7512943 -0.6969146  1.1334643  0.7554655
[3,]  1.0782559  0.5296079 -1.0057891  1.1089107
[4,] -1.8030557  1.5021519  0.7094383 -1.0848102
[5,] -0.5539205  0.7127801 -1.3130555 -0.6742046
[6,] -0.7625295 -1.1712384  0.8147821  0.8036737
plot(multiple_simulations_series)

convert_to_ts(.data = tidy_normal(.num_sims = 10),
              .return_ts = FALSE, 
              .pivot_longer = FALSE)
# A tibble: 50 × 10
       `1`    `2`    `3`      `4`    `5`    `6`     `7`    `8`     `9`   `10`
     <dbl>  <dbl>  <dbl>    <dbl>  <dbl>  <dbl>   <dbl>  <dbl>   <dbl>  <dbl>
 1  1.34    0.300  0.475 -3.07     1.35   1.18   0.403  -0.104 -0.347  -0.590
 2  0.153  -0.563 -1.28  -0.0429   0.354  1.24  -0.184   0.850  0.338  -0.700
 3 -0.448   0.160 -1.22   0.600   -0.532 -0.357 -0.759   0.659  0.691  -0.778
 4  0.0384 -1.38  -0.918  1.12    -1.09   0.949  0.0276  0.456  0.510   1.10 
 5 -0.0148 -0.663  0.401  0.00200 -0.790 -1.98   0.714   0.613  0.658   1.02 
 6  0.528   0.164 -0.104 -0.977   -0.889 -0.589  1.39    0.916  0.496   0.326
 7  1.08   -1.21   0.116  0.685    1.22   0.132  0.608  -0.322 -1.06   -0.624
 8 -0.620   1.45   0.666 -1.39    -1.20  -0.175  0.0665  1.21   1.95   -0.237
 9 -0.143  -1.93  -0.683  0.603   -1.24   0.623 -0.564  -0.417  0.0639  1.34 
10  0.125   0.869 -1.47   0.953    0.608 -1.80   0.272   1.16   1.17   -1.09 
# ℹ 40 more rows
convert_to_ts(.data = tidy_normal(.num_sims = 10),
              .pivot_longer = TRUE)
Time Series:
Start = 1 
End = 50 
Frequency = 1 
              1            2           3           4           5           6
 1 -0.655132170  1.124119928 -1.68015013  0.27119005 -1.34222309 -0.85029821
 2 -0.563753358  0.148496529 -0.08420323  0.52425342 -0.22838128  0.38009238
 3  0.162642987  0.944409541 -0.31995660  1.62739294  0.04586743 -1.00996184
 4 -1.946332513  1.693491637 -0.76559306  2.58711915 -0.67796334  0.73113802
 5  0.009260305  0.563304432 -0.90409541 -0.74206033  1.18933630  1.19979835
 6  0.422154075 -0.827368569  0.38440082  1.58106342  0.53744939 -0.01699670
 7  0.672312916  1.917586521  0.18758750  0.52812847  0.04987565  0.42471806
 8 -0.099748356  0.548332963 -0.44544054 -0.74466540  0.27741779  0.03754982
 9  0.599374087  2.416047894  0.13083797 -0.71925129  1.46397148  0.21928699
10  2.137103506  1.012679620  0.10946528 -1.21309250 -0.40298312 -0.06149162
11 -0.584328794 -0.977150553  1.98763118  0.80100807  1.59761439  0.96962954
12 -0.977410854  0.153227900 -0.89588458 -0.04738268 -0.99671233 -0.79875286
13  0.348559098 -0.139254894  0.17014759 -0.41635428  1.16343543  0.45536856
14  2.050755613  0.379570270 -1.57943509 -0.49670553 -0.49108776 -0.81654431
15 -0.536403083  0.815378564  0.44909230 -0.17645908  2.14187118  0.85912010
16  0.154173100 -0.009842145 -0.06548799  1.36411289 -0.26842334  1.69185208
17  0.526008171 -1.021411558 -0.01515875 -0.39923678 -1.91505446  1.47060381
18 -0.382628412  1.554043999 -0.99083886  0.03813862 -0.12122244  0.78097490
19  0.068027520  1.559917873  1.56624132 -0.72556821  0.04364763 -1.08713622
20 -0.375312760  0.182453162 -0.29118413  0.63647087  1.14593859  0.33562580
21  0.527199674  0.514358677 -0.85199804 -0.65959289  1.34991539 -0.88580797
22  0.606739583 -0.349281940 -0.42159565 -0.29988202  0.86897866  0.83607508
23 -0.750044523 -0.721098218  0.81759595  1.81533167  1.67514533  0.49723656
24 -0.167121348  1.050352702  1.70669358 -2.78197517  0.70872919 -1.19627981
25 -0.442427162 -0.009639385  0.66120474  0.07333735  0.39692306  2.72030582
26 -0.405109397 -0.463937639  2.92697938  0.64263562 -0.59588190  1.27812303
27  0.554957737  1.508452736 -0.67384362 -1.14296113  0.25859866 -0.09516432
28 -0.167333762 -0.154519359  1.29634414  2.05276305  1.22046958 -0.20261165
29 -0.426751358  0.299516899 -0.10270470  0.45218446 -1.01077778  0.41069225
30 -0.915369502 -1.134302489 -0.45195412 -0.02372924 -0.87979497 -2.22429752
31  0.398618595 -0.246544276  0.63197257  0.04685569  0.46524825 -0.41017315
32  0.001083079 -0.530058643  0.82139589  0.39120899  0.63881495  0.63570075
33 -0.502176823 -0.642359996  0.42880920 -0.44803379 -0.01992917  0.38896456
34  0.276462923 -1.042478900  1.14313112 -1.55697201  0.52390061  0.07794736
35  0.923405153 -0.237080174  0.96970857  0.86964379 -0.91567940 -0.63612591
36 -0.400504232  0.217544505 -0.63904106  0.91258477 -0.01156312 -0.41156245
37  0.458010625 -1.456299801 -0.95303905 -1.01123779 -0.04321204 -0.84060963
38 -0.202664022 -0.729410616  1.10544600 -1.54460728  0.48443033 -0.67777269
39  0.041503245 -0.610875862 -0.45167645 -0.47658183 -2.46133081  0.26514433
40 -0.121966128 -0.265657879 -0.40754380  0.41665215 -3.23015392 -0.11733447
41  2.275054279 -0.140453274  1.01738854  1.08335318 -0.72963796 -0.07750213
42  0.305032329 -0.912897889 -0.54486760 -0.06350812  0.35661866 -0.89575613
43 -0.097368038  0.879480923  0.42349178  0.90800105  1.22592750 -1.93884437
44 -0.053250772  0.590070911 -0.04377062  0.38001642 -0.56422962 -1.55652310
45 -0.631346970  0.510970484  1.03953655 -0.52313314  0.66643930 -0.50328900
46 -0.573835910  0.662271162 -0.94615866 -1.09348403  1.51469795  0.02769026
47  2.056373176 -1.438372766  0.64932666 -1.17330573  0.41932438  1.53009528
48 -2.353078785  0.487698963 -0.81844578 -0.92462341  0.27244456  0.42617475
49 -3.231722103 -1.271841203  0.24348256 -1.36611307 -0.97603663 -1.95217754
50  0.753719680 -0.366101153 -0.01044950 -1.59566595  0.08057617  1.11583833
              7            8            9           10
 1  0.171342136 -1.143037486  0.798526236 -0.244110488
 2 -0.940732995 -0.098286237 -0.002464059 -1.329350897
 3 -0.694053671 -0.861938231 -0.981141759 -0.016408426
 4  0.715770296  0.891107430 -1.631236257 -0.812448587
 5 -1.487322100  1.513993297  1.034899433 -1.562309837
 6 -2.299581079 -1.372057771  1.141053483 -0.523175745
 7 -1.551226431  0.584053401 -0.124530500  1.386795935
 8  0.199088420  0.176433940  0.896122531  0.150326444
 9 -2.610686399  1.619626479 -0.304107194  1.999026100
10  0.993024200 -1.717659646 -0.936505161  0.249643134
11  0.242493969 -1.104745018  2.139557395  1.308248416
12  1.438262730 -0.371852512 -0.367182295 -1.589296525
13 -0.149204186 -1.054119573 -0.465127766  0.423034528
14  1.199604760 -0.295676868  1.818224237  1.651671457
15  0.682116022  1.589055554  0.940553190  0.044546697
16 -0.023887103 -0.544176304  0.078750649 -1.618718807
17  0.783402254 -0.024077038  1.530981707  0.610937582
18  0.840292783 -0.781554633  0.177714516 -0.059345413
19 -1.313595307 -1.101811653  0.057190918  0.067426355
20  0.005232829  0.145444788  1.066697084  1.068481723
21 -0.554820885  0.380379950 -0.162190910  1.185489015
22 -0.861222004  0.030283953  0.908438632 -0.231394452
23  1.157935009  0.063995477  2.361496504 -0.396326692
24 -0.897071507  0.369621973 -0.266053668 -0.131590687
25  0.035629927 -0.084923255  0.003248558 -0.368614537
26  0.509364566 -1.832084693  0.890542325  0.888462980
27  1.207424021 -2.671878721  0.063299112 -0.878590418
28  0.211237171  1.535283026  0.759650387  0.549046140
29 -0.595276048 -2.514556134  0.445083701 -0.769968392
30  1.348793576  0.004755218 -0.301946343 -2.037938159
31  0.361619164 -1.340745382 -0.706048393 -0.003291719
32  0.014851985 -0.249794267  0.741063865 -0.398728564
33 -1.172677388 -0.193834398  1.018583201 -0.351067819
34 -0.572769045 -2.072442096  0.577545791  1.284331483
35  0.443800268 -0.108977727  1.866110069 -0.020469667
36  0.926425998 -0.687618149  1.224365387 -0.096690188
37 -0.460173605 -0.302608648  0.671541153 -2.696710002
38  0.277085477  0.335125232 -0.754473314  0.619338071
39  1.279310040 -0.842097806 -0.275860802 -0.768216600
40  0.015055026  0.835779589 -0.535925622 -0.990428811
41  0.690052418  1.488830535  0.318262300 -0.265301715
42 -2.342151157 -0.587400371  1.794438099  1.190162522
43 -1.284973383  0.976120498 -0.678730423  0.895248035
44 -1.857641265 -0.484324204  1.312931115  1.671816010
45 -0.828061584 -1.461679865  1.175113675  0.392315093
46 -0.124694812  0.800465295 -0.328118006  0.170025963
47  0.698593283  0.676449924  1.963221359 -0.477702054
48  0.118048192  0.257227889 -0.600914093  0.908605679
49 -0.101844218  0.458018251  0.177924006 -0.469079298
50  0.249644640 -1.684424651  0.620835209  1.330859032

This example showcases how to leverage TidyDensity’s functionality to convert tidy data into a time series format effortlessly. At this point in time though, the parameters of the ts() function are not utilized, meaning you cannot also pass in a start, end or frequency, but that will be added in the future.

In conclusion, mastering the ts() function in base R and exploring additional tools like convert_to_ts() opens up new avenues for time series analysis. So, roll up your sleeves, experiment with your data, and unlock the insights hidden in the temporal dimension. Happy coding!