Skip to content

Commit 165a7c3

Browse files
committed
Hyndman's residuals() and fitted(); using Authors@R ; more examples
1 parent c65ce0b commit 165a7c3

8 files changed

+70
-27
lines changed

.Rbuildignore

+2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ Done
66
.*\.tgz
77
.*\.mail
88
.*-manual
9+
qed.*\.pdf
910
codes_for_.*
11+
^filters\.R
1012
simul-compare.*
1113
tests/.*linux
1214
tests/windows.*

ChangeLog

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
2019-12-09 Martin Maechler <[email protected]>
2+
3+
* DESCRIPTION (Authors@R): using new format; ready for CRAN
4+
5+
* R/fd-methods.R (summary.fracdiff): renamed '$ coefficients' such
6+
that 'coef()' works.
7+
8+
2019-12-09 Rob Hyndman <[email protected]>
9+
10+
* R/fracdiff.R, fd-methods.R (fracdiff): provide 'residuals()' and
11+
'fitted'()' methods.
12+
113
2018-09-10 Martin Maechler <[email protected]>
214

315
* R/diffseries.R (diffseries): Finally found bug in the fft() based

DESCRIPTION

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
Package: fracdiff
22
Version: 1.5-0
33
VersionNote: Released 1.4-2 on 2012-12-01 on CRAN
4-
Date: 2018-09-10
4+
Date: 2019-12-09
55
Title: Fractionally Differenced ARIMA aka ARFIMA(P,d,q) Models
6-
Author: S original by Chris Fraley, U.Washington, Seattle.
7-
R port by Fritz Leisch at TU Wien; since 2003-12: Martin Maechler;
8-
fdGPH(), fdSperio(), etc by Valderio Reisen and Artur Lemonte.
9-
Maintainer: Martin Maechler <[email protected]>
6+
Authors@R: c(person("Martin","Maechler", role=c("aut","cre"), email="[email protected]",
7+
comment = c(ORCID = "0000-0002-8685-9910"))
8+
, person("Chris", "Fraley", role=c("ctb","cph"), comment = "S original; Fortran code")
9+
, person("Friedrich", "Leisch", role = "ctb",
10+
comment = c("R port", ORCID = "0000-0001-7278-1983"))
11+
, person("Valderio", "Reisen", role="ctb", comment = "fdGPH() & fdSperio()")
12+
, person("Artur", "Lemonte", role="ctb", comment = "fdGPH() & fdSperio()")
13+
, person("Rob", "Hyndman", email="[email protected]", role="ctb",
14+
comment = c("residuals() & fitted()", ORCID = "0000-0002-2140-5352"))
15+
)
1016
Description: Maximum likelihood estimation of the parameters of a fractionally
11-
differenced ARIMA(p,d,q) model (Haslett and Raftery, Appl.Statistics, 1989).
17+
differenced ARIMA(p,d,q) model (Haslett and Raftery, Appl.Statistics, 1989);
18+
including inference and basic methods. Some alternative algorithms to estimate "H".
1219
Imports: stats
13-
Suggests: longmemo, urca
20+
Suggests: longmemo, forecast, urca
1421
License: GPL (>= 2)
22+
URL: https://github.com/mmaechler/fracdiff
23+
BugReports: https://github.com/mmaechler/fracdiff/issues
1524
Encoding: UTF-8
25+
NeedsCompilation: yes

NAMESPACE

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
useDynLib(fracdiff, .registration=TRUE)
22

33
importFrom("stats",
4-
AIC, as.ts
4+
AIC, arima, as.ts
55
, fft, nextn
66
, lm.fit
77
, coef, logLik, resid
88
, na.fail
99
, printCoefmat
1010
, pnorm, qnorm, rnorm
11-
, symnum)
11+
, symnum
12+
, tsp, "tsp<-"
13+
)
1214

