forked from TBrost/BYUI-Timeseries-Drafts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_ch6_lesson2_global_temps.qmd
144 lines (114 loc) · 3.06 KB
/
_ch6_lesson2_global_temps.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
---
title: "Superb AR(1) Model: Global Temperature Linear Part"
---
```{r}
#| include: false
source("common_functions.R")
```
```{r}
#| echo: false
temps_ts <- rio::import("data/global_temparature.csv") |>
as_tsibble(index = year)
```
```{r}
#| code-fold: true
#| code-summary: "Show the code"
#| label: fig-globalTempTimePlot
#| fig-cap: "Time plot of the mean annual temperature globally for select years."
temps_ts <- rio::import("data/global_temparature.csv") |>
as_tsibble(index = year) |>
filter(year >= 1970)
temps_ts |> autoplot(.vars = change) +
labs(
x = "Year",
y = "Temperature Change (Celsius)",
title = paste0("Change in Mean Annual Global Temperature (", min(temps_ts$year), "-", max(temps_ts$year), ")")
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5)
) +
geom_smooth(method='lm', formula= y~x)
```
```{r}
#| code-fold: true
#| code-summary: "Show the code"
#| label: tbl-globalTempsEstParams
#| tbl-cap: "Parameter estimates for the fitted global temperature time series."
global <- tibble(x = scan("data/global.dat")) |>
mutate(
date = seq(
ymd("1856-01-01"),
by = "1 months",
length.out = n()),
year = year(date),
year_month = tsibble::yearmonth(date),
stats_time = year + c(0:11)/12)
global_ts <- global |>
as_tsibble(index = year_month)
temp_tidy <- global_ts |> filter(year >= 1970)
temp_lm <- temp_tidy |>
model(lm = TSLM(x ~ stats_time ))
# tidy(temp_lm) |> pull(estimate)
tidy(temp_lm) |>
mutate(
lower = estimate + qnorm(0.025) * std.error,
upper = estimate + qnorm(0.975) * std.error
) |>
display_table()
```
```{r}
#| echo: false
#| warning: false
#| label: tbl-globalTempsResidACF
#| tbl-cap: "Autocorrelation function of the residuals for the global temperature time series."
acf_lag1 <- residuals(temp_lm) |>
feasts::ACF(var = .resid) |>
as_tibble() |>
head(1) |>
dplyr::select(acf) |>
pull()
residuals(temp_lm) |>
feasts::ACF(var = .resid) |>
mutate(acf = round(acf, 3)) |>
as_tibble() |>
select(-.model) |>
head(10) |>
pivot_wider(names_from = lag, values_from = acf) |>
display_table("0.5in")
```
```{r}
#| output: false
# Load additional packages
pacman::p_load(tidymodels, multilevelmod,
nlme, broom.mixed)
temp_spec <- linear_reg() |>
set_engine("gls", correlation = nlme::corAR1(0.706))
temp_gls <- temp_spec |>
fit(x ~ stats_time, data = global_ts)
tidy(temp_gls) |>
mutate(
lower = estimate + qnorm(0.025) * std.error,
upper = estimate + qnorm(0.975) * std.error
)
```
```{r}
#| echo: false
#| label: tbl-globalTempsGLS
#| tbl-cap: "Parameter estimates for the Generalized Least Squares estimates of the fit for the global temperature time series."
tidy(temp_gls) |>
mutate(
lower = estimate + qnorm(0.025) * std.error,
upper = estimate + qnorm(0.975) * std.error
) |>
display_table()
```
```{r}
temp_lm |>
residuals() |>
acf()
temp_lm |>
residuals() |>
pacf()
# model(arima = ARIMA(.resid ~ 1 + pdq(0,0,3) + PDQ(0, 0, 0)))
```