File: baseMethods.R

package info (click to toggle)
futilities 270.73-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 804 kB
  • ctags: 5
  • sloc: makefile: 13
file content (516 lines) | stat: -rw-r--r-- 11,730 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516

# 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 - 2008, Diethelm Wuertz, Rmetrics Foundation, GPL
#   Diethelm Wuertz <wuertz@itp.phys.ethz.ch>
#   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


################################################################################
# FUNCTION:                 BASIC EXTENSIONS:
#  align                     aligns time series objects by approximation
#  align.default             align default method
#  atoms                     Extracts atoms from 'timeSeries' object
#  atoms.default             atoms default method
#  attach                    attach a database to the R path
#  attach.default            attach default method
#  colnames<-                colnames<- has become a generic function
#  colnames<-.default        colnames<- default method
#  cor                       cor has become a generic function
#  cor.default               cor default method
#  cov                       var has become a generic function
#  cov.default               var default method
#  log                       log has become a generic function
#  log.default               log default method
#  outlier                   outlier added generic function
#  outlier.default           outlier default method
#  rownames<-                rownames<- has become a generic function
#  rownames<-.default        rownames<- default method
#  rank                      rank has become a generic function
#  rank.default              rank default method
#  sample                    sample has become a generic function
#  sample.default            sample default method
#  sort                      sort has become a generic function
#  sort.default              sort default method
#  stdev                     stdev added generic function
#  stdev.default             stdev default method
#  termPlot                  termPlot has become a generic function
#  termPlot.default          termPlot default method
#  var                       var has become a generic function
#  var.default               var default method
#  volatility                volatility has become a generic function
#  volatility.default        volatility default method
################################################################################


.conflicts.OK = TRUE


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


align <-
    function(x, y, xout, ...)
{
    # A function implemented by Diethelm Wuertz

    # FUNCTION:

    # Return Value:
    UseMethod("align")
}


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


align.default <-
    function(x, y, xout, method = "linear", n = 50, rule = 1, f = 0,
    ties = mean, ...)
{
    # A function implemented by Diethelm Wuertz

    # FUNCTION:

    # Align by Approximation:
    ans = approx(x = x, y = y, xout = xout, method = method, n = n,
        rule = rule, f = f, ties = ties, ...)

    # Return Value:
    ans
}


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


atoms <-
    function(x, ...)
{
    # A function implemented by Diethelm WUertz

    # FUNCTION:

    # Return Value:
    UseMethod("atoms")
}


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


atoms.default <-
    function(x, ...)
{
    # A function implemented by Diethelm WUertz

    # FUNCTION:

    # Return Value:
    invisible(x)
}


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


attach <-
    function(what, pos = 2, name = deparse(substitute(what)),
    warn.conflicts = TRUE)
{
    # A function implemented by Diethelm Wuertz

    # FUNCTION:

    # Return Value:
    UseMethod("attach")
}


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


attach.default <- base::attach


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


"colnames<-" =
    function(x, value)
{
    # A function implemented by Diethelm Wuertz

    # FUNCTION:

    # Return Value:
    UseMethod("colnames<-")
}


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


`colnames<-.default` <- base::`colnames<-`


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


cor <-
    function(x, y = NULL, use = "all.obs",
    method = c("pearson", "kendall", "spearman"))
{
    # A function implemented by Diethelm Wuertz

    # FUNCTION:

    # Return Value:
    UseMethod("cor")
}


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


cor.default <- stats::cor


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


cov <-
    function(x, y = NULL, use = "all.obs",
    method = c("pearson", "kendall", "spearman"))
{
    # A function implemented by Diethelm Wuertz

    # FUNCTION:

    # Return Value:
    UseMethod("cov")
}


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


cov.default <- stats::cov


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


log <-
    function(x, base = exp(1))
{   # A function implemented by Diethelm Wuertz

    # FUNCTION:

    # Return Value:
    UseMethod("log")
}


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


log.default <-
    function(x, base = exp(1))
{
    # A function implemented by Diethelm Wuertz

    # FUNCTION:

    # Return Value:
    base::log(x, base)
}


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


rank <-
    function(x, na.last = TRUE,
    ties.method = c("average", "first", "random", "max", "min"))
{
    # A function implemented by Diethelm Wuertz

    # FUNCTION:

    # Return Value:
    UseMethod("rank")
}


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


rank.default <-
    function(x, na.last = TRUE,
    ties.method = c("average", "first", "random", "max", "min"))
{
    # A function implemented by Diethelm Wuertz

    # FUNCTION:

    # Return Value:
    base::rank(x, na.last = na.last, ties.method = ties.method)
}


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


sample <-
    function(x, ...)
{   # A function implemented by Diethelm Wuertz

    # FUNCTION:

    # Return Value:
    UseMethod("sample")
}


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


sample.default <-
    function (x, size, replace = FALSE, prob = NULL, ...)
{
    # FUNCTION:

    base::sample(x, size, replace = replace, prob = prob)
}


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


if(getRversion() < "2.4.0") {

    # Note:
    # sort() has been S3 generic in 'base' since 2.4.0
    # Otherwise use something that works here

    sort <- function (x, decreasing = FALSE, ...)
    {
        if (!is.logical(decreasing) || length(decreasing) != 1)
            stop("'decreasing' must be a length-1 logical vector.\nDid you intend to set 'partial'?")
        UseMethod("sort")
    }

    sort.default <- function(x, decreasing = FALSE, ...) {
        if (is.object(x))
        x[order(x, decreasing = decreasing)]
        else base::sort(x, decreasing = decreasing, ...)
    }

}# endif {only for outdated R}


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


outlier <-
    function(x, sd = 5, complement = TRUE, ...)
{
    # A function implemented by Diethelm Wuertz

    # FUNCTION:

    UseMethod("outlier")
}


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


outlier.default <-
    function(x, sd = 5, complement = TRUE, ...)
{
    # A function implemented by Diethelm Wuertz

    # Description:
    #   Returns outliers

    # Arguments:
    #   x - a numeric vector
    #   sd - a numeric value of standard deviations, e.g. 5
    #       means that values larger or smaller tahn five
    #       times the standard deviation of the series will
    #       be detected.
    #   complement - a logical flag, should the outlier series
    #       or its complements be returned.

    # Note:
    #   This function is thought to find splits in financial
    #   price or index data. If a price or index is splitted we
    #   observe in the returns a big jump of several standard
    #   deviations.

    # FUNCTION:

    # Find Outliers:
    SD = sd * sd(x)
    if (complement) {
        ans  = x[x <= SD]
    } else {
        ans = x[x > SD]
        names(ans) = as.character(which(x > SD))
    }

    # Return Value:
    ans
}


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


"rownames<-" =
    function(x, value)
{   # A function implemented by Diethelm Wuertz

    # FUNCTION:

    # Return Value:
    UseMethod("rownames<-")
}


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


`rownames<-.default` <- base::`rownames<-`


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


stdev.default <-
    function(x, na.rm = FALSE)
{
    # A function implemented by Diethelm Wuertz

    # FUNCTION:

    # Return Value:
    stats::sd(x = x, na.rm = na.rm)
}


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


stdev <- stats::sd


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


termPlot <-
    function(model, ...)
{
    # A function implemented by Diethelm Wuertz

    # FUNCTION:

    # Return Value:
    UseMethod("termPlot")
}


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


termPlot.default <-
function(model, ...)
{
    # A function implemented by Diethelm Wuertz

    # FUNCTION:

    # Return Value:
    stats::termplot(model, ...)
}


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


var <-
    function(x, y = NULL, na.rm = FALSE, use)
{
    # A function implemented by Diethelm Wuertz

    # FUNCTION:

    # Return Value:
    UseMethod("var")
}


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


var.default <- stats::var


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


volatility <-
    function(object, ...)
{
    # A function implemented by Diethelm Wuertz

    # FUNCTION:

    # Return Value:
    UseMethod("volatility")
}


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


volatility.default <-
    function(object, ...)
{
    # A function implemented by Diethelm Wuertz

    # FUNCTION:

    # Compute volatility:
    x = object
    ans = (x - mean(x))^2

    # Return Value:
    ans
}


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