在R中运行几个简单的回归 [英] Running several simple Regression in R

查看:85
本文介绍了在R中运行几个简单的回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个与世界发展指标和出生统计有关的188行和65列的数据集.我试图做一个有目的的选择方法来创建回归模型.第一步是查看所有单个的简单线性模型.

So I have a data set that has 188 rows and 65 columns relating to World development indicators and Birth statistics. I am trying to do a purposeful selection method to create a regression model. The first step of this is to look at all of the individual simple linear models.

我的目标是针对我的每个变量在R中运行回归模型.我知道我可以运行lm(x$v30 ~ x$v1),它将为其中一个变量提供回归.但是,我希望能够一步一步完成,并将所有p值拉入表或将它们写入CSV.

my goal is to run regression models in R for for each of my variables against my response. I know I can run lm(x$v30 ~ x$v1) which would give the regression for one of the variables. however, i am hoping to be able to do this in one step and pull all of the p values into a table or write them to a CSV.

我正在遵循此操作,但这并不能很好地提供P值:

I was following this but this does not give the P-values in a nice manner:R loop for Regression

推荐答案

首先,除非您知道自己在做什么,否则我不建议您这样做.其他内容还涉及选择偏见,错误发现率等问题.

First, I don't recommend you doing this unless you know what you are doing. Else read about things like selection bias, false discovery rate, etc.

在下面,我使用虹膜数据集,并将第四列的前三列进行回归.您可以轻松地将其更改为您拥有的数据.

In the following, I am using the iris dataset, and regress the first three columns on the fourth one. You can easily change this to data you have.

使用扫帚包装不是强制性的.如果您不想这样做,请删除tidy`` command in the lapply`函数.

Using the broom package isn't mandatory. If you don't want that, remove tidy`` command in thelapply` function.

library(broom)

list_out <- lapply(colnames(iris)[1:3], function(i)
             tidy(lm(as.formula(paste("Petal.Width ~", i)), data = iris)))

# [[1]]
# term   estimate  std.error statistic      p.value
# 1  (Intercept) -3.2002150 0.25688579 -12.45773 8.141394e-25
# 2 Sepal.Length  0.7529176 0.04353017  17.29645 2.325498e-37
# 
# [[2]]
# term   estimate std.error statistic      p.value
# 1 (Intercept)  3.1568723 0.4130820  7.642242 2.474053e-12
# 2 Sepal.Width -0.6402766 0.1337683 -4.786461 4.073229e-06
# 
# [[3]]
# term   estimate   std.error statistic      p.value
# 1  (Intercept) -0.3630755 0.039761990 -9.131221 4.699798e-16
# 2 Petal.Length  0.4157554 0.009582436 43.387237 4.675004e-86

将它们放入data.frame

Put them into a data.frame

do.call(rbind, list_out)

#          term   estimate   std.error  statistic      p.value
# 1  (Intercept) -3.2002150 0.256885790 -12.457735 8.141394e-25
# 2 Sepal.Length  0.7529176 0.043530170  17.296454 2.325498e-37
# 3  (Intercept)  3.1568723 0.413081984   7.642242 2.474053e-12
# 4  Sepal.Width -0.6402766 0.133768277  -4.786461 4.073229e-06
# 5  (Intercept) -0.3630755 0.039761990  -9.131221 4.699798e-16
# 6 Petal.Length  0.4157554 0.009582436  43.387237 4.675004e-86

这篇关于在R中运行几个简单的回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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