简体   繁体   中英

How to create a new column with values from another column with index-1

My ultimate goal is to use log return data to forecast future data. I try to achieve this by first converting log return data to log data, and then exp(log data). But I have trouble converting log return data to log data.

log = log return + logy(t) --> I have a log return data column, and I am trying to create a new column (logy(t)) that takes the values of log data with index = index-1.

For example, if log data is 2,3,4,5,6 then logy(t) should be intercept,2,3,4,5.

Here's how I approach it:
name$logyt<-name[row-1]["logdata"]
logyt<-as.numeric(unlist(logyt))
forecast<-predict(model3,newdata=name[205:221,])
model3forecast.ts<-ts(exp(forecast+logyt))

model 3 is a time series, and model3forecast.ts should be a time series forecasted based on historical log return data. I am now stuck at creating column logy(t).

Thank you very much!

One tentative solution with dplyr :

> library(magrittr)
> library(dplyr)
>
> dat <- data.frame(x = 1:6)
>
> dat1 <- dat %>%
+  mutate(x1 = lag(x))
>
> dat1
  x x1
1 1 NA
2 2  1
3 3  2
4 4  3
5 5  4
6 6  5

Is this what you are looking for?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM