-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot-tools.R
185 lines (150 loc) · 6.87 KB
/
plot-tools.R
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
library(plotly)
library(reticulate)
reticulate::py_run_string('import sys, kaleido')
#' Visualization of the basic information about time series.
#'
#' @param serie : Input time series
#' @param title : Title of the sequential plot (default, "Sequential plot")
#' @param alpha : Significance level of ACF and PACF barplot.
#'
#' @return `plotly` figure with sequential plot and ACF and PACF barplots.
#' @export
plot_serie <- function(serie, title='Sequential plot', alpha=0.05) {
if (class(serie) != 'ts') { stop('El parámetro serie debe ser un objeto de tipo ts.') }
# Sequential plot
seq_plot <- plot_ly(type='scatter', mode='lines') %>%
add_trace(x=time(serie), y=serie,
line=list(color='grey'), name=title, showlegend=F) %>%
layout(xaxis=list(zeroline=F), yaxis=list(zeroline=F),
plot_bgcolor='#e5ecf6', separators='.',
annotations=list(
list(x=0.5, y=1.1, text=title, showarrow=F, margin=list(t=50),
xref='paper', yref='paper', font=list(size=16))
)
)
# Simple correlations plot
acfs <- acf(serie, plot=F)
stat <- qnorm(1-alpha/2)/sqrt(acfs$n.used)
sig_lines <- data.frame(x=c(0, 0), xend=rep(max(acfs$lag), 2),
y=c(stat, -stat), yend=c(stat, -stat))
acf_plot <- plot_ly() %>%
add_bars(x=c(acfs$lag), y=c(acfs$acf), type='bar',
width=(max(acfs$lag) - min(acfs$lag))/100,
marker=list(color='grey')) %>%
add_segments(data=sig_lines, x=~x, xend=~xend, y=~y, yend=~yend,
line=list(color='blue', dash='dot'), showlegend=F,
name='acf') %>%
layout(plot_bgcolor='#e5ecf6',
annotations=list(
list(x=max(acfs$lag)/2, y=1.1, text='Autocorrelaciones',
showarrow=F, yref='paper', font=list(size=16))
)
)
# Partial correlations plot
pacfs <- pacf(serie, plot=F)
pstat <- qnorm(1-alpha/2)/sqrt(pacfs$n.used)
psig_lines <- data.frame(x=c(0, 0), xend=rep(max(pacfs$lag), 2),
y=c(pstat, -pstat), yend=c(pstat, -pstat))
pacf_plot <- plot_ly() %>%
add_bars(x=c(pacfs$lag), y=c(pacfs$acf), type='bar',
width=(max(pacfs$lag) - min(pacfs$lag))/100,
marker=list(color='grey')) %>%
add_segments(data=psig_lines, x=~x, xend=~xend, y=~y, yend=~yend,
line=list(color='blue', dash='dot'), showlegend=F,
name='pacf') %>%
layout(plot_bgcolor='#e5ecf6',
annotations=list(
list(x=max(pacfs$lag)/2, y=1.1, text='Autocorrrelaciones parciales',
showarrow=F, yref='paper', font=list(size=16))
)
)
subfig <- subplot(acf_plot, pacf_plot, margin=0.07)
fig <- subplot(seq_plot, subfig, nrows=2, margin=0.07) %>%
layout(showlegend=FALSE, showlegend2=FALSE)
return(fig)
}
plot_residuals <- function(ajuste, title='Gráfico secuencia de los residuos', alpha=0.05) {
# Gráfico distribución de los residuos
bounds <- sqrt(ajuste$sigma2)*3
x_grid <- seq(from=-bounds, to=bounds, by=2*bounds/1000)
hist_residuals <- ggplot(data=NULL, aes(x=c(ajuste$residuals))) +
geom_histogram(aes(y=..density..), fill='lightblue', color='grey', binwidth=0.1) +
geom_rug(color='grey') +
geom_line(aes(x=x_grid, y=dnorm(x_grid, sd=sqrt(ajuste$sigma2))),
color='orange')
hist_residuals <- ggplotly(hist_residuals) %>%
layout(xaxis=list(title='residuos', zeroline=F),
yaxis=list(title='densidad', zeroline=F), plot_bgcolor='#e5ecf6',
annotations=list(
list(x=0, y=1.1, text='Test de normalidad', showarrow=F, yref='paper')
)
)
# Gráfico secuencial de los residuos
seq_plot <- plot_ly(type='scatter', mode='lines') %>%
add_trace(x=time(ajuste$residuals), y=ajuste$residuals,
line=list(color='grey'), name=title, showlegend=F) %>%
layout(xaxis=list(zeroline=F), yaxis=list(zeroline=F), plot_bgcolor='#e5ecf6',
annotations=list(
list(x=0.5, y=1.1, text=title, showarrow=F, xref='paper', yref='paper',
margin=list(t=50))
)
)
# Gráfico de las autocorrelaciones simples
acfs <- acf(ajuste$residuals, plot=F)
stat <- qnorm(1-alpha/2)/sqrt(acfs$n.used)
sig_lines <- data.frame(x=c(0, 0), xend=rep(max(acfs$lag), 2),
y=c(stat, -stat), yend=c(stat, -stat))
acf_plot <- plot_ly() %>%
add_bars(x=c(acfs$lag), y=c(acfs$acf), type='bar',
width=(max(acfs$lag) - min(acfs$lag))/200,
marker=list(color='grey')) %>%
add_segments(data=sig_lines, x=~x, xend=~xend, y=~y, yend=~yend,
line=list(color='blue', dash='dot'), showlegend=F,
name='acf') %>% layout(plot_bgcolor='#e5ecf6') %>%
layout(annotations=list(
list(x=max(acfs$lag)/2, y=1.1, text='Autocorrelaciones', showarrow=F, yref='paper')
))
subfig <- subplot(acf_plot, hist_residuals, margin=0.0)
fig <- subplot(seq_plot, subfig, nrows=2, margin=0.07) %>%
layout(showlegend=FALSE, showlegend2=FALSE)
return(fig)
}
plot_prewhiten <- function(prewhiten_object, alpha=0.05) {
lags <- c(prewhiten_object$ccf$lag)
values <- c(prewhiten_object$ccf$acf)
n <- prewhiten_object$ccf$n.used
stat <- qnorm(1-alpha/2)/sqrt(n)
sig_lines <- data.frame(x=rep(min(lags), 2), xend=rep(max(lags), 2),
y=c(stat, -stat), yend=c(stat, -stat))
corr_plot <- plot_ly() %>%
add_bars(x=lags, y=values, type='bar', width=(max(lags)-min(lags))/100,
marker=list(color='grey'), name='acf') %>%
add_segments(data=sig_lines, x=~x, xend=~xend, y=~y, yend=~yend,
line=list(color='blue', dash='dot'), showlegend=F) %>%
layout(plot_bgcolor='#e5ecf6', title='Gráfico de correlaciones',
xaxis=list(title='lags'), yaxis=list(title='correlations'))
return(corr_plot)
}
default_colors <- c(
'#1f77b4', # or rgb(31, 119, 180) // muted blue
'#ff7f0e', # or rgb(255, 127, 14) // safety orange
'#2ca02c', # or rgb(44, 160, 44) // cooked asparagus green
'#d62728', # or rgb(214, 39, 40) // brick red
'#9467bd', # or rgb(148, 103, 189) // muted purple
'#8c564b', # or rgb(140, 86, 75) // chestnut brown
'#e377c2', # or rgb(227, 119, 194)
'#7f7f7f', # or rgb(127, 127, 127)
'#bcbd22', # or rgb(188, 189, 34)
'#17becf' # or rgb(23, 190, 207)
)
pplot <- function(x, y=NULL, title="", color=default_colors[1]) {
if (is.null(y)) {
y <- x
x <- 1:length(y)
}
fig <- plot_ly(type='scatter', mode='lines')%>%
add_trace(x=x, y=y, line=list(color=color), name=title, showlegend=F) %>%
layout(xaxis=list(zeroline=F), yaxis=list(zeroline=F),
plot_bgcolor='#e5ecf6', separators='.')
return(fig)
}