Introduction¶
In [1]:
* set fredkey xxx, permanently
* import fred NROU, clear
import excel "UTF-8March_C3_A9_20du_20travail.xlsx", sheet("Feuil1") firstrow clear
(3 vars, 37 obs)
In [2]:
label data "Chaumage 2016T2-2025T1, ANSD"
In [3]:
describe, simple
INDICATOR AverageMon~A Tauxdechôm~e
In [4]:
* Renommer des variables
rename (INDICA AverageMon Tauxdech)(time salary unemploy)
In [5]:
* Affichage
describe
Contains data
Observations: 37 Chaumage 2016T2-2025T1, ANSD
Variables: 3
--------------------------------------------------------------------------------
Variable Storage Display Value
name type format label Variable label
--------------------------------------------------------------------------------
time str7 %9s INDICATOR
salary long %10.0g Average Monthly Salary FCFA
unemploy double %10.0g Taux de chômage %
--------------------------------------------------------------------------------
Sorted by:
Note: Dataset has changed since last saved.
Manipulation des dates¶
In [6]:
* Generate a random number within the date range and add it to the start date
generate daten = mdy(1, 1, 2024)
format daten %td
In [7]:
* Simulation de date
set seed 123456
In [8]:
* Simulation de date
replace daten = daten + runiformint(0, 365)
(37 real changes made)
In [9]:
* Différence en jours
generate dure = datediff(daten, mdy(1, 1, 2025), "day")
In [10]:
* Date en texte
generate datestr = string(daten, "%tdCCYY-mon-DD")
In [11]:
* Convertir une date texte en numérique
generate datenb = date(datestr,"YMD"), after(daten)
format datenb %tdCCYY/NN/DD
In [12]:
* Extraire l'année
generate years = year(daten)
In [13]:
* Extraire le semestre (half of year)
generate semestre = halfyear(daten)
In [14]:
* Extraire le trimestre
generate quarter = quarter(daten)
In [15]:
* Extraire le mois
generate month = month(daten)
In [16]:
* Extraire la semaine
generate week = week(daten)
In [17]:
* Extraire le jour
generate days = day(daten)
In [18]:
* Extraire l'ordre du jour dans l'année
generate doys = doy(daten)
In [19]:
* Suppression des variables
drop daten-doys
Déclaration de la série¶
In [20]:
* s2color == defaul
set scheme gg_tableau
In [21]:
* Créer une variable temporelle semestrielle
generate tmp = th(2005h1)+_n-1
In [22]:
format %th tmp
In [23]:
tsset tmp
Time variable: tmp, 2005h1 to 2023h1
Delta: 1 halfyear
In [24]:
twoway tsline salary, yaxis(1) || tsline unemploy, yaxis(2) legend(position(6))
graph export grth.png, as(png) replace
file C:/Users/ibtall/.stata_kernel_cache/graph0.svg saved as SVG format file C:/Users/ibtall/.stata_kernel_cache/graph0.pdf saved as PDF format file grth.png saved as PNG format

In [25]:
* Créer une variable temporelle trimestrielle
tsset, clear
In [26]:
replace tmp = tq(2016q2)+_n-1
(37 real changes made)
In [27]:
format tmp %tqCCYY:q
In [28]:
tsset tmp
Time variable: tmp, 2016:2 to 2025:2
Delta: 1 quarter
In [29]:
tsline unemploy if year(dofq(tmp)) > 2019
graph export grtq.png, as(png) replace
file C:/Users/ibtall/.stata_kernel_cache/graph1.svg saved as SVG format file C:/Users/ibtall/.stata_kernel_cache/graph1.pdf saved as PDF format file grtq.png saved as PNG format

