Reputation: 681
My Goal: I am calculating the area under the curve of one of the columns of my xts for every 5 rows.
My objective: I was wondering if there was a function in R that could do it faster (vectorize it) instead of making a vector from the columns of myxts5 and then looping through the calculations.
Thank you for you help.
I have included my code, the output, and the dataset:
#create an xts object
library(xts)
myxts5<-xts(dat5[,2:7],order.by=as.POSIXct(dat5[,1]))
colnames(myxts5)<-c("Open","High","Low","Close","Volume","RSI_10")
#make the data reproducible
dput(myxts5,file='so4.txt')
#my method to calculate the entire area under the RSI
library(MESS)
y1<-as.vector(myxts5[11:nrow(myxts5),"RSI_10"]) #remove the first 10 NAs
x1<-1:length(y1)
AUC_RSI10<-NA
for(i in 1:(length(y1)-4))
{
AUC_RSI10[i]<-auc(x1,y1,from=i,to=i+4,type="spline")
}
#output the result
dput(AUC_RSI10,file='so5.txt')
Here is my output:
c(212.42031469304, 212.798819901101, 209.986805467201, 204.085562604063,
197.984367949833, 190.26277666882, 186.077746687353, 175.748595915665,
153.079882459862, 130.009179454897, 102.067828644029, 93.1546072252816,
106.253169598421, 126.695279601823, 156.84779427326, 171.19433284721,
177.724612560769, 176.407343545939, 180.185730377015, 185.540559407078,
181.492038514392, 189.691635230233, 184.490969841544, 188.753967108042,
207.705900307578, 214.048826298562, 218.30216750124, 212.29968278624,
199.551248663318, 188.776300102749, 182.52612752936, 181.256931078184,
186.756042540598, 192.301043062924, 192.527605116982, 194.728277897872,
189.022862111989, 184.468524998165, 185.39881870675, 196.154701535433,
206.890484681265, 207.327144844833, 212.703798768185, 198.013450923081,
181.641656558781, 176.54287628256, 164.743975467614, 162.034403175174,
162.412236153867, 162.309298857098, 166.286729859961, 176.547050171716,
192.181072883591, 205.515617818054, 218.07534078917, 218.140824515946,
205.641441980933, 201.576989476802, 196.867549251374, 190.783357752807,
197.903201932701, 202.879574501878, 206.106624821193, 220.374961483055,
227.474013530533, 223.343713145521, 229.246256446896, 243.286328522227,
262.117443779325, 263.840756142047, 240.806125975743, 215.212025274733,
187.537410808637, 181.697615578155, 189.007912680354, 184.797339348203,
181.36323163363, 180.650062530952, 179.317002557559, 185.586705150491,
186.662076288898, 188.57947064012, 203.125520580371, 203.930450146523,
203.495197679754, 202.028430483861, 194.680742214661, 193.31866654904,
191.777680494373, 191.023982678201, 188.354726404744, 188.21830097328,
195.985546556535, 203.55439816918, 208.861239277293, 213.888773137499,
214.806015837923, 214.330645702585, 213.514904083631, 226.469183651925,
236.327655260894, 237.566101775243, 252.190727914684, 243.354777400258,
231.51372836124, 229.798793976413, 221.107200373181, 217.996862355506,
211.122748333001, 210.582316625607, 208.01205675632, 209.26953269887,
225.397380909908, 230.483506508238, 227.694726952225, 224.967853799347,
218.724867445519, 216.905971862255, 221.52922790949, 225.631736902592,
230.891924036962, 241.177062846485, 249.246022464813, 254.246246799188,
256.317954002037, 247.643366520204, 238.622153789957, 227.473438159439,
214.560573157924, 212.790548936815, 208.420352726885, 208.640044465686,
214.931993510179, 212.72755394672, 219.410868549475, 233.687119760904,
246.692220779985, 264.374607939455, 269.70827693888, 268.536133748541,
272.85747899862, 274.53970011536, 283.600460826417, 295.875094542883,
302.745632060615, 309.701548644739, 319.260316467242, 326.643036235032,
332.342520658862, 338.413750490865, 343.136490698001, 348.603529563524,
352.398026008696, 357.650124476616, 358.743285419066, 344.637128752378,
324.347614348694, 296.899987957071, 260.178562036282, 235.165894610707,
218.512708871952, 201.615881861806, 204.662806854938, 214.207761875216,
221.261680991827, 236.578968494141, 248.278613344013, 258.043801399754,
274.96482029892, 286.236076785085, 285.602545656925, 285.686696242866,
283.210913624938, 287.152216650458, 295.150088828741, 299.214294408208,
304.574243471004, 307.788231278327, 313.280310293549, 319.207810872882,
318.414126071909, 305.570556389567, 290.312583077288, 286.828481019533,
284.595391529875, 289.607238027879, 298.301134488251, 292.448822019436,
286.489056604544, 279.206214787399)
Here is the data set:
structure(c(3, 3, 3.24, 3.25, 3.35, 3.3, 3.19, 3.15, 3.2, 3.24,
3.24, 3.239, 3.24, 3.24, 3.24, 3.24, 3.24, 3.23, 3.19, 3.19,
3.18, 3.25, 3.05, 2.95, 2.95, 2.94, 2.96, 3, 3.05, 3, 3, 3.1,
3.15, 3.15, 2.99, 3.04, 3, 3.2, 3.18, 3.18, 3.15, 3.02, 3, 2.99,
2.99, 3, 3.1, 3.05, 2.99, 3, 3, 3, 3, 3.1, 3.2, 2.98, 2.85, 3,
2.9, 2.88, 2.85, 2.86, 2.86, 2.9, 2.9, 3, 3, 3, 3, 2.9, 2.91,
3, 3, 2.92, 3, 3, 3, 3.05, 3, 3, 3, 3.15, 3.3, 3.3, 2.9, 2.95,
2.99, 2.95, 2.91, 2.9, 2.9, 2.92, 2.99, 3, 2.95, 3.1, 3.25, 2.95,
2.92, 2.92, 2.95, 2.92, 2.9, 2.93, 2.93, 2.91, 3, 2.94, 2.99,
3.05, 2.99, 2.92, 3, 3, 3.2, 3.15, 3.21, 3.24, 3.05, 3.1, 3.1,
3.15, 3.01, 3.01, 3.15, 3.15, 3.25, 3.2, 3.23, 3.22, 3.19, 3.24,
3.24, 3.2, 3.24, 3.4, 3.35, 3.35, 3.35, 3.33, 3.3, 3.3, 3.26,
3.28, 3.27, 3.3, 3.3, 3.3, 3.39, 3.39, 3.5, 3.4, 3.45, 3.45,
3.54, 3.59, 3.55, 3.65, 3.68, 3.65, 3.72, 3.79, 3.84, 3.88, 3.9,
3.95, 3.95, 3.95, 4.05, 4.04, 3.97, 3.95, 3.89, 3.86, 3.8, 3.81,
3.95, 3.9, 3.95, 3.97, 4.05, 4.1, 4.13, 4.3, 4.2, 4.23, 4.29,
4.45, 4.48, 4.49, 4.57, 4.68, 4.7, 4.71, 4.77, 4.67, 4.65, 4.74,
5.16, 4.96, 5.26, 5.16, 5.27, 5.19, 3.2, 3.2, 3.25, 3.4, 3.35,
3.3, 3.19, 3.2, 3.2, 3.24, 3.24, 3.24, 3.24, 3.24, 3.24, 3.24,
3.24, 3.24, 3.19, 3.19, 3.18, 3.25, 3.05, 2.95, 2.95, 2.99, 2.96,
3.1, 3.05, 3, 3, 3.1, 3.15, 3.15, 2.99, 3.04, 3, 3.2, 3.18, 3.18,
3.15, 3.05, 3, 3.03, 2.99, 3, 3.1, 3.05, 2.99, 3, 3, 3, 3, 3.2,
3.2, 3.05, 3.05, 3, 2.9, 2.88, 2.85, 2.86, 2.86, 2.9, 2.9, 3,
3, 3, 3, 2.92, 2.92, 3, 3, 2.92, 3, 3, 3, 3.05, 3, 3, 3.15, 3.29,
3.49, 3.3, 2.9, 2.99, 2.99, 2.95, 2.91, 2.9, 2.95, 2.92, 2.99,
3, 2.95, 3.1, 3.25, 2.95, 2.95, 2.95, 2.99, 2.95, 2.9, 2.93,
2.93, 2.95, 3, 2.99, 2.99, 3.05, 2.99, 2.99, 3, 3.2, 3.24, 3.2,
3.25, 3.24, 3.1, 3.1, 3.23, 3.15, 3.01, 3.3, 3.15, 3.25, 3.28,
3.25, 3.23, 3.22, 3.29, 3.24, 3.24, 3.24, 3.4, 3.4, 3.4, 3.35,
3.35, 3.33, 3.3, 3.3, 3.28, 3.31, 3.31, 3.3, 3.3, 3.3, 3.39,
3.54, 3.5, 3.47, 3.47, 3.54, 3.6, 3.59, 3.69, 3.75, 3.69, 3.75,
3.8, 3.84, 3.89, 3.93, 3.95, 3.95, 3.95, 4.1, 4.15, 4.1, 3.97,
3.95, 3.92, 3.86, 3.85, 3.81, 3.95, 4, 3.97, 3.99, 4.1, 4.19,
4.33, 4.33, 4.31, 4.3, 4.46, 4.49, 4.54, 4.65, 4.72, 4.72, 4.79,
4.79, 4.77, 4.7, 4.71, 5.17, 5.3, 5.39, 5.39, 5.39, 5.3, 5.19,
3, 3, 3.24, 3.25, 3.25, 3.1, 3.19, 3.15, 3.2, 3.24, 3.24, 3.239,
3.24, 3.24, 3.24, 3.24, 3.2, 3.2, 3.19, 3.18, 3.18, 3.05, 2.6,
2.95, 2.85, 2.94, 2.95, 3, 3, 3, 3, 3, 3.05, 2.82, 2.99, 3.04,
2.95, 3.2, 3.18, 3.15, 3.02, 3, 3, 2.99, 2.95, 3, 3.1, 2.99,
2.99, 3, 2.98, 3, 3, 3.1, 2.98, 2.8, 2.85, 2.85, 2.89, 2.76,
2.85, 2.86, 2.85, 2.88, 2.9, 3, 3, 3, 3, 2.9, 2.91, 3, 2.92,
2.92, 3, 3, 3, 3.05, 3, 2.85, 3, 3.15, 3.26, 2.8, 2.9, 2.95,
2.99, 2.95, 2.86, 2.9, 2.89, 2.92, 2.9, 3, 2.85, 3.1, 3.05, 2.9,
2.9, 2.9, 2.95, 2.85, 2.9, 2.93, 2.93, 2.91, 3, 2.92, 2.99, 2.99,
2.99, 2.92, 3, 3, 3.01, 3.15, 3.02, 3.05, 3.05, 3.1, 3.1, 3,
3.01, 3.01, 3.1, 3.15, 3.25, 3.18, 3.16, 3.19, 3.13, 3.24, 3.24,
3.2, 3.24, 3.4, 3.35, 3.35, 3.33, 3.3, 3.3, 3.22, 3.22, 3.28,
3.25, 3.3, 3.3, 3.3, 3.39, 3.35, 3.35, 3.38, 3.41, 3.43, 3.45,
3.45, 3.55, 3.6, 3.65, 3.65, 3.67, 3.78, 3.8, 3.86, 3.9, 3.9,
3.85, 3.93, 4.02, 3.97, 3.93, 3.82, 3.8, 3.8, 3.8, 3.8, 3.95,
3.85, 3.95, 3.97, 4.05, 4.06, 4.13, 4.16, 4.2, 4.22, 4.29, 4.35,
4.43, 4.49, 4.51, 4.65, 4.66, 4.71, 4.68, 4.6, 4.6, 4.74, 5,
4.91, 5.16, 5.14, 5.17, 5.19, 3.2, 3.2, 3.25, 3.4, 3.25, 3.1,
3.19, 3.2, 3.2, 3.24, 3.24, 3.24, 3.24, 3.24, 3.24, 3.24, 3.2,
3.2, 3.19, 3.19, 3.18, 3.05, 2.95, 2.95, 2.85, 2.99, 2.95, 3.1,
3, 3, 3, 3, 3.15, 2.85, 2.99, 3.04, 2.97, 3.2, 3.18, 3.15, 3.02,
3, 3, 2.99, 2.98, 3, 3.1, 2.99, 2.99, 3, 2.98, 3, 3, 3.2, 2.98,
3.05, 3.015, 2.85, 2.9, 2.86, 2.85, 2.86, 2.86, 2.88, 2.9, 3,
3, 3, 3, 2.92, 2.92, 3, 2.92, 2.92, 3, 3, 3, 3.05, 3, 3, 3.15,
3.29, 3.26, 2.8, 2.9, 2.99, 2.99, 2.95, 2.9, 2.9, 2.95, 2.92,
2.9, 3, 2.85, 3.1, 3.05, 2.92, 2.92, 2.95, 2.99, 2.9, 2.9, 2.93,
2.93, 2.95, 3, 2.99, 2.99, 2.99, 2.99, 2.99, 3, 3.2, 3.01, 3.2,
3.24, 3.05, 3.1, 3.1, 3.23, 3, 3.01, 3.2, 3.14, 3.25, 3.28, 3.2,
3.16, 3.19, 3.2, 3.24, 3.24, 3.24, 3.31, 3.4, 3.35, 3.35, 3.33,
3.3, 3.3, 3.22, 3.28, 3.31, 3.25, 3.3, 3.3, 3.3, 3.39, 3.42,
3.47, 3.47, 3.41, 3.54, 3.58, 3.59, 3.6, 3.69, 3.69, 3.74, 3.8,
3.8, 3.85, 3.86, 3.95, 3.95, 3.95, 4.1, 4.05, 3.97, 3.96, 3.89,
3.8, 3.85, 3.81, 3.8, 3.95, 3.91, 3.97, 3.99, 4.1, 4.1, 4.33,
4.2, 4.25, 4.3, 4.45, 4.48, 4.43, 4.57, 4.68, 4.72, 4.7, 4.76,
4.7, 4.6, 4.71, 5.16, 5, 5.29, 5.16, 5.3, 5.19, 5.19, 8042, 7247,
1500, 2200, 1600, 1983, 1360, 400, 1500, 4800, 400, 1900, 450,
500, 1600, 25, 9190, 2685, 100, 2450, 2100, 14824, 12366, 400,
550, 7701, 4000, 2000, 2200, 1000, 400, 192, 1800, 9825, 776,
500, 5050, 2100, 1000, 526, 5262, 13470, 150, 5000, 825, 550,
1000, 1000, 835, 1000, 800, 1500, 2000, 6000, 23716, 2510, 5073,
4667, 5332, 6600, 11000, 1000, 4500, 5400, 500, 1000, 14260,
600, 230, 4500, 1500, 1200, 5372, 500, 6128, 1000, 3500, 500,
782, 1318, 2650, 9856, 15240, 9280, 100, 3500, 1000, 500, 9900,
2000, 1073, 3000, 1650, 3000, 5500, 1000, 7120, 3300, 8200, 3600,
630, 12800, 3800, 1500, 5000, 12000, 1433, 8450, 1500, 1420,
1000, 1088, 510, 3390, 15482, 850, 9900, 3240, 4860, 661, 4405,
21850, 675, 12475, 10030, 4747, 6449, 2771, 2400, 1900, 26130,
75, 1000, 20360, 18704, 1100, 6750, 910, 440, 4745, 1115, 12415,
1892, 1000, 4000, 100, 3200, 4000, 1000, 14715, 18365, 8100,
13375, 21693, 34433, 14629, 32341, 26850, 16369, 14310, 12150,
16731, 21046, 11623, 9380, 8760, 10850, 21129, 20312, 14727,
7393, 4879, 16760, 6538, 15113, 1200, 1500, 23771, 9575, 8150,
7774, 20177, 26701, 18467, 14485, 16184, 16318, 10731, 15647,
33617, 9598, 10061, 31855, 13082, 8919, 9716, 17170, 120717,
99373, 48754, 16061, 40634, 71196, 1550, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 53.125, 53.125, 53.125, 53.125, 53.125, 53.125,
47.5346930503198, 47.5346930503198, 46.0394165349133, 46.0394165349133,
44.318305280762, 28.7784405800004, 22.1424718838612, 22.1424718838612,
17.2358309419688, 38.4516614314786, 35.5580201252693, 50.9409073335208,
43.2868577214891, 43.2868577214891, 43.2868577214891, 43.2868577214891,
57.7874913716085, 36.8498892395159, 46.8375224558739, 49.9770019079572,
45.7722513840718, 58.5146700229585, 57.2156823579705, 55.1742932408986,
47.0855992829417, 45.9344690510984, 45.9344690510985, 45.2515723462439,
44.5162256558408, 46.449998432825, 55.1367545352793, 46.0137841205261,
46.0137841205261, 46.9980428383782, 45.1680669242794, 47.441920921326,
47.441920921326, 65.2386620558215, 46.142307043277, 51.193099386028,
48.6580081770167, 38.6361289882013, 42.6156590089499, 40.2929460812456,
39.692011854992, 40.6750999191946, 40.6750999191946, 42.9705189551315,
45.3212400820865, 55.5094323592483, 55.5094323592483, 55.5094323592483,
55.5094323592483, 45.2327792238372, 45.2327792238372, 55.4216085337574,
45.9278679630622, 45.9278679630622, 55.3669503461937, 55.3669503461936,
55.3669503461936, 61.1772096913823, 53.4465567305418, 53.4465567305418,
68.2882384971981, 76.1676317292872, 71.9132908833502, 36.8483156083071,
43.5024785084454, 48.8880404068233, 48.8880404068233, 46.4581004766477,
43.4580365276188, 43.4580365276188, 47.6329025895786, 45.3982034143251,
43.8735164139756, 52.6990228922181, 41.7559573740936, 57.9325988310753,
54.5648674809673, 46.7190334585342, 46.7190334585342, 48.8158219705131,
51.6355364603515, 45.3849436005241, 45.3849436005241, 47.9765292549334,
47.9765292549334, 49.9319385680271, 54.6652697441737, 53.5404685492423,
53.5404685492423, 53.5404685492423, 53.5404685492423, 53.5404685492423,
55.1048795718158, 74.3203906677712, 51.1923456182049, 63.7325679700046,
65.7886989290019, 50.6372228137231, 53.7516204585925, 53.7516204585925,
61.540372521279, 46.233933197312, 46.8722919081336, 57.5199212894259,
53.7407977117634, 59.2011388495268, 60.6100798191561, 54.9839832912961,
52.2875592667179, 54.1608587608694, 54.8178785295968, 57.5237409247848,
57.5237409247849, 57.5237409247849, 62.8627359282454, 68.5160644996568,
62.6307841423264, 62.6307841423264, 60.0822193137844, 56.2662526029152,
56.2662526029152, 46.5358654620614, 53.2702015941206, 56.3263815242224,
49.17892489837, 54.5222862433549, 54.5222862433549, 54.5222862433549,
63.8953027289306, 66.4558554034754, 70.3499117941237, 70.3499117941237,
60.026469332036, 70.4615938291669, 72.8818396542941, 73.4853107348059,
74.125093473292, 79.1548876794736, 79.1548876794736, 81.6071301038229,
84.1009697961463, 84.1009697961463, 86.0472783823051, 86.4167923755497,
89.2608680025719, 89.2608680025719, 89.2608680025719, 92.7374320331654,
82.8087109204784, 69.567643114619, 68.0562327567284, 58.2185653891826,
48.2539781801277, 53.1986881679786, 49.033768692775, 47.9901183825881,
61.6089181051025, 57.1731265162146, 61.7616734481797, 63.221137287655,
70.1771943992812, 70.1771943992812, 79.9607457795924, 66.3010390829917,
68.5938260901332, 70.8011706594854, 76.343417368729, 77.3008696102273,
71.911133672881, 76.9180575079281, 80.0263157494416, 81.0569834689864,
78.7980499036461, 80.600192632844, 73.6449272697599, 63.4978113677881,
68.7589068199776, 81.1248114717251, 70.1544736530449, 76.542694603108,
69.1685415119339, 72.3553875493797, 66.3666023398198, 66.3666023398197
), .Dim = c(204L, 6L), .Dimnames = list(NULL, c("Open", "High",
"Low", "Close", "Volume", "RSI_10")), index = structure(c(1391794931,
1391796453, 1391798428, 1391799253, 1391800289, 1391801398, 1391801991,
1391802911, 1391804252, 1391805482, 1391806437, 1391807621, 1391809180,
1391810164, 1391811085, 1391811953, 1391812974, 1391813874, 1391814356,
1391815389, 1391816676, 1392054295, 1392055042, 1392055641, 1392056480,
1392057860, 1392058660, 1392061125, 1392061818, 1392062871, 1392064406,
1392069454, 1392071161, 1392072828, 1392073926, 1392074435, 1392075588,
1392139818, 1392141787, 1392143229, 1392144209, 1392144981, 1392145281,
1392146999, 1392147895, 1392150775, 1392153349, 1392157752, 1392157949,
1392161101, 1392162668, 1392223583, 1392225001, 1392226078, 1392227071,
1392227853, 1392228856, 1392229625, 1392232022, 1392232952, 1392234128,
1392235169, 1392235962, 1392237413, 1392238825, 1392247565, 1392248355,
1392248933, 1392312896, 1392314399, 1392314919, 1392318896, 1392319091,
1392324415, 1392328718, 1392328923, 1392330638, 1392332327, 1392333020,
1392334866, 1392399762, 1392400798, 1392401577, 1392402571, 1392403101,
1392405234, 1392405314, 1392407097, 1392407384, 1392412536, 1392417358,
1392418366, 1392419602, 1392421312, 1392421904, 1392744561, 1392744882,
1392746369, 1392747095, 1392748082, 1392748953, 1392749704, 1392750668,
1392751491, 1392752100, 1392753520, 1392754037, 1392755275, 1392756297,
1392756874, 1392757566, 1392758527, 1392759503, 1392760777, 1392761557,
1392762587, 1392763449, 1392764255, 1392765258, 1392765722, 1392766867,
1392767946, 1392768196, 1392831756, 1392832587, 1392833666, 1392834331,
1392835307, 1392836371, 1392836795, 1392838185, 1392838874, 1392839984,
1392840851, 1392841781, 1392842368, 1392843599, 1392844415, 1392845281,
1392845989, 1392847091, 1392848774, 1392849795, 1392850553, 1392851505,
1392852595, 1392853479, 1392854131, 1392917223, 1392918230, 1392919097,
1392920010, 1392920884, 1392921703, 1392922797, 1392923647, 1392924593,
1392925480, 1392926372, 1392927191, 1392928198, 1392929083, 1392929988,
1392930880, 1392931799, 1392932644, 1392933511, 1392934478, 1392935364,
1392936276, 1392937197, 1392938098, 1392938952, 1392939870, 1392940796,
1392940828, 1393003427, 1393004649, 1393005578, 1393006382, 1393007310,
1393008220, 1393009197, 1393010056, 1393010989, 1393011852, 1393012799,
1393013653, 1393014504, 1393015449, 1393016265, 1393017288, 1393018147,
1393018902, 1393019985, 1393020847, 1393021573, 1393022698, 1393023579,
1393024498, 1393025388, 1393026292, 1393027175, 1393027473), tzone = "", tclass = c("POSIXct",
"POSIXt")), .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct",
"POSIXt"), .indexTZ = "", tzone = "", class = c("xts", "zoo"))
Upvotes: 1
Views: 458
Reputation: 77096
First, we generate a spline for the entire data set. Second, we use this function to interpolate the midpoints, all at once (thus vectorised). Using those, and the initial function values, we form Simpson's rule for each interval, yielding the exact integral of the spline. Finally, we use a fast roll function to add these partial sums for all combined intervals.
spl <- splinefun(x1, y1) # cubic spline
n <- length(x1)
xmid <- (x1[-1] + x1[-n]) / 2 # midpoints
# Simpson's rule
sums <- diff(x1)/6 * (y1[-1] + 4*spl(xmid) + y1[-n])
library(RcppRoll)
areas <- roll_sum(sums, 4)
# check accuracy
matplot(cbind(areas,AUC_RSI10), t="l")
# comparing with piecewise linear integration of @BondedDust
points(rollapply(y1, FUN=sum, width=4, by=1), pch="+", cex=0.5)
Timing results:
microbenchmark(
+ loop = {
+ AUC_RSI10 <- vector("numeric", length(y1)-4)
+ for(i in 1:(length(y1)-4))
+ {
+ AUC_RSI10[i]<-auc(x1,y1,from=i,to=i+4,type="spline")
+
+ }
+ }, simpson = {
+ spl <- splinefun(x1, y1)
+ n <- length(x1)
+ xmid <- (x1[-1] + x1[-n]) / 2
+ sums <- diff(x1)/6 * (y1[-1] + 4*spl(xmid) + y1[-n] )
+ areas <- roll_sum(sums, 4)
+ }
+ )
Unit: microseconds
expr min lq median uq max neval
loop 56828.857 59390.3055 68435.338 75537.2380 217565.279 100
simpson 283.399 307.3585 325.642 340.8675 603.664 100
Upvotes: 5
Reputation: 263301
I you look at the positioning of the NA values, you find they are all at the beginning, so simply summing them up should give the AUC at least with the assumption that the gap between x values is just 1.
> rle(is.na(coredata(myxts5)[, "RSI_10"]))
Run Length Encoding
lengths: int [1:2] 10 194
values : logi [1:2] TRUE FALSE
> sum(coredata(myxts5)[, "RSI_10"], na.rm=TRUE)
[1] 10998.76
The na.interp
function in pkg:zoo (which I think is attached when 'xts' is loaded) could be used if the NAs were spread out more. To get it in groups of 5 use rollapply
from 'zoo':
> rollapply(coredata(myxts5)[, "RSI_10"], FUN=sum, width=5,by=5, na.rm=TRUE)
[1] 0.0000 0.0000 265.6250 240.2732 134.6175 211.5243 228.0486 266.6539 228.7223 240.6124
[11] 251.4329 221.3958 209.3340 267.2705 247.8771 278.8042 296.7200 231.1503 231.3596 254.7514
[21] 238.3585 265.2186 287.6986 287.6617 265.9073 281.2436 290.2518 310.1261 261.5776 293.9180
[31] 344.0697 387.5273 429.9269 423.6355 276.7612 291.7550 355.2100 373.2746 394.1265 360.0787
Upvotes: 1