-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path06-Functional-Depth.Rmd
193 lines (166 loc) · 4.73 KB
/
06-Functional-Depth.Rmd
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
---
title: "Nonparametric Analysis of US Dairy Production and Consumption"
subtitle: "Functional Depth on clusters"
author:
- "Teo Bucci^[[email protected]]"
- "Filippo Cipriani^[[email protected]]"
- "Gabriele Corbo^[[email protected]]"
- "Andrea Puricelli^[[email protected]]"
output:
pdf_document:
toc: true
toc_depth: 3
number_section: true
#keep_md: TRUE
html_document:
toc: true
toc_float: true
number_sections: true
date: "2023-02-17"
editor_options:
chunk_output_type: inline
---
```{r setup, echo = FALSE}
knitr::opts_chunk$set(
echo = TRUE,
dev = c('pdf'),
fig.align = 'center',
fig.path = 'output/',
fig.height = 6,
fig.width = 12
)
```
# Load libraries and data
```{r, message=FALSE}
library(BNPTSclust)
library(roahd)
library(fda.usc)
```
```{r echo = T, results = 'hide'}
data_path = file.path('data_updated_2021')
output_path = file.path('output')
dairy = read.table(file.path(data_path, 'dairy.csv'),header = T,sep = ';')
```
Remove total cheese consumptions:
```{r}
dairy <- dairy[,-c(12,13,14,15)]
```
# Bayesian nonparametric clustering
```{r functional-consumption-exploratory}
years = 1975:2021
matplot(
years,
dairy,
type = 'l',
lty = 1,
col = "black",
main = "Consumption of different types of cheese",
xlab = "Years",
ylab = "Dairy consumption [lbs/person, normalized]"
)
```
Perform the clustering using the function \texttt{tseriescm} from the package \texttt{BNPTSclust}, based on a nonparamteric bayesian approach.
```{r, eval=FALSE, results = 'hide', message=FALSE}
tseriescm.out <-
tseriescm(
dairy,
maxiter = 100,
burnin = 10,
thinning = 2,
level = FALSE,
trend = TRUE,
seasonality = FALSE,
priorb = TRUE,
b = 0
)
```
```{r, include=FALSE}
load("cluster.RData")
```
The clustering obtains five clusters, three of which non singular:
```{r functional-bayesian-clustering}
par(mfrow = c(1, 3))
matplot(
years,
dairy[, tseriescm.out$gnstar == 1],
type = 'l',
col = "black",
xlab = 'Years',
ylab = 'Dairy consumption [lbs/person, normalized]',
main = "First Cluster"
)
matplot(
years,
dairy[, tseriescm.out$gnstar == 2],
type = 'l',
col = "black",
xlab = 'Years',
ylab = 'Dairy consumption [lbs/person, normalized]',
main = 'Second Cluster'
)
matplot(
years,
dairy[, tseriescm.out$gnstar == 4],
type = 'l',
col = "black",
xlab = 'Years',
ylab = 'Dairy consumption [lbs/person, normalized]',
main = 'Third Cluster'
)
```
# Functional Depth measures on clusters
```{r}
c1 = dairy[, tseriescm.out$gnstar == 1]
c2 = dairy[, tseriescm.out$gnstar == 2]
c3 = dairy[, tseriescm.out$gnstar == 4]
grid = 1:dim(dairy)[1]
c1 = as.matrix(c1)
c1_f = fData(grid, t(c1))
c2 = as.matrix(c2)
c2_f = fData(grid, t(c2))
c3 = as.matrix(c3)
c3_f = fData(grid, t(c3))
```
Compute the modified band depth
```{r}
median_curve1 <- median_fData(fData = c1_f, type = "MBD")
median_curve2 <- median_fData(fData = c2_f, type = "MBD")
median_curve3 <- median_fData(fData = c3_f, type = "MBD")
```
```{r functional-bayesian-clustering-depth-median}
par(mfrow = c(1, 3))
plot(c1_f, col = "grey", xlab = "Years", ylab = "Dairy Consumption", main = "First Cluster")
grid_ecg1 <- seq(median_curve1$t0, median_curve1$tP, by = median_curve1$h)
lines(grid_ecg1, median_curve1$values, col = "blue", lwd = 2)
plot(c2_f, col = "grey", xlab = "Years", ylab = "Dairy Consumption", main = "Second Cluster")
grid_ecg2 <- seq(median_curve2$t0, median_curve2$tP, by = median_curve2$h)
lines(grid_ecg2, median_curve2$values, col = "red", lwd = 2)
plot(c3_f, col = "grey", xlab = "Years", ylab = "Dairy Consumption", main = "Third Cluster")
grid_ecg3 <- seq(median_curve3$t0, median_curve3$tP, by = median_curve3$h)
lines(grid_ecg3, median_curve3$values, col = "black", lwd = 2)
```
We can note an up tren ecc.....
```{r eval=F, echo=T, results = 'hide', message=FALSE}
par(mfrow=c(1,3))
invisible(fbplot(c1_f))
invisible(fbplot(c2_f))
invisible(fbplot(c3_f))
```
There are no outliers in the 3 clusters.
```{r functional-outliergram, warning=FALSE}
invisible(outliergram(c1_f))
invisible(outliergram(c2_f))
invisible(outliergram(c3_f))
```
# Cluster analysis
```{r}
c1_names <- colnames(c1)
knitr::kable(c1_names, col.names="Cluster 1")
#knitr::kable(c1_names, col.names="Cluster 1", format = "latex")
c2_names <- colnames(c2)
knitr::kable(c2_names, col.names="Cluster 2")
#knitr::kable(c2_names, col.names="Cluster 2", format = "latex")
c3_names <- colnames(c3)
knitr::kable(c3_names, col.names="Cluster 3")
#knitr::kable(c3_names, col.names="Cluster 3", format = "latex")
```