library(tidyverse)
## Warning: il pacchetto 'tidyverse' è stato creato con R versione 4.3.3
## Warning: il pacchetto 'ggplot2' è stato creato con R versione 4.3.3
## Warning: il pacchetto 'tibble' è stato creato con R versione 4.3.3
## Warning: il pacchetto 'tidyr' è stato creato con R versione 4.3.3
## Warning: il pacchetto 'readr' è stato creato con R versione 4.3.3
## Warning: il pacchetto 'purrr' è stato creato con R versione 4.3.3
## Warning: il pacchetto 'dplyr' è stato creato con R versione 4.3.3
## Warning: il pacchetto 'stringr' è stato creato con R versione 4.3.3
## Warning: il pacchetto 'forcats' è stato creato con R versione 4.3.3
## Warning: il pacchetto 'lubridate' è stato creato con R versione 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(quantmod)
## Warning: il pacchetto 'quantmod' è stato creato con R versione 4.3.3
## Caricamento del pacchetto richiesto: xts
## Warning: il pacchetto 'xts' è stato creato con R versione 4.3.3
## Caricamento del pacchetto richiesto: zoo
## Warning: il pacchetto 'zoo' è stato creato con R versione 4.3.3
##
## Caricamento pacchetto: 'zoo'
##
## I seguenti oggetti sono mascherati da 'package:base':
##
## as.Date, as.Date.numeric
##
##
## ######################### Warning from 'xts' package ##########################
## # #
## # The dplyr lag() function breaks how base R's lag() function is supposed to #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #
## # source() into this session won't work correctly. #
## # #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
## # dplyr from breaking base R's lag() function. #
## # #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #
## # #
## ###############################################################################
##
## Caricamento pacchetto: 'xts'
##
## I seguenti oggetti sono mascherati da 'package:dplyr':
##
## first, last
##
## Caricamento del pacchetto richiesto: TTR
## Warning: il pacchetto 'TTR' è stato creato con R versione 4.3.3
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
getSymbols("EXPTOTPR", src="FRED")
## [1] "EXPTOTPR"
Il grafico interattivo mostra l’andamento dell’attività economica di Porto Rico nel conteso del commercio intenrazionale nel tempo, permettendo di visualizzare le fluttuazioni e le tendenze a lungo termine. Periodo di Osservazione: Il grafico copre i dati mensili delle esportazioni di beni da Porto Rico dal 1 agosto 1995 al 1 marzo 2024. Unità di Misura: Le esportazioni sono misurate in milioni di dollari e i dati non sono stagionalmente aggiustati. Fonte dei Dati: I dati sono forniti dal U.S. Census Bureau e fanno parte del rilascio “U.S. Trade in Goods by State, by NAICS-Based Product”.
## Index EXPTOTPR
## Min. :1995-08-01 Min. : 383.7
## 1st Qu.:2002-09-23 1st Qu.: 893.2
## Median :2009-11-16 Median :1370.9
## Mean :2009-11-15 Mean :1301.6
## 3rd Qu.:2017-01-08 3rd Qu.:1659.7
## Max. :2024-03-01 Max. :2624.8
Quanto è lunga la serie?
## [1] 344
modEst <- lm(sel_data ~ I(1:n))
summary(modEst)
##
## Call:
## lm(formula = sel_data ~ I(1:n))
##
## Residuals:
## Min 1Q Median 3Q Max
## -1001.93 -215.77 -46.94 162.80 1275.66
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 645.0473 33.9277 19.01 <2e-16 ***
## I(1:n) 3.8059 0.1705 22.33 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 313.9 on 342 degrees of freedom
## Multiple R-squared: 0.5931, Adjusted R-squared: 0.5919
## F-statistic: 498.5 on 1 and 342 DF, p-value: < 2.2e-16
Il trend lineare spiega bene l’andamento dell’dell’attività economica di Porto Rico nel contesto del commercio internazionale,nonostante ci siano dei break strutturali come quello che si nota a settembre del 2017.
library(ggplot2)
library(ggfortify)
## Warning: il pacchetto 'ggfortify' è stato creato con R versione 4.3.3
autoplot(modEst)
Dato che la serie presenta dei periodi di declino che però sono seguiti da un forte crescita dopo i break strutturlai ho introddoto uan dummy nel settembre del 2017 così da poter analizzare meglio l’analisi storica. *
d_09_2017 <- time(sel_data) > "2017-09-01"
modEst <- lm(sel_data ~ I(1:n) + d_09_2017)
summary(modEst)
##
## Call:
## lm(formula = sel_data ~ I(1:n) + d_09_2017)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1296.5 -143.9 -35.1 119.5 1123.4
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 472.2563 32.5053 14.53 <2e-16 ***
## I(1:n) 5.5627 0.2104 26.44 <2e-16 ***
## d_09_2017TRUE -574.4704 49.8988 -11.51 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 266.8 on 341 degrees of freedom
## Multiple R-squared: 0.707, Adjusted R-squared: 0.7053
## F-statistic: 411.4 on 2 and 341 DF, p-value: < 2.2e-16
autoplot(modEst)
Quali altre soluzioni si potrebbero adottare?
Altra questione: - Si osserva una possibile componente stagionale? Potrebbe esserci una componente stagionale, come indicato dalle aree ombreggiate che potrebbero rappresentare periodi economici specifici o recessioni.