Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc81c1c1

Browse files
committed
Updates for gof plot
1 parent8e61439 commitc81c1c1

File tree

6 files changed

+57
-40
lines changed

6 files changed

+57
-40
lines changed

‎NAMESPACE‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export(garma)
2020
export(garma_ggtsdisplay)
2121
export(gg_raw_pgram)
2222
export(ggbr_semipara)
23+
export(gof)
2324
export(version)
2425
importFrom(BB,BBoptim)
2526
importFrom(GA,de)
@@ -67,9 +68,11 @@ importFrom(stats,acf)
6768
importFrom(stats,arima)
6869
importFrom(stats,coef)
6970
importFrom(stats,diffinv)
71+
importFrom(stats,ecdf)
7072
importFrom(stats,end)
7173
importFrom(stats,fft)
7274
importFrom(stats,frequency)
75+
importFrom(stats,ks.test)
7376
importFrom(stats,lm)
7477
importFrom(stats,na.pass)
7578
importFrom(stats,optim)

‎R/garma-package.R‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#' @importFrom Rsolnp solnp gosolnp
2222
#' @importFrom ggplot2 autoplot ggplot geom_line aes geom_vline theme theme_bw scale_colour_manual geom_text labs element_blank
2323
#' @importFrom graphics abline lines par plot
24-
#' @importFrom stats diffinv end sd start ts tsp var spectrum frequency optimise arima Box.test acf pacf tsdiag na.pass optim lm residuals coef fft
24+
#' @importFrom stats diffinv end sd start ts tsp var spectrumspec.pgram ks.test ecdffrequency optimise arima Box.test acf pacf tsdiag na.pass optim lm residuals coef fft
2525
#' @importFrom utils tail packageVersion head globalVariables
2626
NULL
2727

‎R/gof.R‎

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,23 @@
1-
## ' Goodness-of-Fit test for a garma_model.
2-
## This function commented out for now. Does not appear to work well.
3-
## '
4-
## ' Provides a goodness-of-fit test for a GARMA Model, using the method of Delgado, Hidalgo and Velasco (2005).
5-
## '
6-
## ' This routine provides a test for White noise using eqn (6) of Delgado, Hidalgo and Velasco (2005). The
7-
## ' statistic calculated is \eqn{\alpha_n^0=\frac{1}{\sqrt{\tilde{n}}}\left(\frac{G_n^0(\lambda_j)}{G_T^0(\pi)}-\frac{\lambda_j}{\pi}\right)}
8-
## ' which is asymptotically distributed as a Brownian Bridge,
9-
## ' where \eqn{G_n^0(\lambda_j)=\frac{2\pi}{\tilde{n}}\sum_{j=1}^{[\tilde{n}\lambda_j/\pi]}I_\epsilon(\lambda_j)}
10-
## '
11-
## ' So any interval \eqn{[0,\lambda]}
12-
## ' will have an asymptotically \eqn{N\left(0,\frac{\lambda}{\pi}\left(1-\frac{\lambda}{\pi}\right)\right)} distribution.
13-
## '
14-
## ' @param object (garma_model) The garma_model to test.
15-
## ' @param gof.lag (int) max lag to test.
16-
## ' @return None.
17-
## ' @export
18-
# gof<-function(object,gof.lag=10) {
19-
# r <- as.numeric(residuals(object))
20-
# n <- length(r)
21-
# tilde_n <- as.integer(n/2)
22-
# if (gof.lag>tilde_n) stop('ERROR: number of lags not supported by length of data.\n')
23-
#
24-
# I_eps <- spec.pgram(r,taper=0,fast=FALSE,demean=FALSE,detrend=FALSE,plot=FALSE)
25-
# G_lambda <- sum(I_eps$spec[1:gof.lag])
26-
# G_pi <- sum(I_eps$spec)
27-
# # in next line the (2*gof.lag/n) is a simplification:
28-
# # lambda_{gof.lag}/pi = (2*pi*gof.lag/n)/pi = (2*gof.lag/n)
29-
# s <- 1/sqrt(tilde_n)*(G_lambda/G_pi-(2*gof.lag/n))
30-
# var_s <- (2*gof.lag/n)*(1-2*gof.lag/n)
31-
# cat(sprintf('lambda/pi %f\nG_lambda %f\nG_pi %f\ns %f\nvar_s %f\n',2*gof.lag/n,G_lambda,G_pi,s,var_s))
32-
# s2 <- s/sqrt(var_s)
33-
# p <- 1-pnorm(s2)
34-
#
35-
# cat(sprintf('\nTest for H0: Residuals up to lag %d are White Noise.\napprox z-statistic: %0.4f\np-value: %0.4f\n',gof.lag,s2,p))
36-
# }
1+
#' Goodness-of-Fit test for a garma_model.
2+
#'
3+
#' Provides a goodness-of-fit test for a GARMA Model, using Bartletts Tp test.
4+
#' This has been justified for long memory and for GARMA models by Delgado, Hidalgo and Velasco (2005).
5+
#'
6+
#' @param object (garma_model) The garma_model to test.
7+
#' @return Invisibly returns the array of p-values from the test.
8+
#' @export
9+
gof<-function(object) {
10+
r<- as.numeric(residuals(object))
11+
n<- length(r)
12+
tilde_n<- as.integer(n/2)
13+
14+
I_eps<- spec.pgram(r,taper=0,fast=FALSE,demean=FALSE,detrend=FALSE,plot=FALSE)
15+
pv<-numeric(0)
16+
for (iin2:length(I_eps$spec))pv<- c(pv, ks.test(I_eps$spec[1:i],ecdf(I_eps$spec))$p.value)
17+
cat(sprintf('\nBartletts Tp test.\n\nTest for H0: Residuals are White Noise.\nEvaluating %d frequencies, smallest p-value: %0.4f at frequency %.4f period %.4f.\n',
18+
length(I_eps$spec),min(pv),
19+
(which.min(pv)+1)*2*pi/n,
20+
n/((which.min(pv)+1))
21+
))
22+
return(invisible(pv))
23+
}

‎R/tsdiag.R‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,25 @@ tsdiag.garma_model<-function(object, gof.lag=10, ...) {
1919
titles<- .generate_default_plot_title(object,h=0)
2020

2121
## plot standardized residuals, acf of residuals, Ljung-Box p-values
22-
oldpar<- par(mfrow= c(2,1))
22+
oldpar<- par(mfrow= c(3,1))
2323
on.exit(par(oldpar))
2424
rs<-object$residuals
2525
stdres<-rs/sqrt(object$sigma2)
2626
plot(stdres,type="h",main= paste(object$series,"- Standardized Residuals"),ylab="")
2727
abline(h=0)
2828
acf(as.numeric(object$residuals),plot=TRUE,
2929
main= paste(object$series," - ACF of Residuals"),
30-
sub=titles$sub,
3130
na.action=na.pass)
32-
# gof(object,gof.lag)
31+
32+
pv<- gof(object)
33+
plot((2:(length(pv)+1))/length(object$y),
34+
pv,
35+
type='l',
36+
main= expression("p-values for Bartletts T"[p]*" test of residuals."),
37+
sub=titles$sub,
38+
ylab="p-value",
39+
xlab=expression("Frequency (x"*2*pi*")"),
40+
ylim=c(0,1))
41+
abline(h=0.05,col="blue",lty=3)
3342
}
3443

‎inst/garma.pdf‎

615 Bytes
Binary file not shown.

‎man/gof.Rd‎

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp