Assignment
2024-06-19
# Install necessary packages if not already installed
# Load necessary libraries
library(readxl)
## Warning: package 'readxl' was built under R version 4.3.3
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.1
library(urca)
## Warning: package 'urca' was built under R version 4.3.3
library(vars)
## Warning: package 'vars' was built under R version 4.3.3
## Loading required package: MASS
## Loading required package: strucchange
## Warning: package 'strucchange' was built under R version 4.3.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.3.2
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: sandwich
## Warning: package 'sandwich' was built under R version 4.3.3
## Loading required package: lmtest
## Warning: package 'lmtest' was built under R version 4.3.3
library(lmtest)
library(sandwich)
library(aod)
## Warning: package 'aod' was built under R version 4.3.3
# Load the dataset
file_path <- "HAdata.xlsx"
data <- read_excel(file_path, sheet = "Tabelle1")
# Convert data to time series format
data$Quarterly <- as.Date(data$Quarterly)
ts_data <- ts(data[, -1], start = c(1985, 1), frequency = 4)
# 1. Plot the time series of GDP and SPREAD
# Create individual plots
gdp_plot <- ggplot(data, aes(x = Quarterly)) +
geom_line(aes(y = GDP), color = "blue") +
labs(title = "Time Series of GDP", x = "Quarterly", y = "GDP") +
theme_minimal()
spread_plot <- ggplot(data, aes(x = Quarterly)) +
geom_line(aes(y = SPREAD), color = "red") +
labs(title = "Time Series of SPREAD", x = "Quarterly", y = "SPREAD") +
theme_minimal()
# Print plots
print(gdp_plot)
print(spread_plot)
# 2. Perform an ADF test for all variables
adf_gdp <- ur.df(log(data$GDP), type = "trend", lags = 4)
adf_spread <- ur.df(data$SPREAD, type = "trend", lags = 4)
summary(adf_gdp)
##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression trend
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.100909 -0.003043 0.000605 0.004519 0.063065
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.3521178 0.1574980 2.236 0.0269 *
## z.lag.1 -0.0402397 0.0186461 -2.158 0.0326 *
## tt 0.0004361 0.0002122 2.055 0.0417 *
## z.diff.lag1 -0.0521032 0.0818492 -0.637 0.5254
## z.diff.lag2 0.0976401 0.0818038 1.194 0.2346
## z.diff.lag3 0.0784984 0.0819449 0.958 0.3397
## z.diff.lag4 0.0268430 0.0820036 0.327 0.7439
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.01198 on 145 degrees of freedom
## Multiple R-squared: 0.05272, Adjusted R-squared: 0.01352
## F-statistic: 1.345 on 6 and 145 DF, p-value: 0.241
##
##
## Value of test-statistic is: -2.1581 9.6872 2.699
##
## Critical values for test statistics:
## 1pct 5pct 10pct
## tau3 -3.99 -3.43 -3.13
## phi2 6.22 4.75 4.07
## phi3 8.43 6.49 5.47
summary(adf_spread)
##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression trend
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.19709 -0.25711 -0.03748 0.21175 1.06278
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.2150709 0.0835822 2.573 0.011081 *
## z.lag.1 -0.1218045 0.0321123 -3.793 0.000218 ***
## tt -0.0008051 0.0007555 -1.066 0.288401
## z.diff.lag1 0.4447543 0.0787861 5.645 8.41e-08 ***
## z.diff.lag2 -0.0559768 0.0872612 -0.641 0.522220
## z.diff.lag3 0.1277958 0.0863573 1.480 0.141083
## z.diff.lag4 0.1058927 0.0829093 1.277 0.203569
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4063 on 145 degrees of freedom
## Multiple R-squared: 0.234, Adjusted R-squared: 0.2023
## F-statistic: 7.384 on 6 and 145 DF, p-value: 6.598e-07
##
##
## Value of test-statistic is: -3.7931 4.9719 7.4371
##
## Critical values for test statistics:
## 1pct 5pct 10pct
## tau3 -3.99 -3.43 -3.13
## phi2 6.22 4.75 4.07
## phi3 8.43 6.49 5.47
# 3. ADL(p,q) model for first difference of log(GDP)
dlog_gdp <- diff(log(data$GDP))
spread <- data$SPREAD[-1]
# Determine p and q using AIC and BIC
VARselect(cbind(dlog_gdp, spread), lag.max = 4, type = "both")
## $selection
## AIC(n) HQ(n) SC(n) FPE(n)
## 4 2 2 4
##
## $criteria
## 1 2 3 4
## AIC(n) -1.043748e+01 -1.059308e+01 -1.059077e+01 -1.060344e+01
## HQ(n) -1.037283e+01 -1.049610e+01 -1.046146e+01 -1.044181e+01
## SC(n) -1.027833e+01 -1.035435e+01 -1.027247e+01 -1.020556e+01
## FPE(n) 2.931362e-05 2.509118e-05 2.515193e-05 2.483982e-05
# 4. ADL(1,1) model estimation
adldata <- data.frame(
DY = dlog_gdp,
DY_lag = c(NA, dlog_gdp[-length(dlog_gdp)]),
SPREAD = spread,
SPREAD_lag = c(NA, spread[-length(spread)])
)
adldata <- na.omit(adldata)
model <- lm(DY ~ DY_lag + SPREAD + SPREAD_lag, data = adldata)
summary(model)
##
## Call:
## lm(formula = DY ~ DY_lag + SPREAD + SPREAD_lag, data = adldata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.096566 -0.003576 0.000331 0.004532 0.063239
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.012731 0.001703 7.474 5.86e-12 ***
## DY_lag -0.085643 0.081366 -1.053 0.2942
## SPREAD -0.004771 0.002148 -2.221 0.0278 *
## SPREAD_lag 0.005045 0.002155 2.341 0.0206 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.01185 on 151 degrees of freedom
## Multiple R-squared: 0.03757, Adjusted R-squared: 0.01845
## F-statistic: 1.965 on 3 and 151 DF, p-value: 0.1217
# Plot autocorrelogram of residuals
acf(residuals(model))
# Autocorrelation test on residuals
bgtest(model, order = 4)
##
## Breusch-Godfrey test for serial correlation of order up to 4
##
## data: model
## LM test = 3.0389, df = 4, p-value = 0.5513
# If residuals are autocorrelated, re-estimate with Newey-West variance
estimator
coeftest(model, vcov = NeweyWest(model))
##
## t test of coefficients:
##
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0127310 0.0018711 6.8040 2.235e-10 ***
## DY_lag -0.0856430 0.1523950 -0.5620 0.57496
## SPREAD -0.0047712 0.0024259 -1.9667 0.05105 .
## SPREAD_lag 0.0050452 0.0026871 1.8776 0.06237 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Perform Wald test
wald.test(b = coef(model), Sigma = vcovHC(model, type = "HC0"), Terms = c(2,
3))
## Wald test:
## ----------
##
## Chi-squared test:
## X2 = 3.9, df = 2, P(> X2) = 0.14
# Print all results
print(summary(adf_gdp))
##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression trend
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.100909 -0.003043 0.000605 0.004519 0.063065
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.3521178 0.1574980 2.236 0.0269 *
## z.lag.1 -0.0402397 0.0186461 -2.158 0.0326 *
## tt 0.0004361 0.0002122 2.055 0.0417 *
## z.diff.lag1 -0.0521032 0.0818492 -0.637 0.5254
## z.diff.lag2 0.0976401 0.0818038 1.194 0.2346
## z.diff.lag3 0.0784984 0.0819449 0.958 0.3397
## z.diff.lag4 0.0268430 0.0820036 0.327 0.7439
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.01198 on 145 degrees of freedom
## Multiple R-squared: 0.05272, Adjusted R-squared: 0.01352
## F-statistic: 1.345 on 6 and 145 DF, p-value: 0.241
##
##
## Value of test-statistic is: -2.1581 9.6872 2.699
##
## Critical values for test statistics:
## 1pct 5pct 10pct
## tau3 -3.99 -3.43 -3.13
## phi2 6.22 4.75 4.07
## phi3 8.43 6.49 5.47
print(summary(adf_spread))
##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression trend
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1 + tt + z.diff.lag)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.19709 -0.25711 -0.03748 0.21175 1.06278
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.2150709 0.0835822 2.573 0.011081 *
## z.lag.1 -0.1218045 0.0321123 -3.793 0.000218 ***
## tt -0.0008051 0.0007555 -1.066 0.288401
## z.diff.lag1 0.4447543 0.0787861 5.645 8.41e-08 ***
## z.diff.lag2 -0.0559768 0.0872612 -0.641 0.522220
## z.diff.lag3 0.1277958 0.0863573 1.480 0.141083
## z.diff.lag4 0.1058927 0.0829093 1.277 0.203569
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4063 on 145 degrees of freedom
## Multiple R-squared: 0.234, Adjusted R-squared: 0.2023
## F-statistic: 7.384 on 6 and 145 DF, p-value: 6.598e-07
##
##
## Value of test-statistic is: -3.7931 4.9719 7.4371
##
## Critical values for test statistics:
## 1pct 5pct 10pct
## tau3 -3.99 -3.43 -3.13
## phi2 6.22 4.75 4.07
## phi3 8.43 6.49 5.47
print(VARselect(cbind(dlog_gdp, spread), lag.max = 4, type = "both"))
## $selection
## AIC(n) HQ(n) SC(n) FPE(n)
## 4 2 2 4
##
## $criteria
## 1 2 3 4
## AIC(n) -1.043748e+01 -1.059308e+01 -1.059077e+01 -1.060344e+01
## HQ(n) -1.037283e+01 -1.049610e+01 -1.046146e+01 -1.044181e+01
## SC(n) -1.027833e+01 -1.035435e+01 -1.027247e+01 -1.020556e+01
## FPE(n) 2.931362e-05 2.509118e-05 2.515193e-05 2.483982e-05
print(summary(model))
##
## Call:
## lm(formula = DY ~ DY_lag + SPREAD + SPREAD_lag, data = adldata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.096566 -0.003576 0.000331 0.004532 0.063239
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.012731 0.001703 7.474 5.86e-12 ***
## DY_lag -0.085643 0.081366 -1.053 0.2942
## SPREAD -0.004771 0.002148 -2.221 0.0278 *
## SPREAD_lag 0.005045 0.002155 2.341 0.0206 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.01185 on 151 degrees of freedom
## Multiple R-squared: 0.03757, Adjusted R-squared: 0.01845
## F-statistic: 1.965 on 3 and 151 DF, p-value: 0.1217
print(bgtest(model, order = 4))
##
## Breusch-Godfrey test for serial correlation of order up to 4
##
## data: model
## LM test = 3.0389, df = 4, p-value = 0.5513
print(coeftest(model, vcov = NeweyWest(model)))
##
## t test of coefficients:
##
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0127310 0.0018711 6.8040 2.235e-10 ***
## DY_lag -0.0856430 0.1523950 -0.5620 0.57496
## SPREAD -0.0047712 0.0024259 -1.9667 0.05105 .
## SPREAD_lag 0.0050452 0.0026871 1.8776 0.06237 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
print(wald.test(b = coef(model), Sigma = vcovHC(model, type = "HC0"), Terms =
c(2, 3)))
## Wald test:
## ----------
##
## Chi-squared test:
## X2 = 3.9, df = 2, P(> X2) = 0.14