避免在带有BS()项的模型公式中编写大量列名称
我想在拟合logistic回归模型时在数据集中使用bs
函数。
df <- data.frame(a = c(0,1), b = c(0,1), d = c(0,1), e = c(0,1),
f= c("m","f"), output = c(0,1))
library(splines)
model <- glm(output~ bs(a, df=2)+ bs(b, df=2)+ bs(d, df=2)+ bs(e, df=2)+
factor(f) ,
data = df,
family = "binomial")
在我的实际数据集中,我需要将bs()
应用于比此示例更多的列。有没有办法在不编写所有术语的情况下做到这一点?
I want to use bs
function for numerical variables in my dataset when fitting a logistic regression model.
df <- data.frame(a = c(0,1), b = c(0,1), d = c(0,1), e = c(0,1),
f= c("m","f"), output = c(0,1))
library(splines)
model <- glm(output~ bs(a, df=2)+ bs(b, df=2)+ bs(d, df=2)+ bs(e, df=2)+
factor(f) ,
data = df,
family = "binomial")
In my actual dataset, I need to apply bs()
to way more columns than this example. Is there a way I can do this without writing all the terms?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以使用
sprintf
的某些字符串操作,以及Remalulate
:如果要使用其他
df
和级别
code 对于每个样条,它也很简单(请注意,df
不能小于geger
)。是的。这对安全有好处。当然,如果OP希望将所有数值变量包括在“输出”作为预测因素之外,则是一种自动方式。
We can use some string manipulation with
sprintf
, together withreformulate
:If you want to use a different
df
anddegree
for each spline, it is also straightforward (note thatdf
can not be smaller thandegree
).Yes. This is good for safety. And of course, an automatic way if OP wants to include all numerical variables other than "output" as predictors.