# Cut-and-paste code below into window above and Run
#
# Iran (1950-2000)
#
#
#Measurement Matrix (Growth), (LU+KOF) (KOF-EF-LU-EG)
#
# Measurement Matrix
# EN.ATM.CO2E.KT EG.USE.COMM.KT.OE NY.GDP.MKTP.KD SL.TLF.TOTL.IN SP.POP.TOTL SL.UEM.TOTL.ZS HDI EF KOF
#[1,] 0.3664 0.357 0.36749 0.36983 0.35974 0.0381 0.3639 0.359 0.273
#[2,] -0.1003 -0.190 -0.00213 0.00266 -0.00723 0.9091 0.0550 -0.109 0.335
#[3,] -0.0184 -0.217 0.10426 -0.09812 -0.16570 -0.3878 0.0648 -0.258 0.827
#
#Fraction of Variance
#[1] 0.801 0.924 0.979 0.995 0.998 0.999 1.000 1.000 1.000
#
phaseSpace <- function(obj,n=500) UseMethod("phaseSpace")
phaseSpace.TSestModel <-
function(obj,n=500) {
require(scatterplot3d)
m <- dim(outputData(obj))[2]
model <- simulate(obj,sampleT=n,noise=matrix(0,n,m))
data <- outputData(model)
m <- dim(data)[2]
if (m > 3) m <- 3
if (m == 3) scatterplot3d(data[,1:m], type="l") else if (m == 2) plot(data[,1],data[,2],xlab=seriesNames(data)[1],ylab=seriesNames(data)[2], type="l") else stop("no phase space")
}
merge.forecast <- function (fx,n=1) {
x <- splice(fx$pred,fx$forecast[[n]])
colnames(x) <- seriesNames(fx$data$output)
return(x)
}
AIC <- function(model) {informationTestsCalculations(model)[3]}
require(dse)
require(matlab)
f <- matrix( c( 1.01282761, -0.02297798, 0.01177121, 0.174120663,
-0.01833407, 0.93276396, 0.14397290, 0.013356943,
-0.01894606, -0.04263376, 0.93461548, 0.005099341,
0.00000000, 0.0000000, 0.0000000, 1.0000000000
),byrow=TRUE,nrow=4,ncol=4)
#
# To stabilize model, uncomment next line
# f[1,1] <- .98
#
# Choose Phase Space or shock Decomposition
#
shock <- TRUE
h <- eye(3,4)
k <- (f[,1:3,drop=FALSE])
IR_LM <- SS(F=f,H=h,K=k,z0=c(0.174120663, 0.013356943, 0.005099341, 1.0000000000),
output.names=c("IR1","IR2","IR3"))
print(IR_LM)
is.SS(IR_LM)
stability(IR_LM)
#IR_LM.data <- simulate(IR_LM,sampleT=100,start=1950)
IR_LM.data <- simulate(IR_LM,sampleT=100,noise=matrix(0,100,3),start=1950)
IR_LM.f <- forecast(l(IR_LM,IR_LM.data),horizon=150)
IR_LM.fx <- merge.forecast(IR_LM.f)
tfplot(IR_LM.f)
AIC(m <- l(IR_LM,IR_LM.data))
IR_LMx <- SS(F=f,H=h,Q=eye(4,3),R=eye(3,3),z0=c(1.146422e-01, 1.761040e-03, -7.008574e-05, 1.0000000000),
output.names=c("IR1","IR2","IR3"))
AIC(m <- l(IR_LM,IR_LM.data))
if (shock) shockDecomposition(IR_LMx,shock=rep(-1,10)) else phaseSpace(toSSinnov(m))
#
# System Matrix prior to the Iranian Revolution (1980)
#
f <- matrix( c( 1.0806288, -0.2730027, 0.1697075, 0.2154613,
0.1807245, 0.7605047, 0.0998663, 0.5064623,
0.7500598, -1.0685973, 1.0480046, 1.6712519,
0.00000000, 0.0000000, 0.0000000,1.0000000000
),byrow=TRUE,nrow=4,ncol=4)
IR_LM1 <- SS(F=f,H=eye(3,4),Q=eye(4,3),R=eye(3,3),z0=c(0.2154613, 0.5064623, 1.6712519,1.0000000000),
output.names=c("IR1","IR2","IR3"))
AIC(m <- l(IR_LM1,IR_LM.data))
if (shock) shockDecomposition(IR_LMx,shock=rep(-1,10)) else phaseSpace(toSSinnov(m))
#
# System Matrix after the Iranian Revolution (1980)
#
f <- matrix( c( 1.018480324, 0.03914097, 0.1094906, 0.18480778,
-0.023998294, 0.81451123, -0.1947082, -0.02645264,
-0.003087103, 0.01644287, 0.9149801, -0.01833092,
0.00000000, 0.0000000, 0.0000000,1.0000000000
),byrow=TRUE,nrow=4,ncol=4)
IR_LM2 <- SS(F=f,H=eye(3,4),Q=eye(4,3),R=eye(3,3),z0=c(0.18480778, -0.02645264, -0.01833092,
1.0000000000),
output.names=c("IR1","IR2","IR3"))
AIC(m <- l(IR_LMx,IR_LM.data))
if (shock) shockDecomposition(IR_LMx,shock=rep(-1,10)) else phaseSpace(toSSinnov(m))
#
# To understand how the IR_LM model, works using counterfactual analysis,
# change parameters in the F matrix to "probable" values between the LCI-UCI
# (values out of the LCI=UCI range would require "revolutionary" change).
#
# LCI Parameter UCI P>=T[1] P< T[1] Std. Dev. Bias Bias-z
# [1,] 0.16378 0.174121 0.185401 0.71 0.29 0.010776 -0.006491 -0.60240
# [2,] -0.01748 0.013357 0.044701 0.53 0.47 0.024777 0.159664 6.44417
# [3,] -0.01401 0.005099 0.022634 0.56 0.44 0.015775 0.165579 10.49658
# [4,] 0.99850 1.012828 1.026143 0.34 0.66 0.011541 -0.835058 -72.35439
# [5,] -0.02647 -0.018334 -0.010724 0.37 0.63 0.006989 0.193923 27.74602
# [6,] -0.02601 -0.018946 -0.011457 0.48 0.52 0.006907 0.194382 28.14230
# [7,] -0.05630 -0.022978 0.005328 0.48 0.52 0.024581 0.198717 8.08402
# [8,] 0.90071 0.932764 0.960087 0.09 0.91 0.024813 -0.730475 -29.43920
# [9,] -0.06059 -0.042634 -0.026324 0.55 0.45 0.017655 0.217905 12.34216
#[10,] -0.02739 0.011771 0.048541 0.34 0.66 0.033341 0.171196 5.13478
#[11,] 0.09777 0.143973 0.190154 0.76 0.24 0.040273 0.002039 0.05063
#[12,] 0.89022 0.934615 0.972453 0.03 0.97 0.033185 -0.714319 -21.52537
#