【发布时间】:2015-01-11 14:48:47
【问题描述】:
我想使用 10 折交叉验证方法,它测试哪种多项式形式(第一、第二或 三阶)给出了更好的拟合。我想将我的数据集分成 10 个子集,并从 10 个数据集中删除 1 个子集。导出没有该子集的回归模型,使用导出的回归模型预测该子集的输出值,并计算残差。最后对每个子集重复计算例程,并对结果残差的平方求和。 我已经在 Matlab 2013b 上编写了以下代码,它对数据进行采样并测试训练数据的回归。我被困在如何对每个子集重复此操作以及如何比较哪种多项式形式更适合。
% Sample the data
parm = [AT];
n = length(parm);
k = 10; % how many parts to use
allix = randperm(n); % all data indices, randomly ordered
numineach = ceil(n/k); % at least one part must have this many data points
allix = reshape([allix NaN(1,k*numineach-n)],k,numineach);
for p=1:k
testix = allix(p,:); % indices to use for testing
testix(isnan(testix)) = []; % remove NaNs if necessary
trainix = setdiff(1:n,testix); % indices to use for training
%train = parm(trainix); %gives the training data
%test = parm(testix); %gives the testing data
end
% Derive regression on the training data
Sal = Salinity(trainix);
Temp = Temperature(trainix);
At = parm(trainix);
xyz =[Sal Temp At];
% Fit a Polynomial Surface
surffit = fit([xyz(:,1), xyz(:,2)],xyz(:,3), 'poly11');
% Shows equation, rsquare, rmse
[b,bint,r] = fit([xyz(:,1), xyz(:,2)],xyz(:,3), 'poly11');
【问题讨论】:
标签: matlab polynomial-math cross-validation