Skip to content

Commit beadf14

Browse files
committed
fracdiff.var(): did "FIXME"; testing "search" h; update tests
1 parent db0119f commit beadf14

14 files changed

+315
-253
lines changed

.Rbuildignore

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ Archive
22
Calling
33
DESCRIPTION_
44
Done
5+
M1mac
6+
00.*winb
57
.*\.zip
68
.*\.rar
79
.*\.tgz
@@ -11,10 +13,11 @@ qed.*\.pdf
1113
codes_for_.*
1214
^filters\.R
1315
simul-compare.*
14-
tests/.*linux
1516
tests/.*Rout-[36][24]b
16-
tests/windows.*
17+
tests/.*Rout.*_
1718
tests/ex-Vinod.*Rout
19+
tests/.*linux
20+
tests/windows.*
1821
src/.*\.c\+
1922
src/00-.*
2023
src/ftn-struc

ChangeLog

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2024-03-13 Martin Maechler <[email protected]>
2+
3+
* DESCRIPTION (Version): 1.5-4:
4+
* R/fracdiff.var: finally fix 'FIXME': se.ok
5+
* tests/sim*: updates, searching for 'h' in fracdiff.var(.., h = *)
6+
17
2022-10-18 Martin Maechler <[email protected]>
28

39
* DESCRIPTION (Version): 1.5-2:

DESCRIPTION

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: fracdiff
2-
Version: 1.5-3
3-
VersionNote: Released 1.5-0 on 2019-12-09, 1.5-1 on 2020-01-20, 1.5-2 on 2022-10-31
4-
Date: 2024-02-01
2+
Version: 1.5-4
3+
VersionNote: Released 1.5-0 on 2019-12-09, 1.5-1 on 2020-01-20, 1.5-2 on 2022-10-31, 1.5-3 on 2024-02-01
4+
Date: 2024-03-13
55
Title: Fractionally Differenced ARIMA aka ARFIMA(P,d,q) Models
66
Authors@R: c(person("Martin","Maechler", role=c("aut","cre"), email="[email protected]",
77
comment = c(ORCID = "0000-0002-8685-9910"))

R/fd-methods.R

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ logLik.fracdiff <- function(object, ...)
1919
class(r) <- "logLik"
2020
r
2121
}
22+
## ==> AIC(), BIC() do work; the latter as nobs(logLik(.)) works
2223

