2012-12-04 60 views
4

使用loessloess.smooth時出現問題,缺少數據的時間序列。缺少數據的時間序列「黃土」

這兩個命令都不適用於這個玩具數據。

x <- as.Date(c(1, 2, 4, 5, 6), origin="2010-1-1") 
y <- c(4, 8, 8, 28, 11) 

plot(x, y, ylim=c(1,30)) 

lines(loess(y ~ x), col="red") 
lines(loess.smooth(y=y, x=x), col="blue") 

回答

1

我結束了使用下面的代碼:

# Data 

x.1 <- as.Date(c(1, 2, 4, 5, 6), origin="2010-1-1") 
x.2 <- c(1, 2, 4, 5, 6) 
y <- c(4, 8, 8, 28, 11) 


# x.2 - x is numeric variable 

plot(x.2, y, ylim=c(1,30)) 

lines(loess(y ~ x.2, span=1.01), col="black", lwd=2, lty=2) # neccessary to change span default to avoid warnings (span = 0.75) 
lines(loess.smooth(x.2, y, span=1.01), col = "orange", , lwd=2) # neccessary to change span default to avoid warnings (span = 2/3) 
lines(smooth.spline(x.2,y), col="blue", lwd=2) 


# x.1 - x is date variable 

plot(x.1, y, ylim=c(1,30)) 
# loess() cannot deal with date variables, thus convert it to 
lines(loess(y~as.numeric(x.1), span=1.01), col="red", lwd=2) # neccessary to change span default to avoid warnings (span = 0.75) 
lines(loess.smooth(x.1, y, span=1.01), col = "orange", lwd=2) # neccessary to change span default to avoid warnings (span = 2/3) 
lines(smooth.spline(x.1,y), col="blue", lwd=2) 

的問題是: (1)loess是無法處理的日期變量。 (2)必須調整(> 1)span參數。