File: runit4D.R

package info (click to toggle)
fcalendar 240.10068-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,736 kB
  • sloc: sh: 22; makefile: 1
file content (341 lines) | stat: -rw-r--r-- 9,553 bytes parent folder | download
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General 
# Public License along with this library; if not, write to the 
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
# MA  02111-1307  USA

# Copyrights (C)
# for this R-port: 
#   1999 - 2004, Diethelm Wuertz, GPL
#   Diethelm Wuertz <wuertz@itp.phys.ethz.ch>
#   info@rmetrics.org
#   www.rmetrics.org
# for the code accessed (or partly included) from other R-ports:
#   see R's copyright and license files
# for the code accessed (or partly included) from contributed R-ports
# and other sources
#   see Rmetrics's copyright file


################################################################################
# METHODS:             CREATE A TIMESERIES FROM OTHER OBJECTS:
#  is.timeSeries        S3: Tests for a 'timeSeries' object
#  as.timeSeries        S3: Defines method for a 'timeSeries' object
#  as.timeS*.default    S3: Returns the input
#  as.timeS*.numeric    S3: Transforms a numeric vector into a 'timeSeries'
#  as.timeS*.data.frame S3: Transformas a 'data.frame' into a 'timeSeries'
#  as.timeS*.matrix     S3: Transformas a 'matrix' into a 'timeSeries'
#  as.timeS*.ts         S3: Transforms a 'ts' object into a 'timeSeries'
#  as.timeS*.character  S3: Loads and transformas from a demo file
#  as.timeS*.zoo        S3: Transforms a 'zoo' object into a 'timeSeries'
# METHODS:             TRANSFORM A TIMESERIES INTO OTHER OBJECTS:
#  as.vector.timeS*     S3: Converts a univariate 'timeSeries' to a vector
#  as.matrix.timeS*     S3: Converts a 'timeSeries' to a 'matrix'
#  as.data.frame.t*     S3: Converts a 'timeSeries' to a 'data.frame'
#  as.ts.timeSeries     S3: Converts a 'timeSeries' to a 'ts'     
# NEW METHODS 
#  .as.vector.zoo
#  .as.matrix.zoo
#  .quantile.zoo
#  .t.timeSeries
#  .mergeSeries
################################################################################


test.helpFile = 
function()
{
    # Help File:
    helpFile = function() { 
        example(timeSeriesCoercion); return() }
    checkIdentical(
        target = class(try(helpFile())),
        current = "NULL")

    # Return Value:
    return()    
}


# ------------------------------------------------------------------------------


test.asTimeSeries = 
function()
{
    # as.timeSeries.default     Returns the input
    # as.timeSeries.numeric     Transforms a numeric vector into a 'timeSeries'
    # as.timeSeries.data.frame  Transformas a 'data.frame' into a 'timeSeries'
    # as.timeSeries.matrix      Trasformas a 'matrix' into a 'timeSeries'
    # as.timeSeries.ts          Tranf orms a 'ts' object into a 'timeSeries'
    # as.timeSeries.character   Loads and transformas from a demo file
    # as.timeSeries.zoo         Transforms a 'zoo' object into a 'timeSeries'
    

    set.seed(4711)
    data = round(rnorm(12), 3)
    charvec = timeCalendar(2006)
    uTS = timeSeries(data, charvec, units = "uTS")
    uTS
    checkTrue(inherits(uTS, "timeSeries"))
    checkTrue(is.timeSeries(uTS))

    positions = timeCalendar()
    class(positions)
    .whichFormat(format(positions))
    .whichFormat(as.character(positions))

    # Data Input is a Vector:
    x = rnorm(12)
    positions = timeCalendar()
    tS = as.timeSeries(x, positions)
    print(tS)
    tS = as.timeSeries(x, format(positions))
    print(tS)
    tS = as.timeSeries(x, as.character(positions))
    print(tS)
    tS = timeSeries(x, positions)
    print(tS)
    tS = timeSeries(x, format(positions))
    print(tS)
    tS = timeSeries(x, as.character(positions))
    print(tS)
    
    # Data Input is a Data Frame:
    x = data.frame(rnorm(12))
    tS = as.timeSeries(x, positions)
    print(tS)
    tS = as.timeSeries(x, format(positions))
    print(tS)
    tS = as.timeSeries(x, as.character(positions))
    print(tS)
    tS = timeSeries(x, positions)
    print(tS)
    tS = timeSeries(x, format(positions))
    print(tS)
    tS = timeSeries(x, as.character(positions))
    print(tS)
    
    # Data Input is a Matrix:
    x = matrix(rnorm(12))
    tS = as.timeSeries(x, positions)
    print(tS)
    tS = as.timeSeries(x, format(positions))
    print(tS)
    tS = as.timeSeries(x, as.character(positions))
    print(tS)
    tS = timeSeries(x, positions)
    print(tS)
    tS = timeSeries(x, format(positions))
    print(tS)
    tS = timeSeries(x, as.character(positions))
    print(tS)
     
    # Data Input is an Univariate timeSeries: 
    x = as.ts(rnorm(12))
    tS = as.timeSeries(x, positions)
    print(tS)
    tS = as.timeSeries(x, format(positions))
    print(tS)
    tS = as.timeSeries(x, as.character(positions))
    print(tS)
    tS = timeSeries(x, positions)
    print(tS)
    tS = timeSeries(x, format(positions))
    print(tS)
    tS = timeSeries(x, as.character(positions))
    print(tS)
    
    # Return Value:
    return()    
}


