添加新的回归线,但将先前运行中的回归线保留在R中 [英] Add the new regression line but keep the regression lines from previous runs in R

查看:149
本文介绍了添加新的回归线,但将先前运行中的回归线保留在R中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a function called TPN (R code is below the picture). When you run this function, it produces two plots (see picture below). The bottom-row plot samples from the top-row plot and then adds a red regression line. Each time you run the TPN function, the bottom-row plot produces a new red-colored regression line.

底部行图中,我想知道是否有一种方法可以使每次运行TPN函数时都保持先前运行的回归线(请参见下图) )?

In the bottom-row plot, I was wondering if there is a way I could KEEP the regression lines from previous runs each time I run the TPN function (see picture below)?

也就是说,每次我运行新的TPN函数时,都会将前一次运行的回归线保留在原处(出于区分目的,可能使用红色"以外的颜色) ,而新的回归线是否刚刚添加到他的底部行图中?

That is, each time that I run a new TPN function the regression line from a previous run is kept in its place (probably in a color other than "red" for distinction purposes), and the new regression line is just added to he bottom-row plot?

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

推荐答案

这不是那么容易.我做了另一个功能,也编辑了第一个功能.

It ain't that easy. I made another function and edit the first one as well.

总结我所做的事情:

我做了第一个在其末尾设置par(new = TRUE)的功能.另外,将底部行图中的点颜色设置为仅用于格式化的白色.您可以根据需要摆脱col = 'white', bg = 'white'.

I made the first function to set par(new = TRUE) at the end of it. Also, set the color for points in the bottom row plot to be white only for formatting. You can get rid of col = 'white', bg = 'white' if you wish.

然后,在第二个函数中,不会绘制上排图,并且不会从每个测试"中将yaxis添加到下排图.

Then, in the second function top row plot does not get plotted and yaxis won't be added to the bottom row plot from each "test".

看下面:

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

然后您的测试将如下所示:

Then your test would be like this:

## TEST HERE:
TPN()

TPN2()
TPN2()
TPN2()

这是输出:

这篇关于添加新的回归线,但将先前运行中的回归线保留在R中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