Plot anomalies stripes line - r

Hi how can I make this kind of plot for anomalies? Is there ggplot function ?

The geom_tile function does the trick:
library(RColorBrewer)
col_strip <- brewer.pal(11, "RdBu")
library(ggplot2)
ggplot(df,
aes(x = date, y = 1, fill = Temp))+
geom_tile() +
scale_fill_gradientn(colors = rev(col_strip)) +
guides(fill = guide_colorbar(barwidth = 1)) +
theme_void()
Data:
structure(list(date = structure(c(-32872, -32506, -32141, -31776,
-31411, -31045, -30680, -30315, -29950, -29584, -29219, -28854,
-28489, -28123, -27758, -27393, -27028, -26662, -26297, -25932,
-25567, -25202, -24837, -24472, -24107, -23741, -23376, -23011,
-22646, -22280, -21915, -21550, -21185, -20819, -20454, -20089,
-19724, -19358, -18993, -18628, -18263, -17897, -17532, -17167,
-16802, -16436, -16071, -15706, -15341, -14975, -14610, -14245,
-13880, -13514, -13149, -12784, -12419, -12053, -11688, -11323,
-10958, -10592, -10227, -9862, -9497, -9131, -8766, -8401, -8036,
-7670, -7305, -6940, -6575, -6209, -5844, -5479, -5114, -4748,
-4383, -4018, -3653, -3287, -2922, -2557, -2192, -1826, -1461,
-1096, -731, -365, 0, 365, 730, 1096, 1461, 1826, 2191, 2557,
2922, 3287, 3652, 4018, 4383, 4748, 5113, 5479, 5844, 6209, 6574,
6940, 7305, 7670, 8035, 8401, 8766, 9131, 9496, 9862, 10227,
10592, 10957, 11323, 11688, 12053, 12418, 12784, 13149, 13514,
13879, 14245, 14610, 14975, 15340, 15706, 16071, 16436, 16801,
17167, 17532), class = "Date"), Temp = c(15.16, 16.27, 15.17,
14.83, 15.26, 14.89, 15.26, 15.68, 14.99, 14.7, 14.99, 15.02,
15.74, 16.28, 15.2, 16.11, 15.75, 16.31, 16.04, 16.88, 15.85,
15.36, 15.18, 15.75, 15.68, 15.49, 15.82, 15.44, 15.92, 15.67,
15.64, 15.43, 15.61, 15.57, 15.54, 15.77, 15.81, 15.55, 15.64,
16.28, 16.03, 16.11, 15.91, 15.87, 15.55, 15.55, 17.01, 15.9,
15.74, 15.93, 15.9, 15.66, 15.02, 15.63, 14.53, 15.05, 15.25,
15.62, 15.8, 15.46, 16.02, 15.68, 15.58, 16.45, 16.1, 16.44,
15.82, 16.3, 16.68, 17, 16.11, 16.02, 16.41, 16.79, 16.25, 17.17,
15.78, 16.18, 16.31, 16.53, 16.78, NA, 16.13, 15.64, 16.4, 15.73,
16.44, 15.76, NA, NA, 16.33, 15.53, 15.32, 16.24, 15.98, 16.12,
15.95, 16.13, 16.41, 16.22, 16.65, 16.7, 16.74, 16.48, 16.12,
16.62, 16.38, 16.98, 16.7, 17.06, 17.3, 16.56, 16.39, 15.9, 16.62,
17.7, 16.61, 17.62, 17.12, 16.5, 16.77, 16.99, 16.6, 17.36, 17,
16.86, 17.48, 16.8, 16.8, 17.26, 17.09, 17.48, 16.85, 16.84,
17.16, 17.23, 17.59, 17.95, 17.12)), class = "data.frame", row.names = c(NA,
-139L))

Related

Trouble finding correct peaks when using pracma::findpeaks in R

