![](/img/trans.png)
[英]“factor has new level” error during prediction using lm-model in SQL Server R Services
[英]How to put a loop in the summary() of a lm-model?
我正在嘗試分析不同分布的影響。 我創建了兩種類型的分布:mu =累積[epsilon N(0,1)] + eta N(0,1)mu =累積[epsilon N(0,1)] + eta N(0,4)
我已經創建了線性回歸模型,並希望輸出摘要,因為我對R ^ 2感興趣。 由於每次創建循環時數據都是不同的,因此摘要要進行多次,並且打印結果是所有摘要的平均值。
我根本不知道如何創建該循環。
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)
像這樣
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.