gpt4 book ai didi

r - 添加新的回归线,但保留 R 中先前运行的回归线

转载 作者:行者123 更新时间:2023-12-02 08:13:33 25 4
gpt4 key购买 nike

背景

我有一个名为 TPN 的函数(R 代码在图片下方)。当您运行此函数时,它会生成两个图(参见下图)。底行图从顶行图采样,然后添加红色回归线。每次运行TPN函数时, 底行图生成一条新的红色回归线

问题

底行图中,我想知道每次运行TPN函数时是否有一种方法可以保留之前运行的回归线(< strong>见下图)?

也就是说,每次我运行一个新的 TPN 函数时,前一次运行的回归线都保留在它的位置(为了区分目的,可能使用“红色”以外的其他颜色),新的回归线刚刚添加到底行图中?

enter image description here

############## Input Values #################
TPN = function( each.sub.pop.n = 150,
sub.pop.means = 20:10,
predict.range = 10:0,
sub.pop.sd = .75,
n.sample = 2 ) {
#############################################
par( mar = c(2, 4.1, 2.1, 2.1) )

m = matrix( c(1, 2), nrow = 2, ncol = 1 ); layout(m)
set.seed(2460986)
Vec.rnorm <- Vectorize(function(n, mean, sd) rnorm(n, mean, sd), 'mean')

y <- c( Vec.rnorm(each.sub.pop.n, sub.pop.means, sub.pop.sd) )
set.seed(NULL)
x <- rep(predict.range, each = each.sub.pop.n)

plot(x, y, ylim = range(y)) ## Top-Row Plot


sample <- lapply(split(y, x), function(z) sample(z, n.sample, replace = TRUE))
sample <- data.frame(y = unlist(sample),
x = as.numeric(rep(names(sample), each = n.sample)))

x = sample$x ; y = sample$y

plot(x, y, ylim = range(y)) #### BOTTOM-ROW PLOT

abline(lm(y ~ x), col = 'red') # Regression Line

}
## TEST HERE:
TPN()

最佳答案

没那么简单。我制作了另一个功能并编辑了第一个功能。

总结一下我做了什么:

我创建了第一个函数,在它的末尾设置 par(new = TRUE)。此外,将底行图中点的颜色设置为白色,仅用于格式化。如果您愿意,可以去掉 col = 'white', bg = 'white'

然后,在第二个函数中,不会绘制顶行图,并且不会将 yaxis 添加到每个“测试”的底行图中。

往下看:

############## Input Values #################
TPN = function( each.sub.pop.n = 150,
sub.pop.means = 20:10,
predict.range = 10:0,
sub.pop.sd = .75,
n.sample = 2 ) {
#############################################
par( mar = c(2, 4.1, 2.1, 2.1) )

m = matrix( c(1, 2), nrow = 2, ncol = 1 ); layout(m)
set.seed(2460986)
Vec.rnorm <- Vectorize(function(n, mean, sd) rnorm(n, mean, sd), 'mean')

y <- c( Vec.rnorm(each.sub.pop.n, sub.pop.means, sub.pop.sd) )
set.seed(NULL)
x <- rep(predict.range, each = each.sub.pop.n)

par(new = FALSE)
plot(x, y, ylim = range(y)) ## Top-Row Plot


sample <- lapply(split(y, x), function(z) sample(z, n.sample, replace = TRUE))
sample <- data.frame(y = unlist(sample),
x = as.numeric(rep(names(sample), each = n.sample)))

x = sample$x ; y = sample$y

plot(x, y, ylim = range(y), col = 'white', bg = 'white') #### BOTTOM-ROW PLOT
abline(lm(y ~ x), col = 'red') # Regression Line
par(new = TRUE)
}

第二个不绘制第一行:

############## Input Values #################
TPN2 = function( each.sub.pop.n = 150,
sub.pop.means = 20:10,
predict.range = 10:0,
sub.pop.sd = .75,
n.sample = 2 ) {
#############################################
par( mar = c(2, 4.1, 2.1, 2.1) )

m = matrix( c(1, 2), nrow = 2, ncol = 1 ); layout(m)
set.seed(2460986)
Vec.rnorm <- Vectorize(function(n, mean, sd) rnorm(n, mean, sd), 'mean')

y <- c( Vec.rnorm(each.sub.pop.n, sub.pop.means, sub.pop.sd) )
set.seed(NULL)
x <- rep(predict.range, each = each.sub.pop.n)

#par(new = FALSE) #comment-out
#plot(x, y, ylim = range(y)) ##Top-Row Plot #comment-out


sample <- lapply(split(y, x), function(z) sample(z, n.sample, replace = TRUE))
sample <- data.frame(y = unlist(sample),
x = as.numeric(rep(names(sample), each = n.sample)))

x = sample$x ; y = sample$y

plot(x, y, ylim = range(y), axes = FALSE, col = 'white', bg = 'white') ##BOTTOM-ROW PLOT
abline(lm(y ~ x), col = 'blue') # Regression Line
par(new = TRUE)
}

那么你的测试应该是这样的:

## TEST HERE:
TPN()

TPN2()
TPN2()
TPN2()

这是输出:

enter image description here

关于r - 添加新的回归线,但保留 R 中先前运行的回归线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44033567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com