























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
























1 /
title: “ISYE 6402 Midterm 1” output: html_document —
For this practice midterm exam, you will analyze hourly temperature data at the Mt. Everest Base Camp during 2020. Each data point represents a temperature measurement in degrees Fahrenheit taken at hourly intervals. For some questions, you will analyze data spanning the full year; for others, we will analyze only the data spanning the first two months of temperature readings. These datasets have been prepared for you using the helper code below and will be referred to in this exam as the full and abridged data, respectively.
To read the data in R , save the file in your working directory (make sure you have changed the directory if different from the R working directory) and read the data using the R function read.csv()
1a: Analysis Discussion library (zoo) library (lubridate) library (mgcv) library (TSA) library (dynlm) library (prophet) library (ggplot2) #Read in data, split into full and abridged data full<-read.csv("Midterm 1 Data.csv") abridged<-full[1:1440,]
2 / To evaluate the stationarity of the given time series, we first examined the time series plot and the autocorrelation function (ACF) plot. The time series plot shows a clear trend, with temperature values increasing and then decreasing over time, suggesting a non-stationary process. Additionally, seasonal patterns appear at regular intervals, further indicating the presence of a cyclical component in the data. The ACF plot also supports these observations, showing significant autocorrelation at various lags, which is typical for non- stationary data. For a stationary time series, we would expect the autocorrelations to decay more quickly, but here we see persistent correlations at longer lags, reinforcing the non-stationarity of the series. Regarding assumptions of stationarity, the primary assumption being violated here is that the mean and variance of the series do not change over time. The clear trend and seasonality suggest that the time series has changing statistical properties, meaning it’s not strictly stationary in terms of its mean and variance. Additionally, the autocorrelation structure is not constant, further violating the assumption of stationarity. full<-ts(full$temp,freq=24) plot(full,main="Time Series Plot of Temperature Data",ylab="Temperature") acf(full,main="ACF Plot of Temperature Data")
4 / time.pts = c(1:length(full)) time.pts = c(time.pts - min(time.pts))/max(time.pts) mav<- ksmooth(time.pts,full,kernel="box") tsmav<-ts(mav$y,freq=24) x1<-time.pts x2<-time.pts** pqp<-lm(full~x1+x2) tspqp<- ts(fitted(pqp),freq=24) loc<-loess(full~time.pts) tsloc<- ts(fitted(loc),freq=24) spl<-gam(full~s(time.pts)) tsspl<-ts(fitted(spl),freq=24) alltrends<-c(tsmav,tspqp,tsloc,tsspl) ts.plot(full,ylab="Temperature",main="Temperature with Trend Estimations") lines(tsmav,lwd=2,col="red") lines(tspqp,lwd=2,col="orange") lines(tsloc,lwd=2,col="green") lines(tsspl,lwd=2,col="purple")
5 / cat("MAV MAPE:",mean(abs((full-tsmav)/full))100, "\nPQP MAPE:",mean(abs((full-tspqp)/full))100, " nLP MAPE:",mean(abs((full-tsloc)/full))100, "\nSplines MAPE:",mean(abs((full-tsspl)/full))100)
44.55682 ## PQP MAPE: 31.54434 ## LP MAPE: 33.
res.1 <- full - tsmav res.2 <- full
7 / acf(res.4, lag.max = 150, main='Splines Residuals')
8 / acf(res.2, lag.max = 150, main='Local Polynomial Residuals')
10 / 29 1d: Analysis Discussion Based on the analysis of both the plot and MAPE values for the trend-seasonality estimation methods, we can make several observations: MAPE values indicate the performance of both models in terms of prediction accuracy. The non-parametric method has a lower MAPE (0.3058036) compared to the parametric method (0.491038). This suggests that the non-parametric model provides a better fit to the data in terms of forecasting accuracy, making it a more effective model for capturing the underlying trend and seasonality in the time series. The plot of the two trend-seasonality methods also reinforces this conclusion, showing that the abridged<- ts(abridged$temp,freq=24) time.pts = c(1:length(abridged)) time.pts = c(time.pts - min(time.pts))/max(time.pts) spl2<- gam(abridged~s(time.pts)) tsspl2<-ts(fitted(spl2),freq=24) ts.plot(abridged,main="Splines Comparison",ylab="Temperature") lines(tsspl,lwd=2,col="purple") lines(tsspl2,lwd=2,col="yellow") legend(x=43,y=5,legend=c("Full Splines","Abridged
11 / 29 non-parametric method better captures the fluctuations in the data, while the parametric model may be oversimplifying or missing some features.
13 / 29 cat("Parametric MAPE:",mean(abs((abridged - fitted(modelppr)) / abridged)) * 100, "\nNon-Parametric MAPE:",mean(abs((abridged - fitted(modelnp)) / abridged)) * 100)
res.5 <- abridged - ts(fitted(modelppr),freq=24) res.6 <- abridged - ts(fitted(modelnp),freq=24) acf(res.5, lag.max = 150, main='Parametric Residuals')
14 / 29 acf(res.6, lag.max = 150, main='Non-Parametric Residuals')
16 / 29 Prophet may be better at fitting the data for forecasting purposes. The lower MAPE means that Prophet’s predictions are closer to the actual observed values on average.
17 / 29 The Prophet model seems to be a better choice when it comes to tracking the peaks and providing better predictive accuracy, but both models still suffer from residual autocorrelation and non-stationarity. To address these issues, you might consider combining Prophet’s flexibility with additional methods to better handle residual autocorrelation, such as by adding an ARIMA or SARIMA model on the residuals. # Load and prepare the data full <- read.csv("Midterm 1 Data.csv") abridged <- full[1:1440,] # Prepare the data for Prophet full_p <- data.frame(ds = abridged$timestamp, y = abridged$temp) # Initialize Prophet model m <- prophet() # Add seasonalities m <- add_seasonality(m, name = 'hourly', period = 24, fourier.order = 8) m <- add_seasonality(m, name = 'weekly', period = 7, fourier.order = 3) m <- add_seasonality(m, name = 'monthly', period = 30.5, fourier.order = 5) m <- add_seasonality(m, name = 'yearly', period = 365.25, fourier.order = 10) # Fit the model to the data prophet_model <- fit.prophet(m, full_p) # Forecast using the trained model forecast <- predict(prophet_model, full_p) # Plot the forecast plot(prophet_model, forecast)
19 / 29 # Find significant changepoints delta <- prophet_model$params$delta significant_changepoints <- prophet_model$changepoints[abs(delta) > 0.00001] cat("Detected significant changepoints at: ", significant_changepoints, "\n")
20 / 29 # Refit the model with significant changepoints m <- prophet(changepoints = significant_changepoints) m <- add_seasonality(m, name = 'hourly', period = 24, fourier.order = 8) m <- add_seasonality(m, name = 'weekly', period = 7, fourier.order = 3) m <- add_seasonality(m, name = 'monthly', period = 30.5, fourier.order = 5) m <- add_seasonality(m, name = 'yearly', period = 365.25, fourier.order = 10) prophet_model <- fit.prophet(m, full_p) forecast <- predict(prophet_model, full_p) # Plot the forecast with the new model plot(prophet_model, forecast) prophet_plot_components(prophet_model, forecast)