尝试运行格兰杰因果关系检验

huangapple go评论71阅读模式
英文:

Trying to run a Granger Causality test

问题

我有我的数据集,看起来像这样

我然后使用我的 R 代码

我收到错误 Error in .subset(x, j) : invalid subscript type 'list'

我尝试将其转换为向量,但它告诉我年份或国家不存在

我期望它只是运行回归

结构(列表)包含年份和国家数据

利率和通胀的数据

英文:

I have my dataset, it looks something like this

尝试运行格兰杰因果关系检验

I then use my r code

install.packages("vars")
library(vars)
install.packages("plm")
library(plm)
install.packages("panelvar")
library(panelvar)
data <- read.csv("economistdata.csv")
data$year <- as.integer(data$year)
pdata <- pdata.frame(data, index=c("country", "year"), drop.index = TRUE)
summary(pdata)
var_model <- pvargmm(cbind(interest.rates, inflation) ~ 1, data=pdata, lags=2)
granger_test <- causality(var_model, cause="inflation")
summary(granger_test)
granger_test1 <- causality(var_model, cause="interest.rates")
summary(granger_test1)`

I get the error Error in .subset(x, j) : invalid subscript type 'list'

I've tried to make it a vector and instead it tells me that year or country does not exist

I'm expecting it to simply run the regression

I was told to post my dataset so:

structure(list(year = c(2019, 2019, 2019, 2019, 2019, 2019, 2019, 
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2021, 2021, 2021, 2021, 
2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
2021, 2021, 2021, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 
2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 
2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 
2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2023, 
2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 
2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 
2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 
2023, 2023, 2023, 2023, 2023, 2023), country = c("United States", 
"China", "Japan", "Britain", "Canada", "Euro area", "Austria", 
"Belgium", "France", "Germany", "Greece", "Italy", "Netherlands", 
"Spain", "Czech Republic", "Denmark", "Norway", "Poland", "Russia", 
"Sweden", "Switzerland", "Turkey", "Australia", "Hong Kong", 
"India", "Indonesia", "Malaysia", "Pakistan", "Philippines", 
"Singapore", "South Korea", "Taiwan", "Thailand", "Brazil", "Chile", 
"Colombia", "Mexico", "Peru", "Israel", "South Africa", "United States", 
"China", "Japan", "Britain", "Canada", "Euro area", "Austria", 
"Belgium", "France", "Germany", "Greece", "Italy", "Netherlands", 
"Spain", "Czech Republic", "Denmark", "Norway", "Poland", "Russia", 
"Sweden", "Switzerland", "Turkey", "Australia", "Hong Kong", 
"India", "Indonesia", "Malaysia", "Pakistan", "Philippines", 
"Singapore", "South Korea", "Taiwan", "Thailand", "Brazil", "Chile", 
"Colombia", "Mexico", "Peru", "Israel", "South Africa", "United States", 
"China", "Japan", "Britain", "Canada", "Euro area", "Austria", 
"Belgium", "France", "Germany", "Greece", "Italy", "Netherlands", 
"Spain", "Czech Republic", "Denmark", "Norway", "Poland", "Russia", 
"Sweden", "Switzerland", "Turkey", "Australia", "Hong Kong", 
"India", "Indonesia", "Malaysia", "Pakistan", "Philippines", 
"Singapore", "South Korea", "Taiwan", "Thailand", "Brazil", "Chile", 
"Colombia", "Mexico", "Peru", "Israel", "South Africa", "United States", 
"China", "Japan", "Britain", "Canada", "Euro area", "Austria", 
"Belgium", "France", "Germany", "Greece", "Italy", "Netherlands", 
"Spain", "Czech Republic", "Denmark", "Norway", "Poland", "Russia", 
"Sweden", "Switzerland", "Turkey", "Australia", "Hong Kong", 
"India", "Indonesia", "Malaysia", "Pakistan", "Philippines", 
"Singapore", "South Korea", "Taiwan", "Thailand", "Brazil", "Chile", 
"Colombia", "Mexico", "Peru", "Israel", "South Africa", "United States", 
"China", "Japan", "Britain", "Canada", "Euro area", "Austria", 
"Belgium", "France", "Germany", "Greece", "Italy", "Netherlands", 
"Spain", "Czech Republic", "Denmark", "Norway", "Poland", "Russia", 
"Sweden", "Switzerland", "Turkey", "Australia", "Hong Kong", 
"India", "Indonesia", "Malaysia", "Pakistan", "Philippines", 
"Singapore", "South Korea", "Taiwan", "Thailand", "Brazil", "Chile", 
"Colombia", "Mexico", "Peru", "Israel", "South Africa"), interest.rates = c(2.3, 
3, 0, 1.2, 1.9, 0.2, 0.5, 0.6, 0.5, 0.2, 3.7, 2.8, 0.2, 1.2, 
1.9, 0.3, 1.7, 2.9, 8.5, 0.3, -0.3, 14.9, 2.1, 1.8, 7.7, 7.8, 
3.9, 13.1, 6.4, 2.2, 2, 0.8, 2.2, 7.1, 4.1, 6.6, 8.2, 5.6, 2, 
8.7, 1, 2.5, 0, 0.5, 1, -0.6, -0.4, -0.3, -0.2, -0.6, 1.2, 1, 
-0.5, 0.3, 1.2, -0.6, 1.1, 1.7, 6.1, -0.3, -0.8, 11.3, 0.7, 0.9, 
6.2, 6.5, 2.8, 11, 4.2, 1.4, 1.3, 0.5, 0.8, 3.8, 3.4, 5.5, 6.3, 
3.9, 0.4, 8.9, 1.4, 3.1, 0, 0.8, 1.3, -0.3, -0.1, -0.1, -0.1, 
-0.3, 1, 0.7, -0.4, 0.3, 1.7, -0.2, 1.4, 1.5, 6.9, 0.4, -0.3, 
12.9, 1.7, 1.3, 6.2, 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.5, 4, 7.8, 
2.1, 2.2, 7.2, 6.2, 7.1, 7, 9.2, 5.5, 7.7, 7, 6.5, 10.4, 7.2, 
11.9, 6.9, 4.8, 10.1, 21, 5.1, 2.5, 62.2, 5, 4.1, 7.3, 5.3, 3, 
16.1, 4.7, 6, 4.4, 3.8, 5.7, 10.4, 10.2, 9.8, 7.6, 7.1, 4.2, 
6, 4, 2.2, 1.9, 5.9, 3.6, 6, 6.6, 6.1, 5, 6.8, 3.9, 6.8, 7.1, 
4.3, 9.9, 5, 4.6, 12.6, 7.3, 5.6, 2.2, 40.8, 3.8, 2.3, 5.3, 3.9, 
2.3, 9.9, 5.7, 3.1, 2.8, 1.6, 2.2, 4.8, 7.5, 9.7, 5.9, 6.9, 3.4, 
5.1)), class = "data.frame", row.names = c(NA, -200L))

答案1

得分: 1

lmtest 软件包实现了 Granger 测试,其中使用 grangertest 命令。

由于你有面板数据,你可能对 Granger 测试的面板版本感兴趣。plm 软件包使用 pgrangertest 实现了 Dumistrecu/Hurlin (2012) 的面板 Granger 测试。

请参阅以下示例。但是,对于面板 Granger 测试,你的数据太短,因此我展示了来自帮助页面 ?pgrangertest 的另一个示例。

library(lmtest)
grangertest(interest.rates ~ inflation, data = data)

library(plm)
pdata <- pdata.frame(data, index=c("country", "year"), drop.index = TRUE)
pgrangertest(interest.rates ~ inflation, data = pdata)

pdim(pdata)
Balanced Panel: n = 40, T = 5, N = 200

data("Grunfeld", package = "plm")
pgrangertest(inv ~ value, data = Grunfeld)
英文:

Package lmtest implements the Granger test with command grangertest.

As you have panel data you might be interested in a panel version of the Granger test. Package plm implements Dumistrecu/Hurlin (2012) panel Granger test with pgrangertest.

See below for examples of both. However, for the panel Granger test, your data is too short, thus I show another example from the help page ?pgrangertest.

library(lmtest)
grangertest(interest.rates ~ inflation, data = data)
#&gt; Granger causality test
#&gt; 
#&gt; Model 1: interest.rates ~ Lags(interest.rates, 1:1) + Lags(inflation, 1:1)
#&gt; Model 2: interest.rates ~ Lags(interest.rates, 1:1)
#&gt;   Res.Df Df     F Pr(&gt;F)
#&gt; 1    196                
#&gt; 2    197 -1 2.187 0.1408
## panel version: data is not sufficient, needs more time periods per individual
library(plm)
pdata &lt;- pdata.frame(data, index=c(&quot;country&quot;, &quot;year&quot;), drop.index = TRUE)
pgrangertest(interest.rates ~ inflation, data = pdata)
#&gt; Error in pgrangertest(interest.rates ~ inflation, data = pdata):
#&gt; Condition for test = &quot;Ztilde&quot; not met for all individuals: 
#&gt; length of time series must be larger than 5+3*order (&gt;5+3*1=8)
# inspect how many oberservations we have for the 40 countries: 5 for each
pdim(pdata)
Balanced Panel: n = 40, T = 5, N = 200
# Example of with suffiently long panel data
data(&quot;Grunfeld&quot;, package = &quot;plm&quot;)
pgrangertest(inv ~ value, data = Grunfeld)
#&gt; 
#&gt;  Panel Granger (Non-)Causality Test (Dumitrescu/Hurlin (2012))
#&gt; 
#&gt; data:  inv ~ value
#&gt; Ztilde = 3.2896, p-value = 0.001003
#&gt; alternative hypothesis: Granger causality for at least one individual

huangapple
  • 本文由 发表于 2023年4月4日 05:20:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923855.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定