I am having trouble getting the correct peaks using findpeaks
dat <- structure(list(Temp = c(36.36, 36.37, 36.38, 36.37, 36.37, 36.35,
36.34, 36.33, 36.34, 36.33, 36.32, 36.31, 36.3, 36.29, 36.28,
36.27, 36.27, 36.28, 36.27, 36.25, 36.24, 36.22, 36.22, 36.2,
36.19, 36.2, 36.2, 36.22, 36.22, 36.21, 36.21, 36.21, 36.21,
36.21, 36.21, 36.22, 36.22, 36.23, 36.24, 36.24, 36.24, 36.24,
36.25, 36.25, 36.24, 36.24, 36.23, 36.23, 36.22, 36.22, 36.21,
36.2, 36.19, 36.19, 36.19, 36.2, 36.2, 36.21, 36.22, 36.23, 36.23,
36.23, 36.23, 36.23, 36.24, 36.25, 36.24, 36.24, 36.24, 36.24,
36.25, 36.25, 36.27, 36.27, 36.28, 36.28, 36.27, 36.26, 36.26,
36.26, 36.25, 36.25, 36.25, 36.24, 36.24, 36.24, 36.24, 36.23,
36.22, 36.21, 36.21, 36.2, 36.2, 36.19, 36.19, 36.18, 36.18,
36.18, 36.18, 36.18, 36.17, 36.17, 36.17, 36.17, 36.17, 36.18,
36.18, 36.19, 36.18, 36.19, 36.19, 36.19, 36.19, 36.19, 36.18,
36.18, 36.16, 36.16, 36.15, 36.15, 36.15, 36.14, 36.12, 36.11,
36.11, 36.11, 36.11, 36.11, 36.1, 36.1, 36.1, 36.1, 36.11, 36.11,
36.11, 36.11, 36.1, 36.1, 36.09, 36.09, 36.09, 36.09, 36.09,
36.09, 36.1, 36.11, 36.12, 36.12, 36.12, 36.12, 36.12, 36.12,
36.13, 36.13, 36.13, 36.14, 36.14, 36.14, 36.13, 36.13, 36.13,
36.11, 36.1, 36.09, 36.09, 36.1, 36.11, 36.15, 36.17, 36.24,
36.29, 36.43, 36.51, 36.7, 36.8, 37.01, 37.14, 37.41, 37.56,
37.84, 38, 38.33, 38.5, 38.84, 39.01, 39.36, 39.55, 39.91, 40.1,
40.48, 40.79, 40.98, 41.43, 41.62, 41.99, 42.18, 42.56, 42.74,
43.13, 43.32, 43.7, 43.88, 44.15, 44.33, 44.69, 45.07, 45.25,
45.75, 45.93, 46.3, 46.48, 46.84, 47.03, 47.43, 47.63, 48.01,
48.19, 48.54, 48.7, 48.99, 49.12, 49.35, 49.54, 49.63, 49.77,
49.82, 49.88, 49.89, 49.91, 49.91, 49.9, 49.9, 49.86, 49.83,
49.77, 49.73, 49.65, 49.62, 49.53, 49.44, 49.39, 49.29, 49.24,
49.14, 49.08, 48.96, 48.9, 48.78, 48.71, 48.59, 48.53, 48.41,
48.35, 48.23, 48.18, 48.05, 47.98, 47.85, 47.79, 47.66, 47.59,
47.47, 47.32, 47.26, 47.12, 47.05, 46.92, 46.85, 46.73, 46.68,
46.56, 46.49, 46.37, 46.31, 46.18, 46.13, 46.02, 45.97, 45.88,
45.83, 45.77, 45.75, 45.73, 45.74, 45.81, 45.85, 45.97, 46.03,
46.17, 46.33, 46.43, 46.65, 46.76, 47.01, 47.13, 47.4, 47.54,
47.81, 48.14, 48.29, 48.58, 48.73, 49.04, 49.2, 49.53, 49.7,
50.03, 50.36, 50.52, 50.86, 51.03, 51.39, 51.65, 51.9, 52.06,
52.5, 52.87, 53.05, 53.41, 53.59, 53.82, 54, 54.37, 54.7, 55.06,
55.42, 55.61, 55.97, 56.15, 56.49, 56.65, 56.94, 57.06, 57.29,
57.47, 57.55, 57.68, 57.74, 57.81, 57.83, 57.83, 57.82, 57.78,
57.75, 57.66, 57.54, 57.48, 57.35, 57.28, 57.12, 57.03, 56.85,
56.76, 56.57, 56.47, 56.25, 56.15, 55.94, 55.72, 55.62, 55.35,
55.24, 55.01, 54.9, 54.68, 54.56, 54.32, 54.2, 53.96, 53.84,
53.59, 53.47, 53.23, 53.01, 52.9, 52.71, 52.69, 52.55, 52.49,
52.39, 52.35, 52.3, 52.28, 52.28, 52.34, 52.39, 52.45, 52.44,
52.56, 52.61, 52.72, 52.79, 52.93, 52.97, 53.11, 53.29, 53.37,
53.54, 53.62, 53.79, 53.88, 54.06, 54.25, 54.33, 54.51, 54.6,
54.79, 54.88, 55.06, 55.21, 55.38, 55.56, 55.65, 55.83, 55.92,
56.1, 56.19, 56.37, 56.46, 56.64, 56.74, 56.91, 57.07, 57.14,
57.27, 57.33, 57.42, 57.45, 57.48, 57.49, 57.48, 57.48, 57.42,
57.31, 57.24, 57.08, 56.99, 56.84, 56.74, 56.54, 56.44, 56.22,
56.05, 55.89, 55.77, 55.54, 55.31, 55.2, 54.99, 54.88, 54.66,
54.55, 54.34, 54.23, 54.03, 53.84, 53.68, 53.49, 53.4, 53.22,
53.14, 52.97, 52.88, 52.72, 52.63, 52.46, 52.38, 52.2, 52.02,
51.93, 51.76, 51.68, 51.51, 51.43, 51.23, 51.15, 50.99, 50.94,
50.79, 50.72, 50.55, 50.46, 50.29, 50.21, 50.06, 49.91, 49.84,
49.68, 49.6, 49.44, 49.36, 49.2, 49.13, 48.98, 48.91, 48.77,
48.7, 48.56, 48.49, 48.35, 48.28, 48.14, 48.07, 47.94, 47.83,
47.7, 47.64, 47.56, 47.51, 47.39, 47.25, 47.2, 47.09, 47.04,
46.98, 46.94, 46.86, 46.82, 46.76, 46.72, 46.63, 46.6, 46.54,
46.51, 46.48, 46.44, 46.39, 46.37, 46.34, 46.33, 46.3, 46.3,
46.29, 46.28, 46.25, 46.24, 46.22, 46.2, 46.2, 46.2, 46.2, 46.19,
46.19, 46.19, 46.18, 46.16, 46.16, 46.14, 46.13, 46.11, 46.1,
46.09, 46.08, 46.06, 46.06, 46.05, 46.04, 46.02, 46, 45.98, 45.95,
45.94, 45.9, 45.88, 45.85, 45.84, 45.81, 45.78, 45.73, 45.71,
45.67, 45.66, 45.62, 45.59, 45.55, 45.54, 45.53, 45.52, 45.48,
45.44, 45.37, 45.34, 45.29, 45.26, 45.2, 45.15, 45.12, 45.06,
45.03, 44.98, 44.96, 44.9, 44.87, 44.8, 44.77, 44.71, 44.68,
44.61, 44.58, 44.53, 44.5, 44.44, 44.41, 44.37, 44.35, 44.3,
44.28, 44.24, 44.22, 44.14, 44.11, 44.05, 44.02, 43.98, 43.96,
43.89, 43.86, 43.8, 43.78, 43.76, 43.75, 43.7, 43.67, 43.63,
43.6, 43.54, 43.5, 43.41, 43.36, 43.3, 43.26, 43.19, 43.16, 43.12,
43.1, 43.08, 43.07, 43.04, 43.02, 42.97, 42.92, 42.88, 42.87,
42.82, 42.81, 42.78, 42.77, 42.74, 42.74, 42.72, 42.69, 42.68,
42.63, 42.6, 42.54, 42.51, 42.46, 42.43, 42.37, 42.35, 42.32,
42.3, 42.25, 42.24, 42.2, 42.18, 42.14, 42.13, 42.07, 42.05,
42, 41.97, 41.93, 41.9, 41.86, 41.83, 41.8, 41.79, 41.76, 41.75,
41.73, 41.72, 41.69, 41.67, 41.61, 41.58, 41.53, 41.5, 41.46,
41.43, 41.39, 41.37, 41.32, 41.3, 41.26, 41.24, 41.19, 41.17,
41.1, 41.07, 41.01, 40.99, 40.94, 40.92, 40.87, 40.84, 40.78,
40.76, 40.74, 40.73, 40.7, 40.68, 40.64, 40.62, 40.57, 40.56,
40.53, 40.53, 40.51, 40.5, 40.48, 40.47, 40.45, 40.44, 40.42,
40.41, 40.41, 40.41, 40.4, 40.4, 40.39, 40.38, 40.35, 40.34,
40.32, 40.32, 40.32, 40.32, 40.31, 40.3, 40.28, 40.28, 40.27,
40.27, 40.27, 40.26, 40.25, 40.25, 40.26, 40.26, 40.26, 40.26,
40.23, 40.22, 40.23, 40.23, 40.23, 40.23, 40.23, 40.22, 40.21,
40.22, 40.22, 40.22, 40.22, 40.21, 40.2, 40.2, 40.19, 40.18,
40.17, 40.16, 40.16, 40.15, 40.15, 40.15, 40.13, 40.11, 40.09,
40.08, 40.08, 40.08, 40.08, 40.08, 40.09, 40.08, 40.08, 40.06,
40.05, 40.04, 40.04, 40.04, 40.04, 40.02, 40.02, 40.01, 40.01,
40.01, 39.98, 39.97, 39.93, 39.92, 39.91, 39.91, 39.9, 39.89,
39.88, 39.87, 39.85, 39.86, 39.87, 39.87, 39.86, 39.86, 39.85,
39.85, 39.86, 39.87, 39.87, 39.87, 39.87, 39.87, 39.85, 39.85,
39.84, 39.83, 39.82, 39.81, 39.79, 39.78, 39.75, 39.74, 39.72,
39.71, 39.7, 39.68, 39.66, 39.65, 39.64, 39.63, 39.6, 39.59,
39.57, 39.57, 39.55, 39.54, 39.5, 39.49, 39.48, 39.47, 39.47,
39.47, 39.47, 39.47, 39.47, 39.47, 39.46, 39.46, 39.46, 39.45,
39.44, 39.42, 39.4, 39.38, 39.37, 39.35, 39.34, 39.31, 39.28,
39.26, 39.24, 39.22, 39.2, 39.18, 39.17, 39.16, 39.16, 39.17,
39.17, 39.16, 39.15, 39.14, 39.14, 39.14, 39.14, 39.14, 39.14,
39.14, 39.13, 39.12, 39.12, 39.11, 39.11, 39.1, 39.1, 39.09,
39.09, 39.08, 39.07, 39.07, 39.07, 39.07, 39.07, 39.07, 39.06,
39.05, 39.04, 39.04, 39.03, 39.02, 39, 39, 38.99, 38.99, 38.99,
39, 38.99, 38.99, 38.98, 38.98, 38.98, 38.99, 39, 39.01, 39.02,
39.01, 38.99, 38.98, 38.96, 38.96, 38.96, 38.95, 38.94, 38.93,
38.9, 38.89, 38.87, 38.86, 38.84, 38.84, 38.85, 38.85, 38.83,
38.83, 38.83, 38.83, 38.83, 38.83, 38.82, 38.81, 38.81, 38.8,
38.78, 38.78, 38.77, 38.77, 38.76, 38.74, 38.73, 38.73, 38.73,
38.72, 38.72, 38.72, 38.72, 38.7, 38.69, 38.68, 38.68, 38.67,
38.67, 38.65, 38.65, 38.62, 38.61, 38.6, 38.6, 38.59, 38.59,
38.59, 38.59, 38.6, 38.59, 38.58, 38.59, 38.59, 38.59, 38.59,
38.59, 38.58, 38.56, 38.55, 38.55, 38.54, 38.53, 38.51, 38.5,
38.49, 38.47, 38.46, 38.44, 38.43, 38.4, 38.39, 38.38, 38.37,
38.35, 38.34, 38.33, 38.32, 38.32, 38.31, 38.29, 38.27, 38.24,
38.23, 38.21, 38.2, 38.17, 38.16, 38.15, 38.14, 38.12, 38.11,
38.09, 38.09, 38.08, 38.08, 38.07, 38.06, 38.04, 38.03, 38.02,
38.01, 38, 37.99, 37.98, 37.97, 37.95, 37.94, 37.93, 37.93, 37.92,
37.92, 37.91, 37.91, 37.9, 37.9, 37.9, 37.9, 37.9, 37.9, 37.9,
37.9, 37.89, 37.89, 37.88, 37.88, 37.88, 37.87, 37.86, 37.85,
37.85, 37.86, 37.87, 37.87, 37.87, 37.88, 37.89, 37.89, 37.89,
37.88, 37.88, 37.88, 37.87, 37.87, 37.87, 37.85, 37.85, 37.84,
37.84, 37.83, 37.83, 37.82, 37.81, 37.81, 37.81, 37.8, 37.8,
37.8, 37.8, 37.8, 37.79, 37.79, 37.78, 37.76, 37.76, 37.75, 37.75,
37.75, 37.75, 37.74, 37.74, 37.74, 37.74, 37.74, 37.74, 37.74,
37.74, 37.74, 37.74, 37.74, 37.74, 37.74, 37.74, 37.73, 37.73,
37.73, 37.73, 37.74, 37.74, 37.74, 37.74, 37.74, 37.73, 37.73,
37.73, 37.73, 37.73, 37.73, 37.71, 37.7, 37.7, 37.7, 37.7, 37.7,
37.71, 37.7, 37.71, 37.7, 37.7, 37.7, 37.7, 37.71, 37.72, 37.72,
37.74, 37.73, 37.72, 37.72, 37.71, 37.7, 37.69, 37.68, 37.68,
37.68, 37.69, 37.69, 37.7, 37.7, 37.7, 37.69, 37.68, 37.67, 37.66,
37.65, 37.65, 37.64, 37.64, 37.62, 37.62, 37.61, 37.6, 37.6,
37.6, 37.58, 37.57, 37.56, 37.55, 37.54, 37.54, 37.55, 37.55,
37.55, 37.54, 37.54, 37.54, 37.53, 37.53, 37.52, 37.52, 37.52,
37.51, 37.51, 37.5, 37.49, 37.48, 37.49, 37.49, 37.5, 37.5, 37.51,
37.52, 37.54, 37.54, 37.54, 37.54, 37.53, 37.52, 37.51, 37.5,
37.51, 37.52, 37.51, 37.51, 37.51, 37.52, 37.53, 37.53, 37.53,
37.52, 37.52, 37.52, 37.52, 37.51, 37.5, 37.5, 37.5, 37.5, 37.5,
37.5, 37.5, 37.5, 37.5, 37.5, 37.5, 37.5, 37.5, 37.49, 37.49,
37.49, 37.49, 37.49, 37.48, 37.49, 37.49, 37.5, 37.5, 37.5, 37.5,
37.49, 37.5, 37.5, 37.5, 37.5, 37.49, 37.49, 37.49, 37.49, 37.49,
37.49, 37.46, 37.45, 37.42, 37.42, 37.39, 37.37, 37.35, 37.34,
37.33, 37.32, 37.31, 37.3, 37.29, 37.29, 37.28, 37.28, 37.27,
37.26, 37.26, 37.26, 37.26, 37.26, 37.26, 37.25, 37.25, 37.24,
37.24, 37.24, 37.24, 37.24, 37.24, 37.23, 37.23, 37.22, 37.21,
37.21, 37.2, 37.19, 37.19, 37.18, 37.17, 37.15, 37.14, 37.12,
37.11, 37.1, 37.1, 37.09, 37.08, 37.08, 37.09, 37.1, 37.11, 37.12,
37.13, 37.14, 37.15, 37.16, 37.16, 37.17, 37.18, 37.18, 37.19,
37.2, 37.21, 37.22, 37.22, 37.22, 37.22, 37.21, 37.21, 37.2,
37.2, 37.18, 37.18, 37.16, 37.15, 37.14, 37.13, 37.12, 37.12,
37.12, 37.12, 37.13, 37.13, 37.12, 37.11, 37.1, 37.08, 37.08,
37.07, 37.07, 37.07, 37.07, 37.08, 37.08, 37.06, 37.05, 37.04,
37.04, 37.04, 37.04, 37.04, 37.04, 37.03, 37.03, 37.02, 37.02,
37.02, 37.01, 37, 37, 36.98, 36.97, 36.95, 36.95, 36.93, 36.93,
36.92, 36.92, 36.91, 36.9, 36.91, 36.91, 36.91, 36.91, 36.91,
36.91, 36.91, 36.91, 36.91, 36.9, 36.89, 36.89, 36.88, 36.88,
36.87, 36.88, 36.89, 36.9, 36.9, 36.9, 36.9, 36.89, 36.88, 36.87,
36.87, 36.88, 36.89, 36.88, 36.88, 36.88, 36.88, 36.88, 36.88,
36.88, 36.88, 36.88, 36.88, 36.89, 36.89, 36.9, 36.91, 36.92,
36.93, 36.93, 36.94, 36.94, 36.95, 36.96, 36.96, 36.96, 36.97,
36.97, 36.97, 36.97, 36.97, 36.97, 36.96, 36.96, 36.97, 36.98,
37, 37, 36.99, 36.99, 36.99, 37, 37, 37, 37, 37, 36.99, 36.99,
36.98, 36.98, 36.98, 36.98, 36.98, 36.98, 36.98, 36.97, 36.96,
36.95, 36.95, 36.95, 36.95, 36.95, 36.95, 36.95, 36.95, 36.93,
36.93, 36.94, 36.94, 36.94, 36.93, 36.91, 36.91, 36.92, 36.93,
36.93, 36.92, 36.91, 36.91, 36.91, 36.91, 36.9, 36.9, 36.9, 36.89,
36.88, 36.86, 36.85, 36.85, 36.84, 36.84, 36.83, 36.82, 36.81,
36.8, 36.8, 36.81, 36.82, 36.83, 36.83, 36.83, 36.83, 36.83,
36.83, 36.83, 36.83, 36.84, 36.84, 36.85, 36.85, 36.85, 36.85,
36.85, 36.85, 36.83, 36.83, 36.81, 36.81, 36.8, 36.8, 36.81,
36.81, 36.81, 36.8, 36.8, 36.79, 36.79, 36.8, 36.79, 36.78, 36.78,
36.78, 36.79, 36.79, 36.79, 36.78, 36.78, 36.8, 36.81, 36.82,
36.82, 36.84, 36.85, 36.85, 36.85, 36.85, 36.85, 36.84, 36.84,
36.83, 36.84, 36.85, 36.84, 36.83, 36.82, 36.8, 36.8, 36.8, 36.8,
36.8, 36.79, 36.78, 36.77, 36.78, 36.78, 36.79, 36.79, 36.79,
36.79, 36.79, 36.78, 36.77, 36.77, 36.78, 36.77, 36.77, 36.76,
36.76, 36.76, 36.76, 36.76, 36.75, 36.75, 36.75, 36.75, 36.75,
36.75, 36.75, 36.74, 36.74, 36.74, 36.73, 36.71, 36.71, 36.71,
36.71, 36.71, 36.71, 36.7, 36.7, 36.69, 36.67, 36.66, 36.66,
36.66, 36.66, 36.66, 36.66, 36.66, 36.65, 36.65, 36.66, 36.66,
36.67, 36.67, 36.66, 36.66, 36.66, 36.67, 36.66, 36.64, 36.62,
36.62, 36.62, 36.62, 36.61, 36.61, 36.6, 36.61, 36.6, 36.61,
36.6, 36.61, 36.61, 36.63, 36.64, 36.64, 36.65, 36.65, 36.65,
36.64, 36.65, 36.66, 36.66, 36.66, 36.66, 36.64, 36.64, 36.63,
36.63, 36.63, 36.63, 36.61, 36.61, 36.61, 36.63, 36.63, 36.64,
36.64, 36.65, 36.65, 36.64, 36.64, 36.63, 36.63, 36.63, 36.62,
36.61, 36.61, 36.6, 36.59, 36.6, 36.6, 36.61, 36.61, 36.62, 36.62,
36.63, 36.64, 36.65, 36.65, 36.66, 36.65, 36.65, 36.64, 36.64,
36.64, 36.64, 36.64, 36.65, 36.66, 36.66, 36.66, 36.65, 36.65,
36.65, 36.66, 36.66, 36.67, 36.67, 36.68, 36.69, 36.7, 36.7,
36.7, 36.69, 36.69, 36.69, 36.69, 36.69, 36.7, 36.71, 36.73,
36.74, 36.74, 36.75, 36.75, 36.76, 36.76, 36.76, 36.78, 36.78,
36.79, 36.79, 36.8, 36.81, 36.82, 36.82, 36.81, 36.8, 36.79,
36.79, 36.79, 36.79, 36.79, 36.79, 36.8, 36.81, 36.81, 36.81,
36.81, 36.81, 36.81, 36.81, 36.82, 36.82, 36.83, 36.83, 36.84,
36.84, 36.83, 36.83, 36.83, 36.83, 36.84, 36.84, 36.83, 36.83,
36.83, 36.84, 36.84, 36.83, 36.83, 36.83, 36.84, 36.85, 36.86,
36.88, 36.88, 36.87, 36.87, 36.88, 36.88, 36.88, 36.88, 36.87,
36.87, 36.87, 36.87, 36.88, 36.88, 36.88, 36.88, 36.87, 36.87,
36.87, 36.84, 36.83, 36.82, 36.82, 36.81, 36.81, 36.81, 36.81,
36.82, 36.81, 36.81, 36.8, 36.79, 36.79, 36.8, 36.8, 36.8, 36.8,
36.81, 36.82, 36.84, 36.84, 36.84, 36.84, 36.84, 36.85, 36.85,
36.85, 36.84, 36.82, 36.81, 36.8, 36.79, 36.79, 36.8, 36.78,
36.78, 36.78, 36.79, 36.78, 36.78, 36.78, 36.78, 36.77, 36.77,
36.78, 36.79, 36.81, 36.81, 36.81, 36.81, 36.81, 36.82, 36.82,
36.82, 36.82, 36.82, 36.81, 36.8, 36.79, 36.79, 36.79, 36.79,
36.79, 36.77, 36.76, 36.76, 36.75, 36.74, 36.74, 36.72, 36.72,
36.71, 36.71, 36.72, 36.72, 36.71, 36.71, 36.71, 36.71, 36.71,
36.72, 36.72, 36.72, 36.71, 36.71, 36.72, 36.71, 36.7, 36.69,
36.68, 36.67, 36.66, 36.65, 36.64, 36.63, 36.63, 36.61, 36.6,
36.58, 36.59, 36.58, 36.59, 36.6, 36.61, 36.61, 36.61, 36.61,
36.61, 36.61, 36.61, 36.6, 36.6, 36.6, 36.6, 36.6, 36.59, 36.6,
36.6, 36.6, 36.6, 36.6, 36.6, 36.58, 36.57, 36.56, 36.54, 36.54,
36.54, 36.54, 36.53, 36.52, 36.51, 36.51, 36.51, 36.51, 36.48,
36.47, 36.47, 36.47, 36.47, 36.47, 36.47, 36.47, 36.47, 36.47,
36.46, 36.46, 36.47, 36.47, 36.46, 36.46, 36.46, 36.45, 36.43,
36.43, 36.42, 36.42, 36.41, 36.4, 36.4, 36.39, 36.39, 36.37,
36.37, 36.38, 36.38, 36.38, 36.37, 36.35, 36.34, 36.34, 36.34,
36.34, 36.34, 36.35, 36.36, 36.36, 36.36, 36.37, 36.36, 36.36,
36.35), Time = c(0.04, 0.06, 0.08, 0.1, 0.12, 0.14, 0.16, 0.18,
0.2, 0.22, 0.24, 0.26, 0.28, 0.3, 0.32, 0.34, 0.36, 0.38, 0.4,
0.42, 0.44, 0.46, 0.48, 0.5, 0.52, 0.54, 0.56, 0.58, 0.6, 0.62,
0.64, 0.66, 0.68, 0.7, 0.72, 0.74, 0.76, 0.78, 0.8, 0.82, 0.84,
0.86, 0.88, 0.9, 0.92, 0.94, 0.96, 0.98, 1, 1.02, 1.04, 1.06,
1.08, 1.1, 1.12, 1.14, 1.16, 1.18, 1.2, 1.22, 1.24, 1.26, 1.28,
1.3, 1.32, 1.34, 1.36, 1.38, 1.4, 1.42, 1.44, 1.46, 1.48, 1.5,
1.52, 1.54, 1.56, 1.58, 1.6, 1.62, 1.64, 1.66, 1.68, 1.7, 1.72,
1.74, 1.76, 1.78, 1.8, 1.82, 1.84, 1.86, 1.88, 1.9, 1.92, 1.94,
1.96, 1.98, 2, 2.02, 2.04, 2.06, 2.08, 2.1, 2.12, 2.14, 2.16,
2.18, 2.2, 2.22, 2.24, 2.26, 2.28, 2.3, 2.32, 2.34, 2.36, 2.38,
2.4, 2.42, 2.44, 2.46, 2.48, 2.5, 2.52, 2.54, 2.56, 2.58, 2.6,
2.62, 2.64, 2.66, 2.68, 2.7, 2.72, 2.74, 2.76, 2.78, 2.8, 2.82,
2.84, 2.86, 2.88, 2.9, 2.92, 2.94, 2.96, 2.98, 3, 3.02, 3.04,
3.06, 3.08, 3.1, 3.12, 3.14, 3.16, 3.18, 3.2, 3.22, 3.24, 3.26,
3.28, 3.3, 3.32, 3.34, 3.36, 3.38, 3.4, 3.42, 3.44, 3.46, 3.48,
3.5, 3.52, 3.54, 3.56, 3.58, 3.6, 3.62, 3.64, 3.66, 3.68, 3.7,
3.72, 3.74, 3.76, 3.78, 3.8, 3.82, 3.84, 3.86, 3.88, 3.9, 3.92,
3.94, 3.96, 3.98, 4, 4.02, 4.04, 4.06, 4.08, 4.1, 4.12, 4.14,
4.16, 4.18, 4.2, 4.22, 4.24, 4.26, 4.28, 4.3, 4.32, 4.34, 4.36,
4.38, 4.4, 4.42, 4.44, 4.46, 4.48, 4.5, 4.52, 4.54, 4.56, 4.58,
4.6, 4.62, 4.64, 4.66, 4.68, 4.7, 4.72, 4.74, 4.76, 4.78, 4.8,
4.82, 4.84, 4.86, 4.88, 4.9, 4.92, 4.94, 4.96, 4.98, 5, 5.02,
5.04, 5.06, 5.08, 5.1, 5.12, 5.14, 5.16, 5.18, 5.2, 5.22, 5.24,
5.26, 5.28, 5.3, 5.32, 5.34, 5.36, 5.38, 5.4, 5.42, 5.44, 5.46,
5.48, 5.5, 5.52, 5.54, 5.56, 5.58, 5.6, 5.62, 5.64, 5.66, 5.68,
5.7, 5.72, 5.74, 5.76, 5.78, 5.8, 5.82, 5.84, 5.86, 5.88, 5.9,
5.92, 5.94, 5.96, 5.98, 6, 6.02, 6.04, 6.06, 6.08, 6.1, 6.12,
6.14, 6.16, 6.18, 6.2, 6.22, 6.24, 6.26, 6.28, 6.3, 6.32, 6.34,
6.36, 6.38, 6.4, 6.42, 6.44, 6.46, 6.48, 6.5, 6.52, 6.54, 6.56,
6.58, 6.6, 6.62, 6.64, 6.66, 6.68, 6.7, 6.72, 6.74, 6.76, 6.78,
6.8, 6.82, 6.84, 6.86, 6.88, 6.9, 6.92, 6.94, 6.96, 6.98, 7,
7.02, 7.04, 7.06, 7.08, 7.1, 7.12, 7.14, 7.16, 7.18, 7.2, 7.22,
7.24, 7.26, 7.28, 7.3, 7.32, 7.34, 7.36, 7.38, 7.4, 7.42, 7.44,
7.46, 7.48, 7.5, 7.52, 7.54, 7.56, 7.58, 7.6, 7.62, 7.64, 7.66,
7.68, 7.7, 7.72, 7.74, 7.76, 7.78, 7.8, 7.82, 7.84, 7.86, 7.88,
7.9, 7.92, 7.94, 7.96, 7.98, 8, 8.02, 8.04, 8.06, 8.08, 8.1,
8.12, 8.14, 8.16, 8.18, 8.2, 8.22, 8.24, 8.26, 8.28, 8.3, 8.32,
8.34, 8.36, 8.38, 8.4, 8.42, 8.44, 8.46, 8.48, 8.5, 8.52, 8.54,
8.56, 8.58, 8.6, 8.62, 8.64, 8.66, 8.68, 8.7, 8.72, 8.74, 8.76,
8.78, 8.8, 8.82, 8.84, 8.86, 8.88, 8.9, 8.92, 8.94, 8.96, 8.98,
9, 9.02, 9.04, 9.06, 9.08, 9.1, 9.12, 9.14, 9.16, 9.18, 9.2,
9.22, 9.24, 9.26, 9.28, 9.3, 9.32, 9.34, 9.36, 9.38, 9.4, 9.42,
9.44, 9.46, 9.48, 9.5, 9.52, 9.54, 9.56, 9.58, 9.6, 9.62, 9.64,
9.66, 9.68, 9.7, 9.72, 9.74, 9.76, 9.78, 9.8, 9.82, 9.84, 9.86,
9.88, 9.9, 9.92, 9.94, 9.96, 9.98, 10, 10.02, 10.04, 10.06, 10.08,
10.1, 10.12, 10.14, 10.16, 10.18, 10.2, 10.22, 10.24, 10.26,
10.28, 10.3, 10.32, 10.34, 10.36, 10.38, 10.4, 10.42, 10.44,
10.46, 10.48, 10.5, 10.52, 10.54, 10.56, 10.58, 10.6, 10.62,
10.64, 10.66, 10.68, 10.7, 10.72, 10.74, 10.76, 10.78, 10.8,
10.82, 10.84, 10.86, 10.88, 10.9, 10.92, 10.94, 10.96, 10.98,
11, 11.02, 11.04, 11.06, 11.08, 11.1, 11.12, 11.14, 11.16, 11.18,
11.2, 11.22, 11.24, 11.26, 11.28, 11.3, 11.32, 11.34, 11.36,
11.38, 11.4, 11.42, 11.44, 11.46, 11.48, 11.5, 11.52, 11.54,
11.56, 11.58, 11.6, 11.62, 11.64, 11.66, 11.68, 11.7, 11.72,
11.74, 11.76, 11.78, 11.8, 11.82, 11.84, 11.86, 11.88, 11.9,
11.92, 11.94, 11.96, 11.98, 12, 12.02, 12.04, 12.06, 12.08, 12.1,
12.12, 12.14, 12.16, 12.18, 12.2, 12.22, 12.24, 12.26, 12.28,
12.3, 12.32, 12.34, 12.36, 12.38, 12.4, 12.42, 12.44, 12.46,
12.48, 12.5, 12.52, 12.54, 12.56, 12.58, 12.6, 12.62, 12.64,
12.66, 12.68, 12.7, 12.72, 12.74, 12.76, 12.78, 12.8, 12.82,
12.84, 12.86, 12.88, 12.9, 12.92, 12.94, 12.96, 12.98, 13, 13.02,
13.04, 13.06, 13.08, 13.1, 13.12, 13.14, 13.16, 13.18, 13.2,
13.22, 13.24, 13.26, 13.28, 13.3, 13.32, 13.34, 13.36, 13.38,
13.4, 13.42, 13.44, 13.46, 13.48, 13.5, 13.52, 13.54, 13.56,
13.58, 13.6, 13.62, 13.64, 13.66, 13.68, 13.7, 13.72, 13.74,
13.76, 13.78, 13.8, 13.82, 13.84, 13.86, 13.88, 13.9, 13.92,
13.94, 13.96, 13.98, 14, 14.02, 14.04, 14.06, 14.08, 14.1, 14.12,
14.14, 14.16, 14.18, 14.2, 14.22, 14.24, 14.26, 14.28, 14.3,
14.32, 14.34, 14.36, 14.38, 14.4, 14.42, 14.44, 14.46, 14.48,
14.5, 14.52, 14.54, 14.56, 14.58, 14.6, 14.62, 14.64, 14.66,
14.68, 14.7, 14.72, 14.74, 14.76, 14.78, 14.8, 14.82, 14.84,
14.86, 14.88, 14.9, 14.92, 14.94, 14.96, 14.98, 15, 15.02, 15.04,
15.06, 15.08, 15.1, 15.12, 15.14, 15.16, 15.18, 15.2, 15.22,
15.24, 15.26, 15.28, 15.3, 15.32, 15.34, 15.36, 15.38, 15.4,
15.42, 15.44, 15.46, 15.48, 15.5, 15.52, 15.54, 15.56, 15.58,
15.6, 15.62, 15.64, 15.66, 15.68, 15.7, 15.72, 15.74, 15.76,
15.78, 15.8, 15.82, 15.84, 15.86, 15.88, 15.9, 15.92, 15.94,
15.96, 15.98, 16, 16.02, 16.04, 16.06, 16.08, 16.1, 16.12, 16.14,
16.16, 16.18, 16.2, 16.22, 16.24, 16.26, 16.28, 16.3, 16.32,
16.34, 16.36, 16.38, 16.4, 16.42, 16.44, 16.46, 16.48, 16.5,
16.52, 16.54, 16.56, 16.58, 16.6, 16.62, 16.64, 16.66, 16.68,
16.7, 16.72, 16.74, 16.76, 16.78, 16.8, 16.82, 16.84, 16.86,
16.88, 16.9, 16.92, 16.94, 16.96, 16.98, 17, 17.02, 17.04, 17.06,
17.08, 17.1, 17.12, 17.14, 17.16, 17.18, 17.2, 17.22, 17.24,
17.26, 17.28, 17.3, 17.32, 17.34, 17.36, 17.38, 17.4, 17.42,
17.44, 17.46, 17.48, 17.5, 17.52, 17.54, 17.56, 17.58, 17.6,
17.62, 17.64, 17.66, 17.68, 17.7, 17.72, 17.74, 17.76, 17.78,
17.8, 17.82, 17.84, 17.86, 17.88, 17.9, 17.92, 17.94, 17.96,
17.98, 18, 18.02, 18.04, 18.06, 18.08, 18.1, 18.12, 18.14, 18.16,
18.18, 18.2, 18.22, 18.24, 18.26, 18.28, 18.3, 18.32, 18.34,
18.36, 18.38, 18.4, 18.42, 18.44, 18.46, 18.48, 18.5, 18.52,
18.54, 18.56, 18.58, 18.6, 18.62, 18.64, 18.66, 18.68, 18.7,
18.72, 18.74, 18.76, 18.78, 18.8, 18.82, 18.84, 18.86, 18.88,
18.9, 18.92, 18.94, 18.96, 18.98, 19, 19.02, 19.04, 19.06, 19.08,
19.1, 19.12, 19.14, 19.16, 19.18, 19.2, 19.22, 19.24, 19.26,
19.28, 19.3, 19.32, 19.34, 19.36, 19.38, 19.4, 19.42, 19.44,
19.46, 19.48, 19.5, 19.52, 19.54, 19.56, 19.58, 19.6, 19.62,
19.64, 19.66, 19.68, 19.7, 19.72, 19.74, 19.76, 19.78, 19.8,
19.82, 19.84, 19.86, 19.88, 19.9, 19.92, 19.94, 19.96, 19.98,
20, 20.02, 20.04, 20.06, 20.08, 20.1, 20.12, 20.14, 20.16, 20.18,
20.2, 20.22, 20.24, 20.26, 20.28, 20.3, 20.32, 20.34, 20.36,
20.38, 20.4, 20.42, 20.44, 20.46, 20.48, 20.5, 20.52, 20.54,
20.56, 20.58, 20.6, 20.62, 20.64, 20.66, 20.68, 20.7, 20.72,
20.74, 20.76, 20.78, 20.8, 20.82, 20.84, 20.86, 20.88, 20.9,
20.92, 20.94, 20.96, 20.98, 21, 21.02, 21.04, 21.06, 21.08, 21.1,
21.12, 21.14, 21.16, 21.18, 21.2, 21.22, 21.24, 21.26, 21.28,
21.3, 21.32, 21.34, 21.36, 21.38, 21.4, 21.42, 21.44, 21.46,
21.48, 21.5, 21.52, 21.54, 21.56, 21.58, 21.6, 21.62, 21.64,
21.66, 21.68, 21.7, 21.72, 21.74, 21.76, 21.78, 21.8, 21.82,
21.84, 21.86, 21.88, 21.9, 21.92, 21.94, 21.96, 21.98, 22, 22.02,
22.04, 22.06, 22.08, 22.1, 22.12, 22.14, 22.16, 22.18, 22.2,
22.22, 22.24, 22.26, 22.28, 22.3, 22.32, 22.34, 22.36, 22.38,
22.4, 22.42, 22.44, 22.46, 22.48, 22.5, 22.52, 22.54, 22.56,
22.58, 22.6, 22.62, 22.64, 22.66, 22.68, 22.7, 22.72, 22.74,
22.76, 22.78, 22.8, 22.82, 22.84, 22.86, 22.88, 22.9, 22.92,
22.94, 22.96, 22.98, 23, 23.02, 23.04, 23.06, 23.08, 23.1, 23.12,
23.14, 23.16, 23.18, 23.2, 23.22, 23.24, 23.26, 23.28, 23.3,
23.32, 23.34, 23.36, 23.38, 23.4, 23.42, 23.44, 23.46, 23.48,
23.5, 23.52, 23.54, 23.56, 23.58, 23.6, 23.62, 23.64, 23.66,
23.68, 23.7, 23.72, 23.74, 23.76, 23.78, 23.8, 23.82, 23.84,
23.86, 23.88, 23.9, 23.92, 23.94, 23.96, 23.98, 24, 24.02, 24.04,
24.06, 24.08, 24.1, 24.12, 24.14, 24.16, 24.18, 24.2, 24.22,
24.24, 24.26, 24.28, 24.3, 24.32, 24.34, 24.36, 24.38, 24.4,
24.42, 24.44, 24.46, 24.48, 24.5, 24.52, 24.54, 24.56, 24.58,
24.6, 24.62, 24.64, 24.66, 24.68, 24.7, 24.72, 24.74, 24.76,
24.78, 24.8, 24.82, 24.84, 24.86, 24.88, 24.9, 24.92, 24.94,
24.96, 24.98, 25, 25.02, 25.04, 25.06, 25.08, 25.1, 25.12, 25.14,
25.16, 25.18, 25.2, 25.22, 25.24, 25.26, 25.28, 25.3, 25.32,
25.34, 25.36, 25.38, 25.4, 25.42, 25.44, 25.46, 25.48, 25.5,
25.52, 25.54, 25.56, 25.58, 25.6, 25.62, 25.64, 25.66, 25.68,
25.7, 25.72, 25.74, 25.76, 25.78, 25.8, 25.82, 25.84, 25.86,
25.88, 25.9, 25.92, 25.94, 25.96, 25.98, 26, 26.02, 26.04, 26.06,
26.08, 26.1, 26.12, 26.14, 26.16, 26.18, 26.2, 26.22, 26.24,
26.26, 26.28, 26.3, 26.32, 26.34, 26.36, 26.38, 26.4, 26.42,
26.44, 26.46, 26.48, 26.5, 26.52, 26.54, 26.56, 26.58, 26.6,
26.62, 26.64, 26.66, 26.68, 26.7, 26.72, 26.74, 26.76, 26.78,
26.8, 26.82, 26.84, 26.86, 26.88, 26.9, 26.92, 26.94, 26.96,
26.98, 27, 27.02, 27.04, 27.06, 27.08, 27.1, 27.12, 27.14, 27.16,
27.18, 27.2, 27.22, 27.24, 27.26, 27.28, 27.3, 27.32, 27.34,
27.36, 27.38, 27.4, 27.42, 27.44, 27.46, 27.48, 27.5, 27.52,
27.54, 27.56, 27.58, 27.6, 27.62, 27.64, 27.66, 27.68, 27.7,
27.72, 27.74, 27.76, 27.78, 27.8, 27.82, 27.84, 27.86, 27.88,
27.9, 27.92, 27.94, 27.96, 27.98, 28, 28.02, 28.04, 28.06, 28.08,
28.1, 28.12, 28.14, 28.16, 28.18, 28.2, 28.22, 28.24, 28.26,
28.28, 28.3, 28.32, 28.34, 28.36, 28.38, 28.4, 28.42, 28.44,
28.46, 28.48, 28.5, 28.52, 28.54, 28.56, 28.58, 28.6, 28.62,
28.64, 28.66, 28.68, 28.7, 28.72, 28.74, 28.76, 28.78, 28.8,
28.82, 28.84, 28.86, 28.88, 28.9, 28.92, 28.94, 28.96, 28.98,
29, 29.02, 29.04, 29.06, 29.08, 29.1, 29.12, 29.14, 29.16, 29.18,
29.2, 29.22, 29.24, 29.26, 29.28, 29.3, 29.32, 29.34, 29.36,
29.38, 29.4, 29.42, 29.44, 29.46, 29.48, 29.5, 29.52, 29.54,
29.56, 29.58, 29.6, 29.62, 29.64, 29.66, 29.68, 29.7, 29.72,
29.74, 29.76, 29.78, 29.8, 29.82, 29.84, 29.86, 29.88, 29.9,
29.92, 29.94, 29.96, 29.98, 30, 30.02, 30.04, 30.06, 30.08, 30.1,
30.12, 30.14, 30.16, 30.18, 30.2, 30.22, 30.24, 30.26, 30.28,
30.3, 30.32, 30.34, 30.36, 30.38, 30.4, 30.42, 30.44, 30.46,
30.48, 30.5, 30.52, 30.54, 30.56, 30.58, 30.6, 30.62, 30.64,
30.66, 30.68, 30.7, 30.72, 30.74, 30.76, 30.78, 30.8, 30.82,
30.84, 30.86, 30.88, 30.9, 30.92, 30.94, 30.96, 30.98, 31, 31.02,
31.04, 31.06, 31.08, 31.1, 31.12, 31.14, 31.16, 31.18, 31.2,
31.22, 31.24, 31.26, 31.28, 31.3, 31.32, 31.34, 31.36, 31.38,
31.4, 31.42, 31.44, 31.46, 31.48, 31.5, 31.52, 31.54, 31.56,
31.58, 31.6, 31.62, 31.64, 31.66, 31.68, 31.7, 31.72, 31.74,
31.76, 31.78, 31.8, 31.82, 31.84, 31.86, 31.88, 31.9, 31.92,
31.94, 31.96, 31.98, 32, 32.02, 32.04, 32.06, 32.08, 32.1, 32.12,
32.14, 32.16, 32.18, 32.2, 32.22, 32.24, 32.26, 32.28, 32.3,
32.32, 32.34, 32.36, 32.38, 32.4, 32.42, 32.44, 32.46, 32.48,
32.5, 32.52, 32.54, 32.56, 32.58, 32.6, 32.62, 32.64, 32.66,
32.68, 32.7, 32.72, 32.74, 32.76, 32.78, 32.8, 32.82, 32.84,
32.86, 32.88, 32.9, 32.92, 32.94, 32.96, 32.98, 33, 33.02, 33.04,
33.06, 33.08, 33.1, 33.12, 33.14, 33.16, 33.18, 33.2, 33.22,
33.24, 33.26, 33.28, 33.3, 33.32, 33.34, 33.36, 33.38, 33.4,
33.42, 33.44, 33.46, 33.48, 33.5, 33.52, 33.54, 33.56, 33.58,
33.6, 33.62, 33.64, 33.66, 33.68, 33.7, 33.72, 33.74, 33.76,
33.78, 33.8, 33.82, 33.84, 33.86, 33.88, 33.9, 33.92, 33.94,
33.96, 33.98, 34, 34.02, 34.04, 34.06, 34.08, 34.1, 34.12, 34.14,
34.16, 34.18, 34.2, 34.22, 34.24, 34.26, 34.28, 34.3, 34.32,
34.34, 34.36, 34.38, 34.4, 34.42, 34.44, 34.46, 34.48, 34.5,
34.52, 34.54, 34.56, 34.58, 34.6, 34.62, 34.64, 34.66, 34.68,
34.7, 34.72, 34.74, 34.76, 34.78, 34.8, 34.82, 34.84, 34.86,
34.88, 34.9, 34.92, 34.94, 34.96, 34.98, 35, 35.02, 35.04, 35.06,
35.08, 35.1, 35.12, 35.14, 35.16, 35.18, 35.2, 35.22, 35.24,
35.26, 35.28, 35.3, 35.32, 35.34, 35.36, 35.38, 35.4, 35.42,
35.44, 35.46, 35.48, 35.5, 35.52, 35.54, 35.56, 35.58, 35.6,
35.62, 35.64, 35.66, 35.68, 35.7, 35.72, 35.74, 35.76, 35.78,
35.8, 35.82, 35.84, 35.86, 35.88, 35.9, 35.92, 35.94, 35.96,
35.98, 36, 36.02, 36.04, 36.06, 36.08, 36.1, 36.12, 36.14, 36.16,
36.18, 36.2, 36.22, 36.24, 36.26, 36.28, 36.3, 36.32, 36.34,
36.36, 36.38, 36.4, 36.42, 36.44, 36.46, 36.48, 36.5, 36.52,
36.54, 36.56, 36.58, 36.6, 36.62, 36.64, 36.66, 36.68, 36.7,
36.72, 36.74, 36.76, 36.78, 36.8, 36.82, 36.84, 36.86, 36.88,
36.9, 36.92, 36.94, 36.96, 36.98, 37, 37.02, 37.04, 37.06, 37.08,
37.1, 37.12, 37.14, 37.16, 37.18, 37.2, 37.22, 37.24, 37.26,
37.28, 37.3, 37.32, 37.34, 37.36, 37.38, 37.4, 37.42, 37.44,
37.46, 37.48, 37.5, 37.52, 37.54, 37.56, 37.58, 37.6, 37.62,
37.64, 37.66, 37.68, 37.7, 37.72, 37.74, 37.76, 37.78, 37.8,
37.82, 37.84, 37.86, 37.88, 37.9, 37.92, 37.94, 37.96, 37.98,
38, 38.02, 38.04, 38.06, 38.08, 38.1, 38.12, 38.14, 38.16, 38.18,
38.2, 38.22, 38.24, 38.26, 38.28, 38.3, 38.32, 38.34, 38.36,
38.38, 38.4, 38.42, 38.44, 38.46, 38.48, 38.5, 38.52, 38.54,
38.56, 38.58, 38.6, 38.62, 38.64, 38.66, 38.68, 38.7, 38.72,
38.74, 38.76, 38.78, 38.8, 38.82, 38.84, 38.86, 38.88, 38.9,
38.92, 38.94, 38.96, 38.98, 39, 39.02, 39.04, 39.06, 39.08, 39.1,
39.12, 39.14, 39.16, 39.18, 39.2, 39.22, 39.24, 39.26, 39.28,
39.3, 39.32, 39.34, 39.36, 39.38, 39.4, 39.42, 39.44, 39.46,
39.48, 39.5, 39.52, 39.54)), row.names = 2:1977, class = "data.frame")
Initially, I was subsetting my data by visually looking at where the peaks are on the graph and then finding the maximum value within each subset.
#Find 1st peak value
dat_peak1 <- subset(dat, Time > 3.5 & Time < 5.6)
dat_peak1_max <- max(dat_peak1$Temp)
[1] 49.91
#Find 2nd peak value
dat_peak2 <- subset(dat, Time > 5.6 & Time < 7.6)
dat_peak2_max <- max(dat_peak2$Temp)
[1] 57.83
#Find 3rd peak value
dat_peak3 <- subset(dat, Time > 7.6 & Time < 9.6)
dat_peak3_max <- max(dat_peak3$Temp)
[1] 57.49
I plotted the determined peak values to determine if they are indeed the peak values.
graph_peak<-ggplot(dat, height = 50) +
geom_line(mapping = aes(x = Time, y = Temp),size=1)+
geom_hline(yintercept = dat_peak1_max, color = "red")+
geom_hline(yintercept = dat_peak2_max, color = "red")+
geom_hline(yintercept = dat_peak3_max, color = "red")+
xlim(0,10) #changed x-axis limits to better see the peaks
graph_peak
Now, for 3 peaks this seems to do the job but sometimes I need to analyze datasets that have more than 3 peaks. That's why I started looking into functions that can determine the peaks of my data. I have tried turnpoints as well as quantmod::findPeaks but that didn't seem to work that well for me as I prefer to get the actual values as an output instead of indices. And also, I'm getting multiple indices instead of just three. With that, I am posting this question solely to figure out how to make pracma::findpeaks work for my application. The biggest struggle for me is the weak documentation that does not help me tweak my code in order to get the peak values that I need to get. Below you will see my various attempts in getting the said peak values.
Attempt 2 - specify # of desired peaks which in this case is 3:
peak_val2 <- findpeaks(dat$Temp, npeaks = 3)
peak_val2
[,1]
[1,] 36.38
[2,] 36.34
[3,] 36.28
Attempt 3 - include sortstr to return peak values in decreasing order:
peak_val3 <- findpeaks(dat$Temp, npeaks = 3, sortstr = TRUE)
peak_val3
Attempt 3 results:
This is interesting because telling the code to change the order gives us completely different outputs as compared to Attempt 2. The first row gave us a correct peak value but rows 2 and 3 are incorrect.
[,1] [,2] [,3] [,4]
[1,] 57.49 430 388 431
[2,] 52.45 387 384 388
[3,] 40.09 785 784 786

Adding text label on a ggplotly figure

I have compiled the following code in R using ggplot and plotly. Everything with my graph is fine, however I would like to add a text label in the top right corner which will display my R Squared for the model I've specified. When I try to use the stat_poly_eq function it works for my standard ggplot, however it disappears in ggplotly
Is there a way for me to add the R squared automatically in ggplotly or by manually adding the label "R^2 = 0.6"?
Here is my code
library(rmsfuns)
packages <- c('tidyquant','PerformanceAnalytics','PortfolioAnalytics','tidyr',
'dplyr','lubridate','tbl2xts', 'ggplot2','plotly','ggthemes','readxl', 'ISLR','ggrepel', 'ggpmisc','mgcv')
load_pkg(packages)
data <- read_excel(path = "C:/Users/user/Documents/latest website/website/SA_dataset.xlsx", col_names = T)
data <- data %>% mutate(Date = as.Date(Date))
my.formula <- y~splines::bs(x, 3)
p1 <- ggplot(data, aes(x=Oil,y = ZAR)) +
geom_point(aes(col = VIX),size = 2, alpha = 2) +
scale_color_continuous(low = '#fb9d9d', high = '#4f042e') +
geom_smooth(aes(group=1),method ='lm',formula = my.formula,se=FALSE,,color='#56242e', linetype = 'dashed', size = 1.5) +
labs(title = 'USD-ZAR Exchange Rate vs Crude Oil Prices', subtitle = 'Higher oil prices tend to coincide with a stronger Rand',
x = 'Spot Brent Crude, USD per barrel', y= 'USD-ZAR', caption = 'Data downloaded from Yahoo Finance') +
theme_light() +
stat_poly_eq(formula = my.formula,
aes(label = stat(adj.rr.label)),
parse = TRUE, label.x = 'middle', label.y ='middle')
p2 <- ggplotly(p1)
p3 <- ggplotly(p2) %>%
layout(title = list(text = paste0('USD-ZAR Exchange Rate vs Crude Oil Prices',
'<br>',
'<sup>',
'Higher oil prices (and lower global uncertainty) tend to coincide with a stronger Rand',
'</sup>')))
I've added a reproductible of my dataset here
structure(list(Date = structure(c(16437, 16444, 16451, 16458,
16465, 16472, 16479, 16486, 16493, 16500, 16507, 16514, 16521,
16528, 16535, 16542, 16549, 16556, 16563, 16570, 16577, 16584,
16591, 16598, 16605, 16612, 16619, 16626, 16633, 16640, 16647,
16654, 16661, 16668, 16675, 16682, 16689, 16696, 16703, 16710,
16717, 16724, 16731, 16738, 16745, 16752, 16759, 16766, 16773,
16780, 16787, 16794, 16801, 16808, 16815, 16822, 16829, 16836,
16843, 16850, 16857, 16864, 16871, 16878, 16885, 16892, 16899,
16906, 16913, 16920, 16927, 16934, 16941, 16948, 16955, 16962,
16969, 16976, 16983, 16990, 16997, 17004, 17011, 17018, 17025,
17032, 17039, 17046, 17053, 17060, 17067, 17074, 17081, 17088,
17095, 17102, 17109, 17116, 17123, 17130, 17137, 17144, 17151,
17158, 17165, 17172, 17179, 17186, 17193, 17200, 17207, 17214,
17221, 17228, 17235, 17242, 17249, 17256, 17263, 17270, 17277,
17284, 17291, 17298, 17305, 17312, 17319, 17326, 17333, 17340,
17347, 17354, 17361, 17368, 17375, 17382, 17389, 17396, 17403,
17410, 17417, 17424, 17431, 17438, 17445, 17452, 17459, 17466,
17473, 17480, 17487, 17494, 17501, 17508, 17515, 17522, 17529,
17536, 17543, 17550, 17557, 17564, 17571, 17578, 17585, 17592,
17599, 17606, 17613, 17620, 17627, 17634, 17641, 17648, 17655,
17662, 17669, 17676, 17683, 17690, 17697, 17704, 17711, 17718,
17725, 17732, 17739, 17746, 17753, 17760, 17767, 17774, 17781,
17788, 17795, 17802, 17809, 17816, 17823, 17830, 17837, 17844,
17851, 17858, 17865, 17872, 17879, 17886, 17893, 17900, 17907,
17914, 17921, 17928, 17935, 17942, 17949, 17956, 17963, 17970,
17977, 17984, 17991, 17998, 18005, 18012, 18019, 18026, 18033,
18040, 18047, 18054, 18061, 18068, 18075, 18082, 18089, 18096,
18103, 18110, 18117, 18124, 18131, 18138, 18145, 18152, 18159,
18166, 18173, 18180, 18187, 18194, 18201, 18208, 18215, 18222,
18229, 18236, 18243, 18250, 18257, 18264, 18271, 18278, 18285,
18292, 18299, 18306, 18313, 18320, 18327, 18334, 18341, 18348,
18355, 18362, 18369, 18376, 18383, 18390, 18397, 18404, 18411,
18418), class = "Date"), ZAR = c(11.496, 11.549, 11.4148, 11.6468,
11.513, 11.6695, 11.6215, 11.668, 12.0395, 12.479, 12.015, 12.045,
11.798, 11.9953, 12.067, 12.1255, 12.0605, 11.926, 11.794, 11.895,
12.1555, 12.5805, 12.3809, 12.1625, 12.2025, 12.3205, 12.4505,
12.36, 12.6233, 12.6745, 12.6255, 12.8261, 12.97, 13.3044, 13.8545,
13.5795, 13.3255, 13.9135, 13.732, 13.3522, 13.0814, 13.636,
13.8247, 14.1585, 14.4019, 13.9525, 14.4061, 14.3393, 15.88,
15.0983, 15.2865, 15.471, 16.3173, 16.8098, 16.4675, 15.891,
16.0275, 15.8825, 15.398, 16.1688, 15.355, 15.2627, 15.2575,
15.4548, 14.692, 14.98, 14.5697, 14.4, 14.2345, 14.8729, 15.405,
15.6323, 15.7162, 15.093, 15.23, 15.1215, 15.065, 14.5603, 14.5333,
14.58, 14.285, 13.88, 13.725, 13.4737, 13.5075, 14.375, 14.485,
14.4196, 14.1831, 13.7139, 13.725, 13.8692, 14.3193, 13.9834,
13.8265, 13.575, 14.384, 14.4755, 14.1153, 13.8061, 13.8296,
14.08, 14.0074, 13.7386, 13.735, 13.505, 13.595, 13.47, 13.2788,
13.3588, 13.065, 12.96, 13.015, 13.1628, 12.7238, 12.4396, 13.4247,
13.76, 13.419, 13.1313, 13.3693, 13.4215, 13.356, 13.234, 12.8699,
12.8115, 12.951, 12.814, 12.9318, 13.0551, 13.395, 13.0332, 12.9158,
13.0096, 13.455, 13.4418, 13.1614, 13.038, 12.9417, 12.9376,
13.1658, 13.2438, 13.5561, 13.775, 13.2663, 13.64, 14.0641, 14.2239,
14.3704, 13.981, 14.1587, 13.7068, 13.655, 13.1464, 12.5987,
12.3724, 12.3044, 12.3612, 12.191, 11.8672, 12.0916, 12.0352,
11.6363, 11.545, 11.8966, 11.8172, 11.9745, 11.7428, 11.8385,
12.025, 12.069, 12.0989, 12.3282, 12.5, 12.254, 12.7625, 12.491,
12.6906, 13.0655, 13.4344, 13.4383, 13.7275, 13.4702, 13.2684,
13.4051, 13.1793, 13.3256, 14.104, 14.6413, 14.2087, 14.6875,
15.2372, 14.9345, 14.3136, 14.1473, 14.7747, 14.5194, 14.4188,
14.6021, 14.3048, 14.3259, 14.0227, 13.8588, 13.8692, 14.1615,
14.4016, 14.6372, 14.4298, 13.9623, 13.8351, 13.843, 13.6153,
13.3233, 13.6257, 14.0828, 13.9978, 14.229, 14.4393, 14.4146,
14.5, 14.4975, 14.096, 13.9568, 14.055, 14.3807, 14.3511, 14.1598,
14.4188, 14.4, 14.585, 14.9535, 14.8393, 14.325, 14.0808, 14.1775,
13.9857, 13.9409, 14.2963, 14.7885, 15.2623, 15.3056, 15.2519,
15.185, 14.8083, 14.5778, 14.9268, 15.1563, 15.045, 14.8055,
14.7905, 14.6329, 15.0318, 14.8605, 14.7137, 14.7175, 14.6535,
14.627, 14.5226, 14.2751, 14.0326, 14.3164, 14.3679, 14.4569,
14.3936, 15.0106, 15.0649, 14.9117, 15.0065, 15.6695, 15.6723,
16.2771, 17.6077, 17.63, 19.049, 17.9689, 18.7408, 19.0263, 18.8453,
18.3495, 18.5674, 17.6373, 17.548, 16.8765, 16.5863), Interest = c(6,
5.9, 5.8, 5.75, 6.03, 6.04, 5.95, 5.91, 5.91, 5.91, 5.91, 5.91,
6.15, 6, 6.15, 6.13, 6.16, 6.16, 6.25, 6.08, 6.25, 6.5, 6.6,
6.6, 6.45, 6.44, 6.6, 6.38, 6.44, 6.49, 6.48, 6.44, 6.44, 6.49,
6.5, 6.49, 6.49, 6.3, 6.3, 6.55, 6.55, 6.55, 6.65, 6.65, 6.65,
6.65, 6.65, 6.35, 6.35, 6.35, 6.35, 6.35, 7.3, 7.3, 6.9, 7.3,
7.3, 7.3, 7.3, 7.2, 7.3, 7.2, 7.7, 7.2, 7.1, 7.35, 7.15, 7.15,
7.35, 7.35, 7.35, 7.35, 7.15, 7.15, 7.45, 7.9, 8, 7.95, 7.45,
7.55, 8, 8, 7.35, 6.8, 7.3, 6.8, 6.95, 6.9, 7.8, 7.6, 7.3, 7.7,
7.4, 8.05, 7.5, 7.9, 7.75, 7.45, 7.8, 7.5, 7.65, 7.5, 7.15, 8,
7.31, 7.25, 7.25, 7.75, 6.85, 6.98, 6.6, 7.4, 6.8, 7.05, 6.39,
6.1, 6.4, 7.15, 6.65, 7.2, 6.75, 6.5, 6.2, 6.5, 6.4, 6.9, 6.3,
7.25, 6.7, 6.72, 6.8, 6.8, 6.75, 7.15, 6.5, 6.3, 5.95, 6.45,
6.45, 6.35, 6.25, 5.95, 5.5, 6.2, 6.6, 6.35, 7.3, 6.4, 6.15,
6.5, 6.25, 6.9, 6, 6.95, 7.15, 7.05, 5.6, 6.55, 6.55, 5.5, 6.5,
6.5, 6.5, 6.6, 6.6, 6.55, 6.35, 6.6, 7.06, 6.95, 6.35, 6.35,
6.35, 6.3, 6.3, 6.42, 6.95, 6.3, 6.38, 6.83, 6.7, 6.9, 6.9, 6.4,
6.35, 6.29, 6.85, 6.2, 6.9, 7.07, 7.08, 7.08, 6.6, 7.1, 7.1,
7.15, 7.15, 7.06, 7.06, 7.06, 6.78, 6.78, 6.99, 6.02, 7.35, 7.17,
7.3, 6.74, 7.1, 7.2, 6.4, 6.92, 6.84, 6.87, 7.01, 7, 6.95, 6.95,
6.4, 6.33, 6.15, 6.86, 6.77, 6.78, 7.1, 6.88, 6.82, 6.82, 6.81,
6.83, 6.83, 6.82, 6.76, 6.74, 6.22, 5.58, 4.9, 6.73, 6, 6.68,
6.09, 6.59, 6.75, 6.51, 6.4, 6.5, 6.62, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5.5, 5.4,
4.35, 4.3, 3.93, 3.5, 3.65, 2.29, 3.05, 3.63, 3.6, 3.55, 3.8),
Oil = c(56.03, 49.47, 46.58, 46.44, 46.76, 54.62, 56.57,
60.57, 60.63, 60.12, 56.51, 52.72, 55.01, 54.79, 56.51, 59.12,
61.41, 63.49, 65.01, 64.9, 64.31, 61.57, 61.9, 63.24, 60.65,
60.84, 60.36, 56.63, 57.17, 55.75, 54.14, 48.59, 47.94, 46,
43.53, 48.87, 47.6, 47.34, 47.33, 46.99, 51.33, 48.99, 46.81,
47.15, 47.23, 44.75, 41.54, 43.65, 42.41, 38.76, 36.84, 35.9,
36.55, 34.19, 29.1, 27.76, 31.75, 32.18, 30.41, 32.29, 33.12,
36.28, 39.3, 38.5, 39.41, 37, 37.51, 42.35, 43.04, 44.46,
44.2, 45.2, 48.34, 48.73, 48.91, 49.94, 47.39, 48.12, 47.09,
45.96, 45.54, 45.22, 42.55, 40.88, 43.63, 48.6, 48.65, 47.31,
47.51, 46.21, 46.24, 46.74, 49.52, 49.94, 50.33, 48.95, 44.63,
43.09, 44.01, 46.49, 48.78, 52.26, 53.26, 53.81, 54.96, 55.13,
54.02, 54.19, 54.91, 55.53, 54.48, 54.46, 55.4, 54.4, 52.78,
50.29, 50.1, 51.33, 53.28, 54.83, 52.08, 49.55, 48.7, 48,
51.75, 52.58, 50.23, 47.28, 46.18, 44.63, 46.16, 48.17, 47.1,
47.99, 49.95, 52.04, 51.96, 50.58, 52.03, 52.25, 53.88, 55.54,
57.03, 58.75, 56.08, 56.21, 57.78, 58.58, 61.04, 64.29, 61.52,
62.68, 63.73, 63.08, 64.35, 64.36, 66.52, 67.81, 69.47, 69.39,
70.04, 68, 65.5, 62.72, 65.52, 66.02, 65.12, 64.14, 67.4,
68.65, 67.03, 71.24, 73.39, 74.98, 74.42, 76.68, 78.75, 78.59,
75.35, 73.98, 74.26, 74.12, 75.24, 76.33, 75.29, 71.27, 74,
73.37, 71.42, 69.82, 72.77, 76.08, 76.64, 78.11, 78.97, 81.85,
85.44, 83.05, 80.41, 78.23, 74.09, 70.34, 66.21, 61.22, 58.65,
60.47, 59.51, 54.64, 51.03, 54.31, 58.64, 59.83, 61.34, 61.38,
61.71, 63.36, 66.58, 64.56, 64.73, 65.71, 67.34, 67.25, 69.54,
71.33, 70.87, 72.93, 71.59, 71.25, 73.32, 70.89, 69.29, 63.15,
63.19, 64.04, 66.51, 63.84, 65.98, 63.63, 62.8, 62.59, 57.33,
58.25, 59.57, 59.83, 60.23, 62.74, 65.56, 63.15, 59.22, 59.39,
59.32, 60.75, 60.23, 62.39, 62.56, 63.76, 64.74, 64.71, 66.5,
68.9, 68.73, 68.07, 67.93, 64.11, 62.2, 58.57, 54.59, 55.33,
58.61, 54.09, 50.7, 33.72, 25.65, 23.96, 18.72, 22.53, 20,
14.24, 17.05, 23.57, 28.18, 33.94, 33.7, 39.66), VIX = c(17.55,
20.95, 16.66, 20.97, 17.29, 14.69, 14.3, 13.34, 15.2, 16,
13.02, 15.07, 14.67, 12.58, 13.89, 12.29, 12.7, 12.86, 12.38,
12.13, 13.84, 14.21, 13.78, 13.96, 14.02, 16.79, 16.83, 11.95,
13.74, 12.12, 13.39, 12.83, 28.03, 26.05, 27.8, 23.2, 22.28,
23.62, 20.94, 17.08, 15.05, 14.46, 15.07, 14.33, 20.08, 15.47,
15.12, 14.81, 24.39, 20.7, 15.74, 18.21, 27.01, 27.02, 22.34,
20.2, 23.38, 25.4, 20.53, 19.81, 16.86, 16.5, 14.02, 14.74,
13.1, 15.36, 13.62, 13.22, 15.7, 14.72, 15.04, 15.2, 13.12,
13.47, 17.03, 19.41, 25.76, 14.77, 13.2, 12.67, 12.02, 11.87,
11.39, 11.55, 11.34, 13.65, 11.98, 17.5, 15.37, 12.29, 13.29,
13.48, 16.12, 13.34, 16.19, 22.51, 14.17, 12.85, 12.34, 14.12,
11.75, 12.2, 11.44, 14.04, 11.32, 11.23, 11.54, 10.58, 10.97,
10.85, 11.49, 11.47, 10.96, 11.66, 11.28, 12.96, 12.37, 12.87,
15.96, 14.63, 10.82, 10.57, 10.4, 12.04, 9.81, 9.75, 10.7,
10.38, 10.02, 11.18, 11.19, 9.51, 9.36, 10.29, 10.03, 15.51,
14.26, 11.28, 10.13, 12.12, 10.17, 9.59, 9.51, 9.65, 9.61,
9.97, 9.8, 9.14, 11.29, 11.43, 9.67, 11.43, 9.58, 9.42, 9.9,
11.04, 9.22, 10.16, 11.27, 11.08, 17.31, 29.06, 19.46, 16.49,
19.59, 14.64, 15.8, 24.87, 19.97, 21.49, 17.41, 16.88, 15.41,
14.77, 12.65, 13.42, 13.22, 13.46, 12.18, 11.98, 13.77, 16.09,
13.37, 12.18, 12.86, 13.03, 11.64, 13.16, 12.64, 11.99, 12.86,
14.88, 12.07, 11.68, 12.12, 14.82, 21.31, 19.89, 24.16, 19.51,
17.36, 18.14, 21.52, 18.07, 23.23, 21.63, 30.11, 28.34, 21.38,
18.19, 17.8, 17.42, 16.14, 15.72, 14.91, 13.51, 13.57, 16.05,
12.88, 16.48, 13.71, 12.82, 12.01, 12.09, 12.73, 12.87, 16.04,
15.96, 15.85, 18.71, 16.3, 15.28, 15.4, 15.08, 13.28, 12.39,
14.45, 12.16, 17.61, 17.97, 18.47, 19.87, 18.98, 15, 13.74,
15.32, 17.22, 17.04, 15.58, 14.25, 12.65, 12.3, 12.07, 12.05,
12.34, 12.62, 13.62, 12.63, 12.51, 13.43, 14.02, 12.56, 12.1,
14.56, 18.84, 15.47, 13.68, 17.08, 40.11, 41.94, 57.83, 66.04,
65.54, 46.8, 41.67, 38.15, 35.93, 37.19, 27.98, 31.89, 28.16,
27.51, 24.52, 28.27)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -284L))
The issue is that stat_poly_eq is yet not implemented in ggplotly. ggplotly should give you a warning about this. But you can add a text annotation to your plot like so, where I used TeX to get the super- and subscripts:
p3 %>%
add_annotations(
x = mean(p3$x$layout$xaxis$range),
y = mean(p3$x$layout$yaxis$range),
text = TeX("R^{2}_{adj} = 0.48"),
showarrow = F
) %>%
config(mathjax = 'cdn')

Some functions in PerformanceAnalytics package are failing when use with zoo::rollapply

I am trying to use the zoo::rollapply function to chronologically calculate Sterling Ratio using the package PerformanceAnalytics. Below is my calculation -
library(zoo)
library(PerformanceAnalytics)
Orig_Data = structure(c(12.05, 12.75, 12.75, 12.55, 13.05, 14.15, 16.2, 16.5,
15.95, 16.9, 17.3, 17.5, 16.2, 16.7, 16.4, 15.95, 17.15, 16.2,
16.5, 15.35, 15.8, 16.1, 16.35, 18.3, 18.2, 17.35, 17.95, 18.35,
18.3, 17.9, 18.1, 20.65, 20.45, 20.3, 20.65, 18.7, 18.95, 18.3,
16.65, 17.05, 17.15, 16.9, 17.15, 16.55, 16.2, 16, 16.8, 16.8,
16.9, 16.75, 16.4, 15.8, 15.2, 15.15, 16.5, 16.75, 16.5, 15.85,
15.75, 15.15, 15.2, 15.45, 16.15, 16.1, 16.05, 15.7, 15.75, 15.85,
15.9, 15.5, 15.5, 15.7, 15.55, 15.45, 15.2, 14.75, 14.7, 14.1,
14.2, 14.1, 14.25, 14.35, 14, 13.4, 13.4, 13.05, 12.9, 12.8,
12.5, 12.45, 12.45, 12.6, 12.15, 12, 12.1, 12.4, 13, 12.9, 12.9,
12.7, 13.15, 11.75, 11.25, 11.05, 10.95, 11.4, 11.4, 11.45, 11.2,
10.95, 10.65, 9.9, 9.65, 9.65, 9.05, 8.6, 8.75, 8.8, 9.3, 9.85,
10.15, 9.7, 9.8, 9.85, 10.25, 11.15, 12.25, 12.75, 12.55, 12,
12.85, 12.5, 12.3, 12.6, 12.25, 11.65, 11.4, 11.2, 11.15, 10.75
), index = structure(c(15341, 15342, 15343, 15344, 15345, 15348,
15349, 15350, 15351, 15352, 15355, 15356, 15357, 15358, 15359,
15362, 15363, 15364, 15366, 15369, 15370, 15371, 15372, 15373,
15376, 15377, 15378, 15379, 15380, 15383, 15384, 15385, 15386,
15387, 15391, 15392, 15393, 15394, 15397, 15398, 15399, 15400,
15401, 15404, 15405, 15406, 15408, 15411, 15412, 15413, 15414,
15415, 15418, 15419, 15420, 15421, 15422, 15425, 15426, 15427,
15428, 15429, 15432, 15433, 15434, 15439, 15440, 15441, 15442,
15443, 15446, 15447, 15448, 15449, 15450, 15453, 15454, 15455,
15456, 15457, 15460, 15462, 15463, 15464, 15467, 15468, 15469,
15470, 15471, 15474, 15475, 15476, 15477, 15478, 15481, 15482,
15483, 15484, 15485, 15488, 15489, 15490, 15491, 15492, 15495,
15496, 15497, 15498, 15499, 15502, 15503, 15504, 15505, 15506,
15509, 15510, 15511, 15512, 15513, 15516, 15517, 15518, 15519,
15520, 15523, 15524, 15525, 15526, 15527, 15530, 15531, 15532,
15533, 15534, 15537, 15538, 15539, 15540, 15541, 15544), class = "Date"), class = "zoo")
rollapply(Orig_Data,
width = 132,
align = 'right',
function(Data) {
Start = as.character(index(Data)[1])
End = as.character(last(index(Data)))
SterlingRatio = as.vector(SterlingRatio(R = diff(log(Data))))
return(data.frame('Start' = Start,
'End' = End,
'SterlingRatio' = SterlingRatio, check.names = FALSE))
})
This code is failing with an error -
Error in try.xts(x, error = "'x' needs to be timeBased or xtsible") :
'x' needs to be timeBased or xtsible
Couple of issues with the result -
Even if I dont use the function SterlingRatio() above code is not retuning correct values for Start and End
Above error is with SterlingRatio() and SharpeRatio() functions. If I use some other function e.g. SortinoRatio() it works fine.
Any idea what is going wrong would be highly helpful.
Thanks and regards,
Both SterlingRatio and SharpeRatio have some internal checks that use of the function xtsible from the xts package. This just checks if the data is in an xts format. Which in this case it is not, it is a zoo object. Example below would return data.
rollapply(xts::as.xts(Orig_Data),
width = 132,
align = 'right',
SterlingRatio)
But using as.xts inside the function you have created above, would return an error because you would be trying to merge a data.frame with an xts object. So first you need to know how you want your output to look like.

Using lapply to Sample dataframe, include new column and reorder columns

df<-structure(list(BBAS3 = c(22.85, 22.78, 22.8, 22.22, 22.51, 21.11,
20.84, 20.79, 20.67, 20.9, 20.95, 20.7, 21.03, 21.96, 21.9, 21.8,
21.9, 22.49, 22.65, 22.9, 22.19, 22.44, 21.66, 22.5, 22.96, 23.36,
23.64, 23.46, 23.85, 23.74, 23.9, 23.97, 23.95, 23.85, 23.66,
23.52, 23.5, 23.57, 23.28, 23.09, 23.74, 24.09, 23.96, 23.93,
23.07, 23.54, 24.04, 24.82, 24.58, 24.51, 23.88, 23.16, 23.79,
24.61, 25.12, 26, 25.87, 25.44, 25.6, 27.04, 26.8, 27.52, 27.65,
28.36, 28.77, 28.59, 28.63, 28.16, 27.58, 27.49, 27.6, 27.28,
26.95, 27.09, 27.05, 27.29, 26.74, 26.61, 26.04, 26.05, 25.68,
25.8, 25.49, 25.05, 25.33, 25.24, 25.17, 25.16, 25.11, 24.88,
25.44, 24.74, 24.78, 24.97, 25.25, 24.54, 24.64, 24.4, 23.61,
23.24, 23.1, 22.89, 23.29, 23.42, 23.03, 22.57, 22.4, 22.05,
21.88, 22.09, 21.9, 21.47, 21, 20.71, 20.71, 21.13, 20.86, 20.17,
20.46, 20.67, 21.24, 21.29, 21.54, 21.42, 21.06, 20.74, 20.29,
19.56, 19.66, 20.38, 20.93, 21.24, 20.8, 21.05, 20.82, 20.64,
20.73, 20.98, 21.14, 20.89, 20.46, 20.73, 20.56, 20.37, 19.81,
19.27, 19.41, 18.85, 18.73, 18.76, 18.91, 18.63, 19.18, 20.33,
20.31, 19.75, 19.55, 19.5, 19.34, 19.38, 19.41, 20.09, 20.74,
21.09, 21.03, 20.93, 20.5, 21.17, 21.7, 21.49, 22.08, 22.17,
22.7, 23.15, 23.03, 23.45, 24.29, 24.31, 24.2, 24.16, 24.19,
23.8, 24, 23.41, 22.73, 22.29, 22.82, 22.7, 22.96, 22.83, 23,
22.95, 22.01, 22.39, 22.54, 22.7, 22.55, 22.75, 23.25, 23.66,
24.19, 23.99, 24.03, 24.23, 24.93, 25.43, 25.15, 25.61, 25.58,
25.45, 24.92, 24.4, 24.01, 23.88, 24.43, 24.97, 24.96, 24.79,
24.3, 24.07, 23.88, 24.03, 24.21, 24.22, 23.82, 23.47, 23.8,
23.86, 23.77, 24.02, 24.3, 24.39, 23.95, 23.84, 23.59, 23.72,
23.75, 23.33, 23.16, 22.49, 22.5, 22.78, 22.4, 21.52, 21.72,
21.33, 21.61, 21.08, 21.13, 21.09, 20.94, 21.19, 21.31, 21.12,
21.52, 21.61, 21.96, 22.16, 21.88, 22.49, 22.58, 22.71, 22.96,
23.2, 23.74, 23.79, 23.65, 23.85, 23.01, 23.29, 23.65, 23.42,
23.16, 23.03, 23.3, 22.85, 22.67, 22.19, 22.14, 22.32, 22.14,
21.43, 20.35, 19.73, 19.82, 19.98, 20.28, 20.11, 20.11, 19.97,
19.55, 19.63, 19.28, 19.29, 19.15, 19.49, 19.15, 19.15, 19.13,
18.8, 18.28, 18.36, 18.31, 18.28, 18.41, 18.43, 18.07, 18.22,
18.66, 19.53, 20.01, 19.52, 20.08, 18.85, 19.06, 19.04, 18.94,
19.31, 18.94, 19.14, 19.74, 19.74, 19.92, 20.04, 19.83, 20.09,
20.1, 19.52, 19.81, 19.83, 20.44, 21.33, 21.6, 21.83, 21.75,
21.57, 22.44, 22.41, 22.27, 22.98, 23.07, 23.22, 23.01, 22.56,
22.91, 23.05, 22.65, 22.19, 21.71, 21.55, 21.34, 20.82, 20.11,
20.21, 19.93, 20.11, 20.35, 20.69, 20.24, 19.8, 19.87, 20.17,
19.93, 19.99, 20.19, 20.14, 19.8, 19.22, 20.04, 20.68, 20.01,
20.43, 20.14, 20.38, 19.78, 18.89, 18.99, 18.65, 18.82, 18.5,
17.61, 16.47, 16.85, 16.72, 17.04, 17.02, 16.65, 15.57, 15.69,
16.08, 15.93, 15.87, 16.15, 16.63, 17.02, 17.06, 17.06, 16.81,
16.7, 16.05, 16.08, 15.95, 16.34, 17, 17.27, 17.43, 17.5, 16.81,
16.17, 16.01, 16.31, 16.35, 16.35, 16.2, 16.18, 15.85, 16.25,
16.82, 16.86, 16.79, 17.15, 17.37, 17.53, 17.38, 17.29, 18.05,
18.01, 16.98, 16.35, 17.19, 17.3, 17.95, 18.76, 18.77, 18.78,
19.26, 19.22, 19.05, 19, 19.28, 19.82, 19.79, 19.54, 19.36, 19.84,
19.99, 20.26, 20.03, 19.97, 19.65, 20.1, 20.59, 21.16, 20.67,
20.36, 20.24, 20.28, 20.34, 21.62, 21.73)), row.names = c(NA,
-460L), class = c("tbl_df", "tbl", "data.frame"))
With this data frame I create a list of dataframe with the code bellow:
samples_size <- c(9,7,5,3)
list_of_df <- lapply(samples_size,function(i)df %>% slice(1:i))
list_of_df
Now I have this vector bellow which will be my new column to included in each dataframe of the list list_of_df respecting the sample size:
time=seq(samples_size[i],1)
The Final result should be a list of dataframes like this:
list_of_df[[1]]<-cbind(list_of_df[[1]],seq(samples_size[1],1))
list_of_df[[1]]<-cbind(list_of_df[[2]],seq(samples_size[2],1))
list_of_df[[1]]<-cbind(list_of_df[[3]],seq(samples_size[3],1))
list_of_df[[1]]<-cbind(list_of_df[[4]],seq(samples_size[4],1))
And then, after this I would like to put the new column in the first position.
Is it possible to do all of these steps by using the first code above with lapply function :
list_of_df <- lapply(samples_size,function(i)df %>% slice(1:i))
and using dplyr Package??
Yes, and there is no need for dplyr:
lapply(samples_size, function(i) cbind(time = i:1, df[1:i, ]))
For instance, the first element will be
# [[1]]
# time BBAS3
# 1 9 22.85
# 2 8 22.78
# 3 7 22.80
# 4 6 22.22
# 5 5 22.51
# 6 4 21.11
# 7 3 20.84
# 8 2 20.79
# 9 1 20.67
As I was saying in the comment, lapply takes elements of sample_size one by one, in this case they are called i, and the manually defined anonymous function constructs some kind of result. Then all the results are returned a a list (contrary to sapply, which would try to merge the results to, say, a vector). So then this time column is simply i:1, which is a short way to write seq(i, 1), cbind stands for column which, which is what you are after, and by putting time = i:1 we make it as the first column in every intermediate result. Lastly, df[1:i, ] gives the first i rows of df, and, in this case, is a simple base R analogue of slice.

