I'm just learning how to deal with model.matrix
. For example, to create out-of-sample predictions I extract the formula from my model, say it's a linear model. Using the function formula(mymodel)
extracts that:
form <- formula(y ~ x1 + x2 * x3)
Now, to create predictions I need a model.matrix without my y
. I could type that by hand:
X <- model.matrix(~ x1 + x2 * x3, data=out.of.sample.data)
However, is there a way using, for example, update
to get rid of the left part my formula?
Thanks!
It can be done with update
by setting the response variable to NULL
:
form <- formula(y ~ x1 + x2 * x3)
newform <- update(form, NULL ~ .)
This is how I usually do this. I'm not aware of a built-in function for this.
df = data.frame(y=rnorm(10), x1=rnorm(10), x2=rnorm(10), x3=rnorm(10))
mymodel = lm(y ~ x1 + x2 + x3, df)
form_vars_only =
formula(paste("~",strsplit(as.character(formula(mymodel)),"~")[[3]]))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.