R:具有特定范围变量的多元线性回归 [英] R: Multiple Linear Regression with a specific range of variables

查看:138
本文介绍了R:具有特定范围变量的多元线性回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它看起来很简单,但是我不知道如何用R编写代码. 我有一个带有约100个变量的数据框(df),我想在响应(即我的第一个变量(Y))和变量25至60作为回归变量之间进行多元回归.问题是我不想像这样写每个变量名:

It appears simple, but I don't know how to code it in R. I have a dataframe (df) with ~100 variables, and I would like to do a multiple regression between the response which is my First variable (Y) and the variables 25 to 60 as regressors. The problem is that I don't want to write each variable name like:

lm(Y~var25+var26+.......var60, data=df)

我想使用[,​​25:60]之类的东西来选择一个完整的范围.我已经尝试过了,但是没有用:

I would like to use something like [, 25:60] to select a complete range. I have tried it but doesn't works:

test <- lm(Y~df[, 25:60], data=df)
summary(test)

有什么主意吗?

推荐答案

您可以通过仅选择那些列来subset,然后执行lm.

You could subset the dataset by selecting only those columns, and then do the lm.

lm(Y~., data=df1[c(1,25:60)])

假设,如果您需要var25var60,并且数据按列名排序

Suppose, if you need var25 to var60 and if the data is ordered by column names

lm(Y~., data=df1[c(1,26:61)])   

或者另一个选择是使用paste创建公式

Or another option would be to use paste to create the formula

lm(paste("Y ~", paste(paste0('var', 25:60), collapse="+")), data=df1)

数据

set.seed(24)
df1 <- as.data.frame(matrix(sample(1:80, 20*101, replace=TRUE),
   ncol=101, dimnames=list(NULL, c('Y', paste0('var', 1:100)))))

这篇关于R:具有特定范围变量的多元线性回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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