I'm new to Weka and I would like to forecast using the historical data to estimate the future values. To do so, I'm using weka APIs WekaForecaster in Java, but I find difficult to find APIs that will do forecast based on multiple selected targets. I can do that in Weka explorer, but don't know which Weka API to use (if exists) to do it in Java.
This is the Weka panel with 3 selected attributes:
The forecast is done based on all three attributes and shown as individual results, I can do the same thing in Java, but it includes only 1 attribute, not considering other two and so the result is less accurate.
This is my code...
import java.io.BufferedReader;
import java.io.FileReader;
import org.joda.time.DateTime;
import weka.classifiers.Evaluation;
import weka.classifiers.functions.LinearRegression;
import weka.classifiers.timeseries.WekaForecaster;
import weka.core.Instance;
import weka.core.Instances;
import weka.filters.supervised.attribute.TSLagMaker;
public class ForecastStack {
public static void main(String args[]) throws Exception {
Forecast f = new Forecast();
f.predictTemp();
}
public void predictTemp() throws Exception {
Instances data = new Instances(new BufferedReader(
new FileReader("C:\Users\n.lukic\OneDrive\Desktop\Laooconte\Dati\arff\weather.arff")));
data.setClassIndex(data.numAttributes() - 3); // radim uzorak na temp con index = data.length-3
// build model
LinearRegression rd = new LinearRegression();
rd.buildClassifier(data);
Evaluation eval = new Evaluation(data);
eval.evaluateModel(rd, data);
System.out.println("
");
System.out.println("Mean absolute error = " + eval.meanAbsoluteError());
System.out.println("Root mean squared error = " + eval.rootMeanSquaredError());
System.out.println("
");
WekaForecaster forecaster = new WekaForecaster();
forecaster.setFieldsToForecast("Temperature");
forecaster.setBaseForecaster(rd);
forecaster.getTSLagMaker().setRemoveLeadingInstancesWithUnknownLagValues(true);
forecaster.getTSLagMaker().setTimeStampField("Date"); // date time stamp
forecaster.getTSLagMaker().setMinLag(1);
forecaster.getTSLagMaker().setMaxLag(12); // monthly data
forecaster.getTSLagMaker().setAddMonthOfYear(true);
forecaster.getTSLagMaker().setAddQuarterOfYear(true);
forecaster.buildForecaster(data, System.out);
DateTime currentDt = getCurrentDateTime(forecaster.getTSLagMaker());
// output model
System.out.println("Date ActTemperature PredTemperature Difference");
for (int i = 0; i < data.numInstances(); i++) {
double actualValue = data.instance(i).classValue();
Instance newInst = data.instance(i);
double predSMO = rd.classifyInstance(newInst);
currentDt = advanceTime(forecaster.getTSLagMaker(), currentDt);
double diff = predSMO - actualValue;
System.out.println(currentDt + ", " + actualValue + ", " + predSMO + " " + diff);
}
}
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()));
}
}
This is arff file weather.arff:
@relation weather
@attribute Pressure numeric
@attribute Temperature numeric
@attribute Humidity numeric
@attribute Date date 'yyyy-MM-dd'
@data
1040,0,50,2018-01-01
1033,10,40,2018-02-01
1030,14,40,2018-03-01
1033,15,60,2018-04-01
1020,23,38,2018-05-01
1010,29,36,2018-06-01
1000,30,40,2018-07-01
999,37,45,2018-08-01
970,28,50,2018-09-01
1000,19,68,2018-10-01
1030,15,73,2018-11-01
1040,5,70,2018-12-01
1038,2,82,2019-01-01
1035,6,69,2019-02-01
1030,10,55,2019-03-01
1025,14,50,2019-04-01
1022,21,49,2019-05-01
1000,29,44,2019-06-01
995,34,39,2019-07-01
990,33,49,2019-08-01
999,30,52,2019-09-01
1010,18,60,2019-10-01
1030,8,84,2019-11-01
1033,3,67,2019-12-01
1035,0,88,2020-01-01
1030,4,76,2020-02-01
1028,19,56,2020-03-01
1025,22,55,2020-04-01
1022,27,46,2020-05-01
1010,31,36,2020-06-01
1005,34,50,2020-07-01
1000,38,65,2020-08-01
1004,30,66,2020-09-01
1015,23,72,2020-10-01
1025,10,72,2020-11-01
1040,4,82,2020-12-01
Can someone help me? Any advice will be useful.
Thank you in advance.