如何使用循环在R中进行线性回归 [英] how to use loop to do linear regression in R

查看:56
本文介绍了如何使用循环在R中进行线性回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用诸如for循环或应用函数在R中进行线性回归.我有一个包含诸如crim,rm,ad,wd之类的变量的数据框.我想对每个其他变量进行简单的线性回归分析.

I wonder if I can use such as for loop or apply function to do the linear regression in R. I have a data frame containing variables such as crim, rm, ad, wd. I want to do simple linear regression of crim on each of other variable.

谢谢!

推荐答案

如果您真的要这样做,那么对 lapply()来说,它就显得微不足道了.使其在 df 的其他列上循环".自定义函数将每个变量依次作为 x 并拟合该协变量的模型.

If you really want to do this, it's pretty trivial with lapply(), where we use it to "loop" over the other columns of df. A custom function takes each variable in turn as x and fits a model for that covariate.

df <- data.frame(crim = rnorm(20), rm = rnorm(20), ad = rnorm(20), wd = rnorm(20))

mods <- lapply(df[, -1], function(x, dat) lm(crim ~ x, data = dat))

mods 现在是 lm 对象的列表. mods names 包含用于拟合模型的协变量的名称.这样做的主要缺点是所有模型都使用变量 x 进行拟合.付出更多的努力可能可以解决此问题,但我怀疑付出的时间是否值得.

mods is now a list of lm objects. The names of mods contains the names of the covariate used to fit the model. The main negative of this is that all the models are fitted using a variable x. More effort could probably solve this, but I doubt that effort is worth the time.

如果您只是选择可能令人怀疑的模型,则还有其他方法可以实现此目的.例如,通过 leaps 包及其 regsubsets 函数:

If you are just selecting models, which may be dubious, there are other ways to achieve this. For example via the leaps package and its regsubsets function:

library("leapls")
a <- regsubsets(crim ~ ., data = df, nvmax = 1, nbest = ncol(df) - 1)
summa <- summary(a)

然后,例如 plot(a)将显示哪个模型是最佳".

Then plot(a) will show which of the models is "best", for example.

如果我了解您想要的内容(是协变量,而其他变量是您要使用 crim 进行预测/建模的响应),那么您就不会需要一个循环.您可以使用标准 lm()中的矩阵响应来做到这一点.

If I understand what you want (crim is a covariate and the other variables are the responses you want to predict/model using crim), then you don't need a loop. You can do this using a matrix response in a standard lm().

使用一些虚拟数据:

df <- data.frame(crim = rnorm(20), rm = rnorm(20), ad = rnorm(20), wd = rnorm(20))

我们通过 cbind()创建一个矩阵或多元响应,将我们感兴趣的三个响应变量传递给它.对 lm 的调用的其余部分是与单变量响应完全相同:

we create a matrix or multivariate response via cbind(), passing it the three response variables we're interested in. The remaining parts of the call to lm are entirely the same as for a univariate response:

mods <- lm(cbind(rm, ad, wd) ~ crim, data = df)
mods 

> mods

Call:
lm(formula = cbind(rm, ad, wd) ~ crim, data = df)

Coefficients:
             rm        ad        wd      
(Intercept)  -0.12026  -0.47653  -0.26419
crim         -0.26548   0.07145   0.68426

summary()方法为每个响应生成标准的 summary.lm 输出.

The summary() method produces a standard summary.lm output for each of the responses.

这篇关于如何使用循环在R中进行线性回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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