【问题标题】:Understanding the Local Polynomial Regression了解局部多项式回归
【发布时间】:2016-10-13 17:02:12
【问题描述】:

有人能解释一下为什么我在绘图时会得到不同的线条吗?不知怎的,我认为这条线应该是一样的

    data(aircraft)
    help(aircraft)
    attach(aircraft)

    lgWeight <- log(Weight)


    library(KernSmooth)

    # a) Fit a nonparametric regression to data (xi,yi) and save the estimated values mˆ (xi).

    # Regression of degree 2 polynomial of lgWeight against Yr
    op <- par(mfrow=c(2,1))
    lpr1 <- locpoly(Yr,lgWeight, bandwidth=7, degree = 2, gridsize = length(Yr))
    plot(Yr,lgWeight,col="grey", ylab="Log(Weight)", xlab = "Year")
    lines(lpr1,lwd=2, col="blue")
    lines(lpr1$y, col="black")

如何从模型中获取值?如果我打印模型,它会给我$x$y 上的值,但不知何故,如果我绘制它们,与蓝线不同。我需要每个x 的拟合模型(蓝色)的值,有人可以帮我吗?

【问题讨论】:

    标签: r regression


    【解决方案1】:

    拟合模型(蓝色曲线)在lpr1 中正确。正如您所说,正确的 y 值在 lpr1$y 中,正确的 x 值在 lpr1$x 中。

    第二个图看起来像一条直线的原因是因为您只给了plot 函数一个变量lpr1$y。由于您没有指定 x 坐标,R 将自动沿着索引绘制它们,从 1 到 y 变量的长度。

    以下是绘制曲线和直线的两种显式等效方法:

    lines(x = lpr1$x, y = lpr1$y,lwd=2, col="blue")  # plots curve
    lines(x = 1:length(lpr1$y), y = lpr1$y, col="black")  # plot line
    

    【讨论】:

      猜你喜欢
      • 2019-07-04
      • 2020-06-20
      • 1970-01-01
      • 2011-04-23
      • 2019-01-24
      • 2015-11-21
      • 2021-06-03
      • 1970-01-01
      • 2016-12-16
      相关资源
      最近更新 更多