|
8 | 8 | #' |
9 | 9 | #'@export |
10 | 10 |
|
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 |
12 | 16 |
|
13 | | - ind <- (dataSet$year<=1992) # Hamed et al only used 1992 data |
14 | | - |
15 | | - dataSet <- dataSet[ind,] |
| 17 | + dataSet <- dataSet[ind, ] |
16 | 18 | 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) |
18 | 20 |
|
19 | 21 | # 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") |
22 | 24 | # null <- fit_ar1_opt(data,rho=0.5,hypothesis ="null") |
23 | 25 | # alt <- fit_ar1_opt(data,null$rhoEst,hypothesis="alt") |
24 | 26 | # preallocate likelihood ratio statistic vector |
25 | | - LRstat <- vector(mode="numeric",length=nBootSims+1) |
| 27 | + LRstat <- vector(mode = "numeric", length = nBootSims + 1) |
26 | 28 | # 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])) |
29 | 31 | # 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 |
31 | 33 |
|
32 | 34 | # Perform bootstrapping |
33 | | - for (iboot in 2:(nBootSims+1)) { |
| 35 | + for (iboot in 2:(nBootSims + 1)) { |
34 | 36 | # 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 | + ) |
36 | 44 | # 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") |
39 | 47 | # nullBoot <- fit_ar1_opt(bootdata,null$rhoEst,hypothesis="null") |
40 | 48 | # altBoot <- fit_ar1_opt(bootdata,null$rhoEst,hypothesis="alt") |
41 | 49 | # statisicic |
42 | | - LRstat[iboot] <- -2*(nullBoot$likelihood-altBoot$likelihood) |
| 50 | + LRstat[iboot] <- -2 * (nullBoot$likelihood - altBoot$likelihood) |
43 | 51 | } # end bootstrap |
44 | 52 |
|
45 | 53 | # 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)) |
48 | 56 |
|
49 | 57 | # plot fits under the null and alternative |
50 | 58 | #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 | + ) |
56 | 78 | #dev.off() |
57 | | - return(list(null=null, alt=alt,pValChi2=pValChi2)) |
| 79 | + return(list(null = null, alt = alt, pValChi2 = pValChi2)) |
58 | 80 | } |
0 commit comments