envcpt: Assesses whether an environmental time series contains trend,...

Description Usage Arguments Details Value Author(s) References See Also Examples

View source: R/envcpt.R

Description

Evaluates up to 12 different models (see details) and returns the model fits as well as a summary of the likelihood for each model.

Usage

1
2
3
envcpt(data,models=c("mean","meancpt","meanar1","meanar2","meanar1cpt","meanar2cpt",
"trend","trendcpt","trendar1","trendar2","trendar1cpt","trendar2cpt"),minseglen=5,...,
verbose=TRUE)

Arguments

data

A vector or ts object containing the data to fit the models to.

models

A vector containing the subset of models to fit, defaults to all available models. This can either be named (as in the default) or numbered (1:12), see details or defaults for number-model pairings.

minseglen

Positive integer giving the minimum segment length (no. of observations between changes) for the changepoint models, default is the minimum allowed by theory (for the largest model).

...

Additional arguments to pass to the changepoint functions, if none are specified defaults with PELT multiple changepoint algorithm are used. See cpt.meanvar for options.

verbose

If TRUE (default), prints to the console an progress bar indicating progression through the fit of the up to 12 models.

Details

This function is used to automatically fit up to 12 different models all with Normal distribution for errors: 1. A constant mean and variance (using fitdistr) 2. A piecewise constant mean and variance (using cpt.meanvar) 3. A constant mean with AR (1) errors (using arima) 4. A constant mean with AR (2) errors (using arima) 5. A piecewise constant mean with AR(1) errors (using cpt.reg in this package - not exported) 6. A piecewise constant mean with AR(2) errors (using cpt.reg in this package - not exported) 7. A linear trend over time (using lm) 8. A piecewise linear trend over time (using cpt.reg in this package - not exported) 9. A linear trend over time with AR(1) errors (using lm) 10. A linear trend over time with AR(2) errors (using lm) 11. A piecewise linear trend over time with AR(1) errors (using cpt.reg in this package - not exported) 12. A piecewise linear trend over time with AR(2) errors (using cpt.reg in this package - not exported) The default values for each function are used, except for the changepoint functions where multiple changes are identified and thus the PELT algorithm is used (see references or changepoint package for details).

Value

envcpt outputs a list of 13 elements. The first element is a 2x8 matrix where the first row contains the likelihood for each model fit and the second row contains the number of parameters fit in each model. The 12 columns are for the 12 different models in the order above (headings given). If any element is NA then either there was an error fitting this type of model (typically the AR models and this implies nonstationarity) or the model was not specified in the models argument.

Elements 2-13 of the list are the fits for each individual model, these are the direct output from the respective functions so see the individual functions for formats. The first model fit is in element 2 and the twelth model fit is in element 13, but are named for convenience.

Author(s)

Rebecca Killick

References

EnvCpt Algorithm: Beaulieu, C, Killick, R (2018+) Distinguishing trends and shifts from memory in climate data.

PELT Algorithm: Killick R, Fearnhead P, Eckley IA (2012) Optimal detection of changepoints with a linear computational cost, JASA 107(500), 1590–1598

MBIC: Zhang, N. R. and Siegmund, D. O. (2007) A Modified Bayes Information Criterion with Applications to the Analysis of Comparative Genomic Hybridization Data. Biometrics 63, 22-32.

See Also

cpt.meanvar,plot-methods,cpt, plot.envcpt

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
## Not run: 
set.seed(1)
x=c(rnorm(100,0,1),rnorm(100,5,1))
out=envcpt(x) # run all models with default values
out[[1]] # first row is twice the negative log-likelihood for each model
         # second row is the number of parameters
AIC(out) # returns AIC for each model.
which.min(AIC(out)) # gives meancpt (model 2) as the best model fit.
out$meancpt # gives the model fit for the meancpt model.
AICweights(out) # gives the AIC weights for each model
BIC(out) # returns the BIC for each model.
which.min(BIC(out)) # gives meancpt (model 2) as the best model fit too.
plot(out,type='fit') # plots the fits
plot(out,type="aic") # plots the aic values
plot(out,type="bic") # plots the bic values

