了解局部多项式回归
Understanding the Local Polynomial Regression
有人能解释一下为什么我在绘图时得到不同的线条吗?不知何故我认为这条线应该是一样的
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
的拟合模型(蓝色)的值,有人可以帮助我吗?
拟合模型(蓝色曲线)在 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
有人能解释一下为什么我在绘图时得到不同的线条吗?不知何故我认为这条线应该是一样的
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
的拟合模型(蓝色)的值,有人可以帮助我吗?
拟合模型(蓝色曲线)在 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