Practical Assignment #2 tests your ability
Practical Assignment #2 tests your ability
Vikasini Selvaraj
2024-12-14
## [1] "C:/Users/Sys/Downloads"
## [1] "C:/Users/Sys/Downloads"
library(readxl)
head(tradev)
#5 Use the “datev” variable and the range() function to check the time sample
# covered by the "trade" data set. What time period is covered?
# What is the frequency of the data?
#6 Transform “tradev” variable from numeric format to the time series format
# by using ts() function. Label the new variable as "tradets".
# Geting the starting year and month from the "datev" variable
start_year <- as.numeric(format(datev[1], "%Y"))
start_month <- as.numeric(format(datev[1], "%m"))
## Time-Series [1:328] from 1992 to 2019: 131 106 102 105 107 ...
# Based on this graph does the Ratio of Exports to Imports for China
exhibit a trend?
# What about a regular seasonal fluctuation?
## png
## 2
head(tradepost)
# Display the model without AIC (this will give us the chosen order
'q')
trade.ar_no_aic
##
## Call:
## ar(x = tradepre, aic = FALSE)
##
## Coefficients:
## 1 2 3 4 5 6 7
8
## 0.3338 0.3504 0.1329 0.0559 -0.0398 0.0354 0.0550 -
0.0109
## 9 10 11 12 13 14 15
16
## 0.0180 -0.0143 0.0313 -0.1564 0.1071 -0.0476 -0.0137 -
0.0390
## 17 18 19 20 21 22 23
24
## 0.0123 0.1091 0.0107 -0.0438 0.0130 -0.0138 0.1398 -
0.1451
## 25
## -0.0369
##
## Order selected 25 sigma^2 estimated as 66.68
# Display the model with AIC (this will give us the chosen order 'q'
based on AIC)
trade.ar_aic
##
## Call:
## ar(x = tradepre, aic = TRUE)
##
## Coefficients:
## 1 2 3
## 0.3195 0.3951 0.1500
##
## Order selected 3 sigma^2 estimated as 67.21
print(ar_coeffs)
# Calculate residuals
residuals <- trade.ar$resid
print(conf_intervals)
#12 Extract the residuals from the trade.ar model and estimate
# the autocorrelation function. Based on this correlogram would you
say
# trade.ar model does a good job of explaining the trade ratio in
China?
#13 Use trade.ar model and predict() function to create a 4 period ahead forecast
# of the trade ratio in China. Save these predicted values as
"trade.ar.forc"
print(trade.ar.forc$pred)
# Optionally, store the forecast values as a data frame (if you want
to view them more clearly)
forecast_values <- data.frame(
Period = 1:4,
Forecast = trade.ar.forc$pred
)
# Print the forecast data frame
print(forecast_values)
## Period Forecast
## 1 1 119.7888
## 2 2 120.6425
## 3 3 120.0604
## 4 4 119.7225
#14 Use ts.plot() function to plot side-by-side actual values of the trade ratio
# from January 2019-April 2019 period and their forecasted
counterparts.
# (tradepost and trade.ar.forc)
# Please designate red color to represent the actual observed
values,
# and blue doted lines to represent forecasted values.
# How does the ability to predict future trade ratio depends on the
# time horizon of the forecast?
# Extract actual values for the tradepost period (January 2019 - April
2019)
# Assuming tradepost was already created (from previous steps)
# Set the period range for January 2019 to April 2019 (4 periods)
actual_values <- tradepost # Already created earlier
#16 Use time() function and tradepre variable to create a variable called “Time”.
# Assuming tradepre is already defined as a time series object
Time <- time(tradepre)
# Check the result
print(Time)
##
## Call:
## lm(formula = tradepre ~ Time)
##
## Residuals:
## Min 1Q Median 3Q Max
## -29.906 -7.849 -2.364 6.662 57.431
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.113e+03 1.749e+02 -6.363 6.79e-10 ***
## Time 6.130e-01 8.724e-02 7.027 1.26e-11 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 12.24 on 322 degrees of freedom
## Multiple R-squared: 0.133, Adjusted R-squared: 0.1303
## F-statistic: 49.38 on 1 and 322 DF, p-value: 1.263e-11
## 2.5 % 97.5 %
## (Intercept) -1457.3906131 -769.020633
## Time 0.4413904 0.784636
# Assuming you have a data frame or tibble with 'tradepre' and you
want to add 'Seas'
trade_data <- data.frame(tradepre = tradepre, Seas = Seas)
## tradepre Seas
## 1 130.7075 1
## 2 106.0696 2
## 3 101.6057 3
## 4 104.5100 4
## 5 106.5207 5
## 6 106.3442 6
## [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12"
##
## Call:
## lm(formula = tradepre ~ Time + Seas - 1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -32.050 -7.488 -2.213 6.182 55.259
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## Time 6.149e-01 8.839e-02 6.956 2.07e-11 ***
## Seas1 -1.115e+03 1.772e+02 -6.290 1.08e-09 ***
## Seas2 -1.114e+03 1.772e+02 -6.287 1.09e-09 ***
## Seas3 -1.118e+03 1.773e+02 -6.307 9.75e-10 ***
## Seas4 -1.118e+03 1.773e+02 -6.306 9.82e-10 ***
## Seas5 -1.118e+03 1.773e+02 -6.305 9.87e-10 ***
## Seas6 -1.118e+03 1.773e+02 -6.305 9.89e-10 ***
## Seas7 -1.118e+03 1.773e+02 -6.304 9.92e-10 ***
## Seas8 -1.117e+03 1.773e+02 -6.303 9.98e-10 ***
## Seas9 -1.118e+03 1.773e+02 -6.303 9.98e-10 ***
## Seas10 -1.118e+03 1.773e+02 -6.303 9.98e-10 ***
## Seas11 -1.116e+03 1.773e+02 -6.292 1.06e-09 ***
## Seas12 -1.117e+03 1.773e+02 -6.299 1.02e-09 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 12.39 on 311 degrees of freedom
## Multiple R-squared: 0.9892, Adjusted R-squared: 0.9888
## F-statistic: 2194 on 13 and 311 DF, p-value: < 2.2e-16
#20 Extract the residual series from the “trade.lmts” model and save them as
# "trade.lmts.resid". Then, estimate autocorrelation function to
check the
# goodness of the fit. What is the value of autocorrelation at lag
1?
# What can you conclude based on the correlogram of the residual
series?
# Extracting residuals from the trade.lmts model
trade.lmts.resid <- resid(trade.lmts)
# Install and load the nlme package if it's not already installed
# install.packages("nlme") # Uncomment to install if not installed
library(nlme)
## Seas2 -1 1
## Seas3 -1 1 1
## Seas4 -1 1 1 1
## Seas5 -1 1 1 1 1
## Seas6 -1 1 1 1 1 1
## Seas7 -1 1 1 1 1 1 1
## Seas8 -1 1 1 1 1 1 1 1
## Seas9 -1 1 1 1 1 1 1 1 1
## Seas10 -1 1 1 1 1 1 1 1 1 1
## Seas11 -1 1 1 1 1 1 1 1 1 1
1
## Seas12 -1 1 1 1 1 1 1 1 1 1
1 1
##
## Standardized residuals:
## Min Q1 Med Q3 Max
## -2.5862550 -0.6042485 -0.1786069 0.4988113 4.4590491
##
## Residual standard error: 12.39247
## Degrees of freedom: 324 total; 311 residual
# Access the coefficients from the GLS model
coef(trade.gls)
## [1] "new.Time:"
print(new.Time)
print("alpha:")
## [1] "alpha:"
print(alpha)
## Time
## 0.6148695
print("beta:")
## [1] "beta:"
print(beta)
print(new_variables)
print(trade.gls.forc)
#25 Use ts.plot() function to plot side-by-side actual values of the trade ratio
# from January 2019-April 2019 period and their forecasted
counterparts.
# (tradepost and trade.gls.forecast)
# Please designate red color to represent the actual observed
values,
# and blue doted lines to represent forecasted values.
# Add a legend
legend("topright",
legend = c("Actual", "Forecasted"),
col = c("red", "blue"),
lty = c(1, 2))
# Compare the MAPE values and determine which model performs better
if (mape_trade_gls < mape_trade_ar) {
print("trade.gls.forc performs better.")
} else {
print("trade.ar.forc performs better.")
}
#27 Create a variable called tradepreL, that represents the first lagged value
# of the "tradepre" variable. For example tradepreL_t=tradepre_(t-
1).
# Moreover, transform "tradepreL" variable into a time series object
by using ts().
# It should cover the same time period as "tradepre".
# Remove the NA value that will appear at the first position of the
lagged series
tradepreL <- tradepreL[-1]
# Transform 'tradepreL' into a time series object with the same time
period as 'tradepre'
tradepreL_ts <- ts(tradepreL, start = start(tradepre), frequency =
frequency(tradepre))
# Now, ensure the Time and Seas variables also match the adjusted
length of tradepreL_ts
Time <- time(tradepre)[2:length(tradepre)] # Remove the first time
point
Seas <- cycle(tradepre)[2:length(tradepre)] # Adjust the
cycle/seasonal index accordingly
##
## Call:
## lm(formula = tradepre[2:length(tradepre)] ~ tradepreL_ts + Time +
## Seas - 1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -44.463 -4.268 -0.330 3.861 51.352
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## tradepreL_ts 0.701901 0.039685 17.687 < 2e-16 ***
## Time 0.017244 0.002374 7.265 2.87e-12 ***
## Seas 0.003401 0.149383 0.023 0.982
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.242 on 320 degrees of freedom
## Multiple R-squared: 0.9938, Adjusted R-squared: 0.9937
## F-statistic: 1.711e+04 on 3 and 320 DF, p-value: < 2.2e-16
# Checking the new Time variable (from Jan 2019 to Apr 2019)
new.Time <- seq(from = 2019, by = 1/12, length.out = 4)
# Actual values for Jan 2019 to Apr 2019 (replace these with your
actual values)
actual_values <- c(0.15, 0.18, 0.17, 0.20) # Replace with your actual
trade ratio values