I am trying to replicate empirical fluctuation process fit (efp) described in the book "Applied Econometrics with R". ?This fit works when data input is an object of class ts, but not when data input is object of class zoo. ?I prefer to use zoo because it provides better housekeeping with dates. ?Is it possible to achieve the fit with zoo? library(AER) library(strucchange) data(UKDriverDeaths) dd <- log(UKDriverDeaths) dd.z <- zoo(dd, order.by = as.yearmon(time(dd))) dd.z <- merge(dd = dd.z, dd.lag1 = lag(dd.z, k = -1), ? ? ? ? ? ? ? dd.lag12 = lag(dd.z, k = -12)) # Does not work dd.ocus <- efp(dd ~ dd.lag1 + dd.lag12, data = na.trim(dd.z), ? ? ? ? ? ? ? ?type = "OLS-CUSUM") # Error message # Error in eval(attr(mt, "variables")[[2]], data, env) : # numeric 'envir' arg not of length one # Works dd.ocus <- efp(dd ~ dd.lag1 + dd.lag12, data = ts(na.trim(dd.z)), ? ? ? ? ? ? ? ?type = "OLS-CUSUM") # But time stamps are lost plot(dd.ocus) # Time indexed from 0 to 180 Thanks, Naresh
Hi Naresh, The tsbox package on CRAN - https://cran.r-project.org/web/packages/tsbox/index.html - has the following description: tsbox: Class-Agnostic Time Series Time series toolkit with identical behavior for all time series classes: 'ts','xts', 'data.frame', 'data.table', 'tibble', 'zoo', 'timeSeries', 'tsibble', 'tis' or 'irts'. Also converts reliably between these classes. Hopefully this will provide you the necessary tools to solve your problem. Good luck, Eric On Sun, May 1, 2022 at 3:37 PM Naresh Gurbuxani <naresh_gurbuxani at hotmail.com> wrote:> > I am trying to replicate empirical fluctuation process fit (efp) described in the book "Applied Econometrics with R". This fit works when data input is an object of class ts, but not when data input is object of class zoo. I prefer to use zoo because it provides better housekeeping with dates. Is it possible to achieve the fit with zoo? > > library(AER) > library(strucchange) > > data(UKDriverDeaths) > dd <- log(UKDriverDeaths) > dd.z <- zoo(dd, order.by = as.yearmon(time(dd))) > dd.z <- merge(dd = dd.z, dd.lag1 = lag(dd.z, k = -1), > dd.lag12 = lag(dd.z, k = -12)) > > # Does not work > dd.ocus <- efp(dd ~ dd.lag1 + dd.lag12, data = na.trim(dd.z), > type = "OLS-CUSUM") > # Error message > # Error in eval(attr(mt, "variables")[[2]], data, env) : > # numeric 'envir' arg not of length one > > # Works > dd.ocus <- efp(dd ~ dd.lag1 + dd.lag12, data = ts(na.trim(dd.z)), > type = "OLS-CUSUM") > > # But time stamps are lost > plot(dd.ocus) > # Time indexed from 0 to 180 > > Thanks, > Naresh > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On Sun, 1 May 2022, Naresh Gurbuxani wrote:> I am trying to replicate empirical fluctuation process fit (efp) > described in the book "Applied Econometrics with R". ?This fit works > when data input is an object of class ts, but not when data input is > object of class zoo. ?I prefer to use zoo because it provides better > housekeeping with dates. ?Is it possible to achieve the fit with zoo?The efp() function has been written before zoo was available and is hence confined to ts objects. The gefp() function _g_enenralizes the efp idea and also works with zoo series out of the box - and also with models other than lm().. Internally gefp always computes a score-based CUSUM process, based on which various structural change tests can be computed that encompass the OLS-based CUSUM test. Typically, I would recommend to compute the supLM test, though, which is more powerful against many structural change alternatives. See ?gefp and the references therein for more details. A worked example is included below.> library(AER) > library(strucchange) > > data(UKDriverDeaths) > dd <- log(UKDriverDeaths) > dd.z <- zoo(dd, order.by = as.yearmon(time(dd))) > dd.z <- merge(dd = dd.z, dd.lag1 = lag(dd.z, k = -1), > ? ? ? ? ? ? ? dd.lag12 = lag(dd.z, k = -12))Then you can continue with the following for the full score-based CUSUM process and corresponding supLM test with 10% trimming: dd.scus <- gefp(dd ~ dd.lag1 + dd.lag12, fit = lm, data = dd.z) plot(dd.scus, functional = supLM(0.1)) sctest(dd.scus, functional = supLM(0.1)) ## M-fluctuation test ## ## data: dd.scus ## f(efp) = 17.128, p-value = 0.01711 The OLS-based CUSUM test is also a special case but then you need to test just the first score without decorrelation: dd.ocus <- gefp(dd ~ dd.lag1 + dd.lag12, fit = lm, data = dd.z, parm = 1, decorrelate = FALSE) plot(dd.ocus) sctest(dd.ocus) ## M-fluctuation test ## ## data: dd.ocus ## f(efp) = 1.4991, p-value = 0.02234 Compared to the book the results differ slightly due to different degrees of freedom adjustments that are used by default. Best, Achim> # Does not work > dd.ocus <- efp(dd ~ dd.lag1 + dd.lag12, data = na.trim(dd.z), > ? ? ? ? ? ? ? ?type = "OLS-CUSUM") > # Error message > # Error in eval(attr(mt, "variables")[[2]], data, env) : > # numeric 'envir' arg not of length one > > # Works > dd.ocus <- efp(dd ~ dd.lag1 + dd.lag12, data = ts(na.trim(dd.z)), > ? ? ? ? ? ? ? ?type = "OLS-CUSUM") > > # But time stamps are lost > plot(dd.ocus) > # Time indexed from 0 to 180 > > Thanks, > Naresh > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >