iv <- 1:10
rv <- runif(10)
f <- factor(rep(seq.int(3), c(4L, 4L, 2L)),
labels = c("alice", "bob", "charlie"))
df <- data.frame(iv, rv, f)
mm <- makeModelMatrixFromDataFrame(df)
## create test and train matrices with disjoint factor levels
train.df <- df[1:8,]
test.df <- df[9:10,]
train.mm <- makeModelMatrixFromDataFrame(train.df)
test.mm <- makeModelMatrixFromDataFrame(test.df, attr(train.mm, "drop"))
Run the code above in your browser using DataLab