|
8 | 8 | #' @param nBootSims Numeric scalar. Number of bootstrap samples to perform |
9 | 9 | #' @param printFig Boolean. Print data an fit in figure window (Default = F) |
10 | 10 | #' |
| 11 | +#' @return A list containing: |
| 12 | +#' \item{null}{Fitted model under the null hypothesis (no trend)} |
| 13 | +#' \item{alt}{Fitted model under the alternative hypothesis (with trend)} |
| 14 | +#' \item{pValue}{p-value from the bootstrap test} |
| 15 | +#' \item{pValChi2}{p-value from the chi-squared approximation} |
| 16 | +#' \item{data}{The input data set} |
| 17 | +#' |
| 18 | +#' |
11 | 19 | #' @section: ecodata |
12 | 20 | #' |
13 | 21 | #'This function is used in ecodata::geom_lm() |
14 | 22 | #' |
15 | 23 | #'@export |
16 | 24 |
|
17 | | -fit_real_data <- function(dataSet,nBootSims=499,printFig=F) { |
18 | | - |
| 25 | +fit_real_data <- function(dataSet, nBootSims = 499, printFig = F) { |
19 | 26 | dataValidation <- check_data_validation(dataSet) |
20 | 27 | dataSet <- dataValidation$dataSet |
21 | 28 | missingValues <- dataValidation$missingValues |
22 | 29 |
|
23 | 30 | data <- dataSet |
24 | 31 | nT <- nrow(data) |
25 | 32 | # fit under the null and alternative |
26 | | - null <- fit_ar1_opt(data,rho = 0,hypothesis ="null") |
27 | | - alt <- fit_ar1_opt(data,rho = 0,hypothesis="alt") |
| 33 | + null <- fit_ar1_opt(data, rho = 0, hypothesis = "null") |
| 34 | + alt <- fit_ar1_opt(data, rho = 0, hypothesis = "alt") |
28 | 35 |
|
29 | 36 | # preallocate likelihood ratio statistic vector |
30 | | - LRstat <- vector(mode="numeric",length=nBootSims+1) |
| 37 | + LRstat <- vector(mode = "numeric", length = nBootSims + 1) |
31 | 38 | # LR stat for data |
32 | | - LRstat[1] <- -2*(null$likelihood-alt$likelihood) |
| 39 | + LRstat[1] <- -2 * (null$likelihood - alt$likelihood) |
33 | 40 | #print(paste0("LR stat = ",LRstat[1])) |
34 | 41 | # pvalue using chi square approximation |
35 | | - pValChi2 <- 1-pchisq(LRstat[1],1) # uses distributional theory |
| 42 | + pValChi2 <- 1 - pchisq(LRstat[1], 1) # uses distributional theory |
36 | 43 |
|
37 | 44 | # Perform bootstrapping |
38 | | - for (iboot in 2:(nBootSims+1)) { |
| 45 | + for (iboot in 2:(nBootSims + 1)) { |
39 | 46 | # simulate under Null |
40 | | - bootdata <- simulate_ar1(alpha=null$betaEst,beta=0,null$sigmaEst,null$rhoEst,nT,missingValues = missingValues) |
| 47 | + bootdata <- simulate_ar1( |
| 48 | + alpha = null$betaEst, |
| 49 | + beta = 0, |
| 50 | + null$sigmaEst, |
| 51 | + null$rhoEst, |
| 52 | + nT, |
| 53 | + missingValues = missingValues |
| 54 | + ) |
41 | 55 |
|
42 | 56 | dataValidation <- check_data_validation(bootdata) |
43 | 57 | bootdata <- dataValidation$dataSet |
44 | 58 |
|
45 | 59 | # fit under null and alt |
46 | | - nullBoot <- fit_ar1_opt(bootdata,null$rhoEst,hypothesis="null") |
47 | | - altBoot <- fit_ar1_opt(bootdata,null$rhoEst,hypothesis="alt") |
| 60 | + nullBoot <- fit_ar1_opt(bootdata, null$rhoEst, hypothesis = "null") |
| 61 | + altBoot <- fit_ar1_opt(bootdata, null$rhoEst, hypothesis = "alt") |
48 | 62 |
|
49 | 63 | # statisicic |
50 | | - LRstat[iboot] <- -2*(nullBoot$likelihood-altBoot$likelihood) |
| 64 | + LRstat[iboot] <- -2 * (nullBoot$likelihood - altBoot$likelihood) |
51 | 65 | } # end bootstrap |
52 | 66 |
|
53 | 67 | # now we can calculate the p-value based on the bootstrapping |
54 | | - pVal_boot <- sum(LRstat >= LRstat[1])/(nBootSims+1) |
| 68 | + pVal_boot <- sum(LRstat >= LRstat[1]) / (nBootSims + 1) |
55 | 69 |
|
56 | | - |
57 | | - if(printFig) { |
58 | | - print(paste0("pval_boot = ",pVal_boot)) |
59 | | - par(mai=c(1,1.5,0,0),oma=c(0,0,1,1)) |
60 | | - plot(dataSet$x,dataSet$y,type="l",xlab="Year",ylab="Response", |
61 | | - cex.lab=2.5,cex.axis=2,lwd=2) |
62 | | - lines(dataSet$x,rep(null$betaEst,nT),col="black",lty=2,lwd=2) |
63 | | - # lines(dataSet$x,alt$betaEst[1]+alt$betaEst[2]*c(1:nT),col="black",lty=3,lwd=2) |
64 | | - lines(dataSet$x,alt$betaEst[1]+alt$betaEst[2]*dataSet$x,col="black",lty=3,lwd=2) |
| 70 | + if (printFig) { |
| 71 | + print(paste0("pval_boot = ", pVal_boot)) |
| 72 | + par(mai = c(1, 1.5, 0, 0), oma = c(0, 0, 1, 1)) |
| 73 | + plot( |
| 74 | + dataSet$x, |
| 75 | + dataSet$y, |
| 76 | + type = "l", |
| 77 | + xlab = "Year", |
| 78 | + ylab = "Response", |
| 79 | + cex.lab = 2.5, |
| 80 | + cex.axis = 2, |
| 81 | + lwd = 2 |
| 82 | + ) |
| 83 | + lines(dataSet$x, rep(null$betaEst, nT), col = "black", lty = 2, lwd = 2) |
| 84 | + # lines(dataSet$x,alt$betaEst[1]+alt$betaEst[2]*c(1:nT),col="black",lty=3,lwd=2) |
| 85 | + lines( |
| 86 | + dataSet$x, |
| 87 | + alt$betaEst[1] + alt$betaEst[2] * dataSet$x, |
| 88 | + col = "black", |
| 89 | + lty = 3, |
| 90 | + lwd = 2 |
| 91 | + ) |
65 | 92 | } |
66 | 93 |
|
67 | | - |
68 | | - return(list(null=null, alt=alt,pValue=pVal_boot,data=dataSet)) |
| 94 | + return(list( |
| 95 | + null = null, |
| 96 | + alt = alt, |
| 97 | + pValue = pVal_boot, |
| 98 | + pValChi2 = pValChi2, |
| 99 | + data = dataSet |
| 100 | + )) |
69 | 101 | } |
0 commit comments