library(ggplot2)
library(knitr) getwd() DemandDat <- read.table('Assign5TimeSeriesDemand.csv', sep=",", header=TRUE, strip.white=TRUE) head(DemandDat) dim(DemandDat) summary(DemandDat) perf.metrics = NULL for (i in names(DemandDat)[2:length(DemandDat)]) { +
+ # Create training (rows 1-24) and test (rows 25-27) sets. + TrainDat <- DemandDat[1:24,c("Month",i)] + TestDat <- DemandDat[25:27,c("Month",i)] +
+ # Assign generic column name for demand. + colnames(TrainDat)[2] <- "Demand" + colnames(TestDat)[2] <- "Demand" +
+ # Fit a linear regression model, and store model performance statistics for later use. + lm.fit <- lm(Demand ~ Month, data = TrainDat) + perf.metrics <- rbind(perf.metrics, cbind.data.frame( + model = "Linear", city = i, adjr2 = round(summary(lm.fit)$adj.r.squared, digits = 2), msetrain = round(mean(summary(lm.fit)$residuals^2), digits = 2), msetest = round(mean((TestDat[,2] - predict(lm.fit,TestDat))^2), digits = 2), msediff = round(abs(mean(summary(lm.fit)$residuals^2) - mean((TestDat[,2] - predict(lm.fit,TestDat))^2)), digits = 2))) +
+ # Plot data against regression line + predicteddf <- data.frame(Demand = predict(lm.fit, DemandDat[,c("Month",paste(i))]), Month = DemandDat$Month) + print(ggplot(data = DemandDat, aes(x = Month, y = DemandDat[,paste(i)])) + + geompoint(color='blue') + + geomline(color='red', data = predicteddf, aes(x = Month, y = Demand)) + + labs(x = "Month", y = "Demand", title = paste(i, "Demand by Month with Linear Regression Line"))) + assign(paste0(i,"LinearModel"), lm.fit) +
+ # Fit a logarithmic regression model, and store model performance statistics for later use. + log.fit <- lm(Demand ~ log(Month), data = TrainDat) + perf.metrics <- rbind(perf.metrics, cbind.data.frame( + model = "Logarithmic", city = i, adjr2 = round(summary(log.fit)$adj.r.squared, digits = 2), msetrain = round(mean(summary(log.fit)$residuals^2), digits = 2), msetest = round(mean((TestDat[,2] - predict(log.fit,TestDat))^2), digits = 2), msediff = round(abs(mean(summary(log.fit)$residuals^2) - mean((TestDat[,2] - predict(log.fit,TestDat))^2)), digits = 2))) +
+ # Plot data against regression line + predicteddf <- data.frame(Demand = predict(log.fit, DemandDat[,c("Month",paste(i))]), Month = DemandDat$Month) + print(ggplot(data = DemandDat, aes(x = Month, y = DemandDat[,paste(i)])) + + geompoint(color='blue') + + geomline(color='red', data = predicteddf, aes(x = Month, y = Demand)) + + labs(x = "Month", y = "Demand", title = paste(i, "Demand by Month with Logarithmic Regression Line"))) + assign(paste0(i,"LogModel"), log.fit) +
+ # Fit a quadratic regression model, and store model performance statistics for later use. + quad.fit <- lm(Demand ~ Month + I(Month^2), data = TrainDat) + perf.metrics <- rbind(perf.metrics, cbind.data.frame( + model = "Quadratic", city = i, adjr2 = round(summary(quad.fit)$adj.r.squared, digits = 2), msetrain = round(mean(summary(quad.fit)$residuals^2), digits = 2), msetest = round(mean((TestDat[,2] - predict(quad.fit,TestDat))^2), digits = 2), msediff = round(abs(mean(summary(quad.fit)$residuals^2) - mean((TestDat[,2] - predict(quad.fit,TestDat))^2)), digits = 2))) +
+ # Plot data against regression line + predicteddf <- data.frame(Demand = predict(quad.fit, DemandDat[,c("Month",paste(i))]), Month = DemandDat$Month) + print(ggplot(data = DemandDat, aes(x = Month, y = DemandDat[,paste(i)])) + + geompoint(color='blue') + + geomline(color='red', data = predicteddf, aes(x = Month, y = Demand)) + + labs(x = "Month", y = "Demand", title = paste(i, "Demand by Month with Quadratic Regression Line"))) + assign(paste0(i,"QuadModel"), quad.fit) +
+ # Fit a cubic regression model, and store model performance statistics for later use. + cube.fit <- lm(Demand ~ Month + I(Month^2) + I(Month^3), data = TrainDat) + perf.metrics <- rbind(perf.metrics, cbind.data.frame( + model = "Cubic", city = i, adjr2 = round(summary(cube.fit)$adj.r.squared, digits = 2), msetrain = round(mean(summary(cube.fit)$residuals^2), digits = 2), msetest = round(mean((TestDat[,2] - predict(cube.fit,TestDat))^2), digits = 2), msediff = round(abs(mean(summary(cube.fit)$residuals^2) - mean((TestDat[,2] - predict(cube.fit,TestDat))^2)), digits = 2))) +
+ # Plot data against regression line + predicteddf <- data.frame(Demand = predict(cube.fit, DemandDat[,c("Month",paste(i))]), Month = DemandDat$Month) + print(ggplot(data = DemandDat, aes(x = Month, y = DemandDat[,paste(i)])) + + geompoint(color='blue') + + geomline(color='red', data = predicted_df, aes(x = Month, y = Demand)) + + labs(x = "Month", y = "Demand", title = paste(i, "Demand by Month with Cubic Regression Line"))) + assign(paste0(i,"CubeModel"), cube.fit) +
+ # Assign city-specific dataframe names (e.g., ChicagoTrainDat, ChicagoTestDat). + assign(paste0(i,"TrainDat"), TrainDat) + assign(paste0(i,"TestDat"), TestDat) + } perf.metrics
summary(ChicagoCubeModel)