I have a dataset like so:
set.seed(242)
df<- data.frame(month=order(seq(1,20,1),decreasing=TRUE),
psit=sample(1:100,20,replace=TRUE), var=sample(1:10,20,
replace=TRUE))
I wish to do a crude time lag analysis to see how lagged var
data affects psit
data. A lag, as defined in this crude analysis, is var
data T-1, T-2, T-3, etc. months in the past from each psit
data point.
To see how the prior months var
data affects psit
data, I wish to make a timelag
vector which consists of var
data that is one month offset from the psit
variable. Then I'll cbind the timelag
vector to the psit
vector. Here is are examples of the dataframes for a 1 month offset,2 month offset,3 month offset, respectivley:
set.seed(242)
timelag1<- cbind(df[1:12,2], df[2:13,3]) #1 month time lag
timelag2<- cbind(df[1:12,2], df[3:14,3]) #2 month time lag
timelag3<- cbind(df[1:12,2], df[4:15,3]) #3 month time lag
For each dataframe, I want to regress var
against psit
data using the lm()
function and output the R-squared value. This process would be repeated for each subsequent offset. Example below:
model1<-lm(timelag1)
summary(model1)$r.squared
model2<-lm(timelag2)
summary(model2)$r.squared
model3<-lm(timelag3)
summary(model3)$r.squared
I would like to create a loop that iterates this process for a large dataset of 240 months. Then runs an lm()
on each dataframe and then output the r-squared value.