如何在R中为线性模型创建循环 [英] How to create a loop for a linear model in R

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

问题描述

我在这里向您寻求帮助. 我必须对多个因变量使用相同的独立变量集进行一系列OLS回归.

I am here to ask your help. I have to run a series of OLS regression on multiple depended variable using the same set for the independent ones.

即我有一个大小为(1510x5)的数据框,尤其是每个数据框都代表投资组合的回报,我想再次回归同一组因变量(1510x4),在我的案例中这是Carhart模型的因素.既然除了系数的值之外,我对它们的P值和回归R2都感兴趣,有没有办法建立一个循环来允许我存储信息?

I.e. I have a dataframe of size (1510x5), in particular each one represent the return of a portfolio, and I would like to regress it agains the same set of dependent variable (1510x4), which in my case are the factors from the Carhart model. Since, beside the value for the coefficients, I am interested in both their P-value and on the R2 of the regression, is there a way to build a loop that allows me to store the information?

到目前为止,我尝试过的是:

What I have tried so far is:

for (i in 1:ncol(EW_Portfolio)) {
  lmfit <- lm(EW_Portfolio[, i] ~ FFM)
  summary(lmfit_i)
}

希望每次循环重复一次时,我都能看到每个回归的结果.

in the hope that, every time the loop repeated itself, I could see the result of each individual regression.

推荐答案

最简单的方法是将其存储在列表中:

The easiest would be to store it in a list:

resultsList <- list()

for (i in 1:ncol(EW_Portfolio)) {
  lmfit <- lm(EW_Portfolio[, i] ~ FFM)
  resultsList[[i]] <- summary(lmfit_i)
}

然后您可以访问您提到的结果:

You can then access the results you mention:

resultsList[[1]]$coefficients
resultsList[[1]]$r.squared

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

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