M.M
M.M

Reputation: 41

How do I simultaneously extract the F and Pvalue from this object from the grangertest() function?

Model 1: DLPFc ~ Lags(DLPFc, 1:1) + Lags(IPs, 1:1)
Model 2: DLPFc ~ Lags(DLPFc, 1:1)
  Res.Df Df      F    Pr(>F)    
1   4597                        
2   4598 -1 88.522 < 2.2e-16 ***

Upvotes: 1

Views: 269

Answers (1)

markus
markus

Reputation: 26343

library(lmtest)
data(ChickEgg)
(out <- grangertest(egg ~ chicken, order = 3, data = ChickEgg))
#Granger causality test

#Model 1: egg ~ Lags(egg, 1:3) + Lags(chicken, 1:3)
#Model 2: egg ~ Lags(egg, 1:3)
#  Res.Df Df      F Pr(>F)
#1     44                 
#2     47 -3 0.5916 0.6238

Extract F and p-values

out[, c("F", "Pr(>F)")]
#       F Pr(>F)
#1              
#2 0.5916 0.6238

If you want to exclude the empty first row, do out[-1, c("F", "Pr(>F)")]


If we don't want a new object and get the values right away we can do

unlist(grangertest(egg ~ chicken, order = 3, data = ChickEgg)[-1, c("F", "Pr(>F)")])
#        F    Pr(>F) 
#0.5916153 0.6237862

Which returns a named vector.

Upvotes: 3

Related Questions