Search code examples
rtime-seriesforecasting

Trying to forecast the next 24hrs temperature using UCI repository datasets in R programming


Hi I accessed the datasets from the UCI repository http://archive.ics.uci.edu/ml/datasets/Air+Quality

I am trying to predict the next 24hrs temperature.Below is the code which I have written

filling the missing values by NA

library(plyr)
AirQualityUCI[AirQualityUCI==-200.0]<-NA

Replacing the NA by mean of each columns

for(i in 1:ncol(AirQualityUCI)){
 AirQualityUCI[is.na(AirQualityUCI[,i]),i] <- mean(AirQualityUCI[,i], na.rm = TRUE)
}

plot time series

plot(AirQualityUCI$T, type = "l")

How do I set the frequency in hours and predict the temperature of next 24hrs ?

Tempts <- ts(AirQualityUCI)
Temprforecasts <- HoltWinters(Tempts, beta=FALSE, gamma=FALSE)
library(forecast)
accuracy(Temprforecasts,24)

Getting the below error

Error in attr(x, "tsp") <- value : 
  invalid time series parameters specified

Solution

  • library(readxl)
    AirQualityUCI <- read_excel("AirQualityUCI.xlsx") 
    
    library(plyr)
    AirQualityUCI[AirQualityUCI==-200.0]<-NA
    
    #First, limit to the one column you are interested in (make sure data is sorted by time variable before doing this)
    library(data.table)
    temp <- setDT(AirQualityUCI)[,c("T")]
    
    #Replace NA with mean
    temp$T <- ifelse(is.na(temp$T), mean(temp$T, na.rm=TRUE), temp$T)
    
    #Create time series object...in this case freq = 365 * 24 (hours in year)
    Tempts <- ts(temp, frequency = 365*24)
    
    #Model
    Temprforecasts <- HoltWinters(Tempts, beta = FALSE, gamma = FALSE)
    
    #Generate next 24 hours forecast
    library(forecast)
    output.forecast <- forecast.HoltWinters(Temprforecasts, h = 24)