Creating a list of dataframes from one dataframe using dplyr

This is my code:
My dataframe is:
df<-structure(list(BBAS3 = c(22.85, 22.78, 22.8, 22.22, 22.51, 21.11,
20.84, 20.79, 20.67, 20.9, 20.95, 20.7, 21.03, 21.96, 21.9, 21.8,
21.9, 22.49, 22.65, 22.9, 22.19, 22.44, 21.66, 22.5, 22.96, 23.36,
23.64, 23.46, 23.85, 23.74, 23.9, 23.97, 23.95, 23.85, 23.66,
23.52, 23.5, 23.57, 23.28, 23.09, 23.74, 24.09, 23.96, 23.93,
23.07, 23.54, 24.04, 24.82, 24.58, 24.51, 23.88, 23.16, 23.79,
24.61, 25.12, 26, 25.87, 25.44, 25.6, 27.04, 26.8, 27.52, 27.65,
28.36, 28.77, 28.59, 28.63, 28.16, 27.58, 27.49, 27.6, 27.28,
26.95, 27.09, 27.05, 27.29, 26.74, 26.61, 26.04, 26.05, 25.68,
25.8, 25.49, 25.05, 25.33, 25.24, 25.17, 25.16, 25.11, 24.88,
25.44, 24.74, 24.78, 24.97, 25.25, 24.54, 24.64, 24.4, 23.61,
23.24, 23.1, 22.89, 23.29, 23.42, 23.03, 22.57, 22.4, 22.05,
21.88, 22.09, 21.9, 21.47, 21, 20.71, 20.71, 21.13, 20.86, 20.17,
20.46, 20.67, 21.24, 21.29, 21.54, 21.42, 21.06, 20.74, 20.29,
19.56, 19.66, 20.38, 20.93, 21.24, 20.8, 21.05, 20.82, 20.64,
20.73, 20.98, 21.14, 20.89, 20.46, 20.73, 20.56, 20.37, 19.81,
19.27, 19.41, 18.85, 18.73, 18.76, 18.91, 18.63, 19.18, 20.33,
20.31, 19.75, 19.55, 19.5, 19.34, 19.38, 19.41, 20.09, 20.74,
21.09, 21.03, 20.93, 20.5, 21.17, 21.7, 21.49, 22.08, 22.17,
22.7, 23.15, 23.03, 23.45, 24.29, 24.31, 24.2, 24.16, 24.19,
23.8, 24, 23.41, 22.73, 22.29, 22.82, 22.7, 22.96, 22.83, 23,
22.95, 22.01, 22.39, 22.54, 22.7, 22.55, 22.75, 23.25, 23.66,
24.19, 23.99, 24.03, 24.23, 24.93, 25.43, 25.15, 25.61, 25.58,
25.45, 24.92, 24.4, 24.01, 23.88, 24.43, 24.97, 24.96, 24.79,
24.3, 24.07, 23.88, 24.03, 24.21, 24.22, 23.82, 23.47, 23.8,
23.86, 23.77, 24.02, 24.3, 24.39, 23.95, 23.84, 23.59, 23.72,
23.75, 23.33, 23.16, 22.49, 22.5, 22.78, 22.4, 21.52, 21.72,
21.33, 21.61, 21.08, 21.13, 21.09, 20.94, 21.19, 21.31, 21.12,
21.52, 21.61, 21.96, 22.16, 21.88, 22.49, 22.58, 22.71, 22.96,
23.2, 23.74, 23.79, 23.65, 23.85, 23.01, 23.29, 23.65, 23.42,
23.16, 23.03, 23.3, 22.85, 22.67, 22.19, 22.14, 22.32, 22.14,
21.43, 20.35, 19.73, 19.82, 19.98, 20.28, 20.11, 20.11, 19.97,
19.55, 19.63, 19.28, 19.29, 19.15, 19.49, 19.15, 19.15, 19.13,
18.8, 18.28, 18.36, 18.31, 18.28, 18.41, 18.43, 18.07, 18.22,
18.66, 19.53, 20.01, 19.52, 20.08, 18.85, 19.06, 19.04, 18.94,
19.31, 18.94, 19.14, 19.74, 19.74, 19.92, 20.04, 19.83, 20.09,
20.1, 19.52, 19.81, 19.83, 20.44, 21.33, 21.6, 21.83, 21.75,
21.57, 22.44, 22.41, 22.27, 22.98, 23.07, 23.22, 23.01, 22.56,
22.91, 23.05, 22.65, 22.19, 21.71, 21.55, 21.34, 20.82, 20.11,
20.21, 19.93, 20.11, 20.35, 20.69, 20.24, 19.8, 19.87, 20.17,
19.93, 19.99, 20.19, 20.14, 19.8, 19.22, 20.04, 20.68, 20.01,
20.43, 20.14, 20.38, 19.78, 18.89, 18.99, 18.65, 18.82, 18.5,
17.61, 16.47, 16.85, 16.72, 17.04, 17.02, 16.65, 15.57, 15.69,
16.08, 15.93, 15.87, 16.15, 16.63, 17.02, 17.06, 17.06, 16.81,
16.7, 16.05, 16.08, 15.95, 16.34, 17, 17.27, 17.43, 17.5, 16.81,
16.17, 16.01, 16.31, 16.35, 16.35, 16.2, 16.18, 15.85, 16.25,
16.82, 16.86, 16.79, 17.15, 17.37, 17.53, 17.38, 17.29, 18.05,
18.01, 16.98, 16.35, 17.19, 17.3, 17.95, 18.76, 18.77, 18.78,
19.26, 19.22, 19.05, 19, 19.28, 19.82, 19.79, 19.54, 19.36, 19.84,
19.99, 20.26, 20.03, 19.97, 19.65, 20.1, 20.59, 21.16, 20.67,
20.36, 20.24, 20.28, 20.34, 21.62, 21.73)), row.names = c(NA,
-460L), class = c("tbl_df", "tbl", "data.frame"))
I want to create dataframes and put them in a list. The condition to create these dataframes will respect the size of the sample:
I would like to do this with dplyr package.
I did this with a for loop:
samples_size <- c(9,7,5,3)
my_samples <- vector(mode = "list", length(samples_size))
for(i in 1:length(samples_size)){
my_samples[[i]]<-df[1:samples_size[i],]
}
my_samples
How can I do this using dplyr package?
Any help?
Thanks
Using lapply and dplyr
samples_size <- c(9,7,5,3)
list_of_df <- lapply(samples_size,function(i)df %>% sample_n(i))
list_of_df

Resources