#ESTIMATIONS OF TIME VARYING AUTOREGRESSIVE COEFFICIENTS

#############
library(mgcv)
#############

# run alg_dataimport.txt first

# prepare the dataset
dat.pan.w<-read.csv("dat_pan_w1.csv")

dat.pan.w<-pdata.frame(dat.pan.w,index=c("idn","time"))
dat.pan.w$nfl.d1<-diff(dat.pan.w$nfl)
dat.pan.w$nfl.l1<-lag(dat.pan.w$nfl)
dat.pan.w$vol.l1<-lag(dat.pan.w$vol)
write.csv(dat.pan.w,"dat_pan_w.csv",row.names=F)
dat.pan.w<-read.csv("dat_pan_w.csv")

dat.w.north<-subset(dat.pan.w, id=="fr" | id=="fn" | id=="bg" | id=="nl" | id=="oe")
dat.w.south<-subset(dat.pan.w, id=="bd" | id=="it" | id=="es" | id=="pt" | id=="gr")
dat.w.nonemu<-subset(dat.pan.w, id=="uk" | id=="sd" | id=="no" | id=="dk")

#ESTIMATION UNIT ROOT VOL

gam.w.north<-gam(nfl.d1~s(vol,by=nfl.l1)-1,data=dat.w.north); summary(gam.w.north)
gam.w.south<-gam(nfl.d1~s(vol,by=nfl.l1)-1,data=dat.w.south); summary(gam.w.south)
gam.w.nonemu<-gam(nfl.d1~s(vol,by=nfl.l1)-1,data=dat.w.nonemu); summary(gam.w.nonemu)

#plot for north and south and nonemu
postscript("ur_splines_rhovol.eps", paper="special", width=8, height=2.5, horizontal=FALSE)
par(mfrow=c(1,3),mar=c(4, 4, 2, 1) )
plot(gam.w.north,ylab="rho(v)",xlab="v",main="North",ylim=c(-0.25,0.25),shift=1)
plot(gam.w.south,ylab="rho(v)",xlab="v",main="South",ylim=c(-0.25,0.25),shift=1)
plot(gam.w.nonemu,ylab="rho(v)",xlab="v",main="non-EMU",ylim=c(-0.25,0.25),shift=1)
dev.off()


