interaction dummies

2 visualizaciones (últimos 30 días)
Rabeya
Rabeya el 20 de Abr. de 2012
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) ?
  3 comentarios
Walter Roberson
Walter Roberson el 20 de Abr. de 2012
Is this possibly a question about covariance ?
Rabeya
Rabeya el 20 de Abr. de 2012
say T=[1;1;1;2;1;2;2;1;2;1] and R=[1;3;1;2;2;1;3;3;1;2]. So,
group =
1 1
1 3
1 1
2 2
1 2
2 1
2 3
1 3
2 1
1 2
I need dummy variables depending on the rows of group, e.g.,row1 reads as a person in time 1 region 1, row2 a person in time 1 region 3,..etc.

Iniciar sesión para comentar.

Respuesta aceptada

Tom Lane
Tom Lane el 23 de Abr. de 2012
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.

Más respuestas (1)

Walter Roberson
Walter Roberson el 20 de Abr. de 2012
group = [T, R];
row = mat2cell(group, ones(1, length(T)), 2);
Then row{1} would be your row1, row{2} would be your row2, and so on.
If you need automatically generated variables named row1, row2, and so on... then you are probably not programming optimally.
  3 comentarios
Walter Roberson
Walter Roberson el 21 de Abr. de 2012
bsxfun() to do the multiplications.
sortrows(group_dummy, -1)
to sort based upon the first column in descending order
In this context, what do you mean by dummyvar(T) and dummyvar(R) ? T and R are numeric column vectors, right? And they are both 10 elements long in your example, so why would the number of columns in the dummy variables differ? Does it have to do with the number of unique values in each?
Rabeya
Rabeya el 21 de Abr. de 2012
yes, dummyvar is the Matlab command to generate dummy(categorical) variables. It takes the number of unique values in each.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by