In [30]:
* Créer une variable temporelle mensuelle
tsset, clear
In [31]:
replace tmp = tm(2022m6)+_n-1
(37 real changes made)
In [32]:
format %tm tmp
In [33]:
tsset tmp
Time variable: tmp, 2022m6 to 2025m6
Delta: 1 month
In [34]:
tsline unemploy
graph export grtm.png, as(png) replace
file C:/Users/ibtall/.stata_kernel_cache/graph2.svg saved as SVG format file C:/Users/ibtall/.stata_kernel_cache/graph2.pdf saved as PDF format file grtm.png saved as PNG format

In [35]:
* Créer une variable temporelle hebdomadaire
tsset, clear
In [36]:
replace tmp = tw(2024w28)+_n-1
(37 real changes made)
In [37]:
format %tw tmp
In [38]:
tsset tmp
Time variable: tmp, 2024w28 to 2025w12
Delta: 1 week
In [39]:
tsline unemploy
graph export grtw.png, as(png) replace
file C:/Users/ibtall/.stata_kernel_cache/graph3.svg saved as SVG format file C:/Users/ibtall/.stata_kernel_cache/graph3.pdf saved as PDF format file grtw.png saved as PNG format

In [40]:
* Créer une variable temporelle journalière
tsset, clear
In [41]:
replace tmp = td(01jan2025)+_n-1
(37 real changes made)
In [42]:
format %td tmp
In [43]:
tsset tmp
Time variable: tmp, 01jan2025 to 06feb2025
Delta: 1 day
In [44]:
tsline unemploy
graph export grtd.png, as(png) replace
file C:/Users/ibtall/.stata_kernel_cache/graph4.svg saved as SVG format file C:/Users/ibtall/.stata_kernel_cache/graph4.pdf saved as PDF format file grtd.png saved as PNG format

In [45]:
* Créer une variable temporelle à la seconde
tsset, clear
In [46]:
replace tmp = tc(1dec2025 09:00:00.000)+(_n-1)*1000000
(37 real changes made)
In [47]:
format %tc tmp
In [48]:
tsset tmp
Time variable: tmp, 01dec2025 08:59:14 to 01dec2025 18:59:59, but with gaps
Delta: .001 seconds
In [49]:
tsline unemploy if hh(tmp) > 12 & day(dofc(tmp)) == 1
graph export grtc.png, as(png) replace
file C:/Users/ibtall/.stata_kernel_cache/graph5.svg saved as SVG format file C:/Users/ibtall/.stata_kernel_cache/graph5.pdf saved as PDF format file grtc.png saved as PNG format

Analyse de série¶
Lissage de la série¶
In [50]:
* Créer une variable trimestrielle
tsset, clear
In [51]:
replace tmp = tq(2016q2)+_n-1
(37 real changes made)
In [52]:
format %tqCCYY:q tmp
In [53]:
tsset tmp
Time variable: tmp, 2016:2 to 2025:2
Delta: 1 quarter
Les médianes mobiles comme filtres: h étant le lissage de hanning (poinds binomiaux)
In [54]:
* Creéation de filtres non linéaire (nl)
foreach liss in h 3 3h h3 9 3e he 3rssh {
tssmooth nl S`liss' = unemploy, smoother(`liss')
}
Après lissage non linéaire, les variables sont affichées ainsi que les lissages pour observation.
In [55]:
browse
In [56]:
* Médiane et moyenne mobile: t+1 --> 2025q2
tssmooth ma MAh = unemploy, weights(1 < 2 > 1)
The smoother applied was
(1/4)*[1*x(t-1) + 2 *x(t) + 1*x(t+1)]; x(t)= unemploy
In [57]:
* Lissage exponentiel: t+1 --> 2025q2
tssmooth exponential ewma = unemploy, parm(0.3) s0(0.26)
exponential coefficient = 0.3000 sum-of-squared residuals = 1104.3 root mean squared error = 5.5384
In [58]:
* Double exponentielle: t+1 --> 2025q2
tssmooth dexponential dewma = unemploy, parm(0.2)
double-exponential coefficient = 0.2000 sum-of-squared residuals = 585.06 root mean squared error = 4.0314
In [59]:
* Holt-Winters
tssmooth hwinters hw = unemploy, parms(0.7 0.5) samp0(10)
Specified weights:
alpha = 0.7000
beta = 0.5000
sum-of-squared residuals = 775.5534
root mean squared error = 4.64146
In [60]:
* Shwinters
tssmooth shwinters backtest = unemploy, nolog
computing optimal weights
Optimal weights:
alpha = 0.2531
beta = 0.0000
gamma = 0.4458
penalized sum-of-squared residuals = 483.6161
sum-of-squared residuals = 483.6161
root mean squared error = 3.665212
In [61]:
* Quelques illustrations
tsline unemploy Sh-backtest, legend(position(6) cols(2))
quietly graph export grhlsg.png, as(png) replace
file C:/Users/ibtall/.stata_kernel_cache/graph6.svg saved as SVG format file C:/Users/ibtall/.stata_kernel_cache/graph6.pdf saved as PDF format

