Hello world, I am trying to create a matrix of lagged time series with the following code fragment (I know that I can use acf-function...). I want to set the variable/column name to something like "lag 1" etc. If I try to do it I get text like "lagtest.lagtest.lagtest.lag 1" where does this come from? After a conversion to a data.frame it works as expected. What did I miss? Thanks and greetings, Georg ================= > lagtest <- as.ts(seq(1:100)) ; > for (i in 1:4) {lagtest <- cbind(lagtest,lag(ts_test,i)); colnames(lagtest)[i+1]<- paste("lag",i)} > colnames(lagtest)[1]<-"H_GW"; > colnames(lagtest) [1] "H_GW" "lagtest.lagtest.lagtest.lag 1" "lagtest.lagtest.lag 2" [4] "lagtest.lag 3" "lag 4" # this works... > t5 <- as.data.frame(lagtest) > for (i in 1:4) {names(t5)[i+1] <- paste("var",i) } > names(t5) [1] "H_GW" "var 1" "var 2" "var 3" "var 4" >
Georg Hoermann <georg.hoermann <at> gmx.de> writes: : : Hello world, : : I am trying to create a matrix of lagged time series with the : following code fragment (I know that I can use acf-function...). : I want to set the variable/column name to something like : "lag 1" etc. If I try to do it I get text like : "lagtest.lagtest.lagtest.lag 1" where does this come from? : : After a conversion to a data.frame it works as expected. : What did I miss? : : Thanks and greetings, : : Georg : =================: : > lagtest <- as.ts(seq(1:100)) ; : > for (i in 1:4) {lagtest <- cbind(lagtest,lag(ts_test,i)); : colnames(lagtest)[i+1]<- paste("lag",i)} : > colnames(lagtest)[1]<-"H_GW"; : > colnames(lagtest) : [1] "H_GW" "lagtest.lagtest.lagtest.lag 1" : "lagtest.lagtest.lag 2" : [4] "lagtest.lag 3" "lag 4" : : # this works... : : > t5 <- as.data.frame(lagtest) : > for (i in 1:4) {names(t5)[i+1] <- paste("var",i) } : > names(t5) : [1] "H_GW" "var 1" "var 2" "var 3" "var 4" Here is something you could try: # define lags and their names lags <- 0:4 names(lags) <- c("G_HW", paste("lag", 1:4)) # build mts do.call("cbind", lapply(lags, lag, x = lagtest))
Miguel A. Arranz wrote:> You probably want to have a look at help(embed). It might be all you need. >embed wraps around (no NAs at begin and end of the dataset), this makes no sense in hydrology. Gruss Georg -- Georg Hoermann, Dep. of Hydrology, Ecology, Kiel University, Germany Tel. 0431-880-1207, Home: 0451/477032, 0172/4315715, Penguin #189476
You probably want to have a look at help(embed). It might be all you need. On Friday 25 February 2005 12:49, Georg Hoermann wrote:> Hello world, > > I am trying to create a matrix of lagged time series with the > following code fragment (I know that I can use acf-function...). > I want to set the variable/column name to something like > "lag 1" etc. If I try to do it I get text like > "lagtest.lagtest.lagtest.lag 1" where does this come from? > > After a conversion to a data.frame it works as expected. > What did I miss? > > Thanks and greetings, > > Georg > =================> > > lagtest <- as.ts(seq(1:100)) ; > > for (i in 1:4) {lagtest <- cbind(lagtest,lag(ts_test,i)); > > colnames(lagtest)[i+1]<- paste("lag",i)} > > > colnames(lagtest)[1]<-"H_GW"; > > colnames(lagtest) > > [1] "H_GW" "lagtest.lagtest.lagtest.lag 1" > "lagtest.lagtest.lag 2" > [4] "lagtest.lag 3" "lag 4" > > > # this works... > > > t5 <- as.data.frame(lagtest) > > for (i in 1:4) {names(t5)[i+1] <- paste("var",i) } > > names(t5) > > [1] "H_GW" "var 1" "var 2" "var 3" "var 4" > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
Gabor Grothendieck wrote:> > Here is something you could try: > > # define lags and their names > lags <- 0:4 > names(lags) <- c("G_HW", paste("lag", 1:4)) > > # build mts > do.call("cbind", lapply(lags, lag, x = lagtest)) >thank you for the solution, I will try to understand it during the weekend 8-) Now I tried to change the lag from the default value of 1 to -1, but I apparently missed something: > do.call("cbind", lapply(lags, function(x) lag(x,-1), x = lagtest)) Error in FUN(X[[1]], ...) : unused argument(s) ( ...) where is the unused argument? Thanks, Georg -- Georg Hoermann, Dep. of Hydrology, Ecology, Kiel University, Germany Tel. 0431-880-1207, Home: 0451/477032, 0172/4315715, Penguin #189476
From: Georg Hoermann <georg.hoermann at gmx.de>> Gabor Grothendieck wrote: > > > > Here is something you could try: > > > > # define lags and their names > > lags <- 0:4 > > names(lags) <- c("G_HW", paste("lag", 1:4)) > > > > # build mts > > do.call("cbind", lapply(lags, lag, x = lagtest)) > > thank you for the solution, > I will try to understand it during the weekend 8-) > Now I tried to change the lag from the default value of 1 to -1, > but I apparently missed something: > > > do.call("cbind", lapply(lags, function(x) lag(x,-1), x = lagtest)) > Error in FUN(X[[1]], ...) : unused argument(s) ( ...) > > where is the unused argument? >The unused argument is: x = lagtest I think what you meant is: do.call("cbind", lapply(lags, function(k) lag(lagtest,-k))) or equivalently: do.call("cbind", lapply(-lags, function(k) lag(lagtest,k))) or more succintly: do.call("cbind", lapply(-lags, lag, x = lagtest)) The way it works is that lapply calls lags(x,k) repeatedly always using the value of x = lagtest for the first argument. Normally lapply repeatedly sets the first argument but since we have already done that by specifying x=lagtest, lapply uses the next argument k thereby setting k successively to each value of the lag.