27 ottobre 2018

Che cosa sono?

Che cosa sono?

In generale, per Serie si intende la classificazione di diverse osservazioni di un fenomeno rispetto ad un carattere qualitativo.

Se tale carattere è il tempo, la serie viene detta storica o temporale.

Il fenomeno osservato, detto variabile, può essere osservato in dati istanti di tempo (variabile di stato: numero dei dipendenti di un'azienda, quotazione di chiusura di un titolo negoziato in borsa, livello di un tasso di interesse ecc.) o alla fine di periodi di lunghezza definita (variabili di flusso: vendite annuali di un'azienda, PIL trimestrale ecc.).

fonte: https://it.wikipedia.org/wiki/Serie_storica

Che cosa sono?

Y(t) = { Y(1), Y(2), Y(3), …, Y(T) }

Y è il fenomeno

Y(t) è una osservazione nel tempo   t

t è un intero che varia nell'intevallo   1:T

T è il totale degli intevalli

Componenti e comportamento

Comportamento e componenti delle Serie Temporali

  • Trend T(t) è l'andamento del fenomento crescente, decrescente, costante nel tempo (°)
  • Stagionalità S(t) è il ripetersi regolare di effetti in periodi solitamente inferiori all'anno
  • Casualità e(t) è l'alterazione dell'andamento legata ad eventi casuali

(°) Il Trend può assumere comportamenti di Ciclicità. E' l'alternanza di fasi (Es. gli andamenti economici sono detti ciclici per la ripetizione ciclica di periodi di crescita e di crisi)

La Serie Temporale è solitamente constituita dalla combinazione delle componenti

Componenti

Trend crescente (teorico)

x <- 1:120
y.cresc <- x
plot(x, y.cresc, type='l', lwd = 2, col = 3)

Trend decrescente (teorico)

x <- 1:120
y.decr <- (-x)
plot(x, y.decr, type='l', lwd = 2, col = 3)

Trend costante (teorico)

x <- 1:120
y.cost <- rep(1, 120)
plot(x, y.cost, type='l', lwd = 2, col = 3)

Stagionalità (teorico)

x <- 1:120
y.stag <- round(sin(x), 2)
plot(x, y.stag, type='l', lwd = 2, col = 4)

Componente casuale (teorico)

x <- 1:120
set.seed(1)
y.casual <- round(sample(0:10, 120, replace = TRUE), 2)
plot(x, y.casual, type='l', lwd = 2, col = 5)

Combinazione dei componenti

Es. Serie Storica cresc

x <- 1:120
y <- y.cresc
plot(x, y, type='l', lwd = 2, col = 2)

Es. Serie Storica cresc+stag

x <- 1:120
y <- y.cresc + y.stag
plot(x, y, type='l', lwd = 2, col = 2)

Es. Serie Storica cresc+stag+casual

x <- 1:120
set.seed(1)
y <- y.cresc + y.stag + y.casual
plot(x, y, type='l', lwd = 2, col = 2)

Es. Serie Storica cresc+stag+casual

ts() Creiamo l'oggetto time-series

R package:stats

?ts

Time-Series Objects

Description:

The function ts is used to create time-series objects. as.ts and is.ts coerce an object to a time-series and test whether an object is a time series.

Le nostre variabili x e y

x
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
##  [18]  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34
##  [35]  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51
##  [52]  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68
##  [69]  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85
##  [86]  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102
## [103] 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
## [120] 120

Le nostre variabili x e y

y
##   [1]   3.84   6.91   9.14  12.24   6.04  14.72  17.66  15.99  15.41   9.46
##  [11]  12.00  12.46  20.42  18.99  23.65  20.71  23.04  27.25  23.15  28.91
##  [21]  31.84  23.99  29.15  24.09  26.87  30.76  27.96  32.27  37.34  32.01
##  [31]  35.60  38.55  39.00  36.53  43.57  42.01  44.36  39.30  46.96  44.75
##  [41]  49.84  48.08  50.17  50.02  50.85  54.90  47.12  52.23  56.05  56.74
##  [51]  56.67  61.99  57.40  55.44  54.00  56.48  60.44  63.99  66.64  63.70
##  [61]  70.03  64.26  68.17  67.92  72.83  67.97  71.14  75.10  68.89  79.77
##  [71]  74.95  81.25  75.32  76.01  79.61  85.57  87.00  82.51  86.56  89.01
##  [81]  84.37  89.31  87.97  87.73  92.82  87.08  93.18  89.04  91.86  91.89
##  [91]  93.11  91.22  99.05 102.75 103.68 104.98 102.38 101.43 106.00 105.49
## [101] 108.45 105.99 105.62 113.68 110.03 107.27 108.18 113.93 119.82 115.96
## [111] 120.14 119.11 115.90 118.78 116.95 116.24 123.31 118.02 122.63 127.58

ts() Storicizzazione del fenomeno

ts.xy <- ts(data = y, start = c(2000, 1), freq = 12); ts.xy
##         Jan    Feb    Mar    Apr    May    Jun    Jul    Aug    Sep    Oct
## 2000   3.84   6.91   9.14  12.24   6.04  14.72  17.66  15.99  15.41   9.46
## 2001  20.42  18.99  23.65  20.71  23.04  27.25  23.15  28.91  31.84  23.99
## 2002  26.87  30.76  27.96  32.27  37.34  32.01  35.60  38.55  39.00  36.53
## 2003  44.36  39.30  46.96  44.75  49.84  48.08  50.17  50.02  50.85  54.90
## 2004  56.05  56.74  56.67  61.99  57.40  55.44  54.00  56.48  60.44  63.99
## 2005  70.03  64.26  68.17  67.92  72.83  67.97  71.14  75.10  68.89  79.77
## 2006  75.32  76.01  79.61  85.57  87.00  82.51  86.56  89.01  84.37  89.31
## 2007  92.82  87.08  93.18  89.04  91.86  91.89  93.11  91.22  99.05 102.75
## 2008 102.38 101.43 106.00 105.49 108.45 105.99 105.62 113.68 110.03 107.27
## 2009 119.82 115.96 120.14 119.11 115.90 118.78 116.95 116.24 123.31 118.02
##         Nov    Dec
## 2000  12.00  12.46
## 2001  29.15  24.09
## 2002  43.57  42.01
## 2003  47.12  52.23
## 2004  66.64  63.70
## 2005  74.95  81.25
## 2006  87.97  87.73
## 2007 103.68 104.98
## 2008 108.18 113.93
## 2009 122.63 127.58

Plot con ts.plot()

plot.ts(ts.xy, col = 4)
abline(v=2000:2010, lty=2, lwd=0.3)

(nota) Serie Storica (quadrimestrale) ts()

ts.xy_q <- ts(data = y, start = c(2000, 1), freq = 4); ts.xy_q
##        Qtr1   Qtr2   Qtr3   Qtr4
## 2000   3.84   6.91   9.14  12.24
## 2001   6.04  14.72  17.66  15.99
## 2002  15.41   9.46  12.00  12.46
## 2003  20.42  18.99  23.65  20.71
## 2004  23.04  27.25  23.15  28.91
## 2005  31.84  23.99  29.15  24.09
## 2006  26.87  30.76  27.96  32.27
## 2007  37.34  32.01  35.60  38.55
## 2008  39.00  36.53  43.57  42.01
## 2009  44.36  39.30  46.96  44.75
## 2010  49.84  48.08  50.17  50.02
## 2011  50.85  54.90  47.12  52.23
## 2012  56.05  56.74  56.67  61.99
## 2013  57.40  55.44  54.00  56.48
## 2014  60.44  63.99  66.64  63.70
## 2015  70.03  64.26  68.17  67.92
## 2016  72.83  67.97  71.14  75.10
## 2017  68.89  79.77  74.95  81.25
## 2018  75.32  76.01  79.61  85.57
## 2019  87.00  82.51  86.56  89.01
## 2020  84.37  89.31  87.97  87.73
## 2021  92.82  87.08  93.18  89.04
## 2022  91.86  91.89  93.11  91.22
## 2023  99.05 102.75 103.68 104.98
## 2024 102.38 101.43 106.00 105.49
## 2025 108.45 105.99 105.62 113.68
## 2026 110.03 107.27 108.18 113.93
## 2027 119.82 115.96 120.14 119.11
## 2028 115.90 118.78 116.95 116.24
## 2029 123.31 118.02 122.63 127.58

time() window() lag()

R package:stats

time()

Restituisce gli istanti di tempo in cui è definita la serie storica in esame.

time(ts.xy_q)
##         Qtr1    Qtr2    Qtr3    Qtr4
## 2000 2000.00 2000.25 2000.50 2000.75
## 2001 2001.00 2001.25 2001.50 2001.75
## 2002 2002.00 2002.25 2002.50 2002.75
## 2003 2003.00 2003.25 2003.50 2003.75
## 2004 2004.00 2004.25 2004.50 2004.75
## 2005 2005.00 2005.25 2005.50 2005.75
## 2006 2006.00 2006.25 2006.50 2006.75
## 2007 2007.00 2007.25 2007.50 2007.75
## 2008 2008.00 2008.25 2008.50 2008.75
## 2009 2009.00 2009.25 2009.50 2009.75
## 2010 2010.00 2010.25 2010.50 2010.75
## 2011 2011.00 2011.25 2011.50 2011.75
## 2012 2012.00 2012.25 2012.50 2012.75
## 2013 2013.00 2013.25 2013.50 2013.75
## 2014 2014.00 2014.25 2014.50 2014.75
## 2015 2015.00 2015.25 2015.50 2015.75
## 2016 2016.00 2016.25 2016.50 2016.75
## 2017 2017.00 2017.25 2017.50 2017.75
## 2018 2018.00 2018.25 2018.50 2018.75
## 2019 2019.00 2019.25 2019.50 2019.75
## 2020 2020.00 2020.25 2020.50 2020.75
## 2021 2021.00 2021.25 2021.50 2021.75
## 2022 2022.00 2022.25 2022.50 2022.75
## 2023 2023.00 2023.25 2023.50 2023.75
## 2024 2024.00 2024.25 2024.50 2024.75
## 2025 2025.00 2025.25 2025.50 2025.75
## 2026 2026.00 2026.25 2026.50 2026.75
## 2027 2027.00 2027.25 2027.50 2027.75
## 2028 2028.00 2028.25 2028.50 2028.75
## 2029 2029.00 2029.25 2029.50 2029.75

window()

Estrazione di sottovalori

window(ts.xy, start = 2000, end = 2003)
##        Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov
## 2000  3.84  6.91  9.14 12.24  6.04 14.72 17.66 15.99 15.41  9.46 12.00
## 2001 20.42 18.99 23.65 20.71 23.04 27.25 23.15 28.91 31.84 23.99 29.15
## 2002 26.87 30.76 27.96 32.27 37.34 32.01 35.60 38.55 39.00 36.53 43.57
## 2003 44.36                                                            
##        Dec
## 2000 12.46
## 2001 24.09
## 2002 42.01
## 2003

lag() come un taglia e incolla temporale

Traslazione della serie di k (valori positivi (verde) anticipano; valori negativi (rosso) posticipano)

ts.xy_a12 <-lag(ts.xy, k = 12); ts.xy_r12 <-lag(ts.xy, k = -12)
plot.ts(ts.xy, lwd=1.5, xlim = c(1999,2012))
lines(ts.xy_a12, col=3); lines(ts.xy_r12, col=2); abline(v=c(1999:2001,2009:2011), lwd=0.3,lty=4)

Decomposizione (da serie a componenti)

Decomposizione della serie

  • Modelli di Decomposizione Si dispone di una serie e si stimano gli andamenti con tecniche di decomposizione in modo da separarare le diverse componenti: Trend, Ciclicità, Stagionalità e casuale. I legami possono essere di tipo additivo o moltiplicativo. Il moltiplicativo può essere trasformato in additivo con l'ausilio dei logaritmi.

  • Additivo: Y(t) = T(t) + C(t) + S(t) + e(t)
  • Moltiplicativo: Y(t) = T(t) * C(t) * S(t) * e(t)
  • Additivo/logaritmica: log Y(t) = log T(t) + log C(t) + log S(t) + log e(t)

