MATLAB: Example of using crossval function with ar in matlab

MATLAB

Hey,
I have asked this question already but I didn't get it to work even though I had some advice…
Here is an example from Matlab's documentation:
load('fisheriris');
y = species;
X = meas;
cp = cvpartition(y,'k',10); % Stratified cross-validation
classf = @(XTRAIN, ytrain,XTEST)(classify(XTEST,XTRAIN,...
ytrain));
cvMCR = crossval('mcr',X,y,'predfun',classf,'partition',cp)
cvMCR =
0.0200
How can I do this same with the ar-function? Can someone show me the ACTUAL code? I tried this using ar, but I only got different kind of errors…would save a lot of time if someone would just show the code.
I have a vector of values such as:
temperatureData =
-1,4 -0,95 -0,47 -0,16 0,15 0,4 0,34 0,42 0,48 0,56 0,73 0,78 0,86 1,06 1,19 1,3 1,41 1,25 1,17 1,14 0,92 1,3 1,17 1,22 1,25 1,49 1,63 1,84 1,95 1,87 1,95 2,06 2,14
I have created the AR-model doing this:
ar(temperatureData, orderOfModel)
Now I would just need to cross validate the model with the same data. Any examples?

Best Answer

Hi Edvard, There is no such built in methods to cross validate your AR model. The example you show is for a classifier where it makes sense to randomly split your data in to k = 10 parts in order to train your classifier. This is the case when the order of your data does not matter and you can chop it up into random 10 parts. you can do this manually by dividing your code and calling the training function. The crossval is a nice and easy function to call that does for you.
Crossvalidation for an AR model means you need to maintain the order simply because AR is serially autocorrelated, here take a look at the first equation: http://www.mathworks.com/help/econ/specify-ar-models.html So you can randomly chop it up, you need to take serial chunks: tempData(1:n) , tempData(n+1,m) , tempData(m+1,p) for some m,n,p and then train on or build your AR model for the first two and then test it on the 3rd, this method is called leave-one-out cross validation. Unfortunately there is not built in method, you will have to do it yourself. This information should help you write your own code: