我只是在学习如何处理model.matrix. 例如,为了创建样本外预测,我从我的模型中提取公式,假设它是一个线性模型。使用该函数formula(mymodel)提取:
form <- formula(y ~ x1 + x2 * x3)
Run Code Online (Sandbox Code Playgroud)
现在,要创建预测,我需要一个没有我的y. 我可以手动输入:
X <- model.matrix(~ x1 + x2 * x3, data=out.of.sample.data)
Run Code Online (Sandbox Code Playgroud)
但是,有没有办法使用,例如,update摆脱我的公式的左侧部分?
谢谢!
小智 8
可以update通过将响应变量设置为NULL:
form <- formula(y ~ x1 + x2 * x3)
newform <- update(form, NULL ~ .)
Run Code Online (Sandbox Code Playgroud)