是否可以从包lmtest中的grangertest输出计算效果大小?我可以手工计算它,但它只给出F值,而不是平方和。
Granger causality test
Model 1: apwbc ~ Lags(apwbc, 1:1) + Lags(other, 1:1)
Model 2: apwbc ~ Lags(apwbc, 1:1)
Res.Df Df F Pr(>F)
1 163
2 164 -1 4.8495 0.02906 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
答案 0 :(得分:0)
我们说变量 X Granger导致变量 Y 如果基于 X 的滞后值 Y 的预测基于 Y 的滞后值,em>和 Y 明显优于 Y 的预测。这意味着格兰杰因果关系检验是对嵌套模型的检验;具有两个变量滞后的模型是完整模型,并且仅具有滞后 Y 的模型是嵌套或受限模型。我们可以通过嵌套模型的测试计算部分η 2 为( SS restricted - SS full )/ SS restricted (参见例如Wright and London (2009),第21页)。
这很容易编码:
nested_model_partial_eta_sq <- function(full_mod, rest_mod) {
# full_mod is the full model (an lm object)
# rest_mod is the model that omits one or more explanatory variables
SS_full <- sum(full_mod$residuals^2)
SS_rest <- sum(rest_mod$residuals^2)
return((SS_rest - SS_full) / SS_rest)
}
但是,我们可能还需要一个为我们生成回归的函数,以及运行Granger因果关系检验:
custom_granger <- function(x, y, p = 1) {
# Does x Granger cause y? What is the effect size?
# We want to test Granger causality,
# but then also get partial eta squared to measure effect size
# First it will be convenient to store the variable names
varnames <- c(deparse(substitute(x)), deparse(substitute(y)))
lagnames <- paste0(varnames, "_lag", rep(1:p, each = 2))
# Then we created the lagged variables / data for models
VAR_data <- embed(as.matrix(cbind(x, y)), p + 1)
colnames(VAR_data) <- c(varnames, lagnames)
VAR_data <- VAR_data[, -1]
# Run the full model
model_formula <- paste(colnames(VAR_data)[-1], collapse = " + ")
model_formula <- formula(paste0(varnames[2], " ~ ", model_formula))
full_mod <- lm(model_formula, data = as.data.frame(VAR_data))
# Take out the lags of x and run the nested model
VAR_data <- VAR_data[ , seq(from = 1, to = p * 2 + 1, by = 2)]
model_formula <- paste(colnames(VAR_data)[-1], collapse = " + ")
model_formula <- formula(paste0(varnames[2], " ~ ", model_formula))
rest_mod <- lm(model_formula, data = as.data.frame(VAR_data))
# Then we can do the Granger test
granger_test <- anova(full_mod, rest_mod)
# and get partial eta squared
SS_full <- granger_test$RSS[1]
SS_rest <- granger_test$RSS[2]
partial_eta_squared <- (SS_rest - SS_full) / SS_rest
# And return all of it
return(list(VAR_result = full_mod,
granger_test = granger_test,
partial_eta_squared = partial_eta_squared))
}
使用您的数据会产生
df <- read.csv('cormanaz-data.txt')
with(df, custom_granger(other, apwbc))
$VAR_result
Call:
lm(formula = model_formula, data = as.data.frame(VAR_data))
Coefficients:
(Intercept) other_lag1 apwbc_lag1
1.97732 -0.01671 0.48997
$granger_test
Analysis of Variance Table
Model 1: apwbc ~ other_lag1 + apwbc_lag1
Model 2: apwbc ~ apwbc_lag1
Res.Df RSS Df Sum of Sq F Pr(>F)
1 163 6267.9
2 164 6454.4 -1 -186.48 4.8495 0.02906 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
$partial_eta_squared
[1] 0.02889191
您可以看到Granger因果关系检验的 F 统计量与lmtest::grangertest()
相同,但这也会给出VAR系数和部分η 2 < / sup>,对于你的特定情况(我猜你一直以来的数字)约为0.029。