I have two variables, time (T) with two values and region (R) with 10 values, and thus two sets of dummy variables. how can I get interaction dummies
dummyvar(T)*dummyvar(R) ?
Best Answer
Here is a specific example of how to do it. If you need to generalize it and can't figure that out, let me know. The idea is to multiply the first column of d1 by each column of d2, then the second column of d1 by each column of d2:
d1 = dummyvar(T); d2 = dummyvar(R);
d1(:,[1 1 1 2 2 2]).*d2(:,[1 2 3 1 2 3])
Another thing to consider is the x2fx function, using something like this:
x2fx([T R],'interaction',1:2)
This requests that both T and R be treated as categorical, and that all columns for an interaction model be computed. You'll see the result has a column of ones, then dummy columns for T and R separately, then columns for the interaction. The idea is that these will be used in regression, so certain columns are omitted so that the full matrix is not overdetermined.
I'm not sure why you're using string characters instead of numbers but I'll assume you have a reason for that. Here's how to identify the rows of tbl.multicol where the second column contains the character representation of i_pts (which is numeric). Then flip those rows.
rowsToSwitch = strcmp(tbl.multicol(:,2),num2str(i_pts)); %logical vector identifying rows to flip
tbl.multicol(rowsToSwitch,:) = fliplr(tbl.multicol(rowsToSwitch,:)); % Flip those rows
Best Answer