Show the code
plot_sine()
Chapter 5: Lesson 2
Review another student’s journal
What would you add to your learning journal after reading another student’s?
What would you recommend the other student add to their learning journal?
Sign the Learning Journal review sheet for your peer
In the previous lesson, we learned how to incorporate an indicator (or dummy) variable for each season in a period. If there are twelve months in a year, this requires having twelve parameters in the model. Given that many seasonal changes are gradual and can be modeled by a continuous function, we can use sines and cosines to approximate the seasonal variation. This can lead to a smaller number of parameters than is required for the indicator variable approach.
Consider the sine wave with the following parameters:
Here is an interactive plot of this function. Adjust the values of
Notice that this sine function is not linear in the parameters
One of the trigonometric sum and difference identities is:
We apply this to our sine function.
We have transformed this from something that is not linear in the parameters
We can denote the frequency of a sine function as
Figure 1 and Figure 2 illustrate these sine and cosine functions with various values of
One key objective of this lesson is to use a linear combination of functions like those above to model the seasonal component of a time series.
We now explore an important mathematical concept that allows us to approximate any periodic function. If we have an infinite number of terms, the Fourier Series described below gives an exact representation of the function.
The Fourier Series is an infinite series representation of a smooth function
The coefficients
(You will not need to compute any of these integrals.)
As an example, we will approximate the periodic function illustrated here with a Fourier series.
Warning in geom_segment(aes(x = -1.75, y = 0, xend = 1.75, yend = 0), linewidth = 1, : All aesthetics have length 1, but the data has 3 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1.75, y = 0.785, xend = -1, yend = 0.785, : All aesthetics have length 1, but the data has 3 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = 0.785, xend = -1, yend = -0.785, : All aesthetics have length 1, but the data has 3 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = -0.785, xend = 0, yend = -0.785, : All aesthetics have length 1, but the data has 3 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = -0.785, xend = 0, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = 0.785, xend = 1, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = 0.785, xend = 1, yend = -0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = -0.785, xend = 1.75, yend = -0.785, : All aesthetics have length 1, but the data has 3 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning: A numeric `legend.position` argument in `theme()` was deprecated in ggplot2
3.5.0.
ℹ Please use the `legend.position.inside` argument of `theme()` instead.
Warning in geom_segment(aes(x = -1.75, y = 0, xend = 1.75, yend = 0), linewidth = 1, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1.75, y = 0.785, xend = -1, yend = 0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = 0.785, xend = -1, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = -0.785, xend = 0, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = -0.785, xend = 0, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = 0.785, xend = 1, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = 0.785, xend = 1, yend = -0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = -0.785, xend = 1.75, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1.75, y = 0, xend = 1.75, yend = 0), linewidth = 1, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1.75, y = 0.785, xend = -1, yend = 0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = 0.785, xend = -1, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = -0.785, xend = 0, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = -0.785, xend = 0, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = 0.785, xend = 1, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = 0.785, xend = 1, yend = -0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = -0.785, xend = 1.75, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1.75, y = 0, xend = 1.75, yend = 0), linewidth = 1, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1.75, y = 0.785, xend = -1, yend = 0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = 0.785, xend = -1, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = -0.785, xend = 0, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = -0.785, xend = 0, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = 0.785, xend = 1, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = 0.785, xend = 1, yend = -0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = -0.785, xend = 1.75, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1.75, y = 0, xend = 1.75, yend = 0), linewidth = 1, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1.75, y = 0.785, xend = -1, yend = 0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = 0.785, xend = -1, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = -0.785, xend = 0, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = -0.785, xend = 0, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = 0.785, xend = 1, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = 0.785, xend = 1, yend = -0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = -0.785, xend = 1.75, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1.75, y = 0, xend = 1.75, yend = 0), linewidth = 1, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1.75, y = 0.785, xend = -1, yend = 0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = 0.785, xend = -1, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = -0.785, xend = 0, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = -0.785, xend = 0, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = 0.785, xend = 1, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = 0.785, xend = 1, yend = -0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = -0.785, xend = 1.75, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1.75, y = 0, xend = 1.75, yend = 0), linewidth = 1, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1.75, y = 0.785, xend = -1, yend = 0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = 0.785, xend = -1, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = -0.785, xend = 0, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = -0.785, xend = 0, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = 0.785, xend = 1, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = 0.785, xend = 1, yend = -0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = -0.785, xend = 1.75, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1.75, y = 0, xend = 1.75, yend = 0), linewidth = 1, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1.75, y = 0.785, xend = -1, yend = 0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = 0.785, xend = -1, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = -0.785, xend = 0, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = -0.785, xend = 0, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = 0.785, xend = 1, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = 0.785, xend = 1, yend = -0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = -0.785, xend = 1.75, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1.75, y = 0, xend = 1.75, yend = 0), linewidth = 1, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1.75, y = 0.785, xend = -1, yend = 0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = 0.785, xend = -1, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = -0.785, xend = 0, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = -0.785, xend = 0, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = 0.785, xend = 1, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = 0.785, xend = 1, yend = -0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = -0.785, xend = 1.75, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1.75, y = 0, xend = 1.75, yend = 0), linewidth = 1, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1.75, y = 0.785, xend = -1, yend = 0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = 0.785, xend = -1, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = -0.785, xend = 0, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = -0.785, xend = 0, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = 0.785, xend = 1, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = 0.785, xend = 1, yend = -0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = -0.785, xend = 1.75, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1.75, y = 0, xend = 1.75, yend = 0), linewidth = 1, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1.75, y = 0.785, xend = -1, yend = 0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = 0.785, xend = -1, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = -1, y = -0.785, xend = 0, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = -0.785, xend = 0, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 0, y = 0.785, xend = 1, yend = 0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = 0.785, xend = 1, yend = -0.785, colour = "Target Function"), : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
Warning in geom_segment(aes(x = 1, y = -0.785, xend = 1.75, yend = -0.785, : All aesthetics have length 1, but the data has 3501 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
a single row.
We can continue to get more and more precise estimates of the function by just adding more terms to the sum. So, the function
The Fourier series exists for any smooth (continuously differentiable) function. Note theoretically, this allows us to obtain the value of the function at any real value by using this series. However, for a discrete time series with
It turns out, that we only need six terms (which involves twelve coefficients) to fit monthly data with annual cycles. In general, we only need to obtain
Pattern | Number of Seasons, |
Maximum terms in the sum, |
---|---|---|
Days in a Week | 7 | 3 |
Quarters in a Year | 4 | 2 |
Months in a Year | 12 | 6 |
Note that if
The method for fitting seaonal components using indicator variables does not assume any relationship between successive seasons. However, values observed in January are often highly correlated with values observed in February, etc. Fitting a seasonal component using terms in the Fourier Series can often yield a good approximation for the periodic cycles with only a few terms.
For a time series with
The term
The term
We will now focus on the seasonal term,
Note that
As noted above, we can often use a relatively small subset of these terms to get a good approximation of the seasonal component.
The following simulation illustrates harmonic seasonal terms. The values S1, S2, S3,
Adjust the values of the parameters to create different seasonal patterns. Note that this is just a sum of sine and cosine function with various frequencies and amplitudes.