# ------------------------------------------------------------------------------


test.asTimeSeriesDJ1 = 
function()
{   
    # Load Data:
    # require(rmetrics)
    # data(DowJones30) 
    # use instead dummy data set just for testing ...
    Data = matrix(exp(cumsum(rnorm(30*100, sd = 0.1))), ncol = 30) 
    Positions = format(timeSequence("2006-01-01", length.out = 100))
    DowJones30 = data.frame(Positions, Data)
    
    # Taking Dates from First Column:
    DJ = DowJones30[21:30, c(1, 11:15)]
    DJ
    class(DJ) 
    as.timeSeries(DJ)
   
    # Adding Dates through Rownames Assignment:
    DJ = DowJones30[21:30, c(11:15)]
    rownames(DJ)<-DowJones30[21:30, 1]
    DJ
    as.timeSeries(DJ)
   
    # Missing Dates - Using Dummy Dates:
    DJ = DowJones30[21:30, c(11:15)]
    DJ
    class(DJ)
    as.timeSeries(DJ)
   
    # With recordIDs:  
    if (FALSE) {
        DJ = DowJones30[21:30, c(1,11:15)]
        DJ = cbind(DJ, LETTERS[1:10])
        class(DJ)
        tsDJ = as.timeSeries(DJ)
        tsDJ
        tsDJ@recordIDs
    }
   
    DJ = DowJones30[21:30, c(11:15)]
    rownames(DJ) = DowJones30[21:30, 1]
    DJ = cbind(DJ, LETTERS[1:10])
    tsDJ = as.timeSeries(DJ)
    tsDJ
    tsDJ@recordIDs
   
    DJ = DowJones30[21:30, c(11:15)]
    DJ =cbind(DJ, LETTERS[1:10])
    tsDJ = as.timeSeries(DJ)
    tsDJ
    tsDJ@recordIDs

    # Return Value:
    return()    
}


# ------------------------------------------------------------------------------


test.fromTimeSeriesUV = 
function()
{     
    # as.vector.timeSeries      Converts a univariate 'timeSeries' to a vector
    # as.matrix.timeSeries      Converts a 'timeSeries' to a 'matrix'
    # as.data.frame.timeSeries  Converts a 'timeSeries' to a 'data.frame'
    # as.ts.timeSeries          Converts a 'timeSeries' to a 'ts'
    
   
    # Univariate Case:
    set.seed(4711)
    data = round(rnorm(12), 3)
    charvec = timeCalendar(2006)
    uTS = timeSeries(data, charvec, units = "uTS")
    uTS
    
    # Vector:
    VEC = as.vector(uTS) 
    head(VEC)
    class(VEC)
    checkIdentical(class(VEC), "numeric")
    
    # Numeric:
    VEC = as.numeric(uTS) 
    head(VEC)
    class(VEC)
    checkIdentical(class(VEC), "numeric")
    
    # Matrix:
    MAT = as.matrix(uTS)  
    head(MAT)
    class(MAT)
    checkIdentical(class(MAT), "matrix")
    checkIdentical(target = MAT[,1], current = VEC)
    
    # Data Frame:
    DF = as.data.frame(uTS)  
    head(DF)
    checkIdentical(class(DF), "data.frame")
    checkIdentical(target = as.matrix(DF)[,1], current = VEC)
    
    # Time Series:
    TS = as.ts(uTS)
    head(TS)
    class(TS)
    checkIdentical(class(TS), "ts")
    
    # Return Value:
    return()    
}


# ------------------------------------------------------------------------------


test.fromTimeSeriesMV = 
function()
{     
    # as.vector.timeS*     Converts a univariate 'timeSeries' to a vector
    # as.matrix.timeS*     Converts a 'timeSeries' to a 'matrix'
    # as.data.frame.t*     Converts a 'timeSeries' to a 'data.frame'
    # as.ts.timeSeries     Converts a 'timeSeries' to a 'ts'
    
   
    # Multivariate Case:
    set.seed(4711)
    data = matrix(round(rnorm(24), 3), ncol = 2)
    charvec = timeCalendar(2006)
    mTS = timeSeries(data, charvec)
    mTS
     
    # Matrix:
    MAT = as.matrix(mTS)  
    head(MAT)
    class(MAT)
    checkIdentical(
        target = class(MAT), 
        current = "matrix")
    checkIdentical(
        target = as.vector(MAT[, 1]), 
        current = as.numeric(MAT)[1:12])
    
    # Data Frame:
    DF = as.data.frame(mTS)  
    head(DF)
    class(DF)
    checkIdentical(
        target = class(DF), 
        current = "data.frame")
    
    # Time Series:
    TS = as.ts(mTS)
    head(TS)
    class(TS)
    checkIdentical(
        target = class(TS), 
        current = c("mts", "ts"))
    
    # Return Value:
    return()    
}


# ------------------------------------------------------------------------------


if (FALSE) {
    require(RUnit)
    testResult <- runTestFile("C:/Rmetrics/SVN/trunk/fCalendar/test/runit4D.R")
    printTextProtocol(testResult)
}


################################################################################