以编程方式添加公式术语 [英] Add formula terms programmatically
本文介绍了以编程方式添加公式术语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在运行模拟,需要测试不同大小公式的性能.从具有 V1、V2、V3...Vk 列的数据框中绘制,我需要以编程方式创建公式,例如 fit <- ols(y ~ V1 + V2 + V3, data=dataframe)
等等向前.
I am running a simulation and need to test the performance of different sized formulas. Drawing from data frame with columns V1, V2, V3... Vk, I need to programmatically create formulas like fit <- ols(y ~ V1 + V2 + V3, data=dataframe)
and so forth.
如何对公式进行编码以缩放公式的长度?
How can I code the formula to be able to scale the length of the formula?
推荐答案
您可以将文本转换为公式:
You can convert text to a formula:
vars <- c(1, 2, 4)
formula.text <- paste0("y ~ V", paste(vars, collapse=" + V"))
formula.text
# [1] "y ~ V1 + V2 + V4"
library(rms)
fit <- ols(as.formula(formula.text), data=dataframe)
这篇关于以编程方式添加公式术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文