-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeriodCommon.R
executable file
·66 lines (52 loc) · 1.68 KB
/
PeriodCommon.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
PeriodCommon <- function(X, Y) {
# Common period of a time series with time series matrix
# D Meko, Last revised 2021-05-17
#
# X [matrix] multiple time series; time vector as column 1
# Y [matrix] single time series; time vector as column 1
#
# Returns list with parts
# X, Y [matrix] like input X, Y, but for years in which no data are
# missing in Y or in any of the series in X
# Beware that time (e.g., year) is first column of these matrices
# tgo, tsp [numeric]: start and ending times (e.g., years) of X and Y
#
# Why? Written to facilitate organization of predictors and predicand for
# calls to regression functions.
X<-as.matrix(X)
Y<-as.matrix(Y)
# Y ; Separate time column;
y1 <- Y[,-1]
yry1 <-Y[,1]
yrX1<-X[,1]
X1<-X[,-1]
# In case of X having just one series
X1<-as.matrix(X1)
yrX1<-as.matrix(yrX1)
#--- BUILD NA MTX TO HOLD ALL OF AND X AND Y
nA<-ncol(X1)+1
ton<-min(yry1[1],yrX1[1])
toff<-max(yry1[length(yry1)],yrX1[dim(X1)[1]])
mA<-toff-ton+1
A<-matrix(NA,mA,nA)
yrA<-(ton:toff)
#--- FILL COLS OF A, PREDICTAND IN COL 1 if applicable
irow<-yry1-ton+1 # row indices of tarter slots in A for y
A[irow,1]=y1;
irow<-yrX1-ton+1 # row indices of tarter slots in A for X1
A[irow,2:nA]=X1
#-- FIND ROWS WITH NO NA'S AND PULL SEGMENT
L=complete.cases(A)
A<-A[L,]
yrA<-yrA[L]
mA<-nrow(A)
nA<-ncol(A)
# error message if time vector does not increment by 1
L<-all(diff(yrA)==1)
if(!L) stop('year column of A does not increment by 1')
Ynew <- cbind(yrA,A[,1])
Xnew <- cbind(yrA,A[,2:nA])
tgo<-yrA[1]
tsp<-yrA[mA]
Output<-list(X=Xnew,Y=Ynew,tgo=tgo,tsp=tsp)
}