使用lapply和两个参数运行滞后回归 [英] Running lagged regressions with lapply and two arguments
问题描述
我正在运行多个单变量回归,例如在此可重现的示例中:
require(dynlm)
data(USeconomic)
US<-USeconomic
vars<-colnames(US)[-2]
a<-lapply(colnames(US),function(x) dynlm(log(GNP)~get(x),data=US))
a
包含3个单变量回归的列表.现在假设我要进行3次滞后进行相同的回归:l<-c(0,1,4)
其中0当然是我已经得到的情况.有没有办法像
lagged_vars <- paste("L(",rep(vars,each=3),",",l,")",sep="")
那样直接使用向量l
# this did not work for me, I obtain multivariate regressions including all lags at once
lapply(colnames(US),function(x) dynlm(log(GNP)~L(get(x),l),data=US),l=l)
此操作无效后,我尝试了另一种方法并将其添加到以下向量:
lagged_vars <- paste("L(",rep(vars,each=3),",",l,")",sep="")
获取:
[1] "L(log(M1),0)" "L(log(M1),1)" "L(log(M1),4)" "L(rs,0)" "L(rs,1)"
[6] "L(rs,4)" "L(rl,0)" "L(rl,1)" "L(rl,4)"
不幸的是,我不能使用新的字符向量运行它,get()不能帮助您.我不明白为什么会导致它与vars
一起使用,而不能同时与lagged_vars
一起使用,这两个都是字符向量.
请注意,L()
语法来自dynlm软件包.
附带问题:如果我只是打印回归结果中的系数,将其标记为get(x)–我该如何更改?
可能会出现i,j循环,但我宁愿使用lapply或该系列产品中的某些东西...
as.formula
与dynlm
中的L()
不能一起使用.我收到此错误消息:
merge.zoo(log(GNP),L(log(M1),0),retclass ="list",all = FALSE)中的错误: 找不到功能"L"
找到了有趣的帖子bei Achim Zeileis 提到了这个问题.
以下是使用plyr
library(plyr); library(dynlm); library(tseries)
# FUNCTION TO RUN A SINGLE REGRESSION
foo = function(x, l) dynlm(log(GNP) ~ L(get(as.character(x)), l), data = US)
# CREATE PARAMETER GRID
params = expand.grid(x = colnames(US)[-2], l = c(0, 1, 4))
# RUN REGRESSIONS
regressions = mlply(params, foo)
此列表的每个元素均包含有关单个回归的详细信息,您可以从中提取所需的输出
I am running multiple univariate regressions, like in this reproducible example:
require(dynlm)
data(USeconomic)
US<-USeconomic
vars<-colnames(US)[-2]
a<-lapply(colnames(US),function(x) dynlm(log(GNP)~get(x),data=US))
a
contains a list of 3 univariate regressions. Assume now I´d like to run the same regressions with 3 lags: l<-c(0,1,4)
where 0 is of course the case I already got. Is there a way to use the vector l
directly, like
# this did not work for me, I obtain multivariate regressions including all lags at once
lapply(colnames(US),function(x) dynlm(log(GNP)~L(get(x),l),data=US),l=l)
After this did not work I tried another approach and added to following vector:
lagged_vars <- paste("L(",rep(vars,each=3),",",l,")",sep="")
to get:
[1] "L(log(M1),0)" "L(log(M1),1)" "L(log(M1),4)" "L(rs,0)" "L(rs,1)"
[6] "L(rs,4)" "L(rl,0)" "L(rl,1)" "L(rl,4)"
Unfortunately, I can't run it with the new character vector, get() does not help. I can't understand why cause it works with vars
but not lagged_vars
which are both character vectors.
Note, that the L()
syntax comes from the dynlm package.
Side question: If I just print a the coefficients from the regression result remain labelled get(x) – how can I change that?
An i,j loop could be possible solution but I´d rather use lapply or something out of this family...
EDIT:
as.formula
does not work together with L()
from dynlm
. I get the this error message:
Error in merge.zoo(log(GNP), L(log(M1), 0), retclass = "list", all = FALSE) : could not find function "L"
EDIT: found an interesting post bei Achim Zeileis referring to this problem.
Here is an approach using plyr
library(plyr); library(dynlm); library(tseries)
# FUNCTION TO RUN A SINGLE REGRESSION
foo = function(x, l) dynlm(log(GNP) ~ L(get(as.character(x)), l), data = US)
# CREATE PARAMETER GRID
params = expand.grid(x = colnames(US)[-2], l = c(0, 1, 4))
# RUN REGRESSIONS
regressions = mlply(params, foo)
Each element of this list contains details on a single regression from which you can extract your desired output
这篇关于使用lapply和两个参数运行滞后回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!