La componente Ciclicità C(t) è tipica di periodi lunghi, nei periodi infrannuali si presenta la componente Stagionalità S(t)

decompose()

R package:stats

?decompose

Classical Seasonal Decomposition by Moving Averages

Description:

  • Decompose a time series into seasonal, trend and irregular components using moving averages.
  • Deals with additive or multiplicative seasonal component.
  • The additive model used is: Y(t) = T(t) + S(t) + e(t)
  • The multiplicative model used is: Y(t) = T(t) * S(t) * e(t)

decompose()

require(graphics)
ts.xy.decompose <- decompose(ts.xy, type = c("additive")); plot(ts.xy.decompose)

decompose()

head(ts.xy.decompose$trend,12)
##  [1]       NA       NA       NA       NA       NA       NA 12.01333
##  [8] 13.20750 14.31542 15.27292 16.33417 17.56458
head(ts.xy.decompose$seasonal,12)
##  [1]  1.3011921 -1.5714931  0.9992940  0.4982755  1.3512847 -1.2205208
##  [7] -0.5899653  0.8071181 -0.1094097 -0.2191782 -0.6347801 -0.6118171
head(ts.xy.decompose$random,12)
##  [1]        NA        NA        NA        NA        NA        NA  6.236632
##  [8]  1.975382  1.203993 -5.593738 -3.699387 -4.492766

stl()

R package:stats

?stl

Seasonal Decomposition of Time Series by Loess

Description:

  • Decompose a time series into seasonal, trend and irregular components using ‘loess’, acronym STL.

stl() parametro s.window (per Stagionalità)

ts.xy.stl <- stl(ts.xy, s.window="periodic"); plot(ts.xy.stl)

stl() parametro s.window (per Stagionalità)

head(ts.xy.stl$time.series, 12)
##          seasonal     trend   remainder
##  [1,]  0.51096780  6.906801 -3.57776921
##  [2,] -1.95638604  7.821041  1.04534508
##  [3,]  0.42726090  8.735281 -0.02254142
##  [4,]  0.31508371  9.633708  2.29120794
##  [5,]  0.50290830 10.532136 -4.99504448
##  [6,] -0.78761717 11.440677  4.06694002
##  [7,] -0.64013688 12.349218  5.95091876
##  [8,]  0.66962061 13.295514  2.02486536
##  [9,]  0.65437553 14.241810  0.51381454
## [10,] -0.03765794 15.184379 -5.68672105
## [11,] -0.01970087 16.126948 -4.10724716
## [12,]  0.36128233 17.122884 -5.02416598

Analisi più approfondita

Premesse

Analizziamo una Serie storica per capirne il comportamento e stimare un modello di predizione.

Arricchiamo i nostri strumenti di studio con le seguenti librerie in ambiente R:

library('ggplot2')
library('forecast')
library('tseries')

Analizziamo un file esterno con estensione csv, composto da due colonne:

  • una con la date dal 01/01/2015 al 31/12/2016 espresse nel formato (YYYY-MM-DD)

  • una con la rievazione di consumi giornalieri

  • Nome file: serie.csv

  • Funzione per l'importazione: read.csv("path+serie", header=TRUE)

Creazione dati su foglio di calcolo (consiglio)

File serie.csv

head(serie)
##         date consumi
## 1 2015-01-01     758
## 2 2015-01-02     616
## 3 2015-01-03    1038
## 4 2015-01-04    1202
## 5 2015-01-05    1231
## 6 2015-01-06    1235
str(serie)
## 'data.frame':    731 obs. of  2 variables:
##  $ date   : Factor w/ 731 levels "2015-01-01","2015-01-02",..: 1 2 3 4 5 6 7 8 9 10 ...
##  $ consumi: int  758 616 1038 1202 1231 1235 1162 738 632 1016 ...

File serie.csv

Trasformiamo la colonna date in formato Date con as.Date()

serie$date <- as.Date(serie$date)

Prima analisi:

str(serie)
## 'data.frame':    731 obs. of  2 variables:
##  $ date   : Date, format: "2015-01-01" "2015-01-02" ...
##  $ consumi: int  758 616 1038 1202 1231 1235 1162 738 632 1016 ...
summary(serie$consumi)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##      17    2424    3498    3465    4582    6703

