generated from quarto-journals/article-format-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path01-chap1.qmd
107 lines (86 loc) · 4.15 KB
/
01-chap1.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Introduction {#sec-intro}
```{r}
#| label: load_packages
#| include: false
library(tidyverse)
library(tsibble)
library(feasts)
library(fable)
```
This is where you introduce the main ideas of your thesis, and an overview of the context and background.
In a PhD, Chapter 2 would normally contain a literature review. Typically, Chapters 3--5 would contain your own contributions. Think of each of these as potential papers to be submitted to journals. Finally, Chapter 6 provides some concluding remarks, discussion, ideas for future research, and so on. Appendixes can contain additional material that don't fit into any chapters, but that you want to put on record. For example, additional tables, output, etc.
## Quarto
In this template, the rest of the chapter shows how to use quarto. The big advantage of using quarto is that it allows you to include your R or Python code directly into your thesis, to ensure there are no errors in copying and pasting, and that everything is reproducible. It also helps you stay better organized.
For details on using Quarto, see <http://quarto.org>.
## Data
Included in this template is a file called `sales.csv`. This contains quarterly data on Sales and Advertising budget for a small company over the period 1981--2005. It also contains the GDP (gross domestic product) over the same period. All series have been adjusted for inflation. We can load in this data set using the following code:
```{r}
#| label: load_data
sales <- readr::read_csv(here::here("data/sales.csv")) %>%
rename(Quarter = `...1`) %>%
mutate(
Quarter = as.Date(paste0("01-", Quarter), "%d-%b-%y"),
Quarter = yearquarter(Quarter)
) %>%
as_tsibble(index = Quarter)
```
Any data you use in your thesis can go into the `data` directory. The data should be in exactly the format you obtained it. Do no editing or manipulation of the data prior to including it in the `data` directory. Any data munging should be scripted and form part of your thesis files (possibly hidden in the output).
## Figures
@fig-deaths shows time plots of the data we just loaded. Notice how figure captions and references work. Chunk names can be used as figure labels with `fig-` prefixed. Never manually type figure numbers, as they can change when you add or delete figures. This way, the figure numbering is always correct.
```{r}
#| label: fig-deaths
#| fig-cap: "Quarterly sales, advertising and GDP data."
sales %>%
pivot_longer(Sales:GDP) %>%
autoplot(value) +
facet_grid(name ~ ., scales = "free_y") +
theme(legend.position = "none")
```
## Results from analyses
We can fit a dynamic regression model to the sales data.
```{r}
fit <- sales %>%
model(arima = ARIMA(Sales ~ GDP + AdBudget + PDQ(D = 1)))
report(fit)
coef <- tidy(fit)
phi1 <- coef %>%
filter(term == "ar1") %>%
pull(estimate)
Theta1 <- coef %>%
filter(term == "sma1") %>%
pull(estimate)
gdp <- coef %>%
filter(term == "GDP") %>%
pull(estimate)
adbudget <- coef %>%
filter(term == "AdBudget") %>%
pull(estimate)
```
If $y_t$ denotes the sales in quarter $t$, $x_t$ denotes the corresponding advertising budget and $z_t$ denotes the GDP, then the resulting model is:
$$
y_t - y_{t-4} = \beta (x_t-x_{t-4}) + \gamma (z_t-z_{t-4}) + \phi_1 (y_{t-1} - y_{t-5}) + \Theta_1 \varepsilon_{t-4} + \varepsilon_t
$$ {#eq-drm}
where
$\beta = `r sprintf("%.2f", adbudget)`$,
$\gamma = `r sprintf("%.2f", gdp)`$,
$\phi_1 = `r sprintf("%.2f", phi1)`$,
and
$\Theta_1 = `r sprintf("%.2f", Theta1)`$. We can reference this equation using @eq-drm.
## Tables
Let's assume future advertising spend and GDP are at the current levels. Then forecasts for the next year are given in @tbl-salesforecasts.
```{r}
#| label: tbl-salesforecasts
#| tbl-cap: "Forecasts for the next year assuming Advertising budget and GDP are unchanged."
#| tbl-cap-location: bottom
future <- new_data(sales, n = 4) %>%
mutate(
AdBudget = tail(sales$AdBudget, 1),
GDP = tail(sales$GDP, 1)
)
fc <- forecast(fit, new_data = future)
as.data.frame(fc) %>%
select(Quarter, .mean) %>%
rename(`Sales forecast` = .mean) %>%
knitr::kable(booktabs = TRUE, digits = 1)
```
Again, notice the use of labels and references to automatically generate table numbers.