You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言如何直接从原DataFrame选列构建多变量线性回归模型?

How to Directly Select Columns from a DataFrame for Linear Regression in R

Hey there! I totally get the frustration of having to manually create new data frames 60 times—let's fix this so you can work directly with your original Data_Group_7_8 dataframe.

First, let's break down why your initial code didn't work:

mdl <- lm(select(1:4) ~ select(16:20), data=Data_Group_7_8)

The lm() function's formula interface doesn't recognize select() (a dplyr function) as valid syntax here. It tries to treat select(1:4) as a single variable name, which doesn't exist in your dataframe—hence the error.

Luckily, there are several straightforward ways to select columns directly from your original data without creating intermediate data frames:

Method 1: Use cbind() with Column Indices

This is the simplest approach for your use case. You can use cbind() directly in the formula to group multiple columns as either dependent or independent variables:

# Fit model with columns 16-20 as dependent variables, 1-4 as independent variables
mdl <- lm(cbind(16:20) ~ cbind(1:4), data = Data_Group_7_8)

cbind(16:20) tells R to use all columns from index 16 to 20 as your dependent variables, and cbind(1:4) specifies columns 1-4 as predictors.

Method 2: Combine with() and dplyr::select()

If you prefer using dplyr's syntax, wrap your dataframe in with() to access its columns directly, then use select() to pick your variables:

library(dplyr)

mdl <- with(Data_Group_7_8, lm(cbind(select(., 16:20)) ~ cbind(select(., 1:4))))

The . inside select() refers to the Data_Group_7_8 dataframe, so you can use all of dplyr's column selection logic here (like using column names instead of indices if that's easier).

Method 3: Dynamic Formula Building (Perfect for 60 Iterations)

Since you need to run this analysis 60 times, building formulas dynamically will save you a ton of time. Store your column indices as variables, construct a formula string, then convert it to a formula object:

# Define your column ranges (update these in your loop for each iteration)
dep_cols <- 16:20  # Dependent variables
indep_cols <- 1:4  # Independent variables

# Build the formula string
formula_text <- paste0(
  "cbind(", paste(dep_cols, collapse = ","), ") ~ cbind(", paste(indep_cols, collapse = ","), ")"
)

# Convert to a formula object
model_formula <- as.formula(formula_text)

# Fit the model
mdl <- lm(model_formula, data = Data_Group_7_8)

You can wrap this in a loop (like for() or purrr::map()) where you update dep_cols and indep_cols for each of your 60 analyses.

A quick note: When you use multiple dependent variables, lm() returns an mlm (multivariate linear model) object. Most standard lm functions (like summary(), predict()) work with mlm objects, but some outputs will be formatted slightly differently to account for multiple responses.

内容的提问来源于stack exchange,提问作者Kikovic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:40:59