0 | |
testIts <- function(graph=TRUE,New=TRUE)
|
1 | |
{
|
2 | |
addDimnames <- function(mat)
|
3 | |
{
|
4 | |
if(is.null(dimnames(mat))) {dimnames(mat) <- list(NULL,NULL)}
|
5 | |
if(is.null(dimnames(mat)[[1]])&(nrow(mat)>0)) {dimnames(mat)[[1]] <- 1:nrow(mat)}
|
6 | |
if(is.null(dimnames(mat)[[2]])&(ncol(mat)>0)) {dimnames(mat)[[2]] <- 1:ncol(mat)}
|
7 | |
return(mat)
|
8 | |
}
|
9 | |
test <- function(x){if(!identical(x,TRUE))stop()}
|
10 | |
##test procedure for its
|
11 | |
mytimes <- seq.POSIXt(from=Sys.time(),by=24*60*60,length.out=10)
|
12 | |
attr(mytimes,"tzone") <- ""
|
13 | |
mat <- addDimnames(matrix(1:30,10,3))
|
14 | |
dimnames(mat)[[2]] <- c("A","B","C")
|
15 | |
its.format("%Y-%m-%d %X")
|
16 | |
x <- its(mat,mytimes)
|
17 | |
moretimes <- seq.POSIXt(from=Sys.time()+1,by=24*60*60,length.out=11)
|
18 | |
more <- addDimnames(matrix(1:33,11,3))
|
19 | |
dimnames(more)[[2]] <- c("A","B","C")
|
20 | |
x2 <- its(more,moretimes)
|
21 | |
##its-arith**********************************************************
|
22 | |
##arith-methods------------------------------------------------------
|
|
0 |
require(its)
|
|
1 |
|
|
2 |
addDimnames <- function(mat) {
|
|
3 |
if(is.null(dimnames(mat))) {dimnames(mat) <- list(NULL,NULL)}
|
|
4 |
if(is.null(dimnames(mat)[[1]])&(nrow(mat)>0)) {dimnames(mat)[[1]] <- 1:nrow(mat)}
|
|
5 |
if(is.null(dimnames(mat)[[2]])&(ncol(mat)>0)) {dimnames(mat)[[2]] <- 1:ncol(mat)}
|
|
6 |
return(mat)
|
|
7 |
}
|
|
8 |
|
|
9 |
test <- function(x) {
|
|
10 |
if(!x) stop()
|
|
11 |
}
|
|
12 |
|
|
13 |
today <- as.POSIXct(format(Sys.time(),"%Y-%m-%d"))
|
|
14 |
mytimes <- seq.POSIXt(from=today,by="DSTday",length.out=10)
|
|
15 |
attr(mytimes,"tzone") <- ""
|
|
16 |
mat <- addDimnames(matrix(1:30,10,3))
|
|
17 |
dimnames(mat)[[2]] <- c("A","B","C")
|
|
18 |
its.format("%Y-%m-%d")
|
|
19 |
x <- its(mat,mytimes)
|
|
20 |
moretimes <- seq.POSIXt(from=today+1,by="DSTday",length.out=11)
|
|
21 |
more <- addDimnames(matrix(1:33,11,3))
|
|
22 |
dimnames(more)[[2]] <- c("A","B","C")
|
|
23 |
x2 <- its(more,moretimes)
|
|
24 |
|
|
25 |
|
|
26 |
##its-arith**********************************************************
|
|
27 |
##arith-methods------------------------------------------------------
|
|
28 |
test.arith <- function() {
|
23 | 29 |
y1 <- x[,1]+x[,2]
|
24 | 30 |
y2 <- its(x@.Data[,1,drop=FALSE]+x@.Data[,2,drop=FALSE],mytimes)
|
25 | 31 |
test(all.equal(y1@.Data,y2@.Data))
|
|
32 | 38 |
y2 <- its(x@.Data[,1,drop=FALSE]+pi,mytimes)
|
33 | 39 |
test(all.equal(y1@.Data,y2@.Data))
|
34 | 40 |
test(all.equal(y1@dates,y2@dates))
|
35 | |
y3 <- its(mat,mytimes+24*60*60)
|
36 | |
|
37 | |
## changed in version 1.0.4
|
38 | |
## the intersection of the dates is now taken
|
39 | |
##ermsg <- try(x+y3,silent=TRUE)
|
40 | |
##test(ermsg=="Error in x + y3 : dates must match\n")
|
41 | |
##ermsg <- try(x+x2,silent=TRUE)
|
42 | |
##test(ermsg=="Error in x + x2 : dates must match\n")
|
43 | |
|
44 | |
|
45 | |
##extractor**********************************************************
|
|
41 |
}
|
|
42 |
|
|
43 |
##extractor**********************************************************
|
|
44 |
test.extractor <- function() {
|
46 | 45 |
y1 <- x[,1,dates=dates(x)[1:5]]
|
47 | 46 |
y2 <- x[1:5,1]
|
48 | 47 |
y3 <- y1+y2
|
49 | 48 |
test(all(dates(y1)==dates(x)[1:5]))
|
50 | 49 |
test(all(core(y1)==core(x)[1:5,1]))
|
51 | |
##names**************************************************************
|
|
50 |
}
|
|
51 |
|
|
52 |
##names**************************************************************
|
|
53 |
test.names <- function() {
|
52 | 54 |
test(all(names(x)==dimnames(core(x))[[2]]))
|
53 | 55 |
y1 <- x
|
54 | 56 |
names(y1) <- letters[1:ncol(y1)]
|
55 | 57 |
test(all(names(y1)==letters[1:ncol(y1)]))
|
56 | |
##dates**************************************************************
|
|
58 |
}
|
|
59 |
|
|
60 |
##dates**************************************************************
|
|
61 |
test.dates <- function() {
|
57 | 62 |
test(all(dates(x)==x@dates))
|
58 | 63 |
y1 <- x
|
59 | 64 |
dates(y1) <- moretimes[1:nrow(y1)]
|
60 | 65 |
test(all(dates(y1)==moretimes[1:nrow(y1)]))
|
61 | |
##core***************************************************************
|
|
66 |
}
|
|
67 |
|
|
68 |
##core***************************************************************
|
|
69 |
test.core <- function() {
|
62 | 70 |
test(all(core(x)==x@.Data))
|
63 | 71 |
y1 <- x
|
64 | 72 |
core(x) <- addDimnames(matrix(101:130,10,3))
|
65 | 73 |
test(all(core(x)==addDimnames(matrix(101:130,10,3))))
|
66 | |
##its-cumdif*********************************************************
|
67 | |
##cumsum-method------------------------------------------------------
|
|
74 |
}
|
|
75 |
|
|
76 |
##its-cumdif*********************************************************
|
|
77 |
##cumsum-method------------------------------------------------------
|
|
78 |
test.cumsum <- function() {
|
68 | 79 |
foo <- cumsum(x)
|
69 | 80 |
test(all.equal(foo@.Data[,1],cumsum(x@.Data[,1])))
|
70 | 81 |
test(all.equal(foo@dates,mytimes))
|
71 | |
##diff-method--------------------------------------------------------
|
|
82 |
}
|
|
83 |
|
|
84 |
##diff-method--------------------------------------------------------
|
|
85 |
test.diff <- function() {
|
72 | 86 |
foo <- diff(cumsum(x))
|
73 | 87 |
bar <- alignedIts(foo,x,print=FALSE)
|
74 | 88 |
test(all.equal(bar[[1]],bar[[2]]))
|
75 | |
##its-def************************************************************
|
76 | |
##-Functions-
|
77 | |
##is.its-function----------------------------------------------------
|
|
89 |
}
|
|
90 |
|
|
91 |
##its-def************************************************************
|
|
92 |
##-Functions-
|
|
93 |
##is.its-function----------------------------------------------------
|
|
94 |
test.is.its <- function() {
|
78 | 95 |
test(is.its(x))
|
79 | 96 |
test(!is.its(x@.Data))
|
80 | 97 |
test(!is.its(x@dates))
|
81 | |
##as.its-function----------------------------------------------------
|
|
98 |
}
|
|
99 |
|
|
100 |
##as.its-function----------------------------------------------------
|
|
101 |
test.as.its <- function() {
|
82 | 102 |
foo <- as.numeric(mat[,1,drop=F])
|
83 | 103 |
class(foo) <- c("POSIXt","POSIXct")
|
84 | 104 |
bar <- its(mat[,-1],foo)
|
85 | 105 |
waz <- as.its(mat)
|
86 | 106 |
test(all.equal(bar,waz))
|
87 | |
##its-function-------------------------------------------------------
|
88 | |
x <- its(mat,mytimes)
|
|
107 |
}
|
|
108 |
|
|
109 |
##its-function-------------------------------------------------------
|
|
110 |
test.its.creation <- function() {
|
89 | 111 |
test(all.equal(x@dates,mytimes))
|
90 | 112 |
test(all.equal(x@.Data/mat,x@.Data/x@.Data))
|
|
113 |
|
91 | 114 |
##parameters
|
92 | |
years <- 100:105
|
93 | |
hoursecs <- 60*60
|
94 | |
regdaysecs <- 24*hoursecs
|
95 | |
monthdays <- c(28,29,30,31)
|
96 | |
monthsecs <- c(monthdays*regdaysecs,monthdays*regdaysecs-hoursecs,monthdays*regdaysecs+hoursecs)
|
97 | |
weeksecs <- 7*regdaysecs ##+hoursecs*c(-1,0,1)
|
98 | |
daysecs <- regdaysecs+hoursecs*c(-1,0,1)
|
|
115 |
## years <- 100:105
|
|
116 |
## hoursecs <- 60*60
|
|
117 |
## regdaysecs <- 24*hoursecs
|
|
118 |
## monthdays <- c(28,29,30,31)
|
|
119 |
## monthsecs <- c(monthdays*regdaysecs,monthdays*regdaysecs-hoursecs,monthdays*regdaysecs+hoursecs)
|
|
120 |
## weeksecs <- 7*regdaysecs ##+hoursecs*c(-1,0,1)
|
|
121 |
## daysecs <- regdaysecs+hoursecs*c(-1,0,1)
|
|
122 |
## its.format("%Y-%m-%d")
|
|
123 |
}
|
|
124 |
|
|
125 |
##***newIts
|
|
126 |
##newIts-from
|
|
127 |
test.new.its <- function() {
|
99 | 128 |
its.format("%Y-%m-%d")
|
100 | |
|
101 | |
##***newIts
|
102 | |
##newIts-from
|
103 | |
if(as.numeric(R.Version()$major)*1000+as.numeric(R.Version()$minor)*10>=1080 & New)
|
104 | |
{
|
105 | |
|
106 | |
mystarts <- c("2003-01-01","2002-12-31","2003-11-17","2004-10-27")
|
107 | |
myends <- c("2003-02-01","2003-12-31","2004-12-17","2004-11-01")
|
108 | |
|
109 | |
for(ddd in mystarts)
|
110 | |
{
|
111 | |
TEST <- newIts(start=ddd)
|
112 | |
test(start(TEST)==ddd)
|
113 | |
}
|
114 | |
##newIts-to
|
115 | |
for(ddd in myends)
|
116 | |
{
|
117 | |
TEST <- newIts(start="2002-11-17",end=ddd)
|
118 | |
test(end(TEST)==ddd)
|
119 | |
}
|
120 | |
for(i in 1:3)
|
121 | |
{
|
122 | |
TEST <- newIts(start=mystarts[i],end=myends[i])
|
123 | |
test((start(TEST)==mystarts[i])&(end(TEST)==myends[i]))
|
124 | |
}
|
125 | |
|
126 | |
##newIts-by
|
127 | |
now <- Sys.time()
|
128 | |
its.end.date <- now+100*24*60*60
|
129 | |
TEST <- newIts(end=format(its.end.date,"%Y-%m-%d"))
|
130 | |
test(all(diff(as.numeric(TEST@dates))%in%daysecs))
|
131 | |
TEST <- newIts(start="2003-10-1",end="2010-12-1",by="month")
|
132 | |
test(all(diff(as.numeric(TEST@dates))%in%monthsecs))
|
133 | |
##(n.b. no DSTweek available)
|
134 | |
TEST <- newIts(end=format(its.end.date,"%Y-%m-%d"),by="week")
|
135 | |
test(all(diff(as.numeric(TEST@dates))%in%weeksecs))
|
136 | |
|
137 | |
|
138 | |
##newIts-ncol
|
139 | |
ncol(newIts(end=format(its.end.date,"%Y-%m-%d"),ncol=5))==5
|
140 | |
|
141 | |
##***extractIts permutations
|
142 | |
##-weekday
|
143 | |
weekDaySelection <- c(0,6)
|
144 | |
nowt <- newIts(extract=TRUE,weekday=TRUE,select=weekDaySelection)
|
145 | |
test(length(nowt)==0)
|
146 | |
weekDaySelection <- 1:5
|
147 | |
TEST1 <- newIts(extract=TRUE,weekday=TRUE)
|
148 | |
TEST2 <- newIts(extract=TRUE,weekday=TRUE,select=weekDaySelection)
|
149 | |
TEST3 <- newIts()
|
150 | |
test(length(TEST1)>0)
|
151 | |
test(length(TEST2)==length(TEST1))
|
152 | |
test(length(TEST3)>length(TEST2))
|
153 | |
test(all(as.POSIXlt(TEST1@dates)$wday%in%weekDaySelection))
|
154 | |
test(all(weekDaySelection%in%as.POSIXlt(TEST1@dates)$wday))
|
155 | |
##-find ("all","last","first")
|
156 | |
test(identical(newIts(extract=TRUE,select=0:6,period="week"),newIts()))
|
157 | |
test(identical(newIts(extract=TRUE,select=0:6,period="week",find="all"),newIts()))
|
158 | |
TEST1 <- newIts(extract=TRUE,period="week",find="first",partial=FALSE)
|
159 | |
TEST2 <- newIts(extract=TRUE,select=0,period="week")
|
160 | |
test(all(TEST1%in%TEST2))
|
161 | |
TEST1 <- newIts(extract=TRUE,period="week",find="last",partial=FALSE)
|
162 | |
TEST2 <- newIts(extract=TRUE,select=6,period="week")
|
163 | |
test(all(TEST1%in%TEST2))
|
164 | |
TEST1 <- newIts(weekday=TRUE,extract=TRUE,period="week",find="first",partial=FALSE)
|
165 | |
TEST2 <- newIts(weekday=TRUE,extract=TRUE,select=1,period="week")
|
166 | |
test(all(TEST1%in%TEST2))
|
167 | |
TEST1 <- newIts(weekday=TRUE,extract=TRUE,period="week",find="last",partial=FALSE)
|
168 | |
TEST2 <- newIts(weekday=TRUE,extract=TRUE,select=5,period="week")
|
169 | |
test(all(TEST1%in%TEST2))
|
170 | |
##-period
|
171 | |
test(all(as.POSIXlt(newIts(extract=TRUE,period="week",find="first",partial=FALSE)@dates)$wday==0))
|
172 | |
test(all(as.POSIXlt(newIts(extract=TRUE,period="week",find="last",partial=FALSE)@dates)$wday==6))
|
173 | |
test(all(as.POSIXlt(newIts(extract=TRUE,period="month",find="first",partial=FALSE)@dates)$mday==1))
|
174 | |
test(all(as.POSIXlt(newIts(extract=TRUE,period="month",find="last",partial=FALSE)@dates)$mday%in%28:31))
|
175 | |
test(identical(newIts(extract=TRUE,period="week",select=0:6),newIts()))
|
176 | |
test(identical(newIts(extract=TRUE,period="month",select=1:31),newIts()))
|
177 | |
##-partials
|
178 | |
TEST <- newIts(start="2003-11-18")
|
179 | |
TEST1 <- newIts(start="2003-11-18",period="week",find="first",extract=TRUE,partial=TRUE)
|
180 | |
TEST2 <- newIts(start="2003-11-18",period="week",find="first",extract=TRUE,partial=FALSE)
|
181 | |
TEST@dates[1]==TEST1@dates[1]
|
182 | |
test((nrow(TEST1)-1)==nrow(TEST2))
|
183 | |
##-select
|
184 | |
for(i in 0:6)
|
185 | |
{
|
186 | |
TEST <- newIts(extract=TRUE,period="week",select=i)
|
187 | |
test(all(as.POSIXlt(TEST@dates)$wday==i))
|
188 | |
}
|
189 | |
for(i in 30:31)
|
190 | |
{
|
191 | |
TEST <- newIts(extract=TRUE,period="month",select=i)
|
192 | |
test(all(as.POSIXlt(TEST@dates)$mday==i))
|
193 | |
}
|
194 | |
}
|
195 | |
##its-disp***********************************************************
|
196 | |
##plot-method--------------------------------------------------------
|
197 | |
##create 5 sinusoids differing in phase by pi/6
|
|
129 |
mystarts <- c("2003-01-01","2002-12-31","2003-11-17","2004-10-27")
|
|
130 |
myends <- c("2003-02-01","2003-12-31","2004-12-17","2004-11-01")
|
|
131 |
|
|
132 |
for(ddd in mystarts) {
|
|
133 |
TEST <- newIts(start=ddd)
|
|
134 |
test(start(TEST)==ddd)
|
|
135 |
}
|
|
136 |
|
|
137 |
##newIts-to
|
|
138 |
for(ddd in myends) {
|
|
139 |
TEST <- newIts(start="2002-11-17",end=ddd)
|
|
140 |
test(end(TEST)==ddd)
|
|
141 |
}
|
|
142 |
|
|
143 |
for(i in 1:3) {
|
|
144 |
TEST <- newIts(start=mystarts[i],end=myends[i])
|
|
145 |
test((start(TEST)==mystarts[i])&(end(TEST)==myends[i]))
|
|
146 |
}
|
|
147 |
|
|
148 |
##newIts-by
|
|
149 |
now <- as.POSIXct(format(Sys.time(),"%Y-%m-%d"))
|
|
150 |
its.end.date <- now+100*24*60*60
|
|
151 |
|
|
152 |
day.range <- seq.POSIXt(from=now,to=its.end.date,by="DSTday")
|
|
153 |
TEST <- newIts(end=format(its.end.date,"%Y-%m-%d"))
|
|
154 |
test(all.equal(day.range,TEST@dates))
|
|
155 |
|
|
156 |
month.range <- seq.POSIXt(from=as.POSIXct("2003-10-01"),to=as.POSIXct("2010-12-01"),by="months")
|
|
157 |
TEST <- newIts(start="2003-10-01",end="2010-12-01",by="month")
|
|
158 |
test(all.equal(month.range,TEST@dates))
|
|
159 |
|
|
160 |
week.range <- seq.POSIXt(from=now,to=its.end.date,by="weeks")
|
|
161 |
TEST <- newIts(end=format(its.end.date,"%Y-%m-%d"),by="week")
|
|
162 |
test(all.equal(week.range,TEST@dates))
|
|
163 |
|
|
164 |
##newIts-ncol
|
|
165 |
ncol(newIts(end=format(its.end.date,"%Y-%m-%d"),ncol=5))==5
|
|
166 |
|
|
167 |
##***extractIts permutations
|
|
168 |
##-weekday
|
|
169 |
weekDaySelection <- c(0,6)
|
|
170 |
nowt <- newIts(extract=TRUE,weekday=TRUE,select=weekDaySelection)
|
|
171 |
test(length(nowt)==0)
|
|
172 |
weekDaySelection <- 1:5
|
|
173 |
TEST1 <- newIts(extract=TRUE,weekday=TRUE)
|
|
174 |
TEST2 <- newIts(extract=TRUE,weekday=TRUE,select=weekDaySelection)
|
|
175 |
TEST3 <- newIts()
|
|
176 |
test(length(TEST1)>0)
|
|
177 |
test(length(TEST2)==length(TEST1))
|
|
178 |
test(length(TEST3)>length(TEST2))
|
|
179 |
test(all(as.POSIXlt(TEST1@dates)$wday%in%weekDaySelection))
|
|
180 |
test(all(weekDaySelection%in%as.POSIXlt(TEST1@dates)$wday))
|
|
181 |
|
|
182 |
##-find ("all","last","first")
|
|
183 |
test(identical(newIts(extract=TRUE,select=0:6,period="week"),newIts()))
|
|
184 |
test(identical(newIts(extract=TRUE,select=0:6,period="week",find="all"),newIts()))
|
|
185 |
TEST1 <- newIts(extract=TRUE,period="week",find="first",partial=FALSE)
|
|
186 |
TEST2 <- newIts(extract=TRUE,select=0,period="week")
|
|
187 |
test(all(TEST1%in%TEST2))
|
|
188 |
TEST1 <- newIts(extract=TRUE,period="week",find="last",partial=FALSE)
|
|
189 |
TEST2 <- newIts(extract=TRUE,select=6,period="week")
|
|
190 |
test(all(TEST1%in%TEST2))
|
|
191 |
TEST1 <- newIts(weekday=TRUE,extract=TRUE,period="week",find="first",partial=FALSE)
|
|
192 |
TEST2 <- newIts(weekday=TRUE,extract=TRUE,select=1,period="week")
|
|
193 |
test(all(TEST1%in%TEST2))
|
|
194 |
TEST1 <- newIts(weekday=TRUE,extract=TRUE,period="week",find="last",partial=FALSE)
|
|
195 |
TEST2 <- newIts(weekday=TRUE,extract=TRUE,select=5,period="week")
|
|
196 |
test(all(TEST1%in%TEST2))
|
|
197 |
|
|
198 |
##-period
|
|
199 |
test(all(as.POSIXlt(newIts(extract=TRUE,period="week",find="first",partial=FALSE)@dates)$wday==0))
|
|
200 |
test(all(as.POSIXlt(newIts(extract=TRUE,period="week",find="last",partial=FALSE)@dates)$wday==6))
|
|
201 |
test(all(as.POSIXlt(newIts(extract=TRUE,period="month",find="first",partial=FALSE)@dates)$mday==1))
|
|
202 |
test(all(as.POSIXlt(newIts(extract=TRUE,period="month",find="last",partial=FALSE)@dates)$mday%in%28:31))
|
|
203 |
test(identical(newIts(extract=TRUE,period="week",select=0:6),newIts()))
|
|
204 |
test(identical(newIts(extract=TRUE,period="month",select=1:31),newIts()))
|
|
205 |
|
|
206 |
##-partials
|
|
207 |
TEST <- newIts(start="2003-11-18")
|
|
208 |
TEST1 <- newIts(start="2003-11-18",period="week",find="first",extract=TRUE,partial=TRUE)
|
|
209 |
TEST2 <- newIts(start="2003-11-18",period="week",find="first",extract=TRUE,partial=FALSE)
|
|
210 |
TEST@dates[1]==TEST1@dates[1]
|
|
211 |
test((nrow(TEST1)-1)==nrow(TEST2))
|
|
212 |
|
|
213 |
##-select
|
|
214 |
for(i in 0:6) {
|
|
215 |
TEST <- newIts(extract=TRUE,period="week",select=i)
|
|
216 |
test(all(as.POSIXlt(TEST@dates)$wday==i))
|
|
217 |
}
|
|
218 |
|
|
219 |
for(i in 30:31) {
|
|
220 |
TEST <- newIts(extract=TRUE,period="month",select=i)
|
|
221 |
test(all(as.POSIXlt(TEST@dates)$mday==i))
|
|
222 |
}
|
|
223 |
}
|
|
224 |
|
|
225 |
##its-disp***********************************************************
|
|
226 |
##plot-method--------------------------------------------------------
|
|
227 |
##create 5 sinusoids differing in phase by pi/6
|
|
228 |
test.plot.its <- function() {
|
198 | 229 |
its.format("%Y-%m-%d %X")
|
199 | |
if(graph)
|
200 | |
{
|
201 | |
sintimes <- seq.POSIXt(from=Sys.time(),by=24*60*60,length.out=100)
|
202 | |
sintimes.num <- as.numeric(sintimes)
|
203 | |
sinmat <- addDimnames(matrix(NA,100,5))
|
204 | |
for(j in 1:5){sinmat[,j] <- sin((6*pi*sintimes.num/(sintimes.num[100]-sintimes.num[1]))-j*pi/6)}
|
205 | |
dimnames(sinmat)[[2]] <- LETTERS[1:5]
|
206 | |
sinx <- its(sinmat,sintimes)
|
207 | |
par(mfrow=c(3,3))
|
208 | |
##line,point
|
209 | |
plot(sinx,type="p",main="Point")
|
210 | |
plot(sinx,type="l",main="Line")
|
211 | |
plot(sinx,type="b",main="Both")
|
212 | |
##colour, width, type cycling
|
213 | |
plot(sinx,lwdvec=1:3,main="Width")
|
214 | |
plot(sinx,ltypvec=1:3,main="Type")
|
215 | |
plot(sinx,colvec=c(1,2,7),main="Colour")
|
216 | |
##axis
|
217 | |
plot(sinx,format="%B",main="Label")
|
218 | |
plot(sinx,at=sintimes[c(1,100)],main="Position")
|
219 | |
##NA handling
|
220 | |
sinx[10:20,] <- sinx[10:20,]*NA
|
221 | |
sinx[,2] <- sinx[,2]*NA
|
222 | |
plot(sinx,interp="n",main="NAs")
|
223 | |
}
|
224 | |
##print-method-------------------------------------------------------
|
225 | |
print(x)
|
226 | |
##its-file***********************************************************
|
227 | |
##writecsvIts-function-----------------------------------------------
|
|
230 |
sintimes <- seq.POSIXt(from=Sys.time(),by=24*60*60,length.out=100)
|
|
231 |
sintimes.num <- as.numeric(sintimes)
|
|
232 |
sinmat <- addDimnames(matrix(NA,100,5))
|
|
233 |
for(j in 1:5){sinmat[,j] <- sin((6*pi*sintimes.num/(sintimes.num[100]-sintimes.num[1]))-j*pi/6)}
|
|
234 |
dimnames(sinmat)[[2]] <- LETTERS[1:5]
|
|
235 |
sinx <- its(sinmat,sintimes)
|
|
236 |
par(mfrow=c(3,3))
|
|
237 |
##line,point
|
|
238 |
plot(sinx,type="p",main="Point")
|
|
239 |
plot(sinx,type="l",main="Line")
|
|
240 |
plot(sinx,type="b",main="Both")
|
|
241 |
##colour, width, type cycling
|
|
242 |
plot(sinx,lwdvec=1:3,main="Width")
|
|
243 |
plot(sinx,ltypvec=1:3,main="Type")
|
|
244 |
plot(sinx,colvec=c(1,2,7),main="Colour")
|
|
245 |
##axis
|
|
246 |
plot(sinx,format="%B",main="Label")
|
|
247 |
plot(sinx,at=sintimes[c(1,100)],main="Position")
|
|
248 |
##NA handling
|
|
249 |
sinx[10:20,] <- sinx[10:20,]*NA
|
|
250 |
sinx[,2] <- sinx[,2]*NA
|
|
251 |
plot(sinx,interp="n",main="NAs")
|
|
252 |
}
|
|
253 |
|
|
254 |
##print-method-------------------------------------------------------
|
|
255 |
print(x)
|
|
256 |
##its-file***********************************************************
|
|
257 |
##writecsvIts-function-----------------------------------------------
|
|
258 |
test.read.write.its <- function() {
|
228 | 259 |
file <- tempfile()
|
229 | 260 |
writecsvIts(x,file,col.names=FALSE)
|
230 | 261 |
writecsvIts(x,file,row.names=FALSE,col.names=FALSE)
|
|
232 | 263 |
##readcsvIts-function------------------------------------------------
|
233 | 264 |
foo <- its(readcsvIts(file))
|
234 | 265 |
y <- its(x)
|
235 | |
test(all.equal(foo,y))
|
236 | 266 |
test(identical(as.numeric(foo@.Data),as.numeric(y@.Data)))
|
237 | |
test(identical(foo@dates,y@dates))
|
|
267 |
test(all.equal(foo@dates,y@dates))
|
238 | 268 |
test(identical(dimnames(foo),dimnames(y)))
|
239 | |
##its-fin************************************************************
|
240 | |
##accrueIts-function-------------------------------------------------
|
241 | |
test(all.equal(accrueIts(x)-lagIts(x)[-1,]/(365),accrueIts(x)*0))
|
242 | |
##its-info***********************************************************
|
243 | |
##summary-method-----------------------------------------------------
|
|
269 |
}
|
|
270 |
|
|
271 |
##its-fin************************************************************
|
|
272 |
##accrueIts-function-------------------------------------------------
|
|
273 |
##test(all.equal(accrueIts(x)-lagIts(x)[-1,]/(365),accrueIts(x)*0))
|
|
274 |
##its-info***********************************************************
|
|
275 |
##summary-method-----------------------------------------------------
|
|
276 |
test.its.summary <- function() {
|
244 | 277 |
foo <- summary(x)
|
245 | 278 |
test(all.equal(as.numeric(foo[1,]),seq(1,21,10)))
|
246 | 279 |
test(all.equal(as.numeric(foo[6,]),seq(10,30,10)))
|
247 | 280 |
test(all.equal(as.numeric(foo[8,]),rep(10,3)))
|
248 | |
##start-method-------------------------------------------------------
|
|
281 |
}
|
|
282 |
|
|
283 |
##start-method-------------------------------------------------------
|
|
284 |
test.its.start <- function() {
|
249 | 285 |
test(identical(start(x,format="%Y-%m-%d-%X"),format.POSIXct(mytimes[1],format="%Y-%m-%d-%X")))
|
250 | 286 |
test(identical(start(x[2:nrow(x),],format="%Y-%m-%d-%X"),format.POSIXct(mytimes[2],format="%Y-%m-%d-%X")))
|
251 | |
##end -method--------------------------------------------------------
|
|
287 |
}
|
|
288 |
|
|
289 |
##end -method--------------------------------------------------------
|
|
290 |
test.its.end <- function() {
|
252 | 291 |
test(identical(end(x,format="%Y-%m-%d %X"),format.POSIXct(mytimes[10],format="%Y-%m-%d %X")))
|
253 | 292 |
test(identical(end(x[1:(nrow(x)-1),],format="%Y-%m-%d %X"),format.POSIXct(mytimes[nrow(x)-1],format="%Y-%m-%d %X")))
|
254 | |
##its-join***********************************************************
|
255 | |
##alignedIts-function---------------------------------------------
|
|
293 |
}
|
|
294 |
##its-join***********************************************************
|
|
295 |
##alignedIts-function---------------------------------------------
|
|
296 |
test.aligned.its <- function() {
|
|
297 |
its.format("%Y-%m-%d")
|
256 | 298 |
isub <- seq(1,9,2)
|
257 | |
x <- its(x)
|
258 | 299 |
xsub <- x[isub,]
|
259 | 300 |
foo <- alignedIts(x,xsub,print=F)
|
260 | 301 |
|
|
264 | 305 |
|
265 | 306 |
test(identical(foo[[1]]@dates,xsub@dates))
|
266 | 307 |
test(identical(core(foo[[1]]),core(xsub)))
|
267 | |
|
268 | |
##appendIts-function-------------------------------------------------
|
269 | |
|
|
308 |
}
|
|
309 |
|
|
310 |
##appendIts-function-------------------------------------------------
|
|
311 |
test.append.its <- function() {
|
|
312 |
its.format("%Y-%m-%d %X")
|
|
313 |
xx <- its(mat,mytimes)
|
270 | 314 |
## these operations change the order of the attributes of the date
|
271 | 315 |
## after this, identical can't be used to compare series
|
272 | 316 |
## because the attributes order does not match
|
273 | 317 |
later <- mytimes+366*24*60*60
|
274 | 318 |
over <- mytimes+5*24*60*60
|
275 | 319 |
|
276 | |
|
277 | |
x <- its(mat,mytimes)
|
278 | 320 |
xlate <- its(mat,later)
|
279 | 321 |
xover <- its(mat,over)
|
280 | 322 |
|
281 | |
foo <- appendIts(x,xlate)
|
282 | |
bar <- appendIts(xlate,x)
|
|
323 |
foo <- appendIts(xx,xlate)
|
|
324 |
bar <- appendIts(xlate,xx)
|
283 | 325 |
test(all.equal(foo,bar))
|
284 | |
test(all.equal(foo[1:10],x))
|
|
326 |
test(all.equal(foo[1:10],xx))
|
285 | 327 |
## test(identical(foo[11:20],xlate))
|
286 | 328 |
test(all.equal(foo[11:20],xlate))
|
287 | |
foo <- try(appendIts(x,x[(2:(nrow(x)-1)),],but=FALSE),silent=TRUE)
|
|
329 |
foo <- try(appendIts(x,xx[(2:(nrow(x)-1)),],but=FALSE),silent=TRUE)
|
288 | 330 |
test(identical(grep("appendor data must extend appendee data",foo)>0,TRUE))
|
289 | 331 |
foo <- try(appendIts(x,xover,but=FALSE),silent=TRUE)
|
290 | 332 |
test(identical(grep("overlap data does not match",foo)>0,TRUE))
|
|
293 | 335 |
dimnames(xlate)[[2]][1] <- "Z"
|
294 | 336 |
foo <- try(appendIts(x,xlate),silent=TRUE)
|
295 | 337 |
test(foo=="Error in appendIts(x, xlate) : names of the two inputs must match\n")
|
296 | |
##10 cases
|
|
338 |
##10 cases
|
297 | 339 |
## S1 E1 S2 E2
|
298 | 340 |
## 1 1 2 3 4
|
299 | 341 |
## 2 1 3 2 4
|
|
305 | 347 |
## 8 1 2 3 3
|
306 | 348 |
## 9 2 3 1 1
|
307 | 349 |
##10 1 2 1 1
|
308 | |
x <- its(mat,mytimes)
|
|
350 |
|
309 | 351 |
## 1 1 2 3 4
|
310 | |
x1 <- x[1:4,]
|
311 | |
x2 <- x[5:8,]
|
312 | |
foo <- appendIts(x1,x2,but=FALSE,matchnames=FALSE)
|
313 | |
test(identical(x[1:8,],foo))
|
314 | |
foo <- appendIts(x1,x2,but=FALSE,matchnames=TRUE)
|
315 | |
test(identical(x[1:8,],foo))
|
|
352 |
x1 <- xx[1:4,]
|
|
353 |
x2 <- xx[5:8,]
|
|
354 |
foo <- appendIts(x1,x2,but=FALSE,matchnames=FALSE)
|
|
355 |
test(identical(xx[1:8,],foo))
|
|
356 |
foo <- appendIts(x1,x2,but=FALSE,matchnames=TRUE)
|
|
357 |
test(identical(xx[1:8,],foo))
|
316 | 358 |
foo <- appendIts(x1,x2,but=TRUE,matchnames=FALSE)
|
317 | |
test(identical(x[1:8,],foo))
|
|
359 |
test(identical(xx[1:8,],foo))
|
318 | 360 |
foo <- appendIts(x1,x2,but=TRUE,matchnames=TRUE)
|
319 | |
test(identical(x[1:8,],foo))
|
|
361 |
test(identical(xx[1:8,],foo))
|
320 | 362 |
## 2 1 3 2 4
|
321 | |
x1 <- x[1:4,]
|
322 | |
x2 <- x[3:6,]
|
323 | |
foo <- appendIts(x1,x2,but=FALSE,matchnames=FALSE)
|
324 | |
test(identical(x[1:6,],foo))
|
325 | |
foo <- appendIts(x1,x2,but=FALSE,matchnames=TRUE)
|
326 | |
test(identical(x[1:6,],foo))
|
|
363 |
x1 <- xx[1:4,]
|
|
364 |
x2 <- xx[3:6,]
|
|
365 |
foo <- appendIts(x1,x2,but=FALSE,matchnames=FALSE)
|
|
366 |
test(identical(xx[1:6,],foo))
|
|
367 |
foo <- appendIts(x1,x2,but=FALSE,matchnames=TRUE)
|
|
368 |
test(identical(xx[1:6,],foo))
|
327 | 369 |
foo <- try(appendIts(x1,x2,but=TRUE,matchnames=FALSE),silent=TRUE)
|
328 | 370 |
test(identical(grep("overlap not allowed",foo)>0,TRUE))
|
329 | 371 |
foo <- try(appendIts(x1,x2,but=TRUE,matchnames=TRUE),silent=TRUE)
|
330 | 372 |
test(identical(grep("overlap not allowed",foo)>0,TRUE))
|
331 | 373 |
## 3 1 4 2 3
|
332 | |
x1 <- x[1:4,]
|
333 | |
x2 <- x[2:3,]
|
|
374 |
x1 <- xx[1:4,]
|
|
375 |
x2 <- xx[2:3,]
|
334 | 376 |
foo <- try(appendIts(x1,x2,but=FALSE,matchnames=FALSE),silent=TRUE)
|
335 | 377 |
test(identical(grep("appendor data must extend appendee data",foo)>0,TRUE))
|
336 | 378 |
foo <- try(appendIts(x1,x2,but=FALSE,matchnames=TRUE),silent=TRUE)
|
|
340 | 382 |
foo <- try(appendIts(x1,x2,but=TRUE,matchnames=TRUE),silent=TRUE)
|
341 | 383 |
test(identical(grep("overlap not allowed",foo)>0,TRUE))
|
342 | 384 |
## 4 2 3 1 4
|
343 | |
x1 <- x[2:3,]
|
344 | |
x2 <- x[1:4,]
|
|
385 |
x1 <- xx[2:3,]
|
|
386 |
x2 <- xx[1:4,]
|
345 | 387 |
foo <- try(appendIts(x1,x2,but=FALSE,matchnames=FALSE),silent=TRUE)
|
346 | 388 |
test(identical(grep("appendor data must extend appendee data",foo)>0,TRUE))
|
347 | 389 |
foo <- try(appendIts(x1,x2,but=FALSE,matchnames=TRUE),silent=TRUE)
|
|
351 | 393 |
foo <- try(appendIts(x1,x2,but=TRUE,matchnames=TRUE),silent=TRUE)
|
352 | 394 |
test(identical(grep("overlap not allowed",foo)>0,TRUE))
|
353 | 395 |
## 5 2 4 1 3
|
354 | |
x1 <- x[2:4,]
|
355 | |
x2 <- x[1:3,]
|
356 | |
foo <- appendIts(x1,x2,but=FALSE,matchnames=FALSE)
|
357 | |
test(identical(x[1:4,],foo))
|
358 | |
foo <- appendIts(x1,x2,but=FALSE,matchnames=TRUE)
|
359 | |
test(identical(x[1:4,],foo))
|
|
396 |
x1 <- xx[2:4,]
|
|
397 |
x2 <- xx[1:3,]
|
|
398 |
foo <- appendIts(x1,x2,but=FALSE,matchnames=FALSE)
|
|
399 |
test(identical(xx[1:4,],foo))
|
|
400 |
foo <- appendIts(x1,x2,but=FALSE,matchnames=TRUE)
|
|
401 |
test(identical(xx[1:4,],foo))
|
360 | 402 |
foo <- try(appendIts(x1,x2,but=TRUE,matchnames=FALSE),silent=TRUE)
|
361 | 403 |
test(identical(grep("overlap not allowed",foo)>0,TRUE))
|
362 | 404 |
foo <- try(appendIts(x1,x2,but=TRUE,matchnames=TRUE),silent=TRUE)
|
363 | 405 |
test(identical(grep("overlap not allowed",foo)>0,TRUE))
|
364 | 406 |
## 6 3 4 1 2
|
365 | |
x1 <- x[3:4,]
|
366 | |
x2 <- x[1:2,]
|
367 | |
foo <- appendIts(x1,x2,but=FALSE,matchnames=FALSE)
|
368 | |
test(identical(x[1:4,],foo))
|
369 | |
foo <- appendIts(x1,x2,but=FALSE,matchnames=TRUE)
|
370 | |
test(identical(x[1:4,],foo))
|
|
407 |
x1 <- xx[3:4,]
|
|
408 |
x2 <- xx[1:2,]
|
|
409 |
foo <- appendIts(x1,x2,but=FALSE,matchnames=FALSE)
|
|
410 |
test(identical(xx[1:4,],foo))
|
|
411 |
foo <- appendIts(x1,x2,but=FALSE,matchnames=TRUE)
|
|
412 |
test(identical(xx[1:4,],foo))
|
371 | 413 |
foo <- appendIts(x1,x2,but=TRUE,matchnames=FALSE)
|
372 | |
test(identical(x[1:4,],foo))
|
|
414 |
test(identical(xx[1:4,],foo))
|
373 | 415 |
foo <- appendIts(x1,x2,but=TRUE,matchnames=TRUE)
|
374 | |
test(identical(x[1:4,],foo))
|
|
416 |
test(identical(xx[1:4,],foo))
|
375 | 417 |
## 7 1 2 2 2
|
376 | |
x1 <- x[1:4,]
|
377 | |
x2 <- x[4,]
|
378 | |
foo <- appendIts(x1,x2,but=FALSE,matchnames=FALSE)
|
379 | |
test(identical(x[1:4,],foo))
|
380 | |
foo <- appendIts(x1,x2,but=FALSE,matchnames=TRUE)
|
381 | |
test(identical(x[1:4,],foo))
|
|
418 |
x1 <- xx[1:4,]
|
|
419 |
x2 <- xx[4,]
|
|
420 |
foo <- appendIts(x1,x2,but=FALSE,matchnames=FALSE)
|
|
421 |
test(identical(xx[1:4,],foo))
|
|
422 |
foo <- appendIts(x1,x2,but=FALSE,matchnames=TRUE)
|
|
423 |
test(identical(xx[1:4,],foo))
|
382 | 424 |
foo <- try(appendIts(x1,x2,but=TRUE,matchnames=FALSE),silent=TRUE)
|
383 | 425 |
test(identical(grep("overlap not allowed",foo)>0,TRUE))
|
384 | 426 |
foo <- try(appendIts(x1,x2,but=TRUE,matchnames=TRUE),silent=TRUE)
|
385 | 427 |
test(identical(grep("overlap not allowed",foo)>0,TRUE))
|
386 | 428 |
## 8 1 2 3 3
|
387 | |
x1 <- x[1:4,]
|
388 | |
x2 <- x[5,]
|
389 | |
foo <- appendIts(x1,x2,but=FALSE,matchnames=FALSE)
|
390 | |
test(identical(x[1:5,],foo))
|
391 | |
foo <- appendIts(x1,x2,but=FALSE,matchnames=TRUE)
|
392 | |
test(identical(x[1:5,],foo))
|
|
429 |
x1 <- xx[1:4,]
|
|
430 |
x2 <- xx[5,]
|
|
431 |
foo <- appendIts(x1,x2,but=FALSE,matchnames=FALSE)
|
|
432 |
test(identical(xx[1:5,],foo))
|
|
433 |
foo <- appendIts(x1,x2,but=FALSE,matchnames=TRUE)
|
|
434 |
test(identical(xx[1:5,],foo))
|
393 | 435 |
foo <- appendIts(x1,x2,but=TRUE,matchnames=FALSE)
|
394 | |
test(identical(x[1:5,],foo))
|
|
436 |
test(identical(xx[1:5,],foo))
|
395 | 437 |
foo <- appendIts(x1,x2,but=TRUE,matchnames=TRUE)
|
396 | |
test(identical(x[1:5,],foo))
|
|
438 |
test(identical(xx[1:5,],foo))
|
397 | 439 |
## 9 2 3 1 1
|
398 | |
x1 <- x[2:4,]
|
399 | |
x2 <- x[1,]
|
400 | |
foo <- appendIts(x1,x2,but=FALSE,matchnames=FALSE)
|
401 | |
test(identical(x[1:4,],foo))
|
402 | |
foo <- appendIts(x1,x2,but=FALSE,matchnames=TRUE)
|
403 | |
test(identical(x[1:4,],foo))
|
|
440 |
x1 <- xx[2:4,]
|
|
441 |
x2 <- xx[1,]
|
|
442 |
foo <- appendIts(x1,x2,but=FALSE,matchnames=FALSE)
|
|
443 |
test(identical(xx[1:4,],foo))
|
|
444 |
foo <- appendIts(x1,x2,but=FALSE,matchnames=TRUE)
|
|
445 |
test(identical(xx[1:4,],foo))
|
404 | 446 |
foo <- appendIts(x1,x2,but=TRUE,matchnames=FALSE)
|
405 | |
test(identical(x[1:4,],foo))
|
|
447 |
test(identical(xx[1:4,],foo))
|
406 | 448 |
foo <- appendIts(x1,x2,but=TRUE,matchnames=TRUE)
|
407 | |
test(identical(x[1:4,],foo))
|
|
449 |
test(identical(xx[1:4,],foo))
|
408 | 450 |
##10 1 2 1 1
|
409 | |
x1 <- x[2:4,]
|
410 | |
x2 <- x[2,]
|
411 | |
foo <- appendIts(x1,x2,but=FALSE,matchnames=FALSE)
|
412 | |
test(identical(x[2:4,],foo))
|
413 | |
foo <- appendIts(x1,x2,but=FALSE,matchnames=TRUE)
|
414 | |
test(identical(x[2:4,],foo))
|
|
451 |
x1 <- xx[2:4,]
|
|
452 |
x2 <- xx[2,]
|
|
453 |
foo <- appendIts(x1,x2,but=FALSE,matchnames=FALSE)
|
|
454 |
test(identical(xx[2:4,],foo))
|
|
455 |
foo <- appendIts(x1,x2,but=FALSE,matchnames=TRUE)
|
|
456 |
test(identical(xx[2:4,],foo))
|
415 | 457 |
foo <- try(appendIts(x1,x2,but=TRUE,matchnames=FALSE),silent=TRUE)
|
416 | 458 |
test(identical(grep("overlap not allowed",foo)>0,TRUE))
|
417 | 459 |
foo <- try(appendIts(x1,x2,but=TRUE,matchnames=TRUE),silent=TRUE)
|
418 | 460 |
test(identical(grep("overlap not allowed",foo)>0,TRUE))
|
419 | |
##union-method-------------------------------------------------------
|
|
461 |
}
|
|
462 |
|
|
463 |
##union-method-------------------------------------------------------
|
|
464 |
test.union.its <- function() {
|
|
465 |
its.format("%Y-%m-%d")
|
420 | 466 |
isub <- seq(1,9,2)
|
421 | 467 |
ioth <- setdiff(1:10,isub)
|
422 | 468 |
xsub <- x[isub,]
|
|
424 | 470 |
test(identical(xun[,1:3],x))
|
425 | 471 |
test(identical(xun[isub,4:6],xsub))
|
426 | 472 |
test(all(is.na(xun[ioth,4:6])))
|
427 | |
##intersect-method---------------------------------------------------
|
|
473 |
}
|
|
474 |
|
|
475 |
##intersect-method---------------------------------------------------
|
|
476 |
test.intersect.its <- function() {
|
|
477 |
its.format("%Y-%m-%d")
|
|
478 |
isub <- seq(1,9,2)
|
428 | 479 |
xsub <- x[isub,]
|
429 | 480 |
xin <- intersect(x,xsub)
|
430 | 481 |
test(identical(xin[,1:3],xsub))
|
431 | 482 |
test(identical(xin[,4:6],xsub))
|
432 | |
##its-lags***********************************************************
|
433 | |
##lagIts-function----------------------------------------------------
|
|
483 |
}
|
|
484 |
|
|
485 |
##its-lags***********************************************************
|
|
486 |
##lagIts-function----------------------------------------------------
|
|
487 |
test.lag.its <- function() {
|
434 | 488 |
foo <- lagIts(x)
|
435 | 489 |
test(all(foo[-1,]==x[-nrow(x),]))
|
436 | 490 |
test(all(foo@dates==x@dates))
|
437 | |
##lagdistIts-function------------------------------------------------
|
|
491 |
}
|
|
492 |
|
|
493 |
##lagdistIts-function------------------------------------------------
|
|
494 |
test.lagdist.its <- function() {
|
438 | 495 |
foo <- lagdistIts(x[,1],1,3)
|
439 | 496 |
test(all.equal(foo[,1],lagIts(x[,1],1)))
|
440 | 497 |
test(all.equal(foo[,2],lagIts(x[,1],2)))
|
441 | 498 |
test(all.equal(foo[,3],lagIts(x[,1],3)))
|
442 | 499 |
test(all.equal(x@dates,foo@dates))
|
443 | |
##its-subset*********************************************************
|
444 | |
##rangeIts-function--------------------------------------------------
|
|
500 |
}
|
|
501 |
|
|
502 |
##its-subset*********************************************************
|
|
503 |
##rangeIts-function--------------------------------------------------
|
|
504 |
test.its.range <- function() {
|
|
505 |
its.format("%Y-%m-%d")
|
445 | 506 |
now <- format.POSIXct(Sys.time(),format=its.format())
|
446 | 507 |
tomorrow <- format.POSIXct(Sys.time()+24*60*60,format=its.format())
|
447 | |
test(identical(appendIts(rangeIts(x,start=now),rangeIts(x,end=now)),x))
|
448 | |
test(nrow(rangeIts(x,start=now,end=now))==0)
|
449 | |
test(nrow(rangeIts(x,start=now,end=tomorrow))==1)
|
|
508 |
test(identical(appendIts(rangeIts(x,start=tomorrow),rangeIts(x,end=now)),x))
|
|
509 |
test(nrow(rangeIts(x,start=now,end=now))==1)
|
|
510 |
test(nrow(rangeIts(x,start=now,end=tomorrow))==2)
|
|
511 |
}
|
|
512 |
|
|
513 |
test.subset.its <- function() {
|
450 | 514 |
##[-method-----------------------------------------------------------
|
451 | 515 |
i1 <- rep(c(TRUE,FALSE),5)
|
452 | 516 |
j1 <- c(TRUE,FALSE,TRUE)
|
|
465 | 529 |
mat2[c(TRUE,FALSE),c(TRUE,FALSE)] <- c(1000,2000)
|
466 | 530 |
x2[c(TRUE,FALSE),c(TRUE,FALSE)] <- c(1000,2000)
|
467 | 531 |
test(all(mat2==x2))
|
468 | |
##its-times**********************************************************
|
469 | |
##daysecondIts-function----------------------------------------------
|
470 | |
##weekdayIts-function------------------------------------------------
|
|
532 |
}
|
|
533 |
|
|
534 |
##its-times**********************************************************
|
|
535 |
##daysecondIts-function----------------------------------------------
|
|
536 |
##weekdayIts-function------------------------------------------------
|
|
537 |
test.times.its <- function() {
|
471 | 538 |
foo <- as.POSIXlt(mytimes)$wday
|
472 | 539 |
test(identical((foo>0 & foo<6),weekdayIts(x)))
|
473 | |
##collapseIts--------------------------------------------------------
|
474 | |
if(as.numeric(R.Version()$major)*100+as.numeric(R.Version()$minor)*10>=1080 & New)
|
475 | |
{
|
476 | |
foo <- newIts(ncol=4,per="w",find="l",extract=T)[1:5,]
|
477 | |
foo[,1] <- c(NA,NA,NA,1,1)
|
478 | |
foo[,2] <- 1
|
479 | |
foo[,4] <- c(NA, 1, 1,1,1)
|
480 | |
dimnames(foo)[[2]] <- c("A","A","B","A")
|
481 | |
test(all.equal(collapseIts(foo),foo[,2:3]))
|
482 | |
foo[5,1] <- 1.01
|
483 | |
bar <- try(collapseIts(foo),silent=TRUE)
|
484 | |
test(bar=="Error in collapseIts(foo) : column data must match in collapse function\n")
|
485 | |
}
|
486 | |
|
487 | |
##its-utilities******************************************************
|
488 | |
######################################################################################################################################################################################
|
489 | |
##fromirtsIts-function------------------------------------------------
|
490 | |
##identical(fromirtsIts(irts(x@dates,x)),x)
|
491 | |
|
492 | |
##locf---------------------------------------------------------------
|
|
540 |
}
|
|
541 |
|
|
542 |
##collapseIts--------------------------------------------------------
|
|
543 |
test.collapse.its <- function() {
|
|
544 |
foo <- newIts(ncol=4,period="week",find="last",extract=T)[1:5,]
|
|
545 |
foo[,1] <- c(NA,NA,NA,1,1)
|
|
546 |
foo[,2] <- 1
|
|
547 |
foo[,4] <- c(NA, 1, 1,1,1)
|
|
548 |
dimnames(foo)[[2]] <- c("A","A","B","A")
|
|
549 |
test(all.equal(collapseIts(foo),foo[,2:3]))
|
|
550 |
foo[5,1] <- 1.01
|
|
551 |
bar <- try(collapseIts(foo),silent=TRUE)
|
|
552 |
test(bar=="Error in collapseIts(foo) : column data must match in collapse function\n")
|
|
553 |
}
|
|
554 |
|
|
555 |
##its-utilities******************************************************
|
|
556 |
######################################################################################################################################################################################
|
|
557 |
##fromirtsIts-function------------------------------------------------
|
|
558 |
##identical(fromirtsIts(irts(x@dates,x)),x)
|
|
559 |
|
|
560 |
##locf---------------------------------------------------------------
|
|
561 |
test.locf.its <- function() {
|
493 | 562 |
foo <- x
|
494 | 563 |
foo[2:4,] <- NA
|
495 | 564 |
test(identical(dates(x),dates(foo)))
|
496 | |
|
497 | |
##-Utility Methods-
|
498 | |
##validity check-----------------------------------------------------
|
499 | |
##dates<--method-----------------------------------------------------
|
500 | |
##[-method-----------------------------------------------------------
|
501 | |
|
502 | |
##-Utility Functions-
|
503 | |
##addDimnames-function-----------------------------------------------
|
504 | |
##gap.its-function---------------------------------------------------
|
505 | |
##overlaps.its-function----------------------------------------------
|
506 | |
##overlapmatches.its-function----------------------------------------
|
507 | |
##namesmatch.its-function--------------------------------------------
|
508 | |
##its.format-function------------------------------------------------
|
509 | |
##simdates.its-function----------------------------------------------
|
510 | |
##***extractDates
|
|
565 |
}
|
|
566 |
|
|
567 |
##-Utility Methods-
|
|
568 |
##validity check-----------------------------------------------------
|
|
569 |
##dates<--method-----------------------------------------------------
|
|
570 |
##[-method-----------------------------------------------------------
|
|
571 |
|
|
572 |
##-Utility Functions-
|
|
573 |
##addDimnames-function-----------------------------------------------
|
|
574 |
##gap.its-function---------------------------------------------------
|
|
575 |
##overlaps.its-function----------------------------------------------
|
|
576 |
##overlapmatches.its-function----------------------------------------
|
|
577 |
##namesmatch.its-function--------------------------------------------
|
|
578 |
##its.format-function------------------------------------------------
|
|
579 |
##simdates.its-function----------------------------------------------
|
|
580 |
##***extractDates
|
|
581 |
test.extract.its <- function() {
|
511 | 582 |
its.format("%Y-%m-%d")
|
|
583 |
years <- 100:105
|
|
584 |
hoursecs <- 60*60
|
|
585 |
regdaysecs <- 24*hoursecs
|
|
586 |
monthdays <- c(28,29,30,31)
|
|
587 |
monthsecs <- c(monthdays*regdaysecs,monthdays*regdaysecs-hoursecs,monthdays*regdaysecs+hoursecs)
|
|
588 |
weeksecs <- 7*regdaysecs ##+hoursecs*c(-1,0,1)
|
|
589 |
daysecs <- regdaysecs+hoursecs*c(-1,0,1)
|
|
590 |
weekDaySelection <- 1:5
|
|
591 |
|
512 | 592 |
TEST <- newIts(start="2003-11-17",end="2005-12-25")
|
513 | 593 |
##-select
|
514 | |
test(all((as.numeric(extractIts(TEST,period="week",select=2)@dates)-as.numeric(extractIts(TEST,period="week",select=1)@dates))%in%daysecs))
|
|
594 |
test(all((as.numeric(extractIts(TEST,period="week",select=2)@dates)-as.numeric(extractIts(TEST,period="week",select=1)@dates))
|
|
595 |
%in% daysecs))
|
515 | 596 |
##-weekday
|
516 | 597 |
test(all(as.POSIXlt(extractIts(TEST,weekday=TRUE)@dates)$wday
|
517 | 598 |
%in%1:5))
|
|
523 | 604 |
test(all(as.POSIXlt(extractIts(TEST,weekday=TRUE,select=weekDaySelection,period="week",find="first")@dates)$wday==1))
|
524 | 605 |
##-find
|
525 | 606 |
test(all(as.POSIXlt(extractIts(TEST,weekday=TRUE,period="week",find="first")@dates[-1])$wday==1))
|
526 | |
TESTX <-
|
527 | |
extractIts(TEST[1:(length(TEST@dates)-2)],weekday=TRUE,period="week",find="last")@dates
|
|
607 |
TESTX <-extractIts(TEST[1:(length(TEST@dates)-2)],weekday=TRUE,period="week",find="last")@dates
|
528 | 608 |
test(all(as.POSIXlt(TESTX)$wday[-length(TESTX)]==5))
|
529 | 609 |
##-period
|
530 | 610 |
test(all(as.POSIXlt(extractIts(TEST,weekday=FALSE,period="year",find="first",partial=FALSE)@dates)$yday==0))
|
|
538 | 618 |
test(all((as.numeric(extractIts(TEST,period="week",select=2)@dates)-as.numeric(extractIts(TEST,period="week",select=1)@dates))%in%daysecs))
|
539 | 619 |
test(all(as.POSIXlt(extractIts(TEST,period="week",select=2)@dates)$wday==2))
|
540 | 620 |
test(all(as.POSIXlt(extractIts(TEST,period="week",select=2,weekday=TRUE)@dates)$wday==2))
|
541 | |
##
|
542 | |
cat(
|
543 | |
paste(sep="",
|
544 | |
"******************************\n* its test suite successful *\n",
|
545 | |
"* ",
|
546 | |
R.version.string,
|
547 | |
"* ",
|
548 | |
"\n",
|
549 | |
"******************************\n")
|
550 | |
)
|
551 | |
}
|
|
621 |
}
|
|
622 |
|
|
623 |
testIts <- function() {
|
|
624 |
test.arith()
|
|
625 |
test.extractor()
|
|
626 |
test.names()
|
|
627 |
test.dates()
|
|
628 |
test.core()
|
|
629 |
test.cumsum()
|
|
630 |
test.diff()
|
|
631 |
test.is.its()
|
|
632 |
test.as.its()
|
|
633 |
test.its.creation()
|
|
634 |
test.new.its()
|
|
635 |
##test.plot.its()
|
|
636 |
test.read.write.its()
|
|
637 |
test.its.summary()
|
|
638 |
test.its.start()
|
|
639 |
test.its.end()
|
|
640 |
test.aligned.its()
|
|
641 |
test.append.its()
|
|
642 |
test.union.its()
|
|
643 |
test.intersect.its()
|
|
644 |
test.lag.its()
|
|
645 |
test.lagdist.its()
|
|
646 |
test.its.range()
|
|
647 |
test.subset.its()
|
|
648 |
test.times.its()
|
|
649 |
test.collapse.its()
|
|
650 |
test.locf.its()
|
|
651 |
test.extract.its()
|
|
652 |
}
|
|
653 |
|
|
654 |
cat(paste("* its test suite successful *\n",R.version.string,"\n"))
|
|
655 |
|
552 | 656 |
##debug(testIts)
|
553 | |
require(its)
|
554 | |
##require(Rblp)
|
555 | |
print(system.time(testIts(New=TRUE,graph=FALSE)))
|
556 | |
print(system.time(testIts(New=TRUE,graph=TRUE)))
|
|
657 |
print(system.time(testIts()))
|