similar to: lag a data.frame column?

Displaying 20 results from an estimated 40000 matches similar to: "lag a data.frame column?"

2011 Aug 04
2
Efficient way of creating a shifted (lagged) variable?
Hello! I have a data set: set.seed(123) y<-data.frame(week=seq(as.Date("2010-01-03"), as.Date("2011-01-31"),by="week")) y$var1<-c(1,2,3,round(rnorm(54),1)) y$var2<-c(10,20,30,round(rnorm(54),1)) # All I need is to create lagged variables for var1 and var2. I looked around a bit and found several ways of doing it. They all seem quite complicated - while in
2004 Oct 29
2
lag variable addition to data frame question
Hi, I was wondering if there is a more efficient way of handling the following method of creating a lagged value in a data frame without using the recursive 'for(i in 1:n)' loop and without using as.ts #Steps to creating a lag variable in a data frame 'my.dat.fr' # with 275 columns, 2400 rows of numbers and factors . The #variable x is a factor of #with five different levels the
2005 Mar 05
4
How to use "lag"?
Is it possible to fit a lagged regression, "y[t]=b0+b1*x[t-1]+e", using the function "lag"? If so, how? If not, of what use is the function "lag"? I get the same answer from y~x as y~lag(x), whether using lm or arima. I found it using y~c(NA, x[-length(x)])). Consider the following: > set.seed(1) > x <- rep(c(rep(0, 4), 9), len=9) > y <-
2007 Jul 12
2
lead
Hi, is there any function in R that shifts elements of a vector to the opposite direction of what Lag() of the Hmisc package does? (something like, Lag(x, shift = -1) ) Thanks Zava -------------------------------------------------------- This is not an offer (or solicitation of an offer) to buy/se...{{dropped}}
2002 Feb 12
6
A couple of little R things I can't figure out (column percents, regression with lagged variables)
Simple usage questions that I ought to be able to figure on my own, but can't. 1.I'm able to produce a cross tabulation table showing counts with either table or xtabs. But I want column percentages for interpretation, and it seems stupid to sit there with a calculator figuring marginals and column percentages. How to make R do it after this: > x <- c(1,3,1,3,1,3,1,3,4,4)
2008 Jan 31
1
Feature request: about lag(), which.min() and cat().
Hello I'm only user of R and have many little knowledge in programming but I permit to send you some whishes/suggestions for R. which.min like which(), which.min() should also include an argument arr.ind. Note that one can have it with which(a==min(a), arr.ind=TRUE) but if there is a reason to build a special function which.min, why not add also this nice argument? lag() If one wants to
2014 Apr 19
1
lag() not returning a time series object
Dear all, Before I file this as a bug, I wanted to check if I didn't miss something. The help page of lag() says that the function returns a time series object. It actually does return something that looks like a ts object (the attribute tsp is set). But when using a vector, the class "ts" is not added to the result: > avec <- 1:10 > lag(avec) [1] 1 2 3 4 5 6 7 8
2000 Sep 20
1
lag() and lm()
Hi, I am using R interactively for estimation and even data manipulation. I want to use lag() function within lm() function in a way looks like: mymodel<- lm(y~x+I(lag(y,-1)-1, data=anydata) What I get is always perfect fit; (that is, coefficient=1) which is not true. If the model looks like mymodel<- lm(y~z+x+I(lag(x,-1)-1, data=anydata) summary returns only one coefficient for x and
2010 Jul 21
1
The opposite of "lag"
Hello! I have a data frame A (below) with a grouping factor (group). I take my DV and create the new, lagged DV by applying the function lag.it (below). It works fine. A <- data.frame(year=rep(c(1980:1984),3), group= factor(sort(rep(1:3,5))), DV=c(rnorm(15))) lag.it <- function(x) { DV <- ts(x$DV, start = x$year[1]) idx <- seq(length = length(DV)) DVs <- cbind(DV, lag(DV,
2006 Mar 02
1
CCF and Lag questions
I am new to R and new to time series modeling. I have a set of variables (var1, var2, var3, var4, var5) for which I have historical yearly data. I am trying to use this data to produce a prediction of var1, 3 years into the future. I have a few basic questions: 1) I am able to read in my data, and convert it to a time series format using 'ts.' data_ts <- ts(data, start = 1988, end =
2012 Apr 12
4
Definition of "lag" is opposite in ts and xts objects!
Example: Will ts objects be obsolete or modified? > a [,1] 1983 Q1 2.747365190 1983 Q2 2.791594762 1983 Q3 -0.009953715 1983 Q4 -0.015059485 1984 Q1 -1.190061246 1984 Q2 -0.553031799 1984 Q3 0.686874720 1984 Q4 0.953911035> lag(a,4) [,1] 1983 Q1 NA 1983 Q2 NA 1983 Q3 NA 1983 Q4 NA 1984 Q1 2.747365190 1984 Q2
2008 Oct 10
1
lag function doesn't work - what am i doing wrong?
I am trying to lag a time series. My data is in a matrix, but I coerce it into a ts object. But when I lag it and then look at the result, nothing has changed. What am I doing wrong????? residsq<-resid^2 residsq<-as.ts(residsq) lag1residsq<-lag(residsq,-1) > residsq[1:5] 1 2 3 4 5 87.759 329882.188 5325849.333 31512.334
2005 May 10
1
Aggregate lag
hello, Does anybody know how to aggregate a lag series ? when I try to use aggregate I get the following message > try<-ts(1:100,start=c(1985,1),freq=12) > aggregate(try,4,mean,na.rm=T) Qtr1 Qtr2 Qtr3 Qtr4 1985 2 5 8 11 1986 14 17 20 23 1987 26 29 32 35 1988 38 41 44 47 1989 50 53 56 59 1990 62 65 68 71 1991 74 77 80 83 1992
2010 Jan 28
2
Error on using lag function
Hello everyone, I have a vector P and I want to replace each of its missing values by its next element, for example: P[i] = NA --> P[i] = P[i+1] To do this I am using the replace() and lag() functions like this: P <- replace(as.ts(P),is.na(as.ts(P)),as.ts(lag(P,1))) but here is the error that I get: Warning message: In NextMethod("[<-") : number of items to replace is not a
2005 Apr 28
4
wine-20050419 - where to set audio driver type?
Hi, In a completely clean account that's never run Wine before I run wine --version. Wine installs a new .wine directory but this Wine directory has no config file. I looked in the 3 obvious files but none of them seem to have an option for changing between OSS, Alsa and JAck. where do I make this setting change now? Thanks, Mark
2007 Mar 18
2
Lag operator in R does not work
Hi - I'm quite wondering what makes the lag operator does not work for my time series. I have a time series of length about 200000 elements. I would like to have a lag 1 of this time series. I did the following: logprice = log(price, base=exp(1)) # this is my log price which is a vector of price time series of length 200000 ts_logprice = as.ts(logprice, frequency=1) # convert to time series
2012 Jan 10
1
Lag() and lag()
lag is a very confusing function i try to plot lag(x,-h) and y , the results are the same figures for all the h lags. then i saw online people use lag(x,-h) as xreg in arima that won't work in my eyes if lag function is consistent, isnce it returns the same value for all the lags i don't know in which occasion people will use this function besides I suppose Lag(x,h) is the same as
2004 May 11
2
lags and differences
Dear all, could someone please clarify me if this works, so as to implement lags and differences for example in y and in a independent x1 of a regression? model<-lm((diff(y), -i)~x1+lag(x1,-1), data=anydata) Thanks, a lot, Alexandre. ---------- In??cio da mensagem original ----------- De: r-help-bounces at stat.math.ethz.ch Para: r-help at stat.math.ethz.ch Cc:
2011 Apr 05
2
Time series example in Koop
I am trying to reproduce the output of a time series example in Koop's book "Analysis of Financial Data". Koop does the example in Excel and I used the ts function followed by the lm function. I am unable to get the exact coefficients that Koop gives - my coefficients are slightly different. After loading the data file and attaching the frame, my code reads: > y = ts(m.cap) >
2000 Mar 17
4
lag (PR#491)
Full_Name: Hannu Kahra Version: 1.0.0 OS: Windows Submission from: (NULL) (153.1.50.127) x1 <- lag(x, k=1) returns the original series x. The time series is not shiftet at all. Only the time is shiftet: for x1 new start = old start -1 and new end = old end -1. When I run, e.g. lm(x~x1) the result is not what I expect. Both x and x1 are time series. Is this a bug or am I forgetting something?