load spectra; temp = randperm(size(NIR, 1)); P_train = NIR(temp(1:50),:);
T_train = octane(temp(1:50),:); P_test = NIR(temp(51:end),:);
T_test = octane(temp(51:end),:); k = 2;
[Xloadings,Yloadings,Xscores,Yscores,betaPLS,PLSPctVar,MSE,stats] = plsregress(P_train,T_train,k); figure
percent_explained = 100 * PLSPctVar(2,:) / sum(PLSPctVar(2,:));
pareto(percent_explained)
xlabel('主成分')
ylabel('贡献率(%)')
title('PLS:各个主成分的贡献率—Jason niu') N = size(P_test,1);
T_sim = [ones(N,1) P_test] * betaPLS; error = abs(T_sim - T_test) ./ T_test; R2 = (N * sum(T_sim .* T_test) - sum(T_sim) * sum(T_test))^2 / ((N * sum((T_sim).^2) - (sum(T_sim))^2) * (N * sum((T_test).^2) - (sum(T_test))^2)); result = [T_test T_sim error] figure
plot(1:N,T_test,'b:*',1:N,T_sim,'r-o')
legend('真实值','预测值','location','best')
xlabel('预测样本')
ylabel('辛烷值')
string = {'PLS:利用PLS(两个主成分的贡献率就可达100%)提高《测试集辛烷值含量预测结果对比》的准确度—Jason niu';['R^2=' num2str(R2)]};
title(string)