Doxygen Source Code Documentation
makemodel.m
Go to the documentation of this file.00001 function v = makemodel(p,ng)
00002 %MAKEMODEL Helper function to make model matrix
00003 % P = max model term size, NG = number of grouping variables
00004 % or
00005 % P = vector of term codes
00006
00007 % We want a matrix with one row per term. Each row has a 1 for
00008 % the variables participating in the term. There will be rows
00009 % summing up to 1, 2, ..., p.
00010
00011 if numel(p)==1
00012 % Create model matrix from a scalar max order value
00013 vgen = 1:ng;
00014 v = eye(ng); % linear terms
00015 for j=2:min(p,ng)
00016 c = nchoosek(vgen,j); % generate column #'s with 1's
00017 nrows = size(c,1); % generate row #'s
00018 r = repmat((1:nrows)',1,j); % and make it conform with c
00019 m = zeros(nrows,ng); % create a matrix to get new rows
00020 m(r(:)+nrows*(c(:)-1)) = 1; % fill in 1's
00021 v = [v; m]; % append rows
00022 end
00023 else
00024 % Create model matrix from terms encoded as bit patterms
00025 nterms = length(p);
00026 v = zeros(nterms,ng);
00027 for j=1:nterms
00028 tm = p(j);
00029 while(tm)
00030 % Get last-numbered effect remaining
00031 lne = 1 + floor(log2(tm));
00032 tm = bitset(tm, lne, 0);
00033 v(j,lne) = 1;
00034 end
00035 end
00036 end