Running lagged regressions with lapply and two arguments

前端 未结 2 1842
渐次进展
渐次进展 2021-01-25 00:12

I am running multiple univariate regressions, like in this reproducible example:

require(dynlm)
data(USeconomic)
US<-USeconomic
vars<-colnames(US)[-2]
a<         


        
相关标签:
2条回答
  • 2021-01-25 00:26

    To construct R formula, you must paste it all together, not just the predictor side of it. So you need something like:

    formula <- as.formula(
        paste("log(GNP)~",
            paste("L(",rep(vars,each=3),",",l,")",sep=""),
            sep = ""
        )
    )
    

    and then run

    dynlm(formula, data = ...)
    
    0 讨论(0)
  • 2021-01-25 00:32

    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

    0 讨论(0)
提交回复
热议问题