循环函数在回归函数中添加大量预测变量 [英] Loop function to add large numbers of predictors in regression function

查看:41
本文介绍了循环函数在回归函数中添加大量预测变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改善在回归函数中插入预测变量的方法:

I want to improve the way to insert predictors in a regression function:

fm <- lm(formula= df$dependent_variable ~ df[,2] + df[,3]+ df[,4], data = df)

df = data.frame

df = data.frame

在此示例中,我仅放置了4个预测变量和1个dependent_variable.实际上,我有191个预测变量.我认为我需要一个循环脚本来放置所有这些预测变量.有建议吗?

In this example I put only 4 predictors and 1 dependent_variable. Actually I have 191 predictors. I think I need to a loop script to put all these predictors. Suggestions?

推荐答案

以下是一种可能的解决方案:

Here is one possible solution:

yname<-"DVnamehere"
xnames<-colnames(dat)
xnames<-xnames[-which(xnames==yname)]
formula<-as.formula(paste(yname,"~",paste(xnames,collapse="+")))
model<-lm(formula,data=dat)
summary(model)

虽然这不是循环,但只需要您指定因变量的名称,并将数据集中的其余变量用作预测变量,然后将所有内容都放入回归公式中.这有帮助吗?

While this is not a loop it only requires you specify the name of the dependent variable, and uses the rest of the variables in the data set as the predictors then puts everything the regression formula. Does this help?

这篇关于循环函数在回归函数中添加大量预测变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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