The data used in this project comes from a larger dataset, Global Climate Change Data, from data.world. The original had temp data on cities around the world from 1750-2013. All future filtering will be done here in R. I played with the dataset a little in Excel before importing it and filtered it to only include from Chicago from 1968 onward. Some further filtering was done later on to narrow the data to a 20 year span.
# Setup
# Clear the workspace
rm(list = ls()) # Clear environment
gc() # Clear unused memory
## used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
## Ncells 519390 27.8 1149324 61.4 NA 669294 35.8
## Vcells 953989 7.3 8388608 64.0 16384 1839856 14.1
cat("\f") # Clear the console
dev.off # Clear the charts
## function (which = dev.cur())
## {
## if (which == 1)
## stop("cannot shut down device 1 (the null device)")
## .External(C_devoff, as.integer(which))
## dev.cur()
## }
## <bytecode: 0x7fc92e6d0050>
## <environment: namespace:grDevices>
#Install packages
packages <- c("knitr"
,"ggplot2"
, "dplyr"
)
for (i in 1:length(packages)) {# Iterate through each package in the 'packages' vector.
if (!packages[i] %in% rownames(installed.packages())) {#check if package is installed
install.packages(packages[i], #install package if it isn't already
repos = "http://cran.rstudio.com/",
dependencies = TRUE)}
library(packages[i], character.only = TRUE)} # Load the package into the R session.
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# Remove i and packages
rm(i, packages)
# Import data
chicago <- read.csv("/Users/cam/Desktop/PREDICTIVE ANALYTICS/Climate Change.csv")
#format data
chicago$Date <- as.Date(chicago$Date)
# Display how table looks
kable(head(chicago))
| Date | AverageTemperature | AverageTemperatureUncertainty | City | Country | Latitude | Longitude | X | X.1 |
|---|---|---|---|---|---|---|---|---|
| 1781-01-01 | 0.592 | 3.856 | Chicago | United States | 42.59N | 87.27W | NA | NA |
| 1781-02-01 | 0.413 | 5.077 | Chicago | United States | 42.59N | 87.27W | NA | NA |
| 1781-03-01 | 3.256 | 2.299 | Chicago | United States | 42.59N | 87.27W | NA | NA |
| 1781-04-01 | 5.876 | 2.227 | Chicago | United States | 42.59N | 87.27W | NA | NA |
| 1781-05-01 | 11.884 | 2.897 | Chicago | United States | 42.59N | 87.27W | NA | NA |
| 1781-06-01 | 16.343 | 2.148 | Chicago | United States | 42.59N | 87.27W | NA | NA |
Since over 200 years of monthly data would be hard to visualize in a single chart, the data will be filtered to the last 20 years. This is only done for visualization of decomposition in this discussion as more data would be beneficial for the sake of prediction.
# Filter for 20 years
chicago2 <- chicago %>%
filter(Date >= '1992-01-01')
# Create a graph of original data
ggplot(chicago2, aes(x = Date, y = AverageTemperature)) +
geom_line() +
scale_x_date(date_labels = "%Y", date_breaks = "5 years") +
ylab("Avg Temp (C°)")
# Additive time series
chicago_ts <- ts(chicago2$AverageTemperature,
start = min(chicago2$Date),
frequency = 12)
# Additive decomposition
add_decomp <- decompose(chicago_ts,
type = 'additive')
# Plot additive decomp
plot(add_decomp)
# Perform muliplicative decomp
mult_decomp <- decompose(chicago_ts,
type = 'multiplicative')
plot(mult_decomp)
At first glance, it may seem that multiplicative decomp is more generally more accurate than additive decomposition, however there are some points where the remainder value (random in the plots) is substantially further off the mark than additive decomposition. It is hard to tell how it compares to additive decomp with these variables aside. It should be noted that this is for 20 years of data and, as the lack of a clear pattern in trend values shows, the weather can be difficult to accurately predict.
When it comes to strengths vs weaknesses between the models, additive tends to be a better predictor when seasonal variation is relatively constant while multiplicative is better when seasonal variation increases over time. Thus multiplicative might progressively become a better predictor if global warming were to accelerate in the coming years. Since the seasonal variation is slight, additive decomposition is preferable to multiplicative so that predicted values are more likely to be off by only a few degrees.