forked from antoniofabio/tsDyn
/
misc.R
295 lines (253 loc) · 7.7 KB
/
misc.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#.onAttach <- function(...) {
# cat("Version 0.7 of package tsDyn will have probably a few minor revisions rapidly.\n User should #update the package often.\n Minor revisions will be announced only on the tsdyn mailing list: #tsdyn@googlegroups.com\n")
#}
extend <- function(...)
UseMethod("extend")
extend.list <- function(this, subclass, ..., listV) {
class(this) <- c(subclass, class(this))
if(missing(listV))
return(structure(c(this, list(...)), class=class(this)))
else
return(structure(c(this, listV), class=class(this)))
}
MyEnv <- function(...) {
this <- new.env()
vars <- list(...)
nms <- names(vars)
if(length(nms)) for(i in 1:length(nms))
assign(nms[i], vars[[i]], envir=this)
structure(this, class=c("MyEnv",class(this)))
}
extend.MyEnv <- function(this, subclass, ...) {
class(this) <- c(subclass, class(this))
newvars <- list(...)
if(length(names(newvars))) for(nm in names(newvars))
assign(nm, newvars[[nm]], envir=this)
return(this)
}
#'Available models
#'
#'Available built-in time series models
#'
#'Return the list of built-in available \sQuote{nlar} time series models
#'
#'@return A character vector containing built-in time series models. For help
#'on a specific model, type: \code{help(modelName)}.
#'@author Antonio, Fabio Di Narzo
#'@keywords ts
#'@export
#'@examples
#'
#'availableModels()
#'
availableModels <- function()
fitters
latex <- function(obj, ...)
UseMethod("latex")
formatSignedNum <- function(x, ...) {
signChar <- c("-","+")[(x>=0)+1]
nm <- names(x)
res <- paste(signChar,format(abs(x), ...) )
names(res) <- nm
return(res)
}
build <- function(...)
UseMethod("build")
add <- function(...)
UseMethod("add")
#'sigmoid functions
#'
#'Some sigmoid functions. See R sources for their definition
#'
#'
#'@aliases sigmoid d2sigmoid
#'@param x numeric vector
#'@author J. L. Aznarte
#'@keywords ts
#'@export
sigmoid <- function(x) 1/(1 + exp(-x))
#' @export
#' @rdname sigmoid
dsigmoid <- function(x) x * (1 - x)
repmat <- function(a, b, c) kronecker(matrix(1,b,c), a)
norma <- function(v) sqrt(sum(v^2))
###Function to create the threshold in TVAR
TVAR_thresh<-function(mTh, thDelay, thVar=NULL, y, p){
T <- nrow(y)
k <- ncol(y)
if (!is.null(thVar)) {
if (length(thVar) > T) {
z <- thVar[seq_len(T)]
warning("The external threshold variable is not of same length as the original variable")
} else {
z <- thVar
}
z<-embed(z,p+1)[,seq_len(max(thDelay))+1] #if thDelay=2, ncol(z)=2
combin <- matrix(0, ncol=1, nrow=k)
} ###Combination (or single value indicating position) of contemporaneous variables
else {
if (!length(mTh)%in%c(1,k))
stop("length of 'mTh' should be equal to the number of variables, or just one")
if(length(mTh)==1) {
combin <- matrix(0, ncol=1, nrow=k)
combin[mTh,]<-1
} else {
combin<-matrix(mTh, ncol=1, nrow=k)
}
zcombin <- y %*% combin
z <- embed(zcombin,p+1)[,seq_len(max(thDelay))+1] #if thDelay=2, ncol(z)=2
}
# return result
res <- list(trans=as.matrix(z), combin=combin)
res
}
#Function to obtain the number of digits of a value
getndp <- function(x, tol=2*.Machine$double.eps){
internal<-function(x){
ndp <- 0
while(!isTRUE(all.equal(x, round(x, ndp), tol=tol))) ndp <- ndp+1
if(ndp > -log10(tol)) warning("Tolerance reached, ndp possibly underestimated.")
ndp
}
if(!is.null(dim(x)))
x<-c(x)
if(length(x)==1)
return(internal(x))
else if(length(x) %in% c(2:20))
return(max(apply(matrix(x),1,internal)))
else
return(max(apply(matrix(sample(x,size=20)),1,internal)))
}
### Parameter matrix of tar with 1 threshold, given the thresh and the delay
TAR1t_B<-function(thDelay,gamma,yy, xxl,xxh,z,m) {#
isL <- ifelse(z[, thDelay + 1]<= gamma,1,0) ### isL: dummy
ndown<-mean(isL)
xxthresh <- cbind(xxl * isL,xxh * (1 - isL)) ### Lower matrix
B<-round(matrix(solve(crossprod(xxthresh))%*%crossprod(xxthresh,yy), nrow=1),5)
Bcolnames <- c("Trend", c(paste("t -", seq_len(m))))
colnames(B)<-rep(Bcolnames,2)
Bdown <- B[,seq_len(ncol(B)/2)]
Bup <- B[,-seq_len(ncol(B)/2)]
nobs <- c(ndown=ndown, nup=1-ndown)
list(Bdown=Bdown, Bup=Bup, nobs=nobs)
}
### Parameter matrix of tar with 2 thresholds, given the 2 thresh and the delay
TAR2t_B <- function(gam1,gam2,thDelay, yy, xx,z,m){
##Threshold dummies
dummydown <- ifelse(z[, thDelay + 1]<=gam1, 1, 0)
regimedown <- dummydown*xx
ndown <- mean(dummydown)
dummyup <- ifelse(z[, thDelay + 1]>gam2, 1, 0)
regimeup <- dummyup*xx
nup <- mean(dummyup)
##SSR from TAR(3)
XX <- cbind(regimedown, (1-dummydown-dummyup)*xx, regimeup) # dim k(p+1) x t
B <- round(matrix(solve(crossprod(XX))%*%crossprod(XX,yy),nrow=1),5) #SSR
Bcolnames <- c("Trend", c(paste("t -", seq_len(m))))
colnames(B)<-rep(Bcolnames,3)
npar<-ncol(B)/3
Bdown <- B[,c(1:npar)]
Bmiddle <- B[,c(1:npar)+npar]
Bup <- B[,c(1:npar)+2*npar]
nobs <- c(ndown=ndown, nmiddle=1-ndown-nup,nup=nup)
list(Bdown=Bdown, Bmiddle=Bmiddle, Bup=Bup, nobs=nobs)
}
###Check if the AR coefficients in each regime lie inside the unit circle
is.InUnitCircle<-function(B,ninc,m, nthresh){
a<-ninc
B<-as.vector(B)
para<-B[a+seq_len(m)]
charPol<-c(1, -para)
val1<-polyroot(charPol)
root<-Mod(val1)
for(i in 1:(nthresh+1))
#extract coef
#build poly
#extract roots
#name it
if(nthresh==1|nthresh==2){
para2<-B[a+seq_len(m)+m+a]
charPol2<-c(1, -para2)
val2<-polyroot(charPol2)
root<-matrix(c(root,Mod(val2)), nrow=1)
colnames(root)<-c(paste("reg", rep(1,m)),paste("reg", rep(2,m)))
if(nthresh==2){
para3<-B[a+seq_len(m)+2*(m+a)]
charPol3<-c(1, -para3)
val3<-polyroot(charPol3)
root<-matrix(c(root, Mod(val3)), nrow=1)
colnames(root)<-c(paste("reg", rep(1,m)),paste("reg", rep(2,m)),paste("reg", rep(3,m)))
}
}
if(any(root<=1))
list(warn=TRUE, root=root)
else
list(warn=FALSE, root=root)
}
isRoot<-function(coef, regime=c("L", "M", "H", "."), lags){
regime<-match.arg(regime)
coefName<-paste("phi", regime, sep="")
phi<-coef[grep(coefName,names(coef))]
vec<-rep(0, max(lags))
vec[lags]<-phi
charPol<-c(1, -vec)
val<-polyroot(charPol)
root<-Mod(val)
if(any(root<=1)){
regimeName<-switch(regime, "L"="low", "M"="medium", "H"="high", "."="")
message<-paste("Possible unit root in the", regimeName, " regime. Roots are:", paste(round(root,4), collapse=" "))
warn<-TRUE
warning(message, call.=FALSE)
}
else{
message<-NA
warn<-FALSE
}
return(list(warn=FALSE, root=root, message=message))
}
percent<-function(x,digits=3,by100=FALSE){
a<-ifelse(by100,100,1)
paste(a*round(x,digits),"%",sep="")
}
myformat<-function(x,digits, toLatex=FALSE){
r<-x
littlex<-abs(x)<10^-(digits)
r[!littlex]<-formatC(x[!littlex],digits=digits, format="f")
r[littlex]<-format(x[littlex],digits=min(digits,2), scientific=TRUE)
if(toLatex)
r<-gsub("(e.*)","slashtext{\\1}",r)
if(class(x)=="numeric")
return(noquote(r))
if(class(x)=="matrix")
return(matrix(noquote(r), ncol=ncol(x), nrow=nrow(x)))
}
###Assign to class list is class matrix
asListIfMat<-function(x){
if(class(x)=="matrix")
return(list(x))
else
return(x)
}
myInsertCol<-function (m, c, v = NA) {
#m: matrix
#c: columns numbers
#v: value to add (scalar only)
nr <- nrow(m)
nc <- ncol(m)
#first
m2 <- if (1 %in% c) cbind(matrix(v, nrow = nr), m) else m
#inter
for(i in c[!c%in%c(1, length(c)+nc)])
m2 <- cbind(m2[, 1:(i - 1), drop=FALSE], matrix(v, nrow = nr), m2[,i:ncol(m2), drop=FALSE])
#last
if (eval(ncol(m2) + 1) %in% c)
m2 <- cbind(m2, matrix(v, nrow = nr))
return(m2)
}
ct <- function(x) c(t(x))
if(FALSE){
X<-freeny.x
myInsertCol(X, 1, 2)
myInsertCol(X, c(1,2,4,5), 1)
myInsertCol(X, c(1,2,3,5,9), 1)
}