[R] Collecting output of regressions in an intelligent way

Adams, Jean jvadams at usgs.gov
Thu Jun 25 14:51:13 CEST 2015


Francesca,

You don't provide any example data, so the code that I am provided is
untested, but here is one way to put your commands into a loop.


# define range of p values you want to use
ps <- 6:10
# create an empty list to collect the results
waldchi <- vector("list", length(ps))

# loop through the p values
for(i in seq(ps)) {
  v.p <- VAR(cbind(index1, ma_fin), p=ps[i], type="both")
  print(summary(v.p)
  wald_finp.1 <- wald.test(
    b=coef(V.p$varresult[[1]]),
    Sigma=vcov(V.p$varresult[[1]]),
    Terms=seq(from=2, by=2, length=ps[i]-1))
  waldchi[[i]] <- wald_finp.1$result$chi2
}

# combine the results
wald_fin <- do.call(rbind, waldchi)

Jean


On Thu, Jun 25, 2015 at 4:26 AM, Francesca <francesca.pancotto at gmail.com>
wrote:

> Dear R Contributors
> I am asking for some suggestions on how to organize output of a series of
> regressions and tests in an intelligent way.
> I estimate a series of Var models with increasing numbers of lags and the
> perform a Wald test to control Granger Causality: I would like to learn a
> way to do it that allows me not to produce copy and past code.
>
> This is what I do:
> Estimate var models with increasing number of lags,
>
> V.6<-VAR(cbind(index1,ma_fin),p=6,type="both")
> V.7<-VAR(cbind(index1,ma_fin),p=7,type="both")
> V.8<-VAR(cbind(index1,ma_fin),p=8,type="both")
> V.9<-VAR(cbind(index1,ma_fin),p=9,type="both")
>
> then observe results and control significance of regressors:
>
> summary(V.6)
> summary(V.7)
> summary(V.8)
> summary(V.9)
> summary(V.10)
>
> then use the estimated var to perform the test:
>
> wald_fin7.1<-wald.test(b=coef(V.7$varresult[[1]]),
> Sigma=vcov(V.7$varresult[[1]]), Terms=c(2,4,6,8,10,12))
> wald_fin8.1<-wald.test(b=coef(V.8$varresult[[1]]),
> Sigma=vcov(V.8$varresult[[1]]), Terms=c(2,4,6,8,10,12,14))
> wald_fin9.1<-wald.test(b=coef(V.9$varresult[[1]]),
> Sigma=vcov(V.9$varresult[[1]]), Terms=c(2,4,6,8,10,12,14,16))
> wald_fin10.1<-wald.test(b=coef(V.10$varresult[[1]]),
> Sigma=vcov(V.10$varresult[[1]]), Terms=c(2,4,6,8,10,12,14,16,18))
>
> #then collect tests result in a table:
>
> wald_fin<-rbind(wald_fin7.1$result$chi2,
>                 wald_fin12.1$result$chi2,wald_fin21.1$result$chi2,
>                 wald_fin7.2$result$chi2,
>                 wald_fin12.2$result$chi2,wald_fin21.2$result$chi2)
>
>
> My idea is that it is possible to create all this variable with a loop
> across the objects names but it is a level of coding much higher than my
> personal knowledge and ability.
>
> I hope anyone can help
>
> Thanks in advance
>
>
> --
>
> Francesca
>
> ----------------------------------
> Francesca Pancotto, PhD
> Università di Modena e Reggio Emilia
> Viale A. Allegri, 9
> 40121 Reggio Emilia
> Office: +39 0522 523264
> Web: https://sites.google.com/site/francescapancotto/
> ----------------------------------
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list