Frage

I'm currently using the bfast package in R to decompose a time series. I was curious if it was possible to extract the slope of the trend segment before and after a breakpoint (until an end or another breakpoint)?

The example below was taken from the reference manual.

harvest

The code to generate this was here.

require(bfast)
require(strucchange)
require(sandwich)
require(forecast)
require(raster)
require(sp)
fit <- bfast(harvest, season="harmonic", max.iter=2)
plot(fit, type="trend")

The problem is that the result output objects aren't automatic, in other words, I can find the values between breakpoints and try to formulate the slope from those trend values, but the process is very time consuming because I have to manually find the start/end breakpoint values and extract every value in between. So ideally, I want to find an easier way to identify the slope of the trend (blue line) before and after all available breakpoints for multiple time series.

Any help is appreciated, thanks.

War es hilfreich?

Lösung 2

To extract the slope values from the plot so that one can store them as objects for later use.

(plot(fit, ANOVA=TRUE)$slope)

Pretty interesting if you use str() on the plot function.

> str(plot(fit, ANOVA=TRUE))
'data.frame':   5 obs. of  2 variables:
 $ slope: num  0.00109 0.01314 -0.20077 0.14659 0.11524
 $ prob : num  9.07e-01 2.41e-05 1.01e-13 3.08e-13 1.06e-13
> (plot(fit, ANOVA=TRUE)$slope)
[1]  0.001088344  0.013139678 -0.200767197  0.146594801  0.115236688
> 

Andere Tipps

plot(fit, ANOVA = TRUE)

would give you 'Slope and Significance values for each identified trend segment'.

Interestingly, it does work when you specify: type="trend"

This question is quite old, but I found a faster way to do it, without calling the plot() or str() function and the computational time they involve

niter <- length(fit$output)
slopes<-coef(fit$output[[niter]]$bp.Vt)[,2]

I hope it helps you!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top