2017-09-21 55 views
1

我有一個看起來像數據:通過所有的數據點傳遞流暢的線條與超過50分

year mean.streak 
1958 2.142857 
1959 3.066667 
1960 2.166667 
1961 2.190476 

我與局部迴歸情節的代碼如下所示:

ggplot(aes(x = year, y = mean.streak, color = year), data = streaks)+ 
    geom_point(color = 'black')+ 
    geom_smooth(method = 'loess') 

和輸出:

enter image description here

我想捕捉有些正弦波通過傳遞所有數據點的平滑線,而不是典型的參差不齊的geom_line。我嘗試寫多項式插值:

ggplot(df)+ 
    geom_point(aes(x = year, y = mean.streak, colour = year), size = 3) + 
    stat_smooth(aes(x = year, y = mean.streak), method = "lm", 
       formula = y ~ poly(x, 57), se = FALSE) 

摘自this thread。但我得到的錯誤:

Warning message: 
Computation failed in `stat_smooth()`: 
'degree' must be less than number of unique points 

似乎是因爲有太多的數據點,爲this answer似乎表明。

有沒有一種方法可以通過59個數據點的所有數據平滑線?

完整數據:

structure(list(year = 1958:2016, mean.streak = c(2.14285714285714, 
3.06666666666667, 2.16666666666667, 2.19047619047619, 2.35, 2.42857142857143, 
2.28571428571429, 1.92592592592593, 1.69230769230769, 2.61111111111111, 
3, 2.94117647058824, 2.2, 2.5, 2.13636363636364, 1.76923076923077, 
1.36111111111111, 1.41176470588235, 1.76, 2, 2.63157894736842, 
2.08695652173913, 2.86666666666667, 2.125, 3, 3.125, 2.57894736842105, 
1.84, 1.46666666666667, 1.7037037037037, 1.625, 1.67741935483871, 
1.84, 1.6, 3, 3.11111111111111, 3.66666666666667, 4.18181818181818, 
2.85714285714286, 3.66666666666667, 2.66666666666667, 2.92857142857143, 
3.1875, 2.76923076923077, 5.375, 5.18181818181818, 4.08333333333333, 
6.85714285714286, 2.77777777777778, 2.76470588235294, 3.15384615384615, 
3.83333333333333, 3.06666666666667, 3.07692307692308, 4.41666666666667, 
4.9, 5.22222222222222, 5, 5.27272727272727), median.streak = c(1, 
3, 1.5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 1, 1, 1, 1, 2, 
2, 3, 2, 2, 2.5, 2, 2, 1, 1, 1, 1, 1, 1, 1.5, 2, 4, 4, 1, 3, 
2, 2.5, 2, 2, 5.5, 4, 2.5, 9, 2, 2, 2, 1.5, 2, 3, 2.5, 4.5, 4, 
5, 4), max.streak = c(6, 6, 9, 7, 5, 5, 7, 4, 3, 7, 9, 7, 6, 
6, 6, 4, 3, 4, 4, 10, 8, 6, 6, 5, 10, 8, 5, 6, 3, 4, 4, 4, 4, 
5, 8, 8, 11, 8, 8, 11, 10, 5, 12, 7, 10, 12, 12, 10, 7, 10, 10, 
14, 9, 7, 9, 12, 10, 14, 12), mean.std = c(-0.73762950487994, 
-0.480997734887942, -0.517355702126398, -0.387678832192802, -0.315808940316265, 
-0.455313725347534, -0.520453518496716, -0.598412265824216, -0.523171795723798, 
-0.62285788065637, -0.54170040191883, -0.590289727314622, -0.468222025966258, 
-0.639180735884434, -0.656427002478427, -0.565745564840106, -0.473399411312895, 
-0.564475310127763, -0.493531273810312, -0.543209721496256, -0.6, 
-0.510337503791441, -0.596096374402028, -0.504696265560619, -0.620412635042488, 
-0.497008319856979, -0.546623513153538, -0.613345407826292, -0.564945850817486, 
-0.581770706442245, -0.5709080560492, -0.627986564445679, -0.680973485641403, 
-0.548092447365696, -0.554620596559388, -0.483847268000936, -0.67619820292833, 
-0.613245144944101, -0.509832316970819, -0.302654541906113, -0.623276311320811, 
-0.431421947082012, -0.525548788393688, -0.244995094473986, -0.412444188256097, 
-0.112114155982405, -0.299486359079708, -0.300201791042539, -0.240281366191648, 
-0.359719754440627, -0.511417389357902, -0.474906675611613, -0.312106332395495, 
-0.449137693833681, -0.526248555772371, -0.56052848268042, -0.390017880007091, 
-0.537267264953157, -0.444528236868953)), class = c("tbl_df", 
"tbl", "data.frame"), .Names = c("year", "mean.streak", "median.streak", 
"max.streak", "mean.std"), row.names = c(NA, -59L)) 
+0

像'geom_smooth(方法= '黃土',method.args =名單(跨度= 0.2))' 。根據需要調整跨度。另見'help(「黃土」)'。我懷疑你實際上是想要一條通過所有點的線,當然不是一個多項式。 – Roland

+0

@Roland由於某種原因傳遞'span'參數創建完全相同的圖形,無論我調整它有多高或多低 – snapcrack

+0

請提供更多數據以啓用測試。 – Roland

回答

2

調整跨度:

ggplot(aes(x = year, y = mean.streak, color = year), data = streaks)+ 
    geom_point(color = 'black')+ 
    stat_smooth(method = 'loess', span = 0.3) 

resulting plot

或用樣條:

library(splines) 
ggplot(aes(x = year, y = mean.streak, color = year), data = streaks)+ 
    geom_point(color = 'black')+ 
    stat_smooth(method = 'lm', formula = y ~ ns(x, 10)) 

second resulting plot

通常,您不想擬合極高次多項式。這樣的配合看起來很糟糕。這將是更好的,以適應實際的時間序列模型對您的數據:

library(forecast) 
library(zoo) 

ggplot(aes(x = year, y = mean.streak, color = year), data = streaks)+ 
    geom_point(color = 'black')+ 
    geom_line(data = data.frame(year = sort(streaks$year), 
           mean.streak = fitted(auto.arima(zoo(streaks$mean.streak, 
                    order.by = streaks$year)))), 
      show.legend = FALSE) 

third resulting plot