Appendices
(I don’t really understand your meaning of “Copy and paste your R-codes on
the last page of your answer sheet with the title "Appendices". My code is
really long, sorry TAT)
#1. Declare the dataset as a time series, ts, object in R.
setwd("C://Users//news0//Desktop//大三上//计量") #thats my laptop file path ;)
install.packages("readxl")
library(readxl)
ETs=read_excel("Empirical Task.xlsx")
View(ETs)
#1a. unitroot test between 1 to 5
install.packages("urca")
library("urca")
Asset1_ts=ts(ETs$Asset1)
lag_criteria=sapply(1:5, function(lag){
test_result=ur.df(Asset1_ts, type="none",lags=lag,selectlags="Fixed")
return(test_result@cval[1])
})
lag_criteria #-2.6 -2.6 -2.6 -2.6 -2.6
optimal_lag=which.min(lag_criteria)
optimal_lag # 1
#i am such a genius
#1b.ADF equation ( without drift and trend
test_result=ur.df(Asset1_ts,type="none",lags=1)
#1c.ADF equation( with drift and without trend
test_result_drift=ur.df(Asset1_ts,type="drift",lags=1)
#1d. ADF equation(with trend and without drift
test_result_trend=ur.df(Asset1_ts, type="trend",lags=1)
#1e. ADF equation(with trend and drift
test_result_both=ur.df(Asset1_ts,type = "both",lags=1)
#1f.best one
install.packages("tseries")
library("tseries")
adf_result=adf.test(Asset1_ts)
adf_result #dont know what does the result mean
#so, ADF equation without drift and trend is the best one i think
#2 price relationship
install.packages("dynlm")
library("dynlm")
install.packages("dplyr")
library("dplyr")
str(ETs)
ETs$Asset2_L1=lag(ETs$Asset2, 1)
ETs$Asset2_L2=lag(ETs$Asset2, 2)
Asset2_ts=ts(ETs$Asset2)
Asset3_ts=ts(ETs$Asset3)
ETs_cleaned=na.omit(ETs)
model_ARDL=dynlm(Asset1~Asset2_L1+Asset2_L2+Asset3,data=ETs_cleaned)
summary("model_ARDL")
#2a.Write the estimated ARDL(2,0) model results.
dynlm(formula =Asset1_ts ~ L(Asset2_ts, 1:2)+Asset3_ts)
#Coefficients:
coefficients=coef(model_ARDL)
beta0=cat("beta0= ", coefficients[1], "\n")
beta1=cat("beta1= ", coefficients[2], "\n")
beta2=cat("beta2= ", coefficients[3], "\n")
beta3=cat("beta3= ", coefficients[4], "\n")
beta0
beta1
beta2
beta3
residuals=resid(model_ARDL)
cat("error_t: \n")
print(residuals)
Asset1_t=beta0+beta1*Asset2_(t-1)+beta2*Asset2_(t-2)+beta3*Asset3_t+error_t
#too dificult
#2b.Forecast Asset1’s price for Thursday, 2024 − 05 − 30.
last_asset2=tail(ETs$Asset2, 1)
last_asset3=tail(ETs$Asset3, 1)
forecast_data=data.frame(Asset2_L1=last_asset2, Asset2_L2=last_asset2,
Asset3 =last_asset3)
forecast_data
forecast_asset1=predict(model_ARDL,newdata = forecast_data)
forecast_asset1
#dave, you are the smartest people I’ve ever met, 'cause you know everything
about this carzy rstudio
#2c.Compute the 95% forecast confidence
forecast_ci=predict(model_ARDL,newdata=forecast_data,
interval="confidence",level=0.95)
forecast_ci
# fit lwr upr
#1 66.78789 54.15998 79.4158
#3 basic V AR(p) model
data_var=cbind(Asset1_ts,Asset2_ts,Asset3_ts)
data_var
install.packages("vars")
library("vars")
data_var_cleaned=na.omit(data_var)
data_var_cleaned=na.locf(data_var)
lag_selection=VARselect(data_var_cleaned, lag.max = 5, type = "both")
lag_selection$selection
lag_p=2
model_VAR=VAR(data_var_cleaned, p = lag_p, type = "both")
summary(model_VAR)
# 3a. Does the price of Asset2 granger-cause that of Asset1?
granger_test_a=causality(model_VAR, cause="Asset2_ts")
granger_test_a
print(granger_test_a)
#Chi-squared = 9.1044, df = 2, p-value = 0.01054
#p-value = 0.01054
#so, Asset2 granger is granger-cause that of Asset1
#3 b.Do the prices of Asset2 and Asset3 jointly granger-cause that of
Asset1?
granger_test_b=causality(model_VAR,cause = c("Asset2_ts", "Asset3_ts"))
granger_test_b
print(granger_test_b)
#Chi-squared = 6.0281, df = 2, p-value = 0.04909
#so, Asset2 and Asset3 jointly is granger-cause that of Asset1
#alright, its just some words about my feeling, It doesn't matter the results
#to be honestly, i learnt some functions from chatgpt and youtube, because, some
code you taught in class, i cant totally understand.
#so, if you think my code is unsuitable, you can lower my grade.
#i did try my best, even some results are still wrong