【问题标题】:"loess" for time series with missing data缺少数据的时间序列的“loess”
【发布时间】:2012-12-04 00:28:25
【问题描述】:

我在使用带有缺失数据的时间序列的 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")

【问题讨论】:

    标签: r loess


    【解决方案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) 必须调整span 参数(>1)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-06
      • 1970-01-01
      • 2017-04-21
      • 2016-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-13
      相关资源
      最近更新 更多