geom_histogram() … Prima analisi

ggplot(serie, aes(consumi)) + geom_histogram(bins = 30) + theme_bw()

geom_line()

ggplot(serie, aes(date, consumi)) + geom_line() + scale_x_date('month') + theme_bw()

tsclean()

R package:forecast

tsclean() (library forecast) clean outliers

cons.ts <- ts(serie$consumi)
serie$cons_tsclean <- as.numeric(tsclean(cons.ts))
head(serie, 15)
##          date consumi cons_tsclean
## 1  2015-01-01     758          758
## 2  2015-01-02     616          616
## 3  2015-01-03    1038         1038
## 4  2015-01-04    1202         1202
## 5  2015-01-05    1231         1231
## 6  2015-01-06    1235         1235
## 7  2015-01-07    1162         1162
## 8  2015-01-08     738          738
## 9  2015-01-09     632          632
## 10 2015-01-10    1016         1016
## 11 2015-01-11     972          972
## 12 2015-01-12     894          894
## 13 2015-01-13    1082         1082
## 14 2015-01-14    1093         1093
## 15 2015-01-15     960          960

plot (clean outliers)

ggplot(serie, aes(date, cons_tsclean)) + geom_line(col = 2) + scale_x_date('month') + theme_bw()

plot: a confronto prima e dopo tsclean

ma()

R package:forecast

?ma() (library forecast)

Moving-average smoothing

Description:

‘ma’ computes a simple moving average smoother of a given time series.

ma() (library forecast)

Moving-average smoothing

serie$cons_ma7 <- ma(serie$cons_tsclean, order = 7)
head(serie)
##         date consumi cons_tsclean cons_ma7
## 1 2015-01-01     758          758       NA
## 2 2015-01-02     616          616       NA
## 3 2015-01-03    1038         1038       NA
## 4 2015-01-04    1202         1202 1034.571
## 5 2015-01-05    1231         1231 1031.714
## 6 2015-01-06    1235         1235 1034.000
tail(serie)
##           date consumi cons_tsclean cons_ma7
## 726 2016-12-26     339          339 1177.143
## 727 2016-12-27    1626         1626 1178.143
## 728 2016-12-28    2381         2381 1376.857
## 729 2016-12-29    1032         1032       NA
## 730 2016-12-30    1382         1382       NA
## 731 2016-12-31    2099         2099       NA

ma() (library forecast) Moving-average

## Warning: Removed 6 rows containing missing values (geom_path).

seasadj()

R package:forecast

seasadj() (library forecast)

Seasonal adjustment

Description:

Returns seasonally adjusted data constructed by removing the seasonal component.

stl() seasadj() De-Stagionalizzare

cons.clean_ma <- ts(na.omit(serie$cons_ma7), frequency=30)
cons.ma.decomp <- stl(cons.clean_ma, s.window="periodic")
cons.destag <- seasadj(cons.ma.decomp)
head(cons.destag)
## Time Series:
## Start = c(1, 1) 
## End = c(1, 6) 
## Frequency = 30 
## [1] 1096.0342 1111.9820 1106.7127 1125.0263 1076.6599  970.3362

plot consumi (decomposiz. Serie Originaria)

plot cons.ma.decomp (decomposiz. Serie "Depurata")

plot (Serie originaria)

plot (Serie depurata e destagionalizzata)

Modelli basati su AR MA

Modelli basati su AR MA

Partendo dall’analisi di una serie temporale reale si cerca di costruire un modello che la rappresenti e ne stimi un meccanismo di auto-generazione.

In R è possibile applicare le tecniche per lo studio delle serie, giungere allo sviluppo di modelli e fare delle ipotesi di predizione.

Box e Jenkins hanno sviluppato una procedura iterativa per la costruzione di modelli misti stocastici basati su modelli di AutoRegressione e a Media Mobile.

AR (Autoregression) MA (Moving Average)

  • ARMA (Autoregression, Moving Average)

  • ARIMA (Autoregression, Integrated, Moving Average)

L'ipotesi di stazionarietà

