Closed sarahmcandrew closed 5 years ago
Cannot attach R file
R CODE:::
rm(list=ls())
install.packages("ggplot2") install.packages("caret") install.packages("e1071") install.packages("dplyr") install.packages("tidyr") install.packages("plyr") install.packages("rworldmap") install.packages("lubridate") install.packages("sqldf") install.packages("rpart") install.packages("forecast") install.packages("fpp2") install.packages("xts") install.packages("TTR")
library(ggplot2) library(caret) library(e1071) library(dplyr) library(tidyr) library(plyr) library(rworldmap) library(lubridate) library(sqldf) library(rpart) library(GGally) library(forecast) library(fpp2) library(xts) library(TTR)
groupdata <- read.csv("weatherAUS.csv")
View(groupdata)
head(groupdata, 10)
tail(groupdata, 10)
str(groupdata)
summary(groupdata)
sum(is.na(groupdata))
newdataset <- sqldf("select Date, Location, MinTemp, MaxTemp, Rainfall, Temp9am, Temp3pm, RISK_MM, RainTomorrow from groupdata") View(newdataset)
str(newdataset)
sum(is.na(newdataset))
summary(newdataset)
meanmintemp <- mean(newdataset$MinTemp, na.rm = TRUE)
newdataset$MinTemp <- ifelse(is.na(newdataset$MinTemp), as.numeric(meanmintemp), newdataset$MinTemp)
meanmaxtemp <- mean(newdataset$MaxTemp, na.rm = TRUE)
newdataset$MaxTemp <- ifelse(is.na(newdataset$MaxTemp), as.numeric(meanmaxtemp), newdataset$MaxTemp)
meanrainfall <- mean(newdataset$Rainfall, na.rm = TRUE)
newdataset$Rainfall <- ifelse(is.na(newdataset$Rainfall), as.numeric(meanrainfall), newdataset$Rainfall)
meantemp9am <- mean(newdataset$Temp9am, na.rm = TRUE)
newdataset$Temp9am <- ifelse(is.na(newdataset$Temp9am), as.numeric(meantemp9am), newdataset$Temp9am)
meantemp3pm <- mean(newdataset$Temp3pm, na.rm = TRUE)
newdataset$Temp3pm <- ifelse(is.na(newdataset$Temp3pm), as.numeric(meantemp3pm), newdataset$Temp3pm)
sum(is.na(newdataset))
summary(newdataset)
View(newdataset)
write.table(newdataset, file = "finalgroupdataset.csv", sep = ",")
newdataset$Date <- as.Date(newdataset$Date) format(newdataset$Date, format = "%Y %m %d")
View(newdataset) str(newdataset)
area <- 'Albury'
data.sub1 <-subset(newdataset, Location == area) View(data.sub1)
str(data.sub1)
ggplot(data.sub1, aes(Date, MaxTemp)) + geom_line() + scale_x_date() + xlab("") + ylab("Max Temperature") + ggtitle("Time Series For Albury") + theme(panel.background = element_rect(fill = "white"), panel.grid.minor = element_blank(), panel.grid.major = element_blank(), axis.line = element_line(colour = "black", size = 1))
area12 <- 'Albury'
data.sub12 <-subset(newdataset, Location == area12 & Date >= '2009-01-01' & Date <= '2009-12-31') View(data.sub12)
str(data.sub12)
ggplot(data.sub12, aes(Date, MaxTemp)) + geom_line() + scale_x_date() + xlab("") + ylab("Max Temperature") + ggtitle("Time Series For Albury 2009") + theme(panel.background = element_rect(fill = "white"), panel.grid.minor = element_blank(), panel.grid.major = element_blank(), axis.line = element_line(colour = "black", size = 1))
newdataset.xts <- xts(x = data.sub1$MaxTemp, order.by = data.sub1$Date) str(newdataset.xts)
newdataset_date <- apply.monthly(newdataset.xts, mean)
View(newdataset_date)
newdataset_end <- floor(0.8*length(newdataset_date))
newdataset_train <- newdataset_date[1:newdataset_end,]
newdataset_test <- newdataset_date[(newdataset_end+1):length(newdataset_date),]
newdataset_start <- c(year(start(newdataset_train)), month(start(newdataset_train))) newdataset_end <- c(year(end(newdataset_train)), month(end(newdataset_train))) newdataset_train <- ts(as.numeric(newdataset_train), start = newdataset_start, end = newdataset_end, frequency = 12)
newdataset_start <- c(year(start(newdataset_test)), month(start(newdataset_test))) newdataset_end <- c(year(end(newdataset_test)), month(end(newdataset_test))) newdataset_test <- ts(as.numeric(newdataset_test), start = newdataset_start, end = newdataset_end, frequency = 12)
forecast_horizon <- length(newdataset_test)
class(newdataset_train) class(newdataset_test)
newdataset_train_components <- decompose(newdataset_train) plot(newdataset_train_components) + title("for temperature in Albury")
auto.arima(newdataset_train, seasonal = TRUE) fit <- auto.arima(newdataset_train, seasonal = TRUE) tsdisplay(residuals(fit), lag.max = 45, main = '(2,0,2) Model Residuals')
fcast <- forecast(fit, h=30) #Specify forecast horizon h periods plot(fcast) lines(newdataset_test, col = "red") legend("topleft", legend=c("Actual", "ARIMA Forecast"), col=c("red", "blue"), lty=1:2, cex=0.8)
accuracy(fcast, newdataset_test)
newdataset_train_esf <- HoltWinters(newdataset_train, beta=TRUE, gamma=TRUE) %>% forecast(h=forecast_horizon)
head(newdataset_train_esf$fitted) tail(newdataset_train_esf$fitted)
newdataset_train%>% HoltWinters(beta = FALSE, gamma = TRUE) %>% forecast(h=forecast_horizon) %>% plot() lines(newdataset_test, col = "red") legend("topleft", legend=c("Actual", "Holt Winters Forecast"), col=c("red", "blue"), lty=1:2, cex=0.8)
accuracy(newdataset_train_esf, newdataset_test)
predictets <- newdataset_train %>% ma(order = 3, centre = TRUE) %>% forecast(h = forecast_horizon) %>% plot() lines(newdataset_test, col = "red") legend("topleft", legend=c("Actual", "ETS(M, Ad, M) Forecast"), col=c("red", "blue"), lty=1:2, cex=0.8)
accuracy(predictets$mean, newdataset_test)
fit2 <- ets(newdataset_train) fit2 #Discover the best model to use
prediction <- newdataset_train %>% forecast(h=forecast_horizon) %>% plot() lines(newdataset_test, col = "red") legend("topleft", legend=c("Actual", "ETS Forecast(M,N,A"), col=c("red", "blue"), lty=1:2, cex=0.8)
accuracy(prediction$mean, newdataset_test)
fit3 <- stl(newdataset_train, t.window=15, s.window="periodic", robust=TRUE) focaststl <- forecast(fit3, method="naive", h=24) plot(focaststl) lines(newdataset_test, col = "red") legend("topleft", legend=c("Actual", "STL Forecast"), col=c("red", "blue"), lty=1:2, cex=0.8)
accuracy(focaststl, newdataset_test)
Code to be reviewed by Louise and issues reported back to be rectified
Sarah's code above and slimmed down version used in Azure experiment all are both saved in the Github
Discussed with Louise and in line with dataset, opted to do predictive modelling with temperature of one city