Code covered by the BSD License  

Highlights from
x2fx_names - compound predictor names for x2fx

x2fx_names - compound predictor names for x2fx

by

 

returns NAMES of columns for the output of x2fx()

res=x2fx_names(PredictorNames,model)
function res=x2fx_names(PredictorNames,model)
%function res = x2fx_name(PredictorNames, model)
%   returns a cell array of "compound" predictor variable names that 
%   correpond to the x2fx'ed matrix of "normal" predictor variable names.  
%   See help for x2fx() in statistics toolbox.  
%   Does not support categories/levels mode in x2fx()
%   [See help for lasso(), which is often fed the output of x2fx(), for the
%   reasons for writing this function in the first place]
%
% Inputs:
%   PredictorNames: cell array of variable names (one per column) of the 
%                   matrix you're feeding into x2fx
%   model: either a string or the mix-matrix -- same as the second 
%          argument to x2fx
%
% Example:
%   x2fx_names({'one','two','three'},'quadratic')
%   returns: 'constant'    'one'    'two'    'three'    'one*two'    'one*three'    'two*three'    'one^2'    'two^2'    'three^2'

%set up a table of primes (don't feel like calling prime() every time)
prime_table = [...
    2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,...
    127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229];  %that's primes(230) for ya. fifty of them.

%check if have enough primes
%can probably do that n/ln(n) thing to figure out how many primes we actually need, but 50 should be enough for anybody (just like 640KB of memory)
if length(prime_table)<length(PredictorNames)  
    error('internal error: increase size of prime table (really mixing more than 50 variables???)')
end

%run the x2fx to mix a row of prime numbers
mix = x2fx(prime_table(1:length(PredictorNames)),model);

%factor each number in the mix to figure out which variable is mixed where
for i = 1:length(mix)
    %the following should work -- it checks if x2fx has returned a constant
    if mix(i)==1
        res{i} = 'constant';
        continue;
    end
    %figure out the prime factors
    factors = factor(mix(i));
    
    %figure out unique factor list so that we return powers: instead of
    % the ugly A*A*A*B we return prettier A^3+B
    u_factors = unique(factors);
    
    %rinse and repeat
    for factor_cnt = 1:length(u_factors)
        factor_power = sum(u_factors(factor_cnt)==factors);
        factor_name = PredictorNames{find(u_factors(factor_cnt) == prime_table,1)};
        if (factor_power)>1
            factor_name = [factor_name,'^',num2str(factor_power)];
        end
        if (factor_cnt == 1)
            res{i} = factor_name;
        else
            res{i} = [res{i},'*',factor_name];
        end
    end
end

Contact us