Décomposition de la série¶
tsfilter sépare une série temporelle en tendance et cycle
bk Baxter-King tsfilter bk
bw Butterworth tsfilter bw
cf Christiano-Fitzgerald tsfilter cf
hp Hodrick-Prescott tsfilter hp
In [62]:
* filtre Baxter-King
tsfilter bk bkcl_unemploy = unemploy, minperiod(5) maxperiod(1180) trend(bktr_unemploy)
In [63]:
* filtre Baxter-King: illustration
tsline unemploy bk*, yline(0, lcolor(red)) lpattern(solid dash dash_dot longdash_dot) legend(position(6) cols(2))
quietly graph export grbk.png, as(png) replace
file C:/Users/ibtall/.stata_kernel_cache/graph7.svg saved as SVG format file C:/Users/ibtall/.stata_kernel_cache/graph7.pdf saved as PDF format

In [64]:
* filtre Butterworth
tsfilter bw bwcl_unemploy = unemploy, order(2) maxperiod(1180) trend(bwtr_unemploy)
In [65]:
* filtre Butterworth: illustration
tsline unemploy bw*, yline(0, lcolor(red)) lpattern(solid dash dash_dot) legend(position(6) cols(2))
quietly graph export grbw.png, as(png) replace
file C:/Users/ibtall/.stata_kernel_cache/graph8.svg saved as SVG format file C:/Users/ibtall/.stata_kernel_cache/graph8.pdf saved as PDF format

In [66]:
* filtre Christiano-Fitzgerald : stationary and drift
tsfilter cf cfcl_unemploy = unemploy, smaorder(9) minperiod(5) maxperiod(1180) trend(cftr_unemploy)
In [67]:
* filtre Christiano-Fitzgerald: illustration
tsline unemploy cf*, yline(0, lcolor(red)) lpattern(solid dash dash_dot) legend(position(6) cols(2))
quietly graph export grcf.png, as(png) replace
file C:/Users/ibtall/.stata_kernel_cache/graph9.svg saved as SVG format file C:/Users/ibtall/.stata_kernel_cache/graph9.pdf saved as PDF format

In [68]:
* filtre Hodrick-Prescott: smooth(1600 * 3^4)
tsfilter hp hpcl_unemploy = unemploy, smooth(129600) trend(hptr_unemploy)
In [69]:
* filtre Hodrick-Prescott: illustration
tsline unemploy hp*, yline(0, lcolor(red)) lpattern(solid dash dash_dot) legend(position(6) cols(2))
quietly graph export grhp.png, as(png) replace
file C:/Users/ibtall/.stata_kernel_cache/graph10.svg saved as SVG format file C:/Users/ibtall/.stata_kernel_cache/graph10.pdf saved as PDF format

