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

时间:2017-05-17 19:46:04

标签: r layout plot bootstrapping par

背景

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

问题

底行图中,我想知道每次运行TPN函数时是否有办法保留以前运行的回归线(参见下面的图片)?

也就是说,每次运行新的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()

2 个答案:

答案 0 :(得分:3)

这并不容易。我做了另一个功能并编辑了第一个功能。

总结我的所作所为:

我做了第一个功能,在它的末尾设置par(new = TRUE)。此外,将底行图中的点的颜色设置为白色仅用于格式化。如果愿意,你可以摆脱col = 'white', bg = 'white'

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

看下面:

############## 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

答案 1 :(得分:2)

执行您想要的操作的一种简单方法是更改​​主效果(目前无),以返回以前的回归累积副作用< / em>(绘图)到循环这些先前的回归(蓝色)以及当前的回归(红色)。

另一个提示:您可以使用abline(reg=lm(y~x))参数,只在列表中累积lm个对象。没有必要像另一个答案中建议的那样单独存储系数和截距。保留lm对象也是一个好主意,如果你想回去查看平均R平方等等 - 你不能仅使用系数来做到这一点。

您的新功能可能如下所示:

TPN.accum <- function(  each.sub.pop.n = 150, 
                        sub.pop.means = 20:10, 
                        predict.range = 10:0, 
                        sub.pop.sd = .75,
                        n.sample = 2,
                        lm.history = list() # the accumulator
                       ){
  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

  lm.current <- lm(y~x)                     # the current regression

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

  abline(reg = lm.current, col = 'red')     # plot current regression (red)
  for( i in seq_along(lm.history) ){
    abline(reg=lm.history[[i]], col='blue') # plot any previous regressions (blue)
  }
  return(c(lm.history, list(lm.current)))   # append current regression to accumulator
}

要初始化它然后重复运行,只需执行以下操作:

tpn.history <- TPN.accum()
for (i in 1:5) tpn.history <- TPN.accum(lm.history=tpn.history)

您的输出将如下所示:

enter image description here