局部常數擬合方法 例


為了更直觀地說明光滑參數的變化對回歸函數估計效果的影響,下面給出一個數值模擬例子。

設有回歸模型 

#模擬數據
n = 201
x = seq(0,2,0.01)
e = rnorm(n,0,0.2)
y = 2*x + sin(5*pi * x)+e
#局部常數擬合
#h = 0.1
plot(x,y,main = "h = 0.1")
lines(x,2*x + sin(5*pi * x),lty = 1,lwd = 1)#真實曲線 
lines(lowess(x,y,f=0.1),pch = 1,lwd = 2)#lowess
data = as.data.frame(cbind(x,y))
model1 = loess(y~x,data,span = 0.1,degree = 0)#loess
lines(x,model1$fit,col = "red")
legend(0.05,4.5,c("真實曲線","lowess fit","loess fit"),lty = 1,lwd = c(1,2,1),col = c(1,1,"red"))#加標簽
#h = 0.05
plot(x,y,main = "h = 0.05")
lines(x,2*x + sin(5*pi * x),lty = 1,lwd = 1)#真實曲線 
lines(lowess(x,y,f=0.05),pch = 1,lwd = 2)#lowess
model1 = loess(y~x,data,span = 0.05,degree = 0)#loess
lines(x,model1$fit,col = "red")
legend(0.05,4.5,c("真實曲線","lowess fit","loess fit"),lty = 1,lwd = c(1,2,1),col = c(1,1,"red"))#加標簽
#h = 0.025
plot(x,y,main = "h = 0.025")
lines(x,2*x + sin(5*pi * x),lty = 1,lwd = 1)#真實曲線 
lines(lowess(x,y,f=0.025),pch = 1,lwd = 2)#lowess
model1 = loess(y~x,data,span = 0.025,degree = 0)#loess
lines(x,model1$fit,col = "red")
legend(0.05,4.5,c("真實曲線","lowess fit","loess fit"),lty = 1,lwd = c(1,2,1),col = c(1,1,"red"))#加標簽
#h = 0.01
plot(x,y,main = "h = 0.01")
lines(x,2*x + sin(5*pi * x),lty = 1,lwd = 1)#真實曲線 
lines(lowess(x,y,f=0.01),pch = 1,lwd = 2)#lowess
model1 = loess(y~x,data,span = 0.01,degree = 0)#loess
lines(x,model1$fit,col = "red")
legend(0.05,4.5,c("真實曲線","lowess fit","loess fit"),lty = 1,lwd = c(1,2,1),col = c(1,1,"red"))#加標簽

  

 

 

有兩個函數可以做局部加權回歸:

1. lowess(x,y,f = )

f代表窗寬參數,f越大,則擬合曲線越光滑。

2. loess(formula,data,span,degree,…)

loess函數要比lowess函數強大,span是用來控制光滑度的,span越大則擬合曲線越光滑。lowess的參數f與loess的參數span代表含義並不相同。loess函數的參數degree是用來控制使用的多項式的階數,一般可以選擇1或2。(注意,當上面degree參數取0,圖像顯示lowess與loess擬合的結果是相似的)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM