function this = soinitialize(this, yvec, regmat, varargin)
% SOINITIALIZE Initialize parameters of the SVM regression function

%  Copyright 2021-2022 The MathWorks, Inc.

% Check for availability of SMLT
if ~idpack.isStatsInstalled
   error(message('Ident:idnlfun:regmodelRequiresStats','idSupportVectorMachine'));
end

localErrorCheckSoinitialize(yvec,regmat);
nobsd = size(yvec,1);

UnknownInputDim = isempty(this.Inputs);
[nobs, nu] = size(regmat);

if nobsd~=nobs
   error(message('Ident:estimation:soinitialize2'))
end

if UnknownInputDim
   % (testing mode)
   this = initializeIOs(this,"u"+(1:nu),"y");
else
   % do nothing; names are already set
end

% Form PV pairs
topt  = this.EstimationOptions;

PV = getPVFromNonlinearFunction(this);
PV = getPVFromEstimationOptions(topt,PV);

% Estimate RegressionSVMModel
this.prvNonlinearFcn = fitrsvm(regmat,yvec,PV{:});

% Update EstimationOptions and NonlinearFunction
this.EstimationOptions = updateEstOptionsPostEstimation(...
   this.EstimationOptions, this.prvNonlinearFcn);
this = updateNonlinearFunctionPostEstimation(this);
end

%--------------------------------------------------------------------------
function localErrorCheckSoinitialize(yvec, regmat)
if isempty(yvec) || isempty(regmat)
   error(message('Ident:estimation:emptyData'))
end

if ~idpack.isRealVector(yvec) || ~idpack.isRealMatrix(regmat)
   error(message('Ident:estimation:soinitialize1'))
end
end
