R语言 我在做格兰杰因果检验

i1icjdpr  于 2023-04-09  发布在  其他
关注(0)|答案(1)|浏览(191)

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

然后我用我的r代码

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)`

我得到错误Error in .subset(x, j) : invalid subscript type 'list'
我试着把它变成一个矢量,但它告诉我年份或国家不存在
我希望它能简单地进行回归分析
我被告知要发布我的数据集,所以:

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, 6.5, 3.1, 10.2, 4, 1.4, 2, 0.4, 1.6, 8.3, 
2.9, 5.5, 5.7, 4.3, 1.1, 9, 1.9, 2.6, 0, 1.5, 1.8, 0, 0.4, 0.4, 
0.6, 0, 2.4, 1.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.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))
8hhllhi2

8hhllhi21#

lmtest使用命令grangertest实现格兰杰测试。
如果你有面板数据,你可能会对格兰杰检验的面板版本感兴趣。plm包实现了Dumistrecu/Hurlin(2012)的面板格兰杰检验。
下面是两个例子。但是,对于面板格兰杰测试,你的数据太短了,因此我从帮助页面?pgrangertest中展示了另一个例子。

library(lmtest)
grangertest(interest.rates ~ inflation, data = data)
#> Granger causality test
#> 
#> Model 1: interest.rates ~ Lags(interest.rates, 1:1) + Lags(inflation, 1:1)
#> Model 2: interest.rates ~ Lags(interest.rates, 1:1)
#>   Res.Df Df     F Pr(>F)
#> 1    196                
#> 2    197 -1 2.187 0.1408

## panel version: data is not sufficient, needs more time periods per individual
library(plm)
pdata <- pdata.frame(data, index=c("country", "year"), drop.index = TRUE)
pgrangertest(interest.rates ~ inflation, data = pdata)
#> Error in pgrangertest(interest.rates ~ inflation, data = pdata):
#> Condition for test = "Ztilde" not met for all individuals: 
#> length of time series must be larger than 5+3*order (>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("Grunfeld", package = "plm")
pgrangertest(inv ~ value, data = Grunfeld)
#> 
#>  Panel Granger (Non-)Causality Test (Dumitrescu/Hurlin (2012))
#> 
#> data:  inv ~ value
#> Ztilde = 3.2896, p-value = 0.001003
#> alternative hypothesis: Granger causality for at least one individual

相关问题