The predict
method for dummyVars
from the caret
library has documentation that clearly states:
"The predict function produces a data frame."
However, every example that I've produced appear to only be a matrix. The following code is an example of this:
>input<-data.frame(id=c(1, 2, 3), direction=c('up', 'down', 'down'))
>dmy<-dummyVars(" ~ .",input)
>output<-predict(dmy, newdata=input)
>output
id direction.down direction.up
1 1 0 1
2 2 1 0
3 3 1 0
>class(output)
[1] "matrix"
>is.data.frame(output)
[1] FALSE
>is.matrix(output)
[1] TRUE
Everything that I can see indicates that the documentation is wrong and that the predict
function is really returning a matrix rather than a data frame. What's going on?
I think you are right and the documentation is wrong. If you look at the source code, the object that is returned is created at line 19 in the function body like this:
x <- model.matrix(Terms, m)
This is a matrix object. There is some more code in the function body after x is created, but all it does is alter the column names and drop the (Intercept)
column. At no point is it converted to a data frame before it is returned.