Creating time series objects in R
A time series can be thought of as a vector or matrix of numbers along with some information about what times those numbers were recorded. This information is stored in a ts
object in R. In most exercises, you will use time series that are part of existing packages. However, if you want to work with your own data, you need to know how to create a ts
object in R.
Let's look at an example usnim_2002
below, containing net interest margins for US banks for the year 2002 (source: FFIEC).
> usnim_2002
usnim
1 2002-01-01 4.08
2 2002-04-01 4.10
3 2002-07-01 4.06
4 2002-10-01 4.04
> # ts(data, start, frequency, ...)
> usnim_ts = ts(usnim_2002[, 2], start = c(2002, 1), frequency = 4)
The function ts()
takes in three arguments:
data
is set to everything inusnim_2002
except for the date column; it isn't needed since thets
object will store time information separately.start
is set to the formc(year, period)
to indicate the time of the first observation. Here, January corresponds with period 1; likewise, a start date in April would refer to 2, July to 3, and October to 4. Thus, period corresponds to the quarter of the year.frequency
is set to 4 because the data are quarterly.
In this exercise, you will read in some time series data from an xlsx file using read_excel()
, a function from the readxl
package, and store the data as a ts
object. Both the xlsx file and package have been loaded into your workspace.
This is a part of the course
“Forecasting in R”
Exercise instructions
- Use the
read_excel()
function to read the data from"exercise1.xlsx"
intomydata
. - Apply
head()
tomydata
in the R console to inspect the first few lines of the data. Take a look at the dates - there are four observations in 1981, indicating quarterly data with a frequency of four rows per year. The first observation or start date isMar-81
, the first of four rows for year 1981, indicating that March corresponds with the first period. - Create a
ts
object calledmyts
usingts()
. Setdata
,start
andfrequency
based on what you observed.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Read the data from Excel into R
___ <- ___("exercise1.xlsx")
# Look at the first few lines of mydata
___
# Create a ts object called myts
myts <- ts(___[___], start = c(___, ___), frequency = ___)
This exercise is part of the course
Forecasting in R
Learn how to make predictions about the future using time series forecasting in R including ARIMA models and exponential smoothing methods.
The first thing to do in any data analysis task is to plot the data. Graphs enable many features of the data to be visualized, including patterns, unusual observations, and changes over time. The features that are seen in plots of the data must then be incorporated, as far as possible, into the forecasting methods to be used.
Exercise 1: Welcome to Forecasting Using RExercise 2: Creating time series objects in RExercise 3: Time series plotsExercise 4: Seasonal plotsExercise 5: Trends, seasonality, and cyclicityExercise 6: Autocorrelation of non-seasonal time seriesExercise 7: Autocorrelation of seasonal and cyclic time seriesExercise 8: Match the ACF to the time seriesExercise 9: White noiseExercise 10: Stock prices and white noiseWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.