黄土线中的R时间序列

时间:2019-05-20 17:10:51

标签: r plot

我想在R中的时间序列数据的简单图中添加一条平滑线。 我需要使用基本绘图包和黄土函数。

我当前的方法似乎返回正确的值,但是我无法设法将黄土添加到当前图中。

我正在使用fpp package中的数据集a10。

library(fpp)
data(a10)

start(a10) #1991 7
end(a10) #2008 6

tm <- seq(as.Date("1991/7/1"), as.Date("2008/6/1"), by = "month")

plot(tm, as.numeric(a10), type = "l")

res <- loess(a10 ~ as.numeric(tm), span = 0.5)

在这里,我无法将res中的数据添加到我的绘图中。 我想通过lines函数来做到这一点,但是它行不通。

有人对我有提示或解决方案吗? 非常感谢您的帮助!

数据:

 a10 <- structure(c(3.526591, 3.180891, 3.252221, 3.611003, 3.565869, 
 4.306371, 5.088335, 2.81452, 2.985811, 3.20478, 3.127578, 3.270523, 
 3.73785082, 3.55877609, 3.77720173, 3.92449042, 4.38653092, 5.81054917, 
 6.19206769, 3.45085699, 3.77230686, 3.7343029, 3.90539892, 4.04968714, 
 4.31556552, 4.56218455, 4.60866203, 4.66785129, 5.09384145, 7.1799622, 
 6.73147308, 3.84127758, 4.39407557, 4.07534073, 4.5406449, 4.64561508, 
 4.75260653, 5.35060467, 5.20445484, 5.3016513, 5.77374216, 6.20459348, 
 6.74948382, 4.21606735, 4.94934946, 4.8230449, 5.19475419, 5.17078711, 
 5.25674157, 5.85527729, 5.49072901, 6.11529323, 6.08847284, 7.41659792, 
 8.32945212, 5.06979585, 5.26255667, 5.59712628, 6.110296, 5.68916084, 
 6.48684933, 6.30056933, 6.46747574, 6.82862939, 6.64907826, 8.60693721, 
 8.52447101, 5.27791837, 5.71430345, 6.21452908, 6.41192919, 6.66771564, 
 7.05083102, 6.70491861, 7.25098761, 7.81973318, 7.39810106, 10.09623339, 
 8.79851303, 5.91826076, 6.53449298, 6.67573561, 7.06420058, 7.38338118, 
 7.81349587, 7.43189221, 8.27511722, 8.26044138, 8.59615575, 10.55893902, 
 10.3914156, 6.42153456, 8.06261937, 7.2977391, 7.93691594, 8.16532298, 
 8.71742046, 9.07096378, 9.17711337, 9.25188674, 9.93313643, 11.53297428, 
 12.51146235, 7.45719853, 8.59119084, 8.47400037, 9.3868026, 9.56039945, 
 10.8342948, 10.64375083, 9.90816186, 11.7100413, 11.34015074, 
 12.07913184, 14.49758109, 8.04927477, 10.31289116, 9.75335821, 
 10.85038183, 9.96171916, 11.44360144, 11.65923889, 10.64705979, 
 12.65213444, 13.67446631, 12.96573509, 16.30026927, 9.05348536, 
 10.00244879, 10.78875007, 12.10670515, 10.95410107, 12.84456587, 
 12.19649985, 12.85474787, 13.54200436, 13.28764, 15.13491784, 
 16.82834968, 9.80021461, 10.81699371, 10.65422256, 12.51232269, 
 12.16120969, 12.9980462, 12.51727568, 13.26865761, 14.73362169, 
 13.66938174, 16.50396561, 18.00376795, 11.93802987, 12.9979001, 
 12.88264507, 13.94344681, 13.9894722, 15.33909742, 15.37076394, 
 16.142005, 16.685754, 17.636728, 18.869325, 20.778723, 12.154552, 
 13.402392, 14.459239, 14.795102, 15.705248, 15.82955, 17.554701, 
 18.100864, 17.496668, 19.347265, 20.031291, 23.486694, 12.536987, 
 15.467018, 14.233539, 17.783058, 16.291602, 16.980282, 18.612189, 
 16.623343, 21.430241, 23.575517, 23.334206, 28.038383, 16.763869, 
 19.792754, 16.427305, 21.000742, 20.681002, 21.83489, 23.93020353, 
 22.93035694, 23.26333992, 25.25003022, 25.80609, 29.665356, 21.654285, 
 18.264945, 23.107677, 22.91251, 19.43174), .Tsp = c(1991.5, 2008.41666666667, 
 12), class = "ts")

1 个答案:

答案 0 :(得分:1)

使用数据和绘图,您可以通过使用lines()x=tm(即绘图的黄土函数)在绘图的正下方编写y=predict(res)函数来简单地添加黄土绘图 >

    plot(tm, as.numeric(a10), type = "l")
    lines(tm,predict(res),col="red") 

希望这会有所帮助!