Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
jpm-behavior/RunCode.m
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
93 lines (83 sloc)
3.5 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function BestModel = RunCode(RNG, mode, kernel, person, X, pc,y,s) | |
% This function runs the code, so it goes through and creates the model | |
% for the current person | |
% Parameters: | |
% RNG: the random seed integer | |
% mode: the activation function type, 'logistic' or 'linear' | |
% kernel: whether or not there is a direct kernel transformation | |
% person: the ID number of the current person | |
% X: the X dataset | |
% pc: the cleaned dataset vector of person IDs | |
% y: the y dataset | |
% s: the range of sigma to iterate over | |
% Set the random seed | |
rng(RNG); | |
% get the data for the current person | |
index = pc == person; | |
Xp = X(index,:); | |
yp = y(index,1); | |
index_1 = yp == 1; | |
Xp1 = Xp(index_1,:); | |
index_2 = yp == 0; | |
Xp2 = Xp(index_2,:); | |
Xp_0 = [Xp1;Xp2]; | |
Np_1 = size(Xp1,1); | |
Np_2 = size(Xp2,1); | |
yp_0 = [ones(Np_1,1);zeros(Np_2,1)]; | |
K = [Np_1 Np_2]; | |
% If there's not any examples of behavior or the sample size is too | |
% small, do not fit the model and return an empty one | |
if or((sum(yp_0) == 0),(sum(index) < 20)) | |
disp('no cases of behavior or too small sample size') | |
if ~exist('BestModel','var') | |
BestModel.sigma = NaN; | |
BestModel.Best = -1; | |
end | |
return | |
end | |
% Split into training and testing data | |
[Xtrain,ytrain,Xtest,ytest] = SplitData(Xp_0,yp_0,mode); | |
[N,M] = size(Xtrain); | |
% Initialize variables | |
RUNS = 1000; | |
BCR_max = 0; | |
if strcmp(kernel,'yes') | |
m = 15; | |
Prototypes = randsample(N,m); | |
if ~exist('s','var') | |
s = 0.2:0.005:5; | |
end | |
% Iterate through all possible values of sigma | |
for sigma=s | |
Model = GetADALINE_Kernel(Xtrain,ytrain,mode,sigma,m,RUNS,Prototypes); | |
Model = EvaluateModel(Model,Xtrain,Xtest,ytest,mode,kernel,sigma,Prototypes); | |
Model.Prototypes = Prototypes; | |
fprintf('sigma = %.3f, Accuracy(train) = %.4f and Accuracy(test) = %.4f\n',sigma,Model.ACCtr,Model.BCR); | |
if Model.BCR > BCR_max | |
BestModel = Model; | |
BestModel.sigma = sigma; | |
BCR_max = Model.BCR; | |
end | |
end | |
elseif strcmp(kernel,'no') | |
Model = GetADALINE(Xtrain,ytrain,mode,RUNS); | |
Model = EvaluateModel(Model,Xtrain,Xtest,ytest,mode,kernel,[],[]); | |
end | |
% If the best model cannot be found, fill it with empty fields | |
if ~exist('BestModel','var') | |
BestModel = Model; | |
if ~exist('sigma','var') | |
BestModel.sigma = NaN; | |
else | |
BestModel.sigma = sigma; | |
end | |
BestModel.Best = false; | |
else % if the best model can be found, indicate it as such | |
BestModel.Best = true; | |
end | |
% Save training and test sets in the model | |
BestModel.Xtrain = Xtrain; | |
BestModel.ytrain = ytrain; | |
BestModel.Xtest = Xtest; | |
BestModel.ytest = ytest; | |
end |