Ipotiziamo di avere molte osservazioni fino ad oggi. Abbiamo acquisito una certa esperienza sul passato e sul presente, ma non abbiamo informazioni sul futuro. Per definizione, il futuro non lo abbiamo osservato.

Un modello di regressione lineare semplice è valutabile, ma non esaustiva.

Sono necessarie ipotesi aggiuntive per la valutazione del processo stocastico (il motore che genera i dati).

Le serie storiche stazionarie presentano condizioni di invarianza temporale

Verifica stazionarietà

Domanda:

La nostra serie originaria consumi, già pulita dai valori anomali con tsclean(), smussata con ma() e destagionalizzata con seasadj() è stazionaria?

… ad occhio, non sembra!

Grafico di serie stazionaria

adf.test()

R package:tseries

adf.test()

Per verificare l'esistenza della stazionarietà usiamo la funzione adf.test()

Ipotesi del test

  • H0: non stazionarietà
  • H1: stazionarietà

?adf.test() (library tseries)

Augmented Dickey-Fuller Test Description:

Computes the Augmented Dickey-Fuller test for the null that ‘x’ has a unit root.

adf.test(cons.destag)

adf.test(cons.destag, alternative = "stationary")
## Warning in adf.test(cons.destag, alternative = "stationary"): p-value
## greater than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  cons.destag
## Dickey-Fuller = -0.18388, Lag order = 8, p-value = 0.99
## alternative hypothesis: stationary

Non si rifiuta l'ipotesi di non stazionarietà (p-value alto)

diff() stazionarizzare la Serie

Domanda: Help! come faccio a rendere stazionaria la mia Serie?

Una tecnica consiste nel differenziare -> uso del comando diff()

Si calcola la differenza tra il valore al tempo t e il valore al tempo precedente t-1

cons.destag.diff <- diff(cons.destag, differences = 1)

diff()

head(cons.destag)
## Time Series:
## Start = c(1, 1) 
## End = c(1, 6) 
## Frequency = 30 
## [1] 1096.0342 1111.9820 1106.7127 1125.0263 1076.6599  970.3362
head(cons.destag.diff)
## Time Series:
## Start = c(1, 2) 
## End = c(1, 7) 
## Frequency = 30 
## [1]   15.947847   -5.269296   18.313565  -48.366435 -106.323703  -25.194771

diff() adf.test(cons.destag.diff)

adf.test(cons.destag.diff, alternative = "stationary")
## Warning in adf.test(cons.destag.diff, alternative = "stationary"): p-value
## smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  cons.destag.diff
## Dickey-Fuller = -9.9258, Lag order = 8, p-value = 0.01
## alternative hypothesis: stationary

Si rifiuta l'ipotesi di non stazionarietà (p-value basso)

plot.ts(cons.destag) prima

plot.ts(cons.destag.diff) dopo

Acf() Pacf()

R package:forecast

Acf() Pacf() (library forecast)

Passiamo allo studio dei correlogrammi.

Sono grafici con i periodi (detti ritardi) in ascissa e i valori delle autocorrelazioni in ordinata.

I comandi usati in ambiente R sono:

  • Acf() Autocorrelation and Cross-Correlation Function Estimation Description: The function ‘Acf’ computes (and by default plots) an estimate of the autocorrelation function of a (possibly multivariate) time series.

  • Pacf() Partial Autocorrelation and Cross-Correlation Function Estimation Description: Function ‘Pacf’ computes (and by default plots) an estimate of the partial autocorrelation function of a (possibly multivariate) time series. Function ‘Ccf’ computes the cross-correlation or cross-covariance of two univariate series.

Acf() Correlogramma

Acf(cons.destag, lag.max = 200)

Pacf() Correlogramma

Pacf(cons.destag, lag.max = 200)

Acf() Correlogramma

Acf(cons.destag.diff, lag.max = 200)

Pacf() Correlogramma

Pacf(cons.destag.diff, lag.max = 200)

auto.arima() Fit best ARIMA model to univariate time series

R package:forecast

auto.arima()

