function RegressionResults = regress_rhos(x_dist,sigma,RemoveIndex,Squares)
% Function to regress x on rhos

nObs = size(x_dist,1);

rhos = sigma(:);

x_dist = x_dist(:);


M = [RemoveIndex(:,1)];
N = [RemoveIndex(:,2)];

NewIndex = (N-1)*nObs+M;           
Complex = find(imag(rhos) ~= 0);

NewIndex = [NewIndex;Complex];

 x_dist(NewIndex,:) = [];
 rhos(NewIndex,:) = [];

switch Squares
    
    case 1
        try
        L = fitlm([x_dist x_dist.^2],rhos);
        
        StandErrors = table2array(L.Coefficients(:,2));
        coeffs = table2array(L.Coefficients(:,1));
        
        RegressionResults([1;3;5]) = coeffs;
        RegressionResults([2;4;6]) = StandErrors;
        RegressionResults(7) = L.Rsquared.Ordinary;
        catch
            RegressionResults = nan(1,7);
        end
        
    otherwise
        try
        L = fitlm(x_dist,rhos);
        
        StandErrors = table2array(L.Coefficients(:,2));
        coeffs = table2array(L.Coefficients(:,1));
        RegressionResults(1) = length(x_dist);
        RegressionResults([2;4]) = coeffs;
        RegressionResults([3;5]) = StandErrors;
        RegressionResults(6) = L.Rsquared.Ordinary;
        
        catch
            
        RegressionResults  = nan(1,6);

        end
end
end




