In python you can use the dmatrices function from the patsy module when using categorical values for regressions to create 0 and 1 matrices for the categorical values.
Is there a library/function in R that performs the same function?
In python you can use the dmatrices function from the patsy module when using categorical values for regressions to create 0 and 1 matrices for the categorical values.
Is there a library/function in R that performs the same function?
Suppose we have this data frame where columns x and y are numeric and column f is a factor. Then we can run the regression like this and lm
will convert the formula to an appropriate model matrix including 0/1 columns and then run the regression on that:
# test data
set.seed(123)
DF <- transform(data.frame(f = gl(3, 5, labels = letters[1:3]), x = 1:15),
y = rnorm(15, 1:15))
# run regression
fo <- y ~ x + f
lm(fo, DF)
The model matrix is computed in doing the above so there is no need to explicitly compute it but if you want to anyways try this:
# view model matrix
model.matrix(fo, DF)