auto.arima(cons.destag, seasonal = FALSE)
## Series: cons.destag 
## ARIMA(1,1,1) 
## 
## Coefficients:
##          ar1      ma1
##       0.5512  -0.2494
## s.e.  0.0752   0.0850
## 
## sigma^2 estimated as 15492:  log likelihood=-4518.97
## AIC=9043.95   AICc=9043.98   BIC=9057.7
fit <- arima(cons.destag, order = c(1,1,1))

forecast() stimato con auto.arima

Nota: Modello da approfondire con ulteriore analisi.

cons.forecast <- forecast(fit, h=30); plot(cons.forecast)

package: quantmod

Quantitative Financial Modelling & Trading Framework for R

quantmod

library('quantmod')
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.

Importare serie (titolo quotato in borsa)

getSymbols("STM.MI", from="2018-01-01", to="2018-06-30", src = "yahoo")
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## 
## WARNING: There have been significant changes to Yahoo Finance data.
## Please see the Warning section of '?getSymbols.yahoo' for details.
## 
## This message is shown once per session and may be disabled by setting
## options("getSymbols.yahoo.warning"=FALSE).
## Warning: STM.MI contains missing values. Some functions will not work if
## objects contain missing values in the middle of the series. Consider using
## na.omit(), na.approx(), na.fill(), etc to remove or replace them.
## [1] "STM.MI"
head(STM.MI)
##            STM.MI.Open STM.MI.High STM.MI.Low STM.MI.Close STM.MI.Volume
## 2018-01-02       18.24       18.48      17.85        18.43       2515516
## 2018-01-03       18.53       19.09      18.46        19.01       3876386
## 2018-01-04       19.18       19.47      18.97        19.28       3745837
## 2018-01-05       19.36       19.47      19.27        19.43       2344444
## 2018-01-08       19.50       19.52      19.22        19.35       1909146
## 2018-01-09       19.35       19.94      19.26        19.71       2897321
##            STM.MI.Adjusted
## 2018-01-02        18.25607
## 2018-01-03        18.83060
## 2018-01-04        19.09805
## 2018-01-05        19.24664
## 2018-01-08        19.16739
## 2018-01-09        19.52399

Importare serie (titolo quotato in borsa)

head(STM.MI)
##            STM.MI.Open STM.MI.High STM.MI.Low STM.MI.Close STM.MI.Volume
## 2018-01-02       18.24       18.48      17.85        18.43       2515516
## 2018-01-03       18.53       19.09      18.46        19.01       3876386
## 2018-01-04       19.18       19.47      18.97        19.28       3745837
## 2018-01-05       19.36       19.47      19.27        19.43       2344444
## 2018-01-08       19.50       19.52      19.22        19.35       1909146
## 2018-01-09       19.35       19.94      19.26        19.71       2897321
##            STM.MI.Adjusted
## 2018-01-02        18.25607
## 2018-01-03        18.83060
## 2018-01-04        19.09805
## 2018-01-05        19.24664
## 2018-01-08        19.16739
## 2018-01-09        19.52399

chartSeries()

candleChart(STM.MI)

## chartSeries()

chartSeries()

candleChart(STM.MI, theme = "white")

chartSeries()

barChart(STM.MI,theme='white.mono',bar.type='hlc') 

chartSeries()

candleChart(STM.MI, multi.col=TRUE,theme='white')

chartSeries() Add: Simple Moving Average

chartSeries(STM.MI, theme="white", TA="addSMA()") 

chartSeries() Add: Volume e SMA

chartSeries(STM.MI, theme="white", TA="addVo();addSMA()") 

chartSeries() Add Bollinger Bands

chartSeries(STM.MI, theme="white", TA="addBBands()") 

chartSeries() Add Bollinger Bands

chartSeries(window(STM.MI,start="2018-04-01",end="2018-06-30"), theme="white", TA="addBBands()") 

chartSeries() Add Bollinger Bands, MA, Vo

chartSeries(window(STM.MI,start="2018-05-01",end="2018-06-30"),TA="addBBands();addSMA();addVo()") 

chartSeries()

chartSeries(STM.MI, theme="white", TA="addVo();addBBands();addCCI()") 

Grazie per l'attenzione

Creative Commons BY-NC-SA