Forecast next year temperature value weekly using Weka

Chathurika Madhubhashini :

I am new to Weka. I have weekly data set of temperatures in the last 10 years. Using that dataset I'm going to predict weekly temperature withing next year. Below I have attached the code.

import java.io.*;

import java.util.List;
import weka.core.Instances;
import weka.filters.supervised.attribute.TSLagMaker;
import weka.classifiers.functions.GaussianProcesses;
import weka.classifiers.evaluation.NumericPrediction;
import weka.classifiers.timeseries.WekaForecaster;
import org.joda.time.*;

public class TimeSeriesExample {

public static void main(String[] args) {
    try {
        // path to data set

        Instances temp = new Instances(new BufferedReader(new FileReader("sample-data/weeklyMaxTemp.arff")));

        // new forecaster
        WekaForecaster forecaster = new WekaForecaster();

        // set the targets to forecast
        forecaster.setFieldsToForecast("BMxT");

        forecaster.setBaseForecaster(new GaussianProcesses());

        forecaster.getTSLagMaker().setTimeStampField("Date");

        // if there are not enough values in the recent history, return a
        // negative value indicating the steps to wait
        if (forecaster.getTSLagMaker().getMaxLag() > temp.size()) {
            System.out.println("Not enough recent values to make predictions.");
        }

        // add a week of the year indicator field
        forecaster.getTSLagMaker().setAddMonthOfYear(true);

        // add a quarter of the year indicator field
        forecaster.getTSLagMaker().setAddQuarterOfYear(true);

        // build the model
        forecaster.buildForecaster(temp, System.out);
        forecaster.primeForecaster(temp);

        // forecast for 52 units (weeks) beyond the end of the training data
        List<List<NumericPrediction>> forecast = forecaster.forecast(52, System.out);

        DateTime currentDt = getCurrentDateTime(forecaster.getTSLagMaker());

        // output the predictions
        for (int i = 0; i < 52; ++i) {
            List<NumericPrediction> predsAtStep = forecast.get(i);

            for (int j = 0; j < 1; ++j) {
                NumericPrediction predForTarget = predsAtStep.get(j);
                System.out.print(currentDt + " ->> " + predForTarget.predicted() + " ");
            }
            System.out.println();
            currentDt = advanceTime(forecaster.getTSLagMaker(), currentDt);
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

private static DateTime getCurrentDateTime(TSLagMaker lm) throws Exception {
    return new DateTime((long) lm.getCurrentTimeStampValue());
}

private static DateTime advanceTime(TSLagMaker lm, DateTime dt) {
    return new DateTime((long) lm.advanceSuppliedTimeValue(dt.getMillis()));
}

}

52 means number of weeks of the year.

// forecast for 24 units (weeks) beyond the end of the training data
        List<List<NumericPrediction>> forecast = forecaster.forecast(52, System.out);

When I run the code it give 52 weekly values. But result is starting from 52th week from the last data of training data set.

It means the last day of my training data set is 2015.12.30. Next predicted value should be on 2016.01.06. But resulted data set is starting from after 52 weeks.

How can I fix the issue.

Chathurika Madhubhashini :

Changed as below. We should take the current date-time first. Solved

        DateTime currentDt = getCurrentDateTime(forecaster.getTSLagMaker());

        // forecast units (weeks) beyond the end of the training data
        List<List<NumericPrediction>> forecast = forecaster.forecast(52, System.out);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=322449&siteId=1