1

我得到了一些绘制多项式回归曲线的代码:请运行以下代码:

 x = rnorm(20)
    y = 7*x^2 - 0.5*x + rnorm(20) 
y.0 = lm(y ~ 1)
   # We'd get the same constant by just doing mean(y), but fitting it as a
   # regression model means functions like residuals() and predict() are
   # available for use later, the same as our other models
abline(h=y.0$coefficients[1])
# Get evenly spaced points for pretty plotting of other models
d = seq(min(x),max(x),length.out=200)
# Fit polynomials of order 1 to 9
# It would be nicer if we let this run from 0 to 9, but R doesn't allow us to do a polynomial of degree 0
for (degree in 1:9) {
    fm = lm(y ~ poly(x,degree))
    # Store the results in models called y.1, y.2, through y.9
    # The assign/paste trick here is often useful for storing results
        # which do not fit well into arrays!
        # check: y.1; y.2;...
    assign(paste("y",degree,sep="."), fm)
    # Plot them, with different line types
    lines(d, predict(fm, data.frame(x=d)), lty=(degree+1))
}

    x.new = rnorm(2e4)
    y.new = 7*x.new^2 - 0.5*x.new + rnorm(2e4)
    plot(x.new,y.new,xlab="x",ylab="y",pch=24,cex=0.1,col="blue")
    curve(7*x^2-0.5*x,col="grey",add=TRUE)  # the old curve
    abline(h=y.0$coefficients[1])
    d = seq(from=min(x.new),to=max(x.new),length.out=200)
    for (degree in 1:9) {
       fm = get(paste("y",degree,sep="."))
       lines(d, predict(fm,data.frame(x=d)),lty=(degree+1))
    }
    points(x,y,col="red")

基本上,我们将在绘图窗口中看到绘图的最终结果,具有不同的线条样式。这是我的问题:有没有办法在for上面的循环中插入一些 R 代码,以便在运行for循环后,我可以手动更改何时显示下一行绘图?谢谢!

4

1 回答 1

1

如果您每次都制作新图,则可以使用grDevices::devAskNewPage(ask = TRUE).

因为您要添加到单个绘图中,所以可以使用readline. 请注意,您的代码有一个错误(abline应该在绘制后调用),所以我输入了一个plot(y).

myFun <- function() {
     x = rnorm(20)
        y = 7*x^2 - 0.5*x + rnorm(20)
    y.0 = lm(y ~ 1)
    plot(y)
       # We'd get the same constant by just doing mean(y), but fitting it as a
       # regression model means functions like residuals() and predict() are
       # available for use later, the same as our other models
    abline(h=y.0$coefficients[1])
    # Get evenly spaced points for pretty plotting of other models
    d = seq(min(x),max(x),length.out=200)
    # Fit polynomials of order 1 to 9
    # It would be nicer if we let this run from 0 to 9, but R doesn't allow us to do a polynomial of degree 0
    for (degree in 1:9) {
        fm = lm(y ~ poly(x,degree))
        # Store the results in models called y.1, y.2, through y.9
        # The assign/paste trick here is often useful for storing results
            # which do not fit well into arrays!
            # check: y.1; y.2;...
        assign(paste("y",degree,sep="."), fm)
        # Plot them, with different line types
        readline('Press return to see the next line added')
        lines(d, predict(fm, data.frame(x=d)), lty=(degree+1))
    }

        x.new = rnorm(2e4)
        y.new = 7*x.new^2 - 0.5*x.new + rnorm(2e4)
        plot(x.new,y.new,xlab="x",ylab="y",pch=24,cex=0.1,col="blue")
        curve(7*x^2-0.5*x,col="grey",add=TRUE)  # the old curve
        abline(h=y.0$coefficients[1])
        d = seq(from=min(x.new),to=max(x.new),length.out=200)
        for (degree in 1:9) {
           fm = get(paste("y",degree,sep="."))
           readline('Press return to see the next line added')
           lines(d, predict(fm,data.frame(x=d)),lty=(degree+1))
        }
        points(x,y,col="red")
}
myFun()
于 2012-11-10T09:24:22.890 回答