1315
export("fracdiff", "fracdiff.sim", "fracdiff.var",
1416
"diffseries",

R/fd-methods.R

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ coef.fracdiff <- function(object, ...) unlist(object[c("d", "ar", "ma")])
55

66
vcov.fracdiff <- function(object, ...) object$covariance.dpq
77

8-
residuals.fracdiff <- function(object, ...) .NotYetImplemented()
9-
fitted.fracdiff <- function(object, ...) .NotYetImplemented()
8+
## Lines added by RJH. 9 Dec 2019
9+
residuals.fracdiff <- function(object, ...) object$residuals
10+
11+
fitted.fracdiff <- function(object, ...) object$fitted
12+
## End of RJH addition
1013

1114
logLik.fracdiff <- function(object, ...)
1215
{
@@ -40,7 +43,7 @@ summary.fracdiff <- function(object, symbolic.cor = FALSE, ...)
4043
cf <- cbind("Estimate" = cf,
4144
"Std. Error"= se, "z value" = cf / se,
4245
"Pr(>|z|)" = 2 * pnorm(-abs(cf / se)))
43-
object$coef <- cf
46+
object$coefficients <- cf # 'long name' such that coef(.) works
4447
logl <- logLik(object)
4548
object$df <- attr(logl, "df")
4649
object$aic <- AIC(logl)

R/fracdiff.R

+13-2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ fracdiff <- function(x, nar = 0, nma = 0,
9494
if(is.null(dtol))
9595
dtol <- .Machine$double.eps^0.25 # ~ 1.22e-4
9696
## if dtol < 0: the fortran code will choose defaults
97+
tspx <- tsp(x) # Added by RJH. 9 Dec 2019
9798
x <- as.double(x)
9899

99100
## this also initializes "common blocks" that are used in .C(.) calls :
@@ -159,16 +160,26 @@ fracdiff <- function(x, nar = 0, nma = 0,
159160

160161
hstat <- fdf[["hood.etc"]]
161162
var.WN <- hstat[3]
163+
164+
## Following lines added by RJH. 9 Dec 2019
165+
diffx <- diffseries(x, d = fdf$d)
166+
armafit <- arima(diffx, order = c(length(fdf$ar), 0L, length(fdf$ma)),
167+
include.mean = FALSE, fixed = c(fdf$ar, -fdf$ma))
168+
res <- armafit$residuals
169+
tsp(res) <- tspx
170+
162171
structure(list(log.likelihood = hstat[1],
163-
n = n,
172+
n = n,
164173
msg = c(fracdf = fd.msg, fdcov = fdc$msg),
165174
d = fdf$d, ar = fdf$ar, ma = fdf$ma,
166175
covariance.dpq = fdc$covariance.dpq,
167176
fnormMin = hstat[2], sigma = sqrt(var.WN),
168177
stderror.dpq = if(fdc$se.ok) fdc$stderror.dpq, # else NULL
169178
correlation.dpq= if(fdc$se.ok) fdc$correlation.dpq,
170179
h = fdc$h, d.tol = fdf$dtol, M = M, hessian.dpq = hess,
171-
length.w = lenw, call = cl),
180+
length.w = lenw,
181+
residuals = res, fitted = x - res, ## by RJH
182+
call = cl),
172183
class = "fracdiff")
173184
}
174185

man/fd-methods.Rd

+7-6
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
\method{print}{summary.fracdiff}(x, digits = max(3, getOption("digits") - 3),
2323
correlation = FALSE, symbolic.cor = x$symbolic.cor,
2424
signif.stars = getOption("show.signif.stars"), \dots)
25-
% \method{fitted}{fracdiff}(object, \dots)
26-
% \method{residuals}{fracdiff}(object, \dots)
25+
\method{fitted}{fracdiff}(object, \dots)
26+
\method{residuals}{fracdiff}(object, \dots)
2727
\method{vcov}{fracdiff}(object, \dots)
2828
}
2929
\arguments{
@@ -37,17 +37,15 @@
3737
\item{signif.stars}{logical. If \code{TRUE}, \dQuote{significance stars}
3838
are printed for each coefficient.}
3939
}
40-
% \details{
41-
% ~~ If necessary, more details than the description above ~~
42-
% }
4340
% \value{
4441
% ~Describe the value returned
4542
% If it is a LIST, use
4643
% \item{comp1 }{Description of 'comp1'}
4744
% \item{comp2 }{Description of 'comp2'}
4845
% ...
4946
% }
50-
\author{Martin Maechler}
47+
\author{Martin Maechler; Rob Hyndman contributed the
48+
\code{\link{residuals}()} and \code{\link{fitted}()} methods.}
5149
\seealso{\code{\link{fracdiff}} to get \code{"fracdiff"} objects,
5250
\code{\link{confint.fracdiff}} for the \code{\link{confint}} method;
5351
further, \code{\link{fracdiff.var}}.
@@ -63,6 +61,9 @@ smFD <- summary(modFD)
6361
smFD
6462
coef(smFD) # gives the whole table
6563
AIC(modFD) # AIC works because of the logLik() method
64+
stopifnot(exprs = {
65+
66+
})
6667
}
6768
\keyword{print}
6869
\keyword{models}

tests/ex.Rout-64b

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11

2-
R version 2.13.1 Patched (2011-08-08 r56671)
3-
Copyright (C) 2011 The R Foundation for Statistical Computing
4-
ISBN 3-900051-07-0
5-
Platform: x86_64-unknown-linux-gnu (64-bit)
2+
R version 3.6.2 RC (2019-12-06 r77534) -- "Dark and Stormy Night"
3+
Copyright (C) 2019 The R Foundation for Statistical Computing
4+
Platform: x86_64-pc-linux-gnu (64-bit)
65

76
R is free software and comes with ABSOLUTELY NO WARRANTY.
87
You are welcome to redistribute it under certain conditions.
@@ -40,7 +39,7 @@ a list with components:
4039
[5] "ar" "ma" "covariance.dpq" "fnormMin"
4140
[9] "sigma" "stderror.dpq" "correlation.dpq" "h"
4241
[13] "d.tol" "M" "hessian.dpq" "length.w"
43-
[17] "call"
42+
[17] "residuals" "fitted" "call"
4443
> vcov(fd1)
4544
d ar1 ma1
4645
d 0.00059661 -0.00080522 -0.00018971
@@ -120,7 +119,7 @@ d 0.3374 0.0205 16.5 <2e-16 ***
120119
ar 0.7710 0.0414 18.6 <2e-16 ***
121120
ma -0.3810 0.0160 -23.8 <2e-16 ***
122121
---
123-
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
122+
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
124123
sigma[eps] = 1.0088
125124
[d.tol = 0.0001221, M = 100, h = 3.082e-05]
126125
Log likelihood: -2.92e+03 ==> AIC = 5856.5 [4 deg.freedom]
@@ -183,5 +182,8 @@ ma1 1742 1507 -5007
183182
>
184183
> ## Last Line:
185184
> cat('Time elapsed: ', proc.time() - .proctime00,'\n')
186-
Time elapsed: 0.21 0.003 0.213 0 0
185+
Time elapsed: 0.065 0.002 0.067 0 0
187186
>
187+
> proc.time()
188+
user system elapsed
189+
0.164 0.021 0.225

0 commit comments

Comments
 (0)