2324
print.fracdiff <- function(x, digits = getOption("digits"), ...)
2425
{

R/fracdiff.R

+24-15
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
### Patched by Friedrich.Leisch, for use with R, 22.1.1997; then
2+
###
3+
### Copyright 2003--2024 Martin Maechler; fixed, changed enhanced ..
14

25
### Original file:
3-
### copyright 1991 Department of Statistics, Univeristy of Washington
4-
5-
### Patched by Friedrich.Leisch, for use with R, 22.1.1997
6-
### fixed & changed by Martin Maechler, since Dec 2003
6+
### copyright 1991 Department of Statistics, University of Washington
77

88
if(getRversion() < "2.15")
99
paste0 <- function(...) paste(..., sep="")
1010

11-
.fdcov <- function(x, d, h, nar, nma, hess, fdf.work)
11+
.fdcov <- function(x, d, h, # <- missing by default
12+
nar, nma, hess, fdf.work)
1213
{
1314
npq <- as.integer(nar + nma)
1415
npq1 <- npq + 1L # integer, too
@@ -150,10 +151,9 @@ fracdiff <- function(x, nar = 0, nma = 0,
150151
## Cov == (-H)^{-1} == solve(-H)
151152

152153
## Note that the following can be "redone" using fracdiff.var() :
153-
154-
fdc <- .fdcov(x, fdf$d, h,
154+
fdc <- .fdcov(x, fdf$d, h, # <- missing by default
155155
nar=nar, nma=nma, hess=hess, fdf.work = fdf$w)
156-
156+
##==> "vcov" = fdc $ covariance.dpq
157157
dimnames(hess) <- dimnames(fdc$covariance.dpq)
158158
hess[1, ] <- fdc$hd
159159
hess[row(hess) > col(hess)] <- hess[row(hess) < col(hess)]
@@ -172,7 +172,7 @@ fracdiff <- function(x, nar = 0, nma = 0,
172172
n = n,
173173
msg = c(fracdf = fd.msg, fdcov = fdc$msg),
174174
d = fdf$d, ar = fdf$ar, ma = fdf$ma,
175-
covariance.dpq = fdc$covariance.dpq,
175+
covariance.dpq = fdc$covariance.dpq, # == vcov
176176
fnormMin = hstat[2], sigma = sqrt(var.WN),
177177
stderror.dpq = if(fdc$se.ok) fdc$stderror.dpq, # else NULL
178178
correlation.dpq= if(fdc$se.ok) fdc$correlation.dpq,
@@ -226,8 +226,7 @@ fracdiff.var <- function(x, fracdiff.out, h)
226226
fracdiff.out$ar,
227227
rep(0, lwork))),
228228
info = integer(1))
229-
## FIXME: should be *automatically* same messages as inside fracdiff() above!
230-
fracdiff.out$msg <-
229+
msg <-
231230
if(fdc$info) {
232231
msg <-
233232
switch(fdc$info,
@@ -237,10 +236,14 @@ fracdiff.var <- function(x, fracdiff.out, h)
237236
stop("error in gamma function"))
238237
warning(msg)
239238
} else "ok"
240-
se.ok <- fdc$info != 0 || fdc$info < 3 ## << FIXME -- illogical!!
241-
nam <- "d"
242-
if(p) nam <- c(nam, paste0("ar", 1:p))
243-
if(q) nam <- c(nam, paste0("ma", 1:q))
239+
se.ok <- fdc$info %in% 0:2
240+
241+
if("fdcov" %in% names(fracdiff.out$msg)) # fracdiff(): msg = c(fracdf = fd.msg, fdcov = fdc$msg)
242+
fracdiff.out$msg[["fdcov"]] <- msg
243+
else fracdiff.out$msg <- msg
244+
nam <- c("d",
245+
if(p) paste0("ar", 1:p),
246+
if(q) paste0("ma", 1:q))
244247
fracdiff.out$h <- fdc$h
245248
fracdiff.out$covariance.dpq <- array(fdc$cov, c(npq1,npq1), list(nam,nam))
246249
fracdiff.out$stderror.dpq <- if(se.ok) fdc$se # else NULL
@@ -298,3 +301,9 @@ fracdiff.sim <- function(n, ar = NULL, ma = NULL, d, rand.gen = rnorm,
298301
.Machine$double.eps)[["s"]][ii]
299302
list(series = y, ar = ar, ma = ma, d = d, mu = mu, n.start = n.start)
300303
}
304+
305+
## Not exported; used for faster checking, e.g., on CRAN
306+
doExtras <- function() {
307+
interactive() || nzchar(Sys.getenv("R_fracdiff_check_extra")) ||
308+
identical("true", unname(Sys.getenv("R_PKG_CHECKING_doExtras")))
309+
}

man/fd-methods.Rd

-7
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@
3737
\item{signif.stars}{logical. If \code{TRUE}, \dQuote{significance stars}
3838
are printed for each coefficient.}
3939
}
40-
% \value{
41-
% ~Describe the value returned
42-
% If it is a LIST, use
43-
% \item{comp1 }{Description of 'comp1'}
44-
% \item{comp2 }{Description of 'comp2'}
45-
% ...
46-
% }
4740
\author{Martin Maechler; Rob Hyndman contributed the
4841
\code{\link{residuals}()} and \code{\link{fitted}()} methods.}
4942
\seealso{\code{\link{fracdiff}} to get \code{"fracdiff"} objects,

man/fracdiff.var.Rd

+9-6
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ fracdiff.var(x, fracdiff.out, h)
88
\item{x}{a univariate time series or a vector. Missing values (NAs)
99
are not allowed.}
1010
\item{fracdiff.out}{output from \code{fracdiff} for time series \code{x}.}
11-
\item{h}{finite-difference interval for approximating partial
12-
derivatives with respect to the \code{d} parameter.}
11+
\item{h}{finite-difference interval length (\eqn{ > 0}) for approximating partial
12+
derivatives with respect to the \code{d} parameter. Typically smaller
13+
than the one in \code{fracdiff.out}}
1314
}
1415
\description{
1516
Allows the finite-difference interval to be altered for recomputation of the
@@ -27,19 +28,21 @@ fracdiff.var(x, fracdiff.out, h)
2728
}
2829
\examples{
2930
## Generate a fractionally-differenced ARIMA(1,d,1) model :
30-
ts.test <- fracdiff.sim(10000, ar = .2, ma = .4, d = .3)
31+
set.seed(5) # reproducibility; x86_64 Lnx: get warning
32+
tst <- fracdiff.sim(500, ar = .2, ma = .4, d = .3)$series
3133
## estimate the parameters in an ARIMA(1,d,1) model for the simulated series
32-
fd.out <- fracdiff(ts.test$ser, nar= 1, nma = 1)
34+
fd.out <- fracdiff(tst, nar= 1, nma = 1) # warning ... maybe change 'h'
35+
summary(fd.out)## *** Warning ... {has been stored} --> h = 7.512e-6
3336

3437
## Modify the covariance estimate by changing the finite-difference interval
35-
(fd.o2 <- fracdiff.var(ts.test$series, fd.out, h = .0001))
38+
(fd.o2 <- fracdiff.var(tst, fd.out, h = 1e-3))
3639
## looks identical as print(fd.out),
3740
## however these (e.g.) differ :
3841
vcov(fd.out)
3942
vcov(fd.o2)
4043

4144
## A case, were the default variance is *clearly* way too small:
42-
set.seed(1); fdc <- fracdiff(X <- fracdiff.sim(n=100,d=0.25)$series)
45+
set.seed(1); fdc <- fracdiff(X <- fracdiff.sim(n=100, d=0.25)$series)
4346
fdc
4447
# Confidence intervals just based on asymp.normal approx. and std.errors:
4548
confint(fdc) # ridiculously too narrow

src/fdhess.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ hesdpq(double *x, double d_, double *hh, double *hd, double *w)
386386
--w;
387387

388388
/* Function Body */
389-
if (*hh <= 0.) {
389+
if (*hh <= 0.) { // <==> missing(h) [ = default ] in R's .fdcov()
390390
*hh = (fabs(filtfd_.cllf) + 1.) * mauxfd_.epspt5;
391391
}
392392
if(*hh > 0.1) *hh = 0.1;

tests/ex.R

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
library(fracdiff)
22

3-
doExtras <- interactive() # for now
4-
3+
doExtras <- fracdiff:::doExtras()
54
.proctime00 <- proc.time()
65

76
set.seed(107)
@@ -10,10 +9,11 @@ options(digits = 5)
109
## 1)
1110

1211
x1 <- fracdiff.sim(5000, ar = .2, ma = -.4, d = .3, n.start=0, allow.0 = TRUE)
13-
(fd1 <- fracdiff(x1$series, nar = 1, nma = 1, dtol = 1e-10))
12+
summary(fd1 <- fracdiff(x1$series, nar = 1, nma = 1, dtol = 1e-10))
1413
vcov(fd1)
1514
logLik(fd1)
16-
15+
stopifnot(all.equal(structure(-7051.5027, df = 4L, nall = 5000L, nobs = 5000L, class = "logLik"),
16+
logLik(fd1)))
1717
fdCOVcomp <-
1818
c("h", "covariance.dpq", "stderror.dpq", "correlation.dpq", "hessian.dpq")
1919
fd1. <- fracdiff.var(x1$series, fd1, h = fd1$h / 2)
@@ -52,7 +52,9 @@ fd1uL <- list(
5252
-5875, -5420, 4529,
5353
4258, 4529, -5834),
5454
3L, 3L, dimnames = dns))
55-
stopifnot( all.equal(fd1u[fdCOVcomp], fd1uL, tolerance = 2e-4) )
55+
if(doExtras)
56+
print(all.equal(fd1u[fdCOVcomp], fd1uL, tolerance = 0))
57+
stopifnot(all.equal(fd1u[fdCOVcomp], fd1uL, tolerance = 2e-4) )
5658

5759
## 2)
5860

@@ -107,23 +109,25 @@ sfd2S <- ## dput(sapply(fd2.[fdCOVcomp], signif, digits = 5))
107109
1742.3, 1507.3,-5007.4), 3, dimnames=dns))
108110
##
109111
if(doExtras)
110-
print(all.equal(sfd2S, sfd2., tol = 1e-6, countEQ=TRUE)) # 8.7655e-5
112+
print(all.equal(sfd2S, sfd2., tol = 0 , countEQ=TRUE)) # 8.7655e-5
111113
stopifnot(all.equal(sfd2S, sfd2., tol = 2e-4, countEQ=TRUE))
112114

113115
fd2u <- fracdiff.var(x2$series, fd2, h = fd2$h * 8)#-> warning, unable .. corr...
114-
sd2u <- sapply(fd2u[fdCOVcomp], signif, digits = 4)
116+
##= no se.ok -->
117+
fdCOV.0 <- setdiff(fdCOVcomp, c("stderror.dpq", "correlation.dpq"))
118+
sd2u <- sapply(fd2u[fdCOV.0], signif, digits = 4)
115119
sd2uS <- list( ## dput(sapply(sd2u[fdCOVcomp], signif, digits = 5))
116120
h = 0.0002466,
117121
covariance.dpq = matrix(c(-0.0003545, 6e-04, 5.724e-05,
118122
6e-04, -0.0005003, 5.816e-05,
119123
5.724e-05, 5.816e-05, 0.0002371), 3, dimnames=dns),
120-
stderror.dpq = c(0, 0, 0.0154),
121-
correlation.dpq = matrix(0, 3,3),
124+
## stderror.dpq = c(0, 0, 0.0154),
125+
## correlation.dpq = matrix(0, 3,3),
122126
hessian.dpq = matrix(c(-3347, -3811, 1742,
123127
-3811, -2396, 1507,
124128
1742, 1507,-5007), 3, dimnames=dns))
125129
##
126130
if(doExtras)
127-
print(all.equal(sd2uS, sd2u, tol = 1e-8, countEQ=TRUE))# 0.000103 (32b Win); T.(64b F30)
131+
print(all.equal(sd2uS, sd2u, tol = 0 , countEQ=TRUE))# 0.000103 (32b Win); T.(64b F30)
128132
stopifnot(all.equal(sd2uS, sd2u, tol = 4e-4, countEQ=TRUE))
129133

0 commit comments

Comments
 (0)