简体   繁体   English

如何在lm模型的summary()中放入循环?

[英]How to put a loop in the summary() of a lm-model?

I'm trying to analyse the effects of different distributions. 我正在尝试分析不同分布的影响。 I created two distributions of the type: mu = cumsum [epsilon N(0,1)] + eta N(0,1) mu = cumsum [epsilon N(0,1)] + eta N(0,4) 我创建了两种类型的分布:mu =累积[epsilon N(0,1)] + eta N(0,1)mu =累积[epsilon N(0,1)] + eta N(0,4)

I have created a linear regression model and want to output the summary, because I'm interested in the R^2. 我已经创建了线性回归模型,并希望输出摘要,因为我对R ^ 2感兴趣。 Since the data is different every time I want to create a loop so that the summary is done multiple times and the printed result is the mean of all summaries. 由于每次创建循环时数据都是不同的,因此摘要要进行多次,并且打印结果是所有摘要的平均值。

I simply don't know how I could create that loop. 我根本不知道如何创建该循环。

eps = rnorm(200,0,1)
eta200_1 = rnorm(200,0,1)
eta200_4 = rnorm(200,0,4)

y0 = cumsum(eps) + eta200_1
y1 = cumsum(eps) + eta200_4

#The model output that shall be repeated
modely0_y1 = lm(y0 ~ y1)
summary(modely0_y1)

Like so 像这样

iterations <- 100
r_squared <- c()

for(i in 1:iterations) {

  eps = rnorm(200,0,1)
  eta200_1 = rnorm(200,0,1)
  eta200_4 = rnorm(200,0,4)

  y0 = cumsum(eps) + eta200_1
  y1 = cumsum(eps) + eta200_4

  modely0_y1 = lm(y0 ~ y1)
  r_squared <- c(r_squared, summary(modely0_y1)$r.squared)


}

r_squared
# [1] 0.8989347 0.7321244 0.5185411 0.7102153 0.4646995 etc

mean(r_squared)
[1] 0.6025012

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM