#Global Warming #There is an upward trend since about 1970. Is this man-made or natural cycle?? globtemp = ts(scan("I:/AAEC 6311/lectures/R_demos/globtemp.txt"), start=1856) gtemp = window(globtemp, start=1900) # use data from 1990 plot(gtemp, type="o", ylab="Global Temperature Deviations") #Download S&P data sp500 <- read.table("I:/AAEC 6311/lectures/R_demos/sp500.csv", header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE) #Use settlement logged difference of settlement prices returns <- ts(diff(log(sp500[,5])), start=c(1950,2), frequency=12) adf.test(returns) #Augmented Dickey Fuller Test plot(returns) #Plot of returns acf(returns,400) #ACT of returns pacf(returns,400) #PACF of returns ar1 <- arima(returns,order=c(1,0,0)) #Estimate an AR(1) model tsdiag(ar1) #plots diagnostic tests for estimated model ma1 <- arima(returns,order=c(0,0,1)) #Estimate an MA(1) model arma11 <- arima(returns,order=c(1,0,1)) #Estimate an ARMA(1,1) model #Compare models aic <- rbind(ar1$aic, ma1$aic, arma11$aic) model <- c("AR1", "MA1", "ARMA1,1") output.r <- matrix(data=round(aic,3), ncol=1,nrow=3, dimnames=list(model)) output.r garch11 <- garch(returns,order=c(1,1)) #Estimate with GARCH(1,1) AIC <- 2*garch11$n.likeli - 2*(sum(garch11$order)+1) #Compute AIC for GARCH SIC <- 2*garch11$n.likeli - log(length(returns))*(sum(garch11$order)+1) #Compuate SIC for GARCH plot(garch11$fitted.values[,1]) #Plot conditional variance title("Conditional Variance for GARCH(1,1)") #Insert Title into plot ___________________________________________________________________________________________________________________________________ ##Evaluating Corn and Live Cattle Prices data <- read.table("I:/AAEC 6311/lectures/R_demos/corn_cattle.csv", header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE) corn <- ts(data[,2]); lc <- ts(data[,3]); #Plot Corn and Cattle prices stacked par(mfrow=c(2,1)) #Defines the plotting environment to stack 2 plots plot(corn); title("Plot of corn prices 1980 - 2005") plot(lc); title("Plot of live cattle prices 1980 - 2005") adf.test(corn); adf.test(lc) y <- ts.union(corn, lc) #Combines corn and live cattle prices into one mv variable dly <- diff(log(y)) adf.test(dly[,1]) #ADF test on corn library(vars) VARselect(dly,lag.max=10, type="none") #Provides normalized IC measures for VAR(1)-VAR(8) var_8 <- VAR(dly, p=8, type="none") #Estimate VAR(8) var_8 summary(var_8) #View Results causality(var_8, cause="corn") #Test H0: Corn Prices Granger Causes Cattle Prices causality(var_8, cause="lc") #Test H0: Cattle Prices GC Corn Prices var_8_pred <- predict(var_8, n.ahead=30, ci=.95, dumvar=NULL) #Forecast 30 periods ahead pred <- var_8_pred$fcst$corn[,1] var_8_irf <- irf(var_8, n.ahead=100) #Impulse Response Functions plot(var_8_irf)