如何使用循环从线性回归中提取系数输出 [英] How to extract coefficients outputs from a linear regression with loop
问题描述
我想知道如何循环执行n次回归,并且每次使用一组不同的变量时,提取一个data.frame
,其中每一列都是回归,每一行代表一个变量.
I would like to know how I can loop a regression n times, and in each time with a different set of variables, extract a data.frame
where each column is a regression and each row represent a variable.
对于我来说,我的data.frame
为:
dt_deals <- data.frame(Premium=c(1,3,4,5),Liquidity=c(0.2,0.3,1.5,0.8),Leverage=c(1,3,0.5,0.7))
但是我还有另一个解释性的哑变量,称为hubris
,它是二项分布的乘积,均值为0.25.像这样:
But I have another explanatory dummy variable called hubris
, that is the product of a binomial distribution, with 0.25 of mean. Like that:
n <- 10
hubris_dataset <- data.frame(replicate(n, rbinom(4,1,0.25))
从这个意义上讲,我需要对hubris
进行n次模拟,因此我可以对n个具有不同随机二项式分布的集合进行回归,并且每个分布的输出都必须放在data.frame
到目前为止,我可以做到这一点:
In this sense, what I need is to make n simulation of hubris
, so I can, make n regression each one with a different set of random binomial distribution and the output of each distribution needs to be put in a data.frame
So far I could reach this:
# define n as the number of simulations i want
n=10
# define beta as a data.frame to put every coefficient from the lm regression
beta=NULL
for(i in 1:n) {
dt_deals2 <- dt_deals
beta[[i]] <- coef(lm(dt_deals$Premium ~ dt_deals$Liquidity + dt_deals$Leverage + hubris_dataset[,i], data=dt_deals2))
beta <- cbind(reg$coefficients)
}
但是以这种方式,它仅生成第一组系数,而不会为data.frame
再创建十列.
But this way it only generate the first set of coefficient, and doesn't make another ten columns for the data.frame
.
推荐答案
@jogo给出了一种更改for循环方法并使用sapply的想法,并将对象beta更改为list().结果就是这样:
@jogo give an idea to change the for-loop method and use sapply, and change the object beta to list(). This was the result:
beta <- sapply(1:n, function(i) coef(lm(Premium ~ Liquidity +Leverage+ hubris_dataset[,i], data=dt_deals2)))
它奏效了
这篇关于如何使用循环从线性回归中提取系数输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!