Skip to content

Commit efbeed4

Browse files
committed
return chisq approximation.
document output variables
1 parent 86534a1 commit efbeed4

3 files changed

Lines changed: 65 additions & 25 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ url: https://github.com/andybeet/arfit
1010
BugReports: https://github.com/andybeet/arfit/issues
1111
Encoding: UTF-8
1212
Roxygen: list(markdown = TRUE)
13-
RoxygenNote: 7.3.1
13+
RoxygenNote: 7.3.2
1414
Depends:
1515
R (>= 2.10)
1616
LazyData: true

R/fit_real_data.R

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,62 +8,94 @@
88
#' @param nBootSims Numeric scalar. Number of bootstrap samples to perform
99
#' @param printFig Boolean. Print data an fit in figure window (Default = F)
1010
#'
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+
#'
1119
#' @section: ecodata
1220
#'
1321
#'This function is used in ecodata::geom_lm()
1422
#'
1523
#'@export
1624

17-
fit_real_data <- function(dataSet,nBootSims=499,printFig=F) {
18-
25+
fit_real_data <- function(dataSet, nBootSims = 499, printFig = F) {
1926
dataValidation <- check_data_validation(dataSet)
2027
dataSet <- dataValidation$dataSet
2128
missingValues <- dataValidation$missingValues
2229

2330
data <- dataSet
2431
nT <- nrow(data)
2532
# 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")
2835

2936
# preallocate likelihood ratio statistic vector
30-
LRstat <- vector(mode="numeric",length=nBootSims+1)
37+
LRstat <- vector(mode = "numeric", length = nBootSims + 1)
3138
# LR stat for data
32-
LRstat[1] <- -2*(null$likelihood-alt$likelihood)
39+
LRstat[1] <- -2 * (null$likelihood - alt$likelihood)
3340
#print(paste0("LR stat = ",LRstat[1]))
3441
# 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
3643

3744
# Perform bootstrapping
38-
for (iboot in 2:(nBootSims+1)) {
45+
for (iboot in 2:(nBootSims + 1)) {
3946
# 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+
)
4155

4256
dataValidation <- check_data_validation(bootdata)
4357
bootdata <- dataValidation$dataSet
4458

4559
# 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")
4862

4963
# statisicic
50-
LRstat[iboot] <- -2*(nullBoot$likelihood-altBoot$likelihood)
64+
LRstat[iboot] <- -2 * (nullBoot$likelihood - altBoot$likelihood)
5165
} # end bootstrap
5266

5367
# 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)
5569

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+
)
6592
}
6693

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+
))
69101
}

man/fit_real_data.Rd

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

0 commit comments

Comments
 (0)