Skip to content

Commit 6ca0fa1

Browse files
committed
change cedar rapids example to suse optimization routines rather than grid search
1 parent 6b70f60 commit 6ca0fa1

2 files changed

Lines changed: 47 additions & 25 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Encoding: UTF-8
1212
Roxygen: list(markdown = TRUE)
1313
RoxygenNote: 7.3.2
1414
Depends:
15-
R (>= 2.10)
15+
R (>= 3.5)
1616
LazyData: true
1717
Suggests:
1818
rmarkdown,

R/example_cedar_rapids.R

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,73 @@
88
#'
99
#'@export
1010

11-
example_cedar_rapids <- function(dataSet=arfit::cedar_rapids,nBootSims=999) {
11+
example_cedar_rapids <- function(
12+
dataSet = arfit::cedar_rapids,
13+
nBootSims = 999
14+
) {
15+
ind <- (dataSet$year <= 1992) # Hamed et al only used 1992 data
1216

13-
ind <- (dataSet$year<=1992) # Hamed et al only used 1992 data
14-
15-
dataSet <- dataSet[ind,]
17+
dataSet <- dataSet[ind, ]
1618
nT <- dim(dataSet)[1]
17-
data <- data.frame(x=c(1:nT),y=dataSet$riverflow)
19+
data <- data.frame(x = c(1:nT), y = dataSet$riverflow)
1820

1921
# fit under the null and alternative
20-
null <- fit_ar1_grid(data,hypothesis ="null")
21-
alt <- fit_ar1_grid(data,hypothesis="alt")
22+
null <- fit_ar1_opt(data, rho = 0, hypothesis = "null")
23+
alt <- fit_ar1_opt(data, rho = 0, hypothesis = "alt")
2224
# null <- fit_ar1_opt(data,rho=0.5,hypothesis ="null")
2325
# alt <- fit_ar1_opt(data,null$rhoEst,hypothesis="alt")
2426
# preallocate likelihood ratio statistic vector
25-
LRstat <- vector(mode="numeric",length=nBootSims+1)
27+
LRstat <- vector(mode = "numeric", length = nBootSims + 1)
2628
# LR stat for data
27-
LRstat[1] <- -2*(null$likelihood-alt$likelihood)
28-
print(paste0("LR stat = ",LRstat[1]))
29+
LRstat[1] <- -2 * (null$likelihood - alt$likelihood)
30+
print(paste0("LR stat = ", LRstat[1]))
2931
# pvalue using chi square approximation
30-
pValChi2 <- 1-pchisq(LRstat[1],1) # uses distributional theory
32+
pValChi2 <- 1 - pchisq(LRstat[1], 1) # uses distributional theory
3133

3234
# Perform bootstrapping
33-
for (iboot in 2:(nBootSims+1)) {
35+
for (iboot in 2:(nBootSims + 1)) {
3436
# simulate under Null
35-
bootdata <- simulate_ar1(alpha=null$betaEst,beta=0,null$sigmaEst,null$rhoEst,nT)
37+
bootdata <- simulate_ar1(
38+
alpha = null$betaEst,
39+
beta = 0,
40+
null$sigmaEst,
41+
null$rhoEst,
42+
nT
43+
)
3644
# fit under null and alt
37-
nullBoot <- fit_ar1_grid(bootdata,hypothesis="null")
38-
altBoot <- fit_ar1_grid(bootdata,hypothesis="alt")
45+
nullBoot <- fit_ar1_opt(bootdata, rho = null$rhoEst, hypothesis = "null")
46+
altBoot <- fit_ar1_opt(bootdata, rho = null$rhoEst, hypothesis = "alt")
3947
# nullBoot <- fit_ar1_opt(bootdata,null$rhoEst,hypothesis="null")
4048
# altBoot <- fit_ar1_opt(bootdata,null$rhoEst,hypothesis="alt")
4149
# statisicic
42-
LRstat[iboot] <- -2*(nullBoot$likelihood-altBoot$likelihood)
50+
LRstat[iboot] <- -2 * (nullBoot$likelihood - altBoot$likelihood)
4351
} # end bootstrap
4452

4553
# now we can calculate the p-value based on the bootstrapping
46-
pVal_boot <- sum(LRstat >= LRstat[1])/(nBootSims+1)
47-
print(paste0("pval_boot = ",pVal_boot))
54+
pVal_boot <- sum(LRstat >= LRstat[1]) / (nBootSims + 1)
55+
print(paste0("pval_boot = ", pVal_boot))
4856

4957
# plot fits under the null and alternative
5058
#png("Figure7.png",width=900,height=600,units="px")
51-
par(mai=c(1,1.5,0,0),oma=c(0,0,1,1))
52-
plot(dataSet$year,dataSet$riverflow,type="l",xlab="Year",ylab=expression(Discharge~ (f^3/s)),
53-
cex.lab=2.5,cex.axis=2,lwd=2)
54-
lines(dataSet$year,rep(null$betaEst,nT),col="black",lty=2,lwd=2)
55-
lines(dataSet$year,alt$betaEst[1]+alt$betaEst[2]*c(1:nT),col="black",lty=3,lwd=2)
59+
par(mai = c(1, 1.5, 0, 0), oma = c(0, 0, 1, 1))
60+
plot(
61+
dataSet$year,
62+
dataSet$riverflow,
63+
type = "l",
64+
xlab = "Year",
65+
ylab = expression(Discharge ~ (f^3 / s)),
66+
cex.lab = 2.5,
67+
cex.axis = 2,
68+
lwd = 2
69+
)
70+
lines(dataSet$year, rep(null$betaEst, nT), col = "black", lty = 2, lwd = 2)
71+
lines(
72+
dataSet$year,
73+
alt$betaEst[1] + alt$betaEst[2] * c(1:nT),
74+
col = "black",
75+
lty = 3,
76+
lwd = 2
77+
)
5678
#dev.off()
57-
return(list(null=null, alt=alt,pValChi2=pValChi2))
79+
return(list(null = null, alt = alt, pValChi2 = pValChi2))
5880
}

0 commit comments

Comments
 (0)