Hello all, now I'm trying to switch from Excel to R to deal with the data, and as a newbie i got the problem as follows. suppose I have a data named "test" test<- data.frame(year=c(1996:2011), Y=c(74163.6,81658.5,86531.6,91125.0,98749.0,109028.0,120475.6,136613.4,160956.6,187423.5,222712.5,266599.2,315974.6,348775.1,402816.5,465731.3)) in which Y means the GDP of a country If i want to get Delta Y = Y(t)-Y(t-1) , i could use diff() in R diff(test$Y) but what if i want to get gY=(Y(t)-Y(t-1))/Y(t-1)? seems diff(test$Y)/(test$Y)[-1] doesnt work ... thanks in advance [[alternative HTML version deleted]]
R. Michael Weylandt
2013-Jun-19 11:24 UTC
[R] how to get growth rate of a (time series) data?
On Wed, Jun 19, 2013 at 12:04 PM, Yanyuan Zhu <yyz at tongji.edu.cn> wrote:> Hello all, now I'm trying to switch from Excel to R to deal with the data, > and as a newbie i got the problem as follows. > > suppose I have a data named "test" > test<- data.frame(year=c(1996:2011), > Y=c(74163.6,81658.5,86531.6,91125.0,98749.0,109028.0,120475.6,136613.4,160956.6,187423.5,222712.5,266599.2,315974.6,348775.1,402816.5,465731.3)) > in which Y means the GDP of a country > > If i want to get Delta Y = Y(t)-Y(t-1) , i could use diff() in R > diff(test$Y) > > but what if i want to get gY=(Y(t)-Y(t-1))/Y(t-1)? > seems diff(test$Y)/(test$Y)[-1] doesnt work ...Odd, I would have thought it did. No matter anyways: if you are using time series data, I'd strongly recommend that you place your data in an object of the "xts" class and use all of the time series functionality available for those objects. Notably, you'll also want to load the "TTR" package (all of these are available through the install.packages() function off the CRAN mirror system) and to use its ROC function. Cheers, MW> > thanks in advance > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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.
diff(test$Y)/(test$Y)[-1] calculates (Y(t)-Y(t-1))/Y(t). To get (Y(t)-Y(t-1))/Y(t-1) instead, use diff(test$Y)/(test$Y)[-length(test$Y)] or better diff(test[,"Y"])/test[-nrow(test), "Y"] -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Yanyuan Zhu Sent: Mittwoch, 19. Juni 2013 13:04 To: r-help at r-project.org Subject: [R] how to get growth rate of a (time series) data? Hello all, now I'm trying to switch from Excel to R to deal with the data, and as a newbie i got the problem as follows. suppose I have a data named "test" test<- data.frame(year=c(1996:2011), Y=c(74163.6,81658.5,86531.6,91125.0,98749.0,109028.0,120475.6,136613.4,1 60956.6,187423.5,222712.5,266599.2,315974.6,348775.1,402816.5,465731.3)) in which Y means the GDP of a country If i want to get Delta Y = Y(t)-Y(t-1) , i could use diff() in R diff(test$Y) but what if i want to get gY=(Y(t)-Y(t-1))/Y(t-1)? seems diff(test$Y)/(test$Y)[-1] doesnt work ... thanks in advance [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list 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.
Gabor Grothendieck
2013-Jun-19 12:11 UTC
[R] how to get growth rate of a (time series) data?
On Wed, Jun 19, 2013 at 7:04 AM, Yanyuan Zhu <yyz at tongji.edu.cn> wrote:> Hello all, now I'm trying to switch from Excel to R to deal with the data, > and as a newbie i got the problem as follows. > > suppose I have a data named "test" > test<- data.frame(year=c(1996:2011), > Y=c(74163.6,81658.5,86531.6,91125.0,98749.0,109028.0,120475.6,136613.4,160956.6,187423.5,222712.5,266599.2,315974.6,348775.1,402816.5,465731.3)) > in which Y means the GDP of a country > > If i want to get Delta Y = Y(t)-Y(t-1) , i could use diff() in R > diff(test$Y) > > but what if i want to get gY=(Y(t)-Y(t-1))/Y(t-1)? > seems diff(test$Y)/(test$Y)[-1] doesnt work ...As already mentioned, that R expression gives (Y[t] - Y[t-1]) / Yt[t] whereas you want Y <- test$Y n <- length(Y) diff(Y) / Y[-n] or you might want to use a time series class for simpler manipluations: Using ts class: Y.ts <- ts(test[, 2], start = test[1,1]) Ydiff.ts <- diff(Y.ts) / lag(Y.ts, - 1) or, using tis class which is frequently used for equally spaced eoconomic series: Y.tis <- tis(test[, 2], start = test[1,1], freq = 1) Ydiff.tis <- diff(Y.tis) / lag(Y.tis, - 1) or using zoo class which, in addition, supports non-equally spaced series and also allows for two different approaches here: library(zoo) Y.z <- read.zoo(test, FUN = identity) Ydiff.z <- diff(Y.z) / lag(Y.z, - 1) This also works with zoo: Ydiff.z <- diff(Y.z, arith = FALSE) - 1 -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com