Hi, I'm using R to do a time series analysis. In the model, I use the lags of some variables. such the lags of the variables have different length, I just can't use them directly in the lm function. Intuitively, I feel that "subset" might be useful, but I do not know how to use it. Anyone can give me an example syntax? Thanks. Harry [[alternative HTML version deleted]]
Like this?
x<-1:100
y<-runif(100)
df<-data.frame(cbind(x,y))
mycoef.list<-NULL
for (i in 10:65)
{
mycoef<-coef(lm(y~x, data=subset(df, x %in% 1:i)))[2]
mycoef.list <-c(mycoef.list, mycoef)
}
hist(mycoef.list)
regards
milton
brazil=toronto
On Mon, Jun 29, 2009 at 2:50 PM, Hongwei Dong <pdxdong@gmail.com> wrote:
> Hi, I'm using R to do a time series analysis. In the model, I use the
lags
> of some variables. such the lags of the variables have different length, I
> just can't use them directly in the lm function. Intuitively, I feel
that
> "subset" might be useful, but I do not know how to use it. Anyone
can give
> me an example syntax? Thanks.
> Harry
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
The dyn package (and also the dynlm package) can be used for this. library(dyn) y <- ts(1:10) dyn$lm(y ~ lag(y, -1)) On Mon, Jun 29, 2009 at 2:50 PM, Hongwei Dong<pdxdong at gmail.com> wrote:> Hi, I'm using R to do a time series analysis. In the model, I use the lags > of some variables. such the lags of the variables have different length, I > just can't use them directly in the lm function. Intuitively, I feel that > "subset" might be useful, but I do not know how to use it. Anyone can give > me an example syntax? Thanks. > ?Harry > > ? ? ? ?[[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. >