建立模型时,简短公式需要许多变量 [英] short formula call for many variables when building a model

查看:90
本文介绍了建立模型时,简短公式需要许多变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用lm(...)建立回归模型.我的数据集有很多特征(> 50).我不想将代码编写为lm(output~feature1+feature2+feature3+...+feature70).我想知道编写此代码的简写形式是什么.

I am trying to build a regression model with lm(...). My dataset has lots of features(>50). I do not want to write my code as lm(output~feature1+feature2+feature3+...+feature70). I was wondering what is the short hand notation to write this code.

推荐答案

您可以按照formula帮助页面中的说明使用.. .代表公式中所有其他列".

You can use . as described in the help page for formula. The . stands for "all columns not otherwise in the formula".

lm(output ~ ., data = myData).

或者,使用paste手动构造公式.此示例来自as.formula()帮助页面:

Alternatively, construct the formula manually with paste. This example is from the as.formula() help page:

xnam <- paste("x", 1:25, sep="")
(fmla <- as.formula(paste("y ~ ", paste(xnam, collapse= "+"))))

然后可以将此对象插入回归函数:lm(fmla, data = myData).

You can then insert this object into regression function: lm(fmla, data = myData).

这篇关于建立模型时,简短公式需要许多变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