Skip to content

Advanced modelling

Jean Palate edited this page Apr 9, 2024 · 2 revisions

Daily series with multiple periodicities and time varying regression

edf<-read.csv("./Data/edf.csv")
y<-log(edf$Electricity)

# Create holidays. See also testholidays.R for other examples and for weekly variables.
FR <- rjd3toolkit::national_calendar(list(
  rjd3toolkit::fixed_day(7,14),
  rjd3toolkit::fixed_day(5,8),
  rjd3toolkit::special_day('NEWYEAR'),
  rjd3toolkit::special_day('CHRISTMAS'),
  rjd3toolkit::special_day('MAYDAY'),
  rjd3toolkit::special_day('EASTERMONDAY'),
  rjd3toolkit::special_day('ASCENSION'),
  rjd3toolkit::special_day('WHITMONDAY'),
  rjd3toolkit::special_day('ASSUMPTION'),
  rjd3toolkit::special_day('ALLSAINTSDAY'),
  rjd3toolkit::special_day('ARMISTICE')
))


hol<-rjd3toolkit::holidays(FR, "1996-01-01", 
              length = length(y), type = "Skip")

n<-length(y)
idx<-(n-366*6):n

model<-rjd3sts::model()
ll<-rjd3sts::locallineartrend('l')
seas1<-rjd3sts::seasonal('w', 7, "HarrisonStevens", variance = 0.1, fixed=TRUE )
seas2<-rjd3sts::splines_regular('y', 365.25, nnodes=45)
#seas2<-rjd3sts::splines_daily('y', startYear=1996, nodes=seq(1, 365, 9), variance = 0.01, fixed = FALSE)
n<-rjd3sts::noise('n')
rjd3sts::add(model, ll)
rjd3sts::add(model, seas1)
rjd3sts::add(model, seas2)
rjd3sts::add(model, rjd3sts::reg("cal", hol, 0.1))
rjd3sts::add(model, n)

rslt<-rjd3sts::estimate(model, y)
m1<-rjd3sts::smoothed_components(rslt)

plot(idx, m1[idx, 3], "l")
lines(idx, m1[idx,2], col='red')
lines(idx, m1[idx,4], col='blue')

daily

Births in France

library(rjd3sts)
library(rjd3toolkit)

data<-read.csv("./Data/FR_Births.csv")
y<-data$births

# build the calendar

# Create holidays. See also testholidays.R for other examples and for weekly variables.
FR <- national_calendar(list(
  fixed_day(7,14),
  fixed_day(5,8),
  special_day('NEWYEAR'),
  special_day('CHRISTMAS'),
  special_day('MAYDAY'),
  special_day('EASTERMONDAY'),
  special_day('ASCENSION'),
  special_day('WHITMONDAY'),
  special_day('ASSUMPTION'),
  special_day('ALLSAINTSDAY'),
  special_day('ARMISTICE')
))


hol<-holidays(FR, "1968-01-01", 
                             length = length(y), type = "Skip")

# create the model
sm<-model()
eq<-equation("eq")
# create the components and add them to the model
add(sm, reg("cal", hol, 0.1))
add(sm, locallineartrend("ll"))
add(sm, splines_regular('y', 365.25, nnodes=6))
add(sm, seasonal("s", 7, type='HarrisonStevens'))
add(sm, noise("n"))
#estimate the model
rslt<-estimate(sm, log(y))

ss<-result(rslt, "ssf.smoothing.states")

colfunc<-colorRampPalette(c("red","blue","green","#196F3D"))
colors <- (colfunc(12))
plot(ss[,1], type='l', col=colors[1], ylim=c(-0.4, 0.05))
lines(ss[,2], col=colors[2])
lines(ss[,3], col=colors[3])
lines(ss[,4], col=colors[4])
lines(ss[,5], col=colors[5])
lines(ss[,6], col=colors[6])
lines(ss[,7], col=colors[7])
lines(ss[,8], col=colors[8])
lines(ss[,9], col=colors[9])
lines(ss[,10], col=colors[10])
lines(ss[,11], col=colors[11])

cmps<-smoothed_components(rslt)
plot(ylim=c(-0.5, 0.2),cmps[,4], type='l', col='gray')
lines(cmps[,1], col='blue')
lines(cmps[,3], col='red')

births_hol

births