Prévision de la série¶
In [70]:
* Prévision des derniers trimestres de 2025
tssmooth exponential ewma24q1 = unemploy if tmp < tq(2024q1), parms(0.9) forecast(8) //parm(0.3) s0(0.26)
exponential coefficient = 0.9000 sum-of-squared residuals = 610.37 root mean squared error = 4.4373
In [71]:
* Prévision des derniers trimestres de 2025: illustration
tsline unemploy ewm*, lpattern(solid dash dash_dot) tline(2024q1, lcolor(red)) legend(pos(6) cols(2))
quietly graph export fcexp.png, as(png) replace
file C:/Users/ibtall/.stata_kernel_cache/graph11.svg saved as SVG format file C:/Users/ibtall/.stata_kernel_cache/graph11.pdf saved as PDF format

In [72]:
* Prévision avec l'exponentielle double
tssmooth dexponential dewma24q1 = unemploy if tmp < tq(2024q1), parms(0.5) forecast(8) //default coef=0.0001
double-exponential coefficient = 0.5000 sum-of-squared residuals = 709.34 root mean squared error = 4.7835
In [73]:
* Prévision avec l'exponentielle double: illustration
tsline unemploy dew*, lpattern(solid dash dash_dot) tline(2024q1, lcolor(red)) legend(position(6) cols(2))
quietly graph export fcdexp.png, as(png) replace
file C:/Users/ibtall/.stata_kernel_cache/graph12.svg saved as SVG format file C:/Users/ibtall/.stata_kernel_cache/graph12.pdf saved as PDF format

In [74]:
* Prévision par Hot-Winter
tssmooth hwinters hw24q1 = unemploy if tmp < tq(2024q1), parms(0.8 0.3) forecast(8)
Specified weights:
alpha = 0.8000
beta = 0.3000
sum-of-squared residuals = 716.3611
root mean squared error = 4.807122
In [75]:
* Prévision par Hot-Winter: illustration
tsline unemploy hw*, lpattern(solid dash dash_dot) tline(2024q1, lcolor(red)) legend(position(6) cols(2))
quietly graph export fchwgr.png, as(png) replace
file C:/Users/ibtall/.stata_kernel_cache/graph13.svg saved as SVG format file C:/Users/ibtall/.stata_kernel_cache/graph13.pdf saved as PDF format

In [76]:
* Repeat forecast-2a, except for the tsset error (t+1)
tssmooth shwinters backtest24q1 = unemploy if tmp <= tq(2024q1), forecast(7) nolog
computing optimal weights
Optimal weights:
alpha = 0.2495
beta = 0.0000
gamma = 0.4594
penalized sum-of-squared residuals = 481.0011
sum-of-squared residuals = 481.0011
root mean squared error = 3.87702
In [78]:
* Repeat forecast-2a, except for the tsset error: illustration
tsline unemploy backt* , lpattern(solid dash dash_dot) tline(2024q1, lcolor(blue)) legend(position(6) cols(2))
quietly graph export fcshwgr1.png, as(png) replace
file C:/Users/ibtall/.stata_kernel_cache/graph15.svg saved as SVG format file C:/Users/ibtall/.stata_kernel_cache/graph15.pdf saved as PDF format

In [79]:
* Illustration des prévisions
format %9.1f *24q1
list tmp unemploy *24q1 if tmp > tq(2023q4), sep(4)
+-------------------------------------------------------------+
| tmp unemploy ewma24q1 dewma2~1 hw24q1 backte~1 |
|-------------------------------------------------------------|
32. | 2024:1 23.2 22.0 21.6 21.8 22.2 |
33. | 2024:2 21.6 22.0 21.8 22.0 20.2 |
34. | 2024:3 20.3 22.0 22.0 22.2 20.1 |
35. | 2024:4 20 22.0 22.2 22.4 20.4 |
|-------------------------------------------------------------|
36. | 2025:1 21.7 22.0 22.5 22.7 21.9 |
37. | 2025:2 . 22.0 22.7 22.9 19.4 |
38. | 2025:3 . 22.0 22.9 23.1 19.3 |
39. | 2025:4 . 22.0 23.1 23.3 19.6 |
+-------------------------------------------------------------+