0
$\begingroup$

Would anyone else advise me, how to create time series with lagged in R. I would the result is the difference with lagged, there is a function Delt() but the result is the percentage change. Please advise how to do that. Thank you

getSymbols("^GSPC")
DeltLagGSPC<-Delt(Cl(GSPC),k=1:5)   

update

I would calculate the computed result with the successive difference. The output format of Delt() is same as I expected, but the result is the percentage change with lagged. I would calculate the difference with lagged. Is there any function, same parameters as Delt() but calculate the difference change.

$\endgroup$

closed as off-topic by Bob Jansen Apr 9 '16 at 16:45

  • This question does not appear to be about quantitative finance within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ did you have a look at the function "lag" ? Furthermore this is at most a question for stackoverflow ... $\endgroup$ – Richard Apr 9 '16 at 16:16
  • $\begingroup$ That, or implement shift or get it from data.table. You can do a few things but I agree it doesn't fit here. On StackOverflow it would be a duplicate, so closing. $\endgroup$ – Bob Jansen Apr 9 '16 at 16:45
  • 1
    $\begingroup$ I'm voting to close this question as off-topic because belongs on StackOverflow where it would be a duplicate $\endgroup$ – Bob Jansen Apr 9 '16 at 16:45
1
$\begingroup$

Look at the function "lag" and if you want a lag function that does not depend on some time series structure of the object then you can use this one:

shift<-function(x,shift_by){
  stopifnot(is.numeric(shift_by))
  stopifnot(is.numeric(x))

  if (length(shift_by)>1)
    return(sapply(shift_by,shift, x=x))

  out<-NULL
  abs_shift_by=abs(shift_by)
  if (shift_by > 0 )
    out<-c(tail(x,-abs_shift_by),rep(NA,abs_shift_by))
  else if (shift_by < 0 )
    out<-c(rep(NA,abs_shift_by), head(x,-abs_shift_by))
  else
    out<-x
  out
}

I prefer it in the meanwhile. It was taken from here.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.