For循环在数据帧和池摘要统计的多个子集上执行时间序列

For循环在数据帧和池摘要统计的多个子集上执行时间序列,r,for-loop,time-series,linear-regression,R,For Loop,Time Series,Linear Regression,我有一个数据框架df,由40个插补数据集imp组成,每个数据集有228个月month的寄生蜂频率psit和气候变化数据var set.seed(560) df<-data.frame(imp= rep(1:40, each=228), month=rep(1:228), psit= rep(rnorm(228, 20, 10)), var=rnorm(9120, 50, 10)) ……在剩下的213个月里也是如此。因此,将对每个imp子集进行多个时滞分析。然后,我将对所有插补数据集重复

我有一个数据框架
df
,由40个插补数据集
imp
组成,每个数据集有228个月
month
的寄生蜂频率
psit
和气候变化数据
var

set.seed(560)
df<-data.frame(imp= rep(1:40, each=228), month=rep(1:228), psit= 
rep(rnorm(228, 20, 10)), var=rnorm(9120, 50, 10))
……在剩下的213个月里也是如此。因此,将对每个
imp
子集进行多个时滞分析。然后,我将对所有插补数据集重复这一点。从每个时间差,我想计算r平方值。如果对所有40个插补数据集都这样做,则timelag 1的r平方值为40,timelag 2的r平方值为40,timelag 3的r平方值为40,依此类推。所以我想把这些r平方值放在一起。因此,在循环结束时,对于每个时间滞后,我应该有一个集合r平方值的向量

例如:

 model_for_timelag1<-lm(psit ~ lag(var, -1), tail(df, 12+1) #lm for 
  timelag1
 summary(model_for_timelag1)$r.squared
 #Repeat model 1 for the 39 remaining imputed datasets

 model_for_timelag2<-lm(psit ~ lag(var, -2), tail(df, 12+2)  #lm for 
 timelag2
 summary(model_for_timelag2)$r.squared
 #Repeat model 2 for the 39 remaining imputed datasets

 model_for_timelag3<-lm(psit ~ lag(var, -3), tail(df, 12+3)  #lm for 
 timelag3
 summary(model_for_timelag3)$r.squared 
 #Repeat model 3 for the 39 remaining imputed datasets

 pool.r.squared(all_model1) #pool all 40(all imputed datasets)model1s
 pool.r.squared(all_model2) #pool all 40(all imputed datasets)model2s
 pool.r.squared(all_model3) #pool all 40(all imputed datasets)model3s
model\u for\u timelag1可能有用:可能有用:
 model_for_timelag1<-lm(psit ~ lag(var, -1), tail(df, 12+1) #lm for 
  timelag1
 summary(model_for_timelag1)$r.squared
 #Repeat model 1 for the 39 remaining imputed datasets

 model_for_timelag2<-lm(psit ~ lag(var, -2), tail(df, 12+2)  #lm for 
 timelag2
 summary(model_for_timelag2)$r.squared
 #Repeat model 2 for the 39 remaining imputed datasets

 model_for_timelag3<-lm(psit ~ lag(var, -3), tail(df, 12+3)  #lm for 
 timelag3
 summary(model_for_timelag3)$r.squared 
 #Repeat model 3 for the 39 remaining imputed datasets

 pool.r.squared(all_model1) #pool all 40(all imputed datasets)model1s
 pool.r.squared(all_model2) #pool all 40(all imputed datasets)model2s
 pool.r.squared(all_model3) #pool all 40(all imputed datasets)model3s
 rsquared<-NA

 for (i in length(df$imp)) {
 model[i]<- lm(psit ~ lag(var, var[[-i]]), tail(df, 12+var[[i]])
 rsquared[i]<- summary$r.squared
 pool.r.squared()
    }