function [correlation,viaCorrelation]=ridgeCVvia(savePath,data,via,goal,k,ridgeParams,eegOffset)
    load(savePath);
    clear classification;
    eeg=false;
    if strcmp(data,'EEG')
        eeg=true;
        trainingData=trainingDataEEG(eegOffset+1:end,:);
    elseif strcmp(data,'EMG')
        trainingData=trainingDataEMG;
    elseif strcmp(data,'LF')
        eeg=true;
        trainingData=trainingDataEEGlf(eegOffset+1:end,:);
    else
        error('only EEG, EMG and LF are valid inputs for data');
    end
    
    factor=size(trainingDataEMG,1)/(size(trainingData,1)+eegOffset);


    if strcmp(via,'Autoenc')
        viaData=shiftingMean(synergiesAutoenc,factor);
    elseif strcmp(via,'PCA')
        viaData=shiftingMean(synergiesPCA,factor);
    elseif strcmp(via,'NNMF')
        viaData=shiftingMean(synergiesNNMF,factor);
    else
        error('only Autoenc, PCA nad NNMF are valid inputs for via');
    end
    
        
    if strcmp(goal,'kin')
        predicted=shiftingMean(kinematics,factor);
    elseif strcmp(goal,'EMG')
        predicted=trainingDataEMG;
    else
        error('only kin and EMG are valid inputs for goal');
    end

    clear trainingDataEEG;
    clear trainingDataEEGlf;
    clear trainingDataEMG;
    clear kinematics;
    clear synergies*;

    if eeg
        viaData=viaData(1:end-eegOffset,:);
        predicted=predicted(1:end-eegOffset,:);
    end

    viaCorr=zeros([k,size(viaData,2)]);
    finalCorr=zeros([k,size(predicted,2)]);
    
    randMap=randperm(size(trainingData,1));
    for i=1:k
        leaveData=trainingData(mod(randMap,k)==i-1,:);
        leaveVia=viaData(mod(randMap,k)==i-1,:);
        leavePred=predicted(mod(randMap,k)==i-1,:);
        remainingData=trainingData(mod(randMap,k)~=i-1,:);
        remainingVia=viaData(mod(randMap,k)~=i-1,:);
        remainingPred=predicted(mod(randMap,k)~=i-1,:);
        viaDataPredicted=zeros(size(viaData));
        
        coeffs=zeros(size(viaData));
        for j=1:size(viaData,2)
            coeffs(:,j)=ridge(remainingVia(:,j),remainingData,ridgeParams(1),0);

            via=coeffs(1,j)+leaveData*coeffs(2:end,j);
            viaCorr(i,j)=corr(leaveVia(:,j),via);
            clear via
            viaDataPredicted(:,j)=coeffs(1,j)+trainingData*coeffs(2:end,j);
        end
        
        clear coeffs
        
        viaDataPredictedLeave=viaDataPredicted(mod(randMap,k)==i-1,:);
        viaDataPredictedRemaining=viaDataPredicted(mod(randMap,k)~=i-1,:);

        for j=1:size(predicted,2)

            coeffs=ridge(remainingPred(:,j),viaDataPredictedRemaining,ridgeParams(1),0);

            finalCorr(i,j)=ridgeCorrelation(viaDataPredictedLeave,leavePred,coeffs);

        end
    end
    viaCorrelation=mean(viaCorr,1);
    correlation=mean(finalCorr,1);
end