It can handle one more simplification step
# Rich Heiberger, removing intercept, including haireye data
haireye.df <- cbind(
as.data.frame(haireye),
model.matrix(Freq ~ Hair + Eye, data=haireye,
contrasts.arg=list(Hair=diag(4), Eye=diag(4)))[,-1]
)
haireye.df
On Tue, Dec 30, 2
On 12/30/14 7:16 PM, Richard M. Heiberger wrote:
I like this very simple version. Note that you don't need as.data.frame().
model.matrix(Freq ~ Hair + Eye, data=haireye,
contrasts.arg=list(Hair=diag(4), Eye=diag(4)))
Thanks to all who replied to this question. model.matrix() was what I
had mi
"More comprehensible" depends on context, which we don't have. You could
be simply trying to illustrate the logic of the transformation (solution 1
below) or providing a recipe which by its brevity is (perhaps?) memorable
(solution 2 below).
Solution 1:
appendDummys <- function( DF, keycol, d
I like this very simple version. Note that you don't need as.data.frame().
model.matrix(Freq ~ Hair + Eye, data=haireye,
contrasts.arg=list(Hair=diag(4), Eye=diag(4)))
On Tue, Dec 30, 2014 at 6:05 PM, Michael Friendly wrote:
> In a manuscript, I have the following code to illustrate dummy codin
On Dec 30, 2014, at 3:05 PM, Michael Friendly wrote:
> In a manuscript, I have the following code to illustrate dummy coding of two
> factors in a contingency table.
>
> It works, but is surely obscured by the method I used, involving outer() to
> find equalities and 0+outer()
> to convert to
Hi Michael,
At first I thought that as.numeric() would do it, but that loses the matrix
structure. Here are two solutions; I think that I prefer the second.
--- snip
> (dummy.hair <- outer(haireye.df$Hair,
+ levels(haireye.df$Hair), function(x, y) as.numeric(x
6 matches
Mail list logo