set.seed(10)
x=c(0.01*(1:100),1.5-0.02*((101:250)-101))+rnorm(250,0,0.2)
out=envcpt(x,minseglen=10) # run all models with a minimum of 10 observations between changes
AIC(out) # returns the AIC for each model
which.min(AIC(out)) # gives trendcpt (model 8) as the best model fit.
out$trendcpt # gives the model fit for the trendcpt model.
AICweights(out) # gives the AIC weights for each model
BIC(out) # returns the BIC for each model.
which.min(BIC(out)) # gives trendcpt (model 8) as the best model fit too.
plot(out,type='fit') # plots the fits
plot(out,type="aic") # plots the aic values
plot(out,type="bic") # plots the bic values

set.seed(100)
x=arima.sim(model=list(ar=c(0.7,0.2)),n=500)+0.01*(1:500)
out=envcpt(x,models=c(3:6,9:12)) # runs a subset of models (those with AR components) 
AIC(out) # returns the AIC for each model
which.min(AIC(out)) # gives trendar2 (model 10) as the best model fit.
out$trendar2 # gives the model fit for the trendar2 model. Notice that the trend is tiny but does 
# produce a significantly better fit than the meanar2 model.
AICweights(out) # gives the AIC weights for each model
BIC(out) # returns the BIC for each model.
which.min(BIC(out)) # best fit is trendar2 (model 10) again.
plot(out,type='fit') # plots the fits
plot(out,type="aic") # plots the aic values
plot(out,type="bic") # plots the bic values

## End(Not run)

Example output

Loading required package: changepoint
Loading required package: zoo

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric

Successfully loaded changepoint package version 2.2.2
 NOTE: Predefined penalty values changed in version 2.2.  Previous penalty values with a postfix 1 i.e. SIC1 are now without i.e. SIC and previous penalties without a postfix i.e. SIC are now with a postfix 0 i.e. SIC0. See NEWS and help files for further details.
Loading required package: MASS
Fitting 12 models

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |======================================================================| 100%
           mean meancpt  meanar1  meanar2 meanar1cpt meanar2cpt    trend
logLik 949.2517 535.486 683.8642 640.5247   532.9724   530.5236 735.4193
nparam   2.0000   5.000   3.0000   4.0000     7.0000     9.0000   3.0000
       trendcpt trendar1 trendar2 trendar1cpt trendar2cpt
logLik  535.414  648.192 619.5107    532.7957    530.4183
nparam    7.000    4.000   5.0000      9.0000     11.0000
       mean     meancpt     meanar1     meanar2  meanar1cpt  meanar2cpt 
   953.2517    545.4860    689.8642    648.5247    546.9724    548.5236 
      trend    trendcpt    trendar1    trendar2 trendar1cpt trendar2cpt 
   741.4193    549.4140    656.1920    629.5107    550.7957    552.4183 
meancpt 
      2 
Class 'cpt' : Changepoint Object
       ~~   : S4 class containing 12 slots with names
              cpttype date version data.set method test.stat pen.type pen.value minseglen cpts ncpts.max param.est 

Created on  : Thu May  4 11:08:19 2017 

summary(.)  :
----------
Created Using changepoint version 2.2.2 
Changepoint type      : Change in mean and variance 
Method of analysis    : PELT 
Test Statistic  : Normal 
Type of penalty       : MBIC with value, 21.19327 
Minimum Segment Length : 5 
Maximum no. of cpts   : Inf 
Changepoint Locations : 100 
        mean      meancpt      meanar1      meanar2   meanar1cpt   meanar2cpt 
1.471637e-89 5.164235e-01 2.299732e-32 2.179893e-23 2.456047e-01 1.130823e-01 
       trend     trendcpt     trendar1     trendar2  trendar1cpt  trendar2cpt 
1.467638e-43 7.245090e-02 4.715216e-25 2.932684e-19 3.630777e-02 1.613078e-02 
       mean     meancpt     meanar1     meanar2  meanar1cpt  meanar2cpt 
   959.8483    561.9775    699.7591    661.7179    570.0606    578.2084 
      trend    trendcpt    trendar1    trendar2 trendar1cpt trendar2cpt 
   751.3143    572.5022    669.3852    646.0023    580.4806    588.6998 
meancpt 
      2 
Fitting 12 models

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |======================================================================| 100%
       mean     meancpt     meanar1     meanar2  meanar1cpt  meanar2cpt 
  578.94707   -67.51376    27.29077   -29.55647    27.29077    29.29077 
      trend    trendcpt    trendar1    trendar2 trendar1cpt trendar2cpt 
  468.36581  -113.41437    20.92805   -33.70269  -109.94452  -105.29104 
trendcpt 
       8 
Class 'cpt.reg' : Changepoint Regression Object
       ~~   : S4 class containing 12 slots with names
              cpttype date version data.set method test.stat pen.type pen.value minseglen cpts ncpts.max param.est 

Created on  : Thu May  4 11:08:19 2017 

summary(.)  :
----------
Created Using changepoint version 2.2.2 
Changepoint type     : Change in regression 
Method of analysis   : PELT 
Test Statistic : Normal 
Type of penalty      : MBIC with value, 27.6073 
Maximum no. of cpts   : Inf 
Changepoint Locations : 100 
         mean       meancpt       meanar1       meanar2    meanar1cpt 
3.790989e-151  9.035235e-11  2.340974e-31  5.171508e-19  2.340974e-31 
   meanar2cpt         trend      trendcpt      trendar1      trendar2 
 8.611961e-32 3.900936e-127  8.377773e-01  5.636952e-30  4.111095e-18 
  trendar1cpt   trendar2cpt 
 1.477958e-01  1.442682e-02 
       mean     meancpt     meanar1     meanar2  meanar1cpt  meanar2cpt 
  585.98999    24.04422    37.85515   -15.47062    37.85515    43.37661 
      trend    trendcpt    trendar1    trendar2 trendar1cpt trendar2cpt 
  478.93019   -88.76414    35.01389   -16.09538   -78.25138   -66.55497 
trendcpt 
       8 
Fitting 8 models

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |====================================================                  |  75%
       mean     meancpt     meanar1     meanar2  meanar1cpt  meanar2cpt 
         NA          NA    1478.915    1455.634    1478.915    1480.915 
      trend    trendcpt    trendar1    trendar2 trendar1cpt trendar2cpt 
         NA          NA    1452.451    1430.101    1452.451    1430.101 
trendar2 
      10 

Call:
lm(formula = data[-c(1:2)] ~ c(1:(n - 2)) + data[2:(n - 1)] + 
    data[1:(n - 2)])

Coefficients:
    (Intercept)     c(1:(n - 2))  data[2:(n - 1)]  data[1:(n - 2)]  
      -0.069419         0.001703         0.661337         0.186613  

        mean      meancpt      meanar1      meanar2   meanar1cpt   meanar2cpt 
          NA           NA 1.256735e-11 1.428051e-06 1.256735e-11 4.623269e-12 
       trend     trendcpt     trendar1     trendar2  trendar1cpt  trendar2cpt 
          NA           NA 7.011284e-06 4.999923e-01 7.011284e-06 4.999923e-01 
       mean     meancpt     meanar1     meanar2  meanar1cpt  meanar2cpt 
         NA          NA    1491.553    1472.484    1491.553    1497.765 
      trend    trendcpt    trendar1    trendar2 trendar1cpt trendar2cpt 
         NA          NA    1469.302    1451.164    1469.302    1451.164 
trendar2 
      10 

EnvCpt documentation built on July 2, 2020, 12:52 a.m.

Related to envcpt in EnvCpt...