Here is my data:
test <- structure(list(date = structure(c(16436, 16437, 16438, 16439,
16440, 16441, 16442, 16443, 16444, 16445, 16446, 16447, 16448,
16449, 16450, 16451, 16452, 16453, 16454, 16455, 16456, 16457,
16458, 16459, 16460, 16461, 16462, 16463, 16464, 16465, 16466,
16467, 16468, 16469, 16470, 16471, 16472, 16473, 16474, 16475,
16476, 16477, 16478, 16479, 16480, 16481, 16482, 16483, 16484,
16485, 16486, 16487, 16488, 16489, 16490, 16491, 16492, 16493,
16494, 16495, 16496, 16497, 16498, 16499, 16500, 16501, 16502,
16503, 16504, 16505, 16506, 16507, 16508, 16509, 16510, 16511,
16512, 16513, 16514, 16515, 16516, 16517, 16518, 16519, 16520,
16521, 16522, 16523, 16524, 16525, 16526, 16527, 16528, 16529,
16530, 16531, 16532, 16533, 16534, 16535, 16536, 16537, 16538,
16539, 16540, 16541, 16542, 16543, 16544, 16545, 16546, 16547,
16548, 16549, 16550, 16551, 16552, 16553, 16554, 16555, 16556,
16557, 16558, 16559, 16560, 16561, 16562, 16563, 16564, 16565,
16566, 16567, 16568, 16569, 16570, 16571, 16572, 16573, 16574,
16575, 16576, 16577, 16578, 16579, 16580, 16581, 16582, 16583,
16584, 16585, 16586, 16587, 16588, 16589, 16590, 16591, 16592,
16593, 16594, 16595, 16596, 16597, 16598, 16599, 16600, 16601,
16602, 16603, 16604, 16605, 16606, 16607, 16608, 16609, 16610,
16611, 16612, 16613, 16614, 16615, 16616, 16617, 16618, 16619,
16620, 16621, 16622, 16623, 16624, 16625, 16626, 16627, 16628,
16629, 16630, 16631, 16632, 16633, 16634, 16635, 16636, 16637,
16638, 16639, 16640, 16641, 16642, 16643, 16644, 16645, 16646,
16647, 16648, 16649, 16650, 16651, 16652, 16653, 16654, 16655,
16656, 16657, 16658, 16659, 16660, 16661, 16662, 16663, 16664,
16665, 16666, 16667, 16668, 16669, 16670, 16671, 16672, 16673,
16674, 16675, 16676, 16677, 16678, 16679, 16680, 16681, 16682,
16683, 16684, 16685, 16686, 16687, 16688, 16689, 16690, 16691,
16692, 16693, 16694, 16695, 16696, 16697, 16698, 16699, 16700,
16701, 16702, 16703, 16704, 16705, 16706, 16707, 16708, 16709,
16710, 16711, 16712, 16713, 16714, 16715, 16716, 16717, 16718,
16719, 16720, 16721, 16722, 16723, 16724, 16725, 16726, 16727,
16728, 16729, 16730, 16731, 16732, 16733, 16734, 16735, 16736,
16737, 16738, 16739, 16740, 16741, 16742, 16743, 16744, 16745,
16746, 16747, 16748, 16749, 16750, 16751, 16752, 16753, 16754,
16755, 16756, 16757, 16758, 16759, 16760, 16761, 16762, 16763,
16764, 16765, 16766, 16767, 16768, 16769, 16770, 16771, 16772,
16773, 16774, 16775, 16776, 16777, 16778, 16779, 16780, 16781,
16782, 16783, 16784, 16785, 16786, 16787, 16788, 16789, 16790,
16791, 16792, 16793, 16794, 16795, 16796, 16797, 16798, 16799,
16800), class = "Date"), radn = c(9.66, 9.54, 8.21, 5, 5.98,
9.39, 8.54, 9.68, 6.74, 2.95, 9.24, 7.39, 10.47, 9.04, 7.1, 4.12,
6.42, 6.89, 10.96, 9.49, 11.72, 8.83, 11.48, 11.42, 11.49, 10.98,
2.87, 11.92, 8.92, 4, 12.92, 8.37, 5.73, 4.47, 8.73, 5.76, 9.34,
10.41, 6.72, 8.44, 13.34, 11.95, 12.2, 10.94, 10.5, 15.72, 14.63,
15.67, 15.91, 14.79, 14.11, 15.89, 17.07, 17.62, 17.22, 14.93,
11.17, 4.83, 8.78, 17.46, 10.35, 19.09, 19.39, 19.48, 19.12,
18.94, 19.93, 20.24, 17.47, 6.07, 19.4, 18.26, 10, 6.33, 10.67,
15.2, 21.39, 22.43, 18.02, 19.4, 18.55, 14.91, 9.15, 21.84, 22.8,
23.16, 23.43, 24.16, 22.56, 23.58, 23.45, 25.09, 25.46, 22.85,
17.05, 23.87, 12.45, 8.88, 25.7, 25.86, 17.28, 24.77, 25.08,
15.62, 27.4, 27.35, 27.71, 26.91, 27.93, 27.99, 26.42, 20.49,
27.9, 11.89, 10.38, 28.43, 28.74, 29.2, 27.62, 28.88, 28.81,
28.92, 29.07, 24.41, 29.1, 26.43, 18, 23.94, 30.68, 29.47, 18.88,
18.58, 25.79, 18.76, 12.18, 12.92, 20.18, 10.75, 14.09, 19.86,
19.47, 15.9, 12.82, 22.62, 21.23, 24.62, 29.5, 30.21, 30.12,
21.87, 25.45, 31.68, 32.18, 29.67, 17.27, 22.41, 24.28, 31.27,
30, 30.12, 21.6, 32.76, 32.27, 32.24, 32.81, 32.45, 32.66, 30.52,
30.5, 32.68, 32.85, 30.42, 32.62, 32.45, 31.29, 32.15, 25.84,
26.21, 27.22, 26.36, 30.72, 26.26, 24.34, 21.45, 18.58, 25.95,
29.09, 21.53, 21.88, 20.76, 17.56, 24.69, 22.83, 27.72, 28.07,
31.18, 30.23, 28.86, 30.61, 30.79, 30.08, 27.28, 16.81, 23.82,
30.09, 30.29, 30.45, 30.8, 31.12, 30.89, 30.19, 25.01, 24.27,
18.93, 28.27, 26.62, 27.97, 22.9, 11.1, 22.29, 24.4, 27.78, 28.17,
28.41, 26.01, 27.18, 25.08, 26.65, 27.95, 27.67, 24.39, 26.59,
26.9, 26.54, 26.02, 25.31, 26.03, 22.22, 24.29, 21.01, 19.73,
23.03, 25.38, 24.98, 24.74, 19.75, 20.24, 24.99, 21.01, 24.53,
24.3, 23.95, 23.36, 22.92, 20.66, 15.42, 6.66, 15.28, 16.1, 16.73,
22.14, 22.02, 21.59, 21.4, 21.41, 21.45, 15.48, 17.78, 19.93,
15.58, 19.22, 17.29, 8.64, 8.94, 15.46, 12.52, 17.79, 18.36,
18.28, 15.27, 13.04, 13.78, 17.88, 17.88, 17.5, 17.31, 16.84,
14.55, 15.17, 7.43, 4.34, 5.23, 12.79, 15.84, 13.32, 15.43, 11.48,
6.13, 14.64, 9.04, 5.09, 11.84, 9.86, 11.4, 4.92, 2.81, 5.76,
7.92, 9.15, 13.14, 13.14, 9.94, 9.77, 11.15, 12.45, 12.33, 11.99,
11.8, 6.92, 11.23, 6.2, 9.6, 4.89, 11.43, 11.05, 10.83, 7.44,
5.4, 6.17, 3.52, 10.71, 10.64, 10.67, 10.6, 10.17, 6.02, 6.96,
6.5, 7.43, 3.49, 2.03, 5.22, 5.02, 4.24, 4.44, 5.52, 2.72, 3.75,
2.31, 8.38, 1.88, 3.07, 2.02, 2.66, 1.67, 5.77, 7.59, 1.9, 1.5,
9.72, 2.66, 2.39, 1.67, 2.38, 9.88), maxt = c(-4.4, -1.9, 0.8,
4.8, 6.8, 11, 13, 12.6, 11.4, 7, 5.8, 10, 7.2, 6.5, 5.9, 5.5,
10.4, 12, 15.6, 11.2, 7.1, 6.3, 6.5, 9.4, 12.8, 14.6, 14.3, 7.8,
11.9, 9.6, 4.5, 10.8, 13.2, 11.4, 14, 14.8, 14.9, 16.3, 17.2,
15.4, 13.3, 12.4, 15.1, 17.6, 19.6, 19.8, 15.1, 12.8, 15.9, 18.7,
18, 13.1, 10.6, 6, 7.6, 12.7, 14, 9.2, 8.3, 7.1, 9.5, 10, 6,
10.1, 15.5, 18.4, 19.9, 19.6, 19.9, 21.5, 13.9, 17, 20.5, 20.6,
22.7, 18.4, 18.5, 16, 19.9, 22.2, 19.1, 19.3, 12.6, 11.7, 17.1,
22.2, 26.5, 19.7, 22.9, 26.3, 20.7, 12.2, 12.4, 16.3, 17.4, 12.7,
12.7, 13, 11.4, 16.4, 20.6, 16.6, 18.4, 24.4, 11.7, 11.8, 18.6,
23, 21.9, 23.3, 24.6, 26, 22.5, 21.6, 13.2, 11.9, 14.8, 21.2,
25.8, 25.5, 22.6, 26.7, 27.6, 26.9, 27.2, 24.2, 18.6, 14.1, 20.5,
21.6, 24.2, 22.6, 20.9, 19.6, 16.9, 14.8, 17.1, 20.6, 18.3, 16.9,
20.2, 21.2, 19.6, 19.2, 22.6, 24, 23.9, 25.6, 27.1, 29.3, 30.2,
31.6, 26.4, 24.7, 25.2, 21, 25.9, 26.4, 30.7, 33.4, 34.7, 29,
30.5, 32.3, 31.9, 32.6, 32.6, 32.7, 33.6, 34, 31.6, 32.4, 31.4,
31.5, 33.7, 35.9, 37.1, 38.8, 39.2, 38.9, 37.8, 38.4, 38.3, 38.6,
37.2, 35.7, 27.9, 33.4, 32.7, 27.5, 29.2, 26.3, 26.9, 28, 29.1,
31.1, 32, 33.1, 29.4, 29.2, 32.3, 34, 33, 29, 29.3, 30.8, 31.5,
30.4, 24.9, 28.5, 33.6, 36.3, 37.7, 38.2, 34.5, 33.2, 33.9, 29.2,
32.3, 25.4, 28.8, 32.4, 32.9, 34.9, 34.6, 36.2, 34.5, 32, 34.1,
33.7, 33.3, 34.8, 34.5, 32.7, 32.3, 35.7, 35.3, 35, 34.2, 33.5,
33.9, 31.4, 27.6, 30.9, 32.2, 30.5, 25.9, 23.5, 19.6, 24.1, 28.1,
30.8, 33.2, 34.8, 35.8, 35.4, 33.5, 27.7, 21.7, 19.4, 20.1, 23.7,
28.5, 31.5, 31.6, 31, 29.3, 31.2, 32.6, 30.5, 28.6, 29.8, 30.9,
26.8, 21.1, 21.8, 20.4, 22.5, 24.9, 26.7, 27.1, 28, 30.7, 29.6,
25.5, 29.3, 30.4, 30.8, 30.5, 29, 22, 18, 13.1, 16, 19, 19.1,
19.3, 20.1, 20, 20.4, 18.6, 15.2, 13.7, 17.1, 22.3, 18.1, 6.3,
6, 5.7, 7.1, 10.3, 11.1, 14.2, 8, 7.1, 8.9, 10.7, 12.3, 14.8,
10.8, 3.2, 7.6, 12.6, 14.4, 9.6, 10.6, 11.7, 12.3, 13.4, 1.3,
-0.9, -0.2, 0.6, 2.5, 4, 5.4, 7.3, 13, 8, 6.7, 11.5, 13.2, 14.2,
14.9, 12.3, 5.5, 6.1, 11.1, 0.3, 0.5, 2, 2.8, 7, 4.9, 2.4, 7.3,
6.2, 2.9, 0.5, -1.2, -2.5, -4, -2.7, -1.1, -3), mint = c(-15.9,
-16.5, -14.4, -11.2, -5.7, -2.4, -2.5, -3.2, -4.3, -4.6, -1.5,
-1, -0.9, -6.3, -7, -5.7, -1.2, -0.9, 0.3, -2.7, -5.9, -10.1,
-8.7, -7.3, -5.7, -3.5, -1.2, -0.4, -0.9, -0.7, -4.3, -4.3, -2.8,
1, 2.7, 3.1, 5.8, 6.2, 3.8, 2.2, -0.7, -1.5, -0.9, -0.3, 1, 1,
-1.6, -3.8, -3.9, -1.9, -0.6, -0.8, -3.8, -7, -8.8, -7, -2.2,
-0.3, -1.1, -2.9, -5.1, -5.2, -9.2, -9.7, -6.9, -4.2, -3.1, -3.5,
-3.8, -2.3, 3.5, 0.3, 0.7, 5.8, 7, 7.4, 2.3, -0.6, -2.2, 0.7,
0.9, 1.6, 3.8, -0.9, -2.5, 1, 2.6, 1.8, -1.6, 2.3, -4.2, -6.6,
-4.7, -4.2, -0.5, -1.4, -3, 0.3, -2.9, -2.3, 1.1, -0.4, -1.5,
0.5, -6.1, -7.3, -5, -0.5, 0.6, 0.7, 1.2, 2.9, 4.3, 4.7, 2.1,
0.3, 0.5, 1.4, 3.4, 5, 4.9, 4.2, 6.3, 6.7, 6, 6.3, 3.6, 3.5,
3.7, 1.1, 1.9, 4.9, 0.7, 1.2, 5.8, 5.6, 4, 6.2, 8.3, 7, 6, 4.7,
7, 9.2, 8.1, 6.9, 7.9, 8.6, 9.6, 9.4, 10.3, 10.4, 9.6, 8.2, 9.4,
9.8, 7.2, 9.4, 10.8, 12.4, 14.5, 11.8, 11, 10.7, 11.3, 10.8,
9.7, 10.4, 10.6, 12.1, 10.3, 10.5, 11.3, 10, 12.6, 13.6, 17.4,
19.9, 19.9, 18.9, 18.4, 18.9, 20.1, 19, 17, 16.9, 14.8, 13.1,
14, 11.5, 10.6, 11.1, 12.7, 11.4, 11.9, 12.5, 13.3, 13.6, 13.2,
11.8, 11.8, 12.6, 15, 11.4, 10, 9.6, 9.3, 9.3, 8.2, 9.6, 9.7,
12, 14.3, 16.1, 16.5, 12.8, 13.7, 11.3, 10.3, 12.2, 11.4, 11.8,
11.1, 10.9, 11.2, 13, 11.8, 9, 9.7, 8.9, 10.1, 10, 11.5, 10.6,
12.2, 10.9, 12.6, 11.9, 11.9, 13.1, 13.4, 11.4, 6.9, 6, 7.7,
9.7, 7.8, 2.2, 1.5, 0.9, 2.3, 4.8, 6.3, 8.3, 10.4, 11.2, 12.8,
11, 7.5, 6.1, 5.5, 2.4, 3.5, 5.8, 5.9, 6.2, 5.6, 6.1, 7.4, 9.9,
7.8, 6.4, 7.8, 11, 10.1, 4.8, 3.5, 6.6, 4.6, 5.5, 5.9, 9.8, 8.3,
8.6, 6.4, 4.4, 6, 7.1, 6.9, 7.5, 7.8, 6.9, 3.9, 1.8, 0.3, 0.3,
-0.5, 3.2, 2.4, -0.3, 0.2, 5.1, -1.5, -1.4, 4.7, 5.6, 1.6, -1.3,
-3.8, -4.1, -4.6, -3.5, -0.8, -1.4, -6.5, -6, -5, -4.9, -3.9,
-4.2, -6.1, -1.7, -0.2, -0.3, -3.6, -7.1, -6.4, -3.4, -5.2, -8.6,
-9.6, -13.8, -16.3, -15.6, -14.5, -11.8, -4.6, 0, -7.6, -7.7,
-1.3, 4.8, 4.6, 2.3, 0.1, -2.2, -1.4, -2.6, -4.7, -9, -6.8, -4.4,
-3.7, -3.9, -5.1, 0, -1.8, -3.2, -9, -14.2, -17.4, -13, -8.2,
-12.7, -17.5), rain = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.8,
0.96, 0, 0, 0, 1.38, 0.25, 0.32, 0, 0, 0, 0, 0, 0, 0, 0, 5.68,
0, 0, 0, 0, 0, 1.12, 0, 0, 0, 4.24, 0.13, 6.84, 1.44, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.28, 2.13, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.65,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.65, 0, 3.6, 1.9, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.59, 1.19, 11.03, 5.43, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.77, 0, 0, 0, 0, 0, 5.06, 5.6,
0.01, 2.23, 5.45, 7.43, 4.47, 0.11, 4.02, 6.36, 0.38, 0.79, 1.46,
0, 0, 0, 0, 0, 0, 0, 0, 0.82, 3.06, 0.06, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.37, 0,
2.3, 1.74, 3.2, 1.72, 3.53, 2, 1.08, 0.46, 0.38, 0.3, 0, 0, 0,
0.47, 0, 0, 0.56, 4.86, 9.66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.86,
0, 0, 0, 0, 2.44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0.55, 0.83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16.08,
0.93, 0.01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.24, 4.25, 14.52,
13.45, 0, 0, 0, 0, 0, 0, 0, 0, 1.2, 1.23, 0, 0, 4.15, 11.05,
2.29, 0, 0, 0, 0, 0.77, 3.04, 0, 0, 0, 0, 0, 0.88, 0, 0, 0, 0,
0, 0, 0, 0, 0.94, 0, 0, 0, 0, 0, 0, 0.02, 0, 0, 0, 0, 0, 0, 0,
0.66, 1.85, 0.95, 0.61, 3.89, 0, 0, 1.23, 4.81, 0, 1.96, 1.67,
6.94, 9.65, 0, 1.99, 0, 0, 2.24, 2.67, 0.16, 0.52), evap = c(8.48,
8.48, 8.48, 8.48, 8.48, 8.48, 8.48, 8.31, 8.31, 8.31, 8.31, 8.31,
8.31, 8.31, 8.09, 8.09, 8.09, 8.09, 8.09, 8.09, 8.09, 7.86, 7.86,
7.86, 7.86, 7.86, 7.86, 7.86, 7.62, 7.62, 7.62, 7.62, 7.62, 7.62,
7.62, 7.39, 7.39, 7.39, 7.39, 7.39, 7.39, 7.39, 7.16, 7.16, 7.16,
7.16, 7.16, 7.16, 7.16, 6.93, 6.93, 6.93, 6.93, 6.93, 6.93, 6.93,
6.71, 6.71, 6.71, 6.71, 6.71, 6.71, 6.71, 6.48, 6.48, 6.48, 6.48,
6.48, 6.48, 6.48, 6.23, 6.23, 6.23, 6.23, 6.23, 6.23, 6.23, 5.96,
5.96, 5.96, 5.96, 5.96, 5.96, 5.96, 5.66, 5.66, 5.66, 5.66, 5.66,
5.66, 5.66, 5.32, 5.32, 5.32, 5.32, 5.32, 5.32, 5.32, 4.95, 4.95,
4.95, 4.95, 4.95, 4.95, 4.95, 4.56, 4.56, 4.56, 4.56, 4.56, 4.56,
4.56, 4.15, 4.15, 4.15, 4.15, 4.15, 4.15, 4.15, 3.75, 3.75, 3.75,
3.75, 3.75, 3.75, 3.75, 3.38, 3.38, 3.38, 3.38, 3.38, 3.38, 3.38,
3.05, 3.05, 3.05, 3.05, 3.05, 3.05, 3.05, 2.78, 2.78, 2.78, 2.78,
2.78, 2.78, 2.78, 2.58, 2.58, 2.58, 2.58, 2.58, 2.58, 2.58, 2.45,
2.45, 2.45, 2.45, 2.45, 2.45, 2.45, 2.37, 2.37, 2.37, 2.37, 2.37,
2.37, 2.37, 2.35, 2.35, 2.35, 2.35, 2.35, 2.35, 2.35, 2.38, 2.38,
2.38, 2.38, 2.38, 2.38, 2.38, 2.46, 2.46, 2.46, 2.46, 2.46, 2.46,
2.46, 2.57, 2.57, 2.57, 2.57, 2.57, 2.57, 2.57, 2.72, 2.72, 2.72,
2.72, 2.72, 2.72, 2.72, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 3.1,
3.1, 3.1, 3.1, 3.1, 3.1, 3.1, 3.33, 3.33, 3.33, 3.33, 3.33, 3.33,
3.33, 3.57, 3.57, 3.57, 3.57, 3.57, 3.57, 3.57, 3.83, 3.83, 3.83,
3.83, 3.83, 3.83, 3.83, 4.13, 4.13, 4.13, 4.13, 4.13, 4.13, 4.13,
4.47, 4.47, 4.47, 4.47, 4.47, 4.47, 4.47, 4.85, 4.85, 4.85, 4.85,
4.85, 4.85, 4.85, 5.26, 5.26, 5.26, 5.26, 5.26, 5.26, 5.26, 5.67,
5.67, 5.67, 5.67, 5.67, 5.67, 5.67, 6.08, 6.08, 6.08, 6.08, 6.08,
6.08, 6.08, 6.46, 6.46, 6.46, 6.46, 6.46, 6.46, 6.46, 6.79, 6.79,
6.79, 6.79, 6.79, 6.79, 6.79, 7.09, 7.09, 7.09, 7.09, 7.09, 7.09,
7.09, 7.35, 7.35, 7.35, 7.35, 7.35, 7.35, 7.35, 7.6, 7.6, 7.6,
7.6, 7.6, 7.6, 7.6, 7.84, 7.84, 7.84, 7.84, 7.84, 7.84, 7.84,
8.07, 8.07, 8.07, 8.07, 8.07, 8.07, 8.07, 8.28, 8.28, 8.28, 8.28,
8.28, 8.28, 8.28, 8.46, 8.46, 8.46, 8.46, 8.46, 8.46, 8.46, 8.58,
8.58, 8.58, 8.58, 8.58, 8.58, 8.58, 8.63, 8.63, 8.63, 8.63, 8.63,
8.63, 8.63, 8.6, 8.6, 8.6, 8.6, 8.6, 8.6, 8.6, 8.6), index = 8767:9131), .Names = c("date",
"radn", "maxt", "mint", "rain", "evap", "index"), na.action = structure(1L, .Names = "1", class = "omit"), row.names = 8768:9132, class = "data.frame")
I am trying to optimize a function to it to simulate some data. I have done this in the past with other datasets with success, but with this data optim is converging but visually the fit is terrible. I do a much better job using guess and check. Here I am looking at minimum temperature. I have many years of data, but in the interest of space I only included 1 year.
Here is my optimization code:
TMIN <- function(a,b,x){a*sin(b*x)}
plot(h$mint~h$index,type='l')
curve(TMIN(x, a=20, b=.017),add=TRUE, col="red")
normTMIN<-function(params,k){
a=params[1]
b=params[2]
c=params[3]
Mean<-mean(a*sin(b*k))
-sum(dnorm(k,mean=Mean,sd=c,log=TRUE)) #shape= Mean(a,b)/scale
}
optTMIN <- optim(par=c(a=60,b=.017,c=1),k=test$mint,fn=normTMIN) #par doesn't equal params
optTMIN
curve(TMIN(optTMIN$par[1],optTMIN$par[2],x), add=TRUE,col="blue")
I can't figure out why optim is going so terribly wrong. Thanks in advance.
Do you want to do something like the following (find list square estimate):
head(test)
TMIN <- function(a,b,x){a*sin(b*x)}
plot(test$mint~test$index,type='l')
curve(TMIN(x, a=20, b=.017),add=TRUE, col="red")
normTMIN<-function(params,k,x){
a=params[1]
b=params[2]
sum((k - TMIN(a,b,x))^2)
}
optTMIN <- optim(par=c(a=1,b=0.001),k=test$mint,x=test$index,fn=normTMIN, control=list(trace = TRUE)) #par doesn't equal params
optTMIN
curve(TMIN(optTMIN$par[1],optTMIN$par[2],x), add=TRUE,col="blue")
#$par
# a b
#10.97271664 0.01349994
Related
I have a .csv of a gridded dataset. Each grid (represented by lat/lon coordinates) has a annual timeseries from 1950-2100 and accompanyning values.
It's formated like this:
time
lon
lat
value1
value2
value3
1950-01-01
-80.79
42.96
1
2
3
1951-01-01
-80.79
42.96
2
4
6
1952-01-01
-80.79
42.96
3
6
9
1953-01-01
-80.79
42.96
4
8
12
1954-01-01
-80.79
42.96
5
10
15
1955-01-01
-80.79
42.96
1
2
3
1956-01-01
-80.79
42.96
2
4
6
1957-01-01
-80.79
42.96
3
6
9
1958-01-01
-80.79
42.96
4
8
12
1959-01-01
-80.79
42.96
5
10
15
1960-01-01
-80.79
42.96
1
2
3
.....
.....
.....
.....
.....
.....
1950-01-01
-80.63
42.96
1
2
3
1951-01-01
-80.79
42.96
2
4
6
.....
.....
.....
.....
.....
.....
1950-01-01
-79.88
44.29
1
2
3
1951-01-01
-79.88
44.26
2
4
6
You can get an real example of the .csv dataset at https://climatedata.ca/download/ (pick frequency: annual; variable: anyone; location: pick 2+ grids on the map)
I"m trying to figure out how to take 30 year averages for each grid.
For example, for for the first grid (-80.79, 42.96) I want to take the averages from 1950-1969, 1970-2005, 2006-2039, 2049-2069, and 2070-2100. Then do the same thing for the second grid. Etc.
Or,
Take the average from 1950-1969 for each grid, then take the average for 1970-2005 for each grid. Etc.
Basically I want to be able to repeat the same calcualtions for one grid onto all of them.
Here's a way using dplyr. group_by for each period (here rounding down to the nearest 30 years) and the grid cell, and then we can use summarize(across(... to calculate the mean for those three values within each group.
library(dplyr)
df1 %>%
group_by(yr30 = floor(lubridate::year(time)/30)*30, lon, lat) %>%
summarize(across(value1:value3, mean), .groups = "drop")
# A tibble: 4 × 6
yr30 lon lat value1 value2 value3
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1950 -80.8 43.0 2.75 5.5 8.25
2 1950 -80.6 43.0 1 2 3
3 1950 -79.9 44.3 2 4 6
4 1950 -79.9 44.3 1 2 3
sample data used:
df1 <- data.frame(
time = c("1950-01-01","1951-01-01","1952-01-01",
"1953-01-01","1954-01-01","1955-01-01","1956-01-01",
"1957-01-01","1958-01-01","1959-01-01","1960-01-01","1950-01-01",
"1951-01-01","1950-01-01","1951-01-01"),
lon = c(-80.79,-80.79,-80.79,-80.79,-80.79,
-80.79,-80.79,-80.79,-80.79,-80.79,-80.79,-80.63,-80.79,
-79.88,-79.88),
lat = c(42.96,42.96,42.96,42.96,42.96,42.96,
42.96,42.96,42.96,42.96,42.96,42.96,42.96,44.29,44.26),
value1 = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 1L, 2L, 1L, 2L),
value2 = c(2L, 4L, 6L, 8L, 10L, 2L, 4L, 6L, 8L, 10L, 2L, 2L, 4L, 2L, 4L),
value3 = c(3L, 6L, 9L, 12L, 15L, 3L, 6L, 9L, 12L, 15L, 3L, 3L, 6L, 3L, 6L)
)
df1$time = as.Date(df1$time)
Here is an option, where we can use case_when to create the groups, then summarise:
library(tidyverse)
df %>%
mutate(time = format(as.Date(time), "%Y")) %>%
group_by(lon, lat,
grp = case_when(time >= 1950 & time <= 1969 ~ "1950-1969",
time >= 1970 & time <= 2005 ~ "1970-2005",
time >= 2006 & time <= 2039 ~ "2006-2039",
time >= 2040 & time <= 2069 ~ "2040-2069",
time >= 2070 & time <= 2100 ~ "2070-2100",
TRUE ~ NA_character_)) %>%
summarise(across(-time, ~ mean(.x, na.rm = T)))
Output
lon lat grp rcp26_tg_mean_p10 rcp26_tg_mean_p… rcp26_tg_mean_p… rcp45_tg_mean_p… rcp45_tg_mean_p… rcp45_tg_mean_p… rcp85_tg_mean_p… rcp85_tg_mean_p… rcp85_tg_mean_p…
<dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 -112.88 49.7 1950-1969 3.98 5.43 6.80 3.98 5.43 6.80 3.98 5.42 6.79
2 -112.88 49.7 1970-2005 4.38 5.77 7.11 4.38 5.77 7.12 4.39 5.76 7.11
3 -112.88 49.7 2006-2039 5.62 7.10 8.58 5.64 7.09 8.53 5.67 7.28 8.67
4 -112.88 49.7 2040-2069 6.05 7.69 9.36 6.79 8.31 9.98 7.48 9.08 10.8
5 -112.88 49.7 2070-2100 6.28 7.73 9.38 7.36 9.06 10.8 9.36 11.3 13.1
6 -112.79 49.7 1950-1969 3.88 5.34 6.71 3.88 5.34 6.71 3.88 5.33 6.71
7 -112.79 49.7 1970-2005 4.28 5.67 7.03 4.28 5.67 7.03 4.28 5.66 7.02
8 -112.79 49.7 2006-2039 5.52 7.01 8.49 5.54 7.00 8.44 5.57 7.19 8.58
9 -112.79 49.7 2040-2069 5.95 7.60 9.27 6.69 8.22 9.90 7.38 8.98 10.8
10 -112.79 49.7 2070-2100 5.97 7.66 9.06 7.37 8.93 10.6 8.63 10.3 12.1
Data
df <- structure(list(time = c("1950-01-01", "1951-01-01", "1952-01-01",
"1953-01-01", "1954-01-01", "1955-01-01", "1956-01-01", "1957-01-01",
"1958-01-01", "1959-01-01", "1960-01-01", "1961-01-01", "1962-01-01",
"1963-01-01", "1964-01-01", "1965-01-01", "1966-01-01", "1967-01-01",
"1968-01-01", "1969-01-01", "1970-01-01", "1971-01-01", "1972-01-01",
"1973-01-01", "1974-01-01", "1975-01-01", "1976-01-01", "1977-01-01",
"1978-01-01", "1979-01-01", "1980-01-01", "1981-01-01", "1982-01-01",
"1983-01-01", "1984-01-01", "1985-01-01", "1986-01-01", "1987-01-01",
"1988-01-01", "1989-01-01", "1990-01-01", "1991-01-01", "1992-01-01",
"1993-01-01", "1994-01-01", "1995-01-01", "1996-01-01", "1997-01-01",
"1998-01-01", "1999-01-01", "2000-01-01", "2001-01-01", "2002-01-01",
"2003-01-01", "2004-01-01", "2005-01-01", "2006-01-01", "2007-01-01",
"2008-01-01", "2009-01-01", "2010-01-01", "2011-01-01", "2012-01-01",
"2013-01-01", "2014-01-01", "2015-01-01", "2016-01-01", "2017-01-01",
"2018-01-01", "2019-01-01", "2020-01-01", "2021-01-01", "2022-01-01",
"2023-01-01", "2024-01-01", "2025-01-01", "2026-01-01", "2027-01-01",
"2028-01-01", "2029-01-01", "2030-01-01", "2031-01-01", "2032-01-01",
"2033-01-01", "2034-01-01", "2035-01-01", "2036-01-01", "2037-01-01",
"2038-01-01", "2039-01-01", "2040-01-01", "2041-01-01", "2042-01-01",
"2043-01-01", "2044-01-01", "2045-01-01", "2046-01-01", "2047-01-01",
"2048-01-01", "2049-01-01", "2050-01-01", "2051-01-01", "2052-01-01",
"2053-01-01", "2054-01-01", "2055-01-01", "2056-01-01", "2057-01-01",
"2058-01-01", "2059-01-01", "2060-01-01", "2061-01-01", "2062-01-01",
"2063-01-01", "2064-01-01", "2065-01-01", "2066-01-01", "2067-01-01",
"2068-01-01", "2069-01-01", "2070-01-01", "2071-01-01", "2072-01-01",
"2073-01-01", "2074-01-01", "2075-01-01", "2076-01-01", "2077-01-01",
"2078-01-01", "2079-01-01", "2080-01-01", "2081-01-01", "2082-01-01",
"2083-01-01", "2084-01-01", "2085-01-01", "2086-01-01", "2087-01-01",
"2088-01-01", "2089-01-01", "2090-01-01", "2091-01-01", "2092-01-01",
"2093-01-01", "2094-01-01", "2095-01-01", "2096-01-01", "2097-01-01",
"2098-01-01", "2099-01-01", "2100-01-01", "1950-01-01", "1951-01-01",
"1952-01-01", "1953-01-01", "1954-01-01", "1955-01-01", "1956-01-01",
"1957-01-01", "1958-01-01", "1959-01-01", "1960-01-01", "1961-01-01",
"1962-01-01", "1963-01-01", "1964-01-01", "1965-01-01", "1966-01-01",
"1967-01-01", "1968-01-01", "1969-01-01", "1970-01-01", "1971-01-01",
"1972-01-01", "1973-01-01", "1974-01-01", "1975-01-01", "1976-01-01",
"1977-01-01", "1978-01-01", "1979-01-01", "1980-01-01", "1981-01-01",
"1982-01-01", "1983-01-01", "1984-01-01", "1985-01-01", "1986-01-01",
"1987-01-01", "1988-01-01", "1989-01-01", "1990-01-01", "1991-01-01",
"1992-01-01", "1993-01-01", "1994-01-01", "1995-01-01", "1996-01-01",
"1997-01-01", "1998-01-01", "1999-01-01", "2000-01-01", "2001-01-01",
"2002-01-01", "2003-01-01", "2004-01-01", "2005-01-01", "2006-01-01",
"2007-01-01", "2008-01-01", "2009-01-01", "2010-01-01", "2011-01-01",
"2012-01-01", "2013-01-01", "2014-01-01", "2015-01-01", "2016-01-01",
"2017-01-01", "2018-01-01", "2019-01-01", "2020-01-01", "2021-01-01",
"2022-01-01", "2023-01-01", "2024-01-01", "2025-01-01", "2026-01-01",
"2027-01-01", "2028-01-01", "2029-01-01", "2030-01-01", "2031-01-01",
"2032-01-01", "2033-01-01", "2034-01-01", "2035-01-01", "2036-01-01",
"2037-01-01", "2038-01-01", "2039-01-01", "2040-01-01", "2041-01-01",
"2042-01-01", "2043-01-01", "2044-01-01", "2045-01-01", "2046-01-01",
"2047-01-01", "2048-01-01", "2049-01-01", "2050-01-01", "2051-01-01",
"2052-01-01", "2053-01-01", "2054-01-01", "2055-01-01", "2056-01-01",
"2057-01-01", "2058-01-01", "2059-01-01", "2060-01-01", "2061-01-01",
"2062-01-01", "2063-01-01", "2064-01-01", "2065-01-01", "2066-01-01",
"2067-01-01", "2068-01-01", "2069-01-01", "2070-01-01", "2071-01-01",
"2072-01-01", "2073-01-01", "2074-01-01", "2075-01-01", "2076-01-01",
"2077-01-01", "2078-01-01"), lat = c(49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71, 49.71,
49.71, 49.71, 49.71, 49.71, 49.71, 49.71), lon = c(-112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.88, -112.88, -112.88, -112.88, -112.88,
-112.88, -112.88, -112.79, -112.79, -112.79, -112.79, -112.79,
-112.79, -112.79, -112.79, -112.79, -112.79, -112.79, -112.79,
-112.79, -112.79, -112.79, -112.79, -112.79, -112.79, -112.79,
-112.79, -112.79, -112.79, -112.79, -112.79, -112.79, -112.79,
-112.79, -112.79, -112.79, -112.79, -112.79, -112.79, -112.79,
-112.79, -112.79, -112.79, -112.79, -112.79, -112.79, -112.79,
-112.79, -112.79, -112.79, -112.79, -112.79, -112.79, -112.79,
-112.79, -112.79, -112.79, -112.79, -112.79, -112.79, -112.79,
-112.79, -112.79, -112.79, -112.79, -112.79, -112.79, -112.79,
-112.79, -112.79, -112.79, -112.79, -112.79, -112.79, -112.79,
-112.79, -112.79, -112.79, -112.79, -112.79, -112.79, -112.79,
-112.79, -112.79, -112.79, -112.79, -112.79, -112.79, -112.79,
-112.79, -112.79, -112.79, -112.79, -112.79, -112.79, -112.79,
-112.79, -112.79, -112.79, -112.79, -112.79, -112.79, -112.79,
-112.79, -112.79, -112.79, -112.79, -112.79, -112.79, -112.79,
-112.79, -112.79, -112.79, -112.79, -112.79, -112.79, -112.79,
-112.79, -112.79, -112.79, -112.79, -112.79, -112.79, -112.79,
-112.79, -112.79, -112.79, -112.79, -112.79, -112.79, -112.79,
-112.79, -112.79, -112.79, -112.79, -112.79), rcp26_tg_mean_p10 = c(4.8,
4.62, 4.23, 4.16, 4.63, 4.1, 4.52, 3.43, 3.7, 3.32, 3.66, 4.16,
3.78, 4.57, 3.34, 3.59, 3.75, 4.46, 3.1, 3.74, 3.87, 4.37, 4.07,
4.25, 3.52, 4.52, 4.44, 4.41, 5.22, 4.19, 4.18, 3.54, 3.63, 4.11,
4.44, 3.77, 4.98, 4.26, 4.06, 4.96, 4.04, 4.1, 3.99, 4.36, 4.44,
3.85, 4.41, 4.23, 4.93, 5.21, 5.29, 4.82, 4.68, 4.82, 5.13, 4.68,
4.83, 5.19, 5.82, 5.58, 4.28, 4.94, 5.66, 5.32, 4.92, 5.73, 5.94,
5.16, 4.83, 5.84, 5.81, 5.77, 5.21, 5.53, 6.1, 5.1, 6.45, 5.28,
6.19, 5.91, 6.47, 5.86, 5.7, 6.02, 6.06, 5.85, 6.06, 6.17, 5.64,
5.78, 6.12, 5.24, 5.92, 6.26, 4.91, 6.3, 6.34, 5.53, 6.26, 6.01,
6.29, 5.92, 6.04, 5.39, 6.43, 6.52, 6.3, 5.73, 6.05, 5.89, 6.61,
6.5, 6.21, 5.5, 5.39, 5.83, 6.54, 6.56, 6.39, 6.43, 6.1, 6.48,
5.57, 6.3, 7.05, 6.23, 6.38, 5.67, 4.75, 6.12, 6.27, 6.47, 6.66,
6.12, 6.16, 6.75, 6.4, 6.18, 6.85, 6.66, 6.12, 6.51, 6.07, 6.48,
6.03, 6.17, 6.31, 6.53, 6.19, 6.5, 6.61, 4.7, 4.51, 4.14, 4.05,
4.54, 4, 4.44, 3.32, 3.58, 3.22, 3.56, 4.07, 3.67, 4.47, 3.24,
3.48, 3.64, 4.36, 2.99, 3.64, 3.75, 4.27, 3.97, 4.16, 3.42, 4.39,
4.33, 4.31, 5.11, 4.08, 4.07, 3.42, 3.51, 4, 4.34, 3.66, 4.88,
4.16, 3.96, 4.85, 3.96, 4.02, 3.89, 4.26, 4.34, 3.75, 4.31, 4.11,
4.83, 5.12, 5.19, 4.72, 4.58, 4.72, 5, 4.57, 4.73, 5.11, 5.72,
5.48, 4.17, 4.85, 5.55, 5.21, 4.82, 5.63, 5.83, 5.06, 4.74, 5.76,
5.71, 5.67, 5.1, 5.43, 5.97, 5, 6.33, 5.17, 6.09, 5.81, 6.38,
5.75, 5.58, 5.92, 5.95, 5.75, 5.98, 6.08, 5.54, 5.68, 6.01, 5.14,
5.82, 6.16, 4.81, 6.21, 6.23, 5.42, 6.18, 5.93, 6.2, 5.8, 5.95,
5.3, 6.36, 6.43, 6.18, 5.61, 5.97, 5.79, 6.51, 6.4, 6.11, 5.39,
5.28, 5.72, 6.45, 6.45, 6.28, 6.33, 6, 6.38, 5.49, 6.22, 6.96,
6.14, 6.28, 5.57, 4.65), rcp26_tg_mean_p50 = c(6.02, 5.96, 5.44,
5.65, 5.45, 5.52, 5.44, 5.37, 5.24, 5.21, 5.26, 5.75, 5.63, 5.59,
4.64, 5.19, 5.06, 5.59, 5.36, 5.15, 5.53, 5.69, 5.37, 5.23, 5.42,
5.34, 5.77, 5.69, 5.81, 6.03, 5.24, 5.83, 5.62, 5.1, 6.04, 5.24,
5.79, 5.74, 5.68, 5.82, 5.87, 5.7, 5.05, 5.66, 5.88, 5.38, 5.75,
5.84, 6.12, 6.25, 6.42, 6.05, 6.85, 6.15, 6.28, 6.34, 6.6, 6.05,
6.83, 6.94, 6.45, 6.29, 6.98, 6.64, 7.14, 6.89, 7.19, 6.89, 7.21,
6.66, 6.92, 7.13, 6.76, 7.27, 7.73, 7.03, 7.16, 7.4, 7.2, 7.29,
7.5, 7.15, 7.46, 7.14, 7.74, 7.83, 7.56, 7.33, 7.83, 7.32, 7.39,
6.86, 7.19, 7.43, 7.48, 8.01, 7.86, 7.3, 7.43, 7.63, 7.97, 8.05,
7.54, 7.33, 8.24, 8.32, 8.33, 8.13, 7.63, 7.76, 7.88, 7.81, 7.39,
7.8, 7.93, 7.76, 7.51, 7.49, 7.54, 7.79, 7.68, 7.75, 7.14, 8.08,
7.87, 7.72, 7.97, 7.78, 7.79, 7.63, 7.71, 7.82, 7.65, 7.93, 7.67,
7.79, 8.02, 7.28, 8.29, 8.23, 7.29, 7.67, 7.83, 7.38, 7.41, 7.75,
7.4, 7.8, 7.3, 7.9, 8.08, 5.94, 5.87, 5.39, 5.55, 5.37, 5.42,
5.34, 5.27, 5.16, 5.12, 5.17, 5.66, 5.54, 5.5, 4.54, 5.1, 4.97,
5.5, 5.26, 5.05, 5.43, 5.6, 5.27, 5.14, 5.32, 5.24, 5.67, 5.6,
5.72, 5.94, 5.15, 5.73, 5.54, 5.01, 5.95, 5.14, 5.67, 5.65, 5.59,
5.72, 5.77, 5.61, 4.94, 5.55, 5.79, 5.31, 5.66, 5.73, 6.02, 6.17,
6.34, 5.95, 6.72, 6.06, 6.18, 6.23, 6.49, 5.96, 6.75, 6.83, 6.35,
6.21, 6.89, 6.55, 7.05, 6.79, 7.11, 6.79, 7.1, 6.54, 6.83, 7.08,
6.67, 7.19, 7.66, 6.93, 7.06, 7.31, 7.11, 7.19, 7.41, 7.05, 7.37,
7.03, 7.65, 7.74, 7.47, 7.25, 7.73, 7.23, 7.29, 6.76, 7.1, 7.32,
7.37, 7.92, 7.76, 7.21, 7.34, 7.54, 7.88, 7.96, 7.44, 7.23, 8.16,
8.21, 8.26, 8.04, 7.54, 7.64, 7.79, 7.71, 7.29, 7.71, 7.84, 7.66,
7.42, 7.4, 7.45, 7.7, 7.6, 7.66, 7.04, 8, 7.78, 7.62, 7.87, 7.69,
7.7), rcp26_tg_mean_p90 = c(7.35, 7.39, 6.72, 6.63, 6.87, 6.74,
6.86, 7.1, 6.51, 6.28, 7.08, 7.07, 7.02, 7.43, 5.95, 5.8, 6.8,
6.82, 6.73, 6.81, 6.86, 7.04, 5.92, 7.09, 6.64, 6.24, 7.02, 7.06,
6.81, 7.74, 6.95, 6.95, 6.52, 6.35, 6.81, 6.77, 7.44, 6.7, 6.72,
7.07, 6.7, 7.6, 7.55, 7.19, 7.17, 7.5, 6.69, 7.75, 7.38, 7.12,
7.74, 7.14, 7.81, 8.08, 7.91, 8.1, 7.61, 8.19, 7.58, 8.22, 8.09,
7.89, 8.37, 8.11, 8.27, 7.89, 9, 8.42, 8.23, 7.9, 8.59, 8.38,
9.35, 8.18, 8.88, 8.26, 8.14, 9.29, 8.92, 8.95, 8.59, 9.43, 8.89,
9.25, 9.14, 8.98, 9.52, 8.8, 8.5, 9.82, 9.35, 9.08, 9.47, 8.39,
9.24, 9.3, 9.44, 9.55, 9.41, 8.53, 9.06, 9.58, 9.39, 8.99, 9.78,
10, 9.67, 9.74, 9.48, 10.07, 9.96, 9.43, 10.15, 8.77, 9.45, 9.17,
8.86, 9.11, 9.27, 9.1, 9.33, 9.07, 8.82, 9.61, 9.03, 9.71, 9.52,
8.75, 8.58, 8.87, 9.42, 9.24, 9.16, 9.63, 9.27, 9.86, 9.4, 10,
10.19, 9.81, 9.41, 9.09, 9.66, 9.49, 8.63, 10.09, 9.37, 9.69,
8.59, 9.83, 9.63, 7.26, 7.3, 6.65, 6.54, 6.78, 6.66, 6.77, 7.01,
6.43, 6.19, 6.99, 6.97, 6.93, 7.34, 5.85, 5.71, 6.73, 6.75, 6.67,
6.73, 6.77, 6.98, 5.83, 7.02, 6.55, 6.14, 6.93, 6.98, 6.72, 7.66,
6.86, 6.86, 6.42, 6.25, 6.73, 6.68, 7.35, 6.61, 6.63, 6.97, 6.61,
7.54, 7.46, 7.12, 7.08, 7.42, 6.6, 7.65, 7.31, 7.02, 7.66, 7.05,
7.72, 7.99, 7.84, 8.01, 7.52, 8.1, 7.5, 8.12, 7.99, 7.79, 8.28,
8.04, 8.18, 7.81, 8.92, 8.34, 8.12, 7.8, 8.52, 8.27, 9.24, 8.09,
8.8, 8.18, 8.06, 9.21, 8.81, 8.85, 8.5, 9.35, 8.81, 9.17, 9.06,
8.9, 9.44, 8.72, 8.4, 9.75, 9.25, 8.98, 9.39, 8.3, 9.16, 9.21,
9.34, 9.46, 9.32, 8.43, 8.98, 9.5, 9.3, 8.92, 9.69, 9.92, 9.58,
9.66, 9.38, 10, 9.89, 9.35, 10.07, 8.67, 9.38, 9.08, 8.77, 9.01,
9.19, 9.01, 9.23, 8.98, 8.72, 9.52, 8.93, 9.62, 9.43, 8.66, 8.49
), rcp45_tg_mean_p10 = c(4.8, 4.62, 4.23, 4.16, 4.63, 4.1, 4.52,
3.43, 3.7, 3.32, 3.66, 4.16, 3.78, 4.57, 3.34, 3.59, 3.75, 4.46,
3.1, 3.74, 3.87, 4.37, 4.07, 4.25, 3.52, 4.52, 4.44, 4.41, 5.22,
4.19, 4.17, 3.52, 3.63, 4.09, 4.47, 3.76, 5.01, 4.29, 4.07, 5.01,
4.05, 4.1, 3.99, 4.35, 4.43, 3.85, 4.44, 4.23, 4.95, 5.26, 5.3,
4.81, 4.65, 4.83, 5.14, 4.59, 5.57, 5.25, 4.6, 5.58, 5.4, 5.51,
5.4, 4.88, 5.07, 4.73, 4.85, 5.67, 4.9, 5.73, 5, 5.57, 5.4, 5.21,
6.46, 5.58, 5.86, 5.75, 5.59, 5.62, 6.38, 5.77, 5.55, 6.32, 6.71,
5.91, 5.76, 6.99, 6.93, 6.2, 6.41, 6.17, 6.79, 6.13, 6.09, 6.45,
6.7, 7.19, 6.04, 6.26, 6.82, 6.73, 7.33, 6.92, 6.59, 6.6, 6.65,
6.76, 7.47, 6.78, 7.05, 6.81, 7.27, 6.44, 7.8, 6.9, 7.84, 7.35,
7.01, 6.26, 7.1, 7.76, 7.07, 7.53, 7.04, 7.37, 7.82, 7.84, 7.7,
6.86, 7.81, 6.87, 7.87, 7.19, 7.09, 6.34, 7.37, 7.07, 6.9, 6.76,
7.64, 7.66, 7.37, 7.82, 7.01, 7.53, 8.04, 6.87, 7.89, 7.79, 7.11,
4.7, 4.51, 4.14, 4.05, 4.54, 4, 4.44, 3.32, 3.58, 3.22, 3.56,
4.07, 3.67, 4.47, 3.24, 3.48, 3.64, 4.36, 2.99, 3.64, 3.75, 4.27,
3.97, 4.16, 3.42, 4.39, 4.33, 4.31, 5.11, 4.08, 4.07, 3.4, 3.51,
3.98, 4.35, 3.65, 4.91, 4.19, 3.97, 4.89, 3.97, 4.02, 3.88, 4.24,
4.34, 3.75, 4.34, 4.11, 4.85, 5.17, 5.21, 4.71, 4.55, 4.73, 5,
4.49, 5.47, 5.15, 4.51, 5.48, 5.3, 5.4, 5.3, 4.77, 4.97, 4.61,
4.74, 5.57, 4.79, 5.63, 4.89, 5.48, 5.3, 5.12, 6.37, 5.47, 5.76,
5.65, 5.49, 5.52, 6.29, 5.66, 5.44, 6.23, 6.58, 5.82, 5.66, 6.89,
6.82, 6.09, 6.3, 6.09, 6.69, 6.03, 5.99, 6.34, 6.61, 7.08, 5.95,
6.16, 6.7, 6.64, 7.22, 6.84, 6.5, 6.5, 6.57, 6.67, 7.37, 6.71,
6.96, 6.71, 7.17, 6.34, 7.71, 6.79, 7.74, 7.26, 6.93, 6.15, 6.99,
7.69, 6.97, 7.43, 6.94, 7.28, 7.72, 7.75, 7.6), rcp45_tg_mean_p50 = c(6.02,
5.96, 5.44, 5.65, 5.45, 5.52, 5.44, 5.37, 5.24, 5.21, 5.26, 5.75,
5.63, 5.59, 4.64, 5.19, 5.06, 5.59, 5.36, 5.15, 5.53, 5.69, 5.37,
5.23, 5.42, 5.34, 5.77, 5.69, 5.81, 6.03, 5.26, 5.82, 5.62, 5.1,
6.04, 5.25, 5.77, 5.75, 5.68, 5.81, 5.86, 5.69, 5.07, 5.64, 5.89,
5.4, 5.75, 5.85, 6.16, 6.26, 6.42, 6.04, 6.85, 6.2, 6.27, 6.32,
6.95, 6.48, 6.54, 6.82, 6.39, 6.74, 6.25, 6.42, 6.26, 6.8, 6.38,
6.94, 6.98, 6.77, 6.79, 6.92, 6.83, 7.64, 7.64, 7.21, 7.64, 7.54,
7.69, 6.76, 7.1, 7.1, 7.69, 7.62, 7.84, 7.7, 7.27, 8.22, 7.78,
7.49, 7.31, 7.8, 7.78, 7.98, 7.98, 8.16, 8.26, 8.36, 7.72, 8.09,
7.71, 8.04, 8.43, 8.81, 8.72, 8.87, 8.04, 8.16, 9.09, 8.41, 7.86,
8.09, 9.02, 8.5, 8.77, 8.84, 9.01, 8.78, 8.33, 8.5, 8.86, 9.36,
9.64, 8.85, 8.5, 8.9, 9.13, 9, 8.95, 9.36, 8.4, 8.85, 8.83, 8.7,
9.16, 9.02, 8.62, 8.6, 8.65, 9.2, 9.21, 9.22, 9.68, 9.13, 8.93,
9.44, 9.3, 9.2, 9.31, 9.85, 8.88, 5.94, 5.87, 5.39, 5.55, 5.37,
5.42, 5.34, 5.27, 5.16, 5.12, 5.17, 5.66, 5.54, 5.5, 4.54, 5.1,
4.97, 5.5, 5.26, 5.05, 5.43, 5.6, 5.27, 5.14, 5.32, 5.24, 5.67,
5.6, 5.72, 5.94, 5.16, 5.72, 5.53, 5.01, 5.94, 5.14, 5.66, 5.65,
5.59, 5.7, 5.76, 5.59, 4.96, 5.53, 5.8, 5.33, 5.66, 5.75, 6.07,
6.18, 6.33, 5.94, 6.74, 6.11, 6.17, 6.22, 6.86, 6.38, 6.42, 6.72,
6.29, 6.66, 6.15, 6.32, 6.18, 6.71, 6.28, 6.82, 6.89, 6.68, 6.69,
6.84, 6.71, 7.55, 7.55, 7.13, 7.53, 7.43, 7.6, 6.65, 6.99, 7,
7.6, 7.54, 7.74, 7.6, 7.17, 8.12, 7.7, 7.4, 7.21, 7.7, 7.68,
7.87, 7.89, 8.07, 8.16, 8.26, 7.64, 8, 7.61, 7.95, 8.33, 8.72,
8.64, 8.78, 7.95, 8.06, 9.02, 8.32, 7.75, 7.99, 8.91, 8.4, 8.67,
8.74, 8.92, 8.7, 8.23, 8.39, 8.74, 9.27, 9.56, 8.78, 8.4, 8.82,
9.04, 8.9, 8.85), rcp45_tg_mean_p90 = c(7.35, 7.39, 6.72, 6.63,
6.87, 6.74, 6.86, 7.1, 6.51, 6.28, 7.08, 7.07, 7.02, 7.43, 5.95,
5.8, 6.8, 6.82, 6.73, 6.81, 6.86, 7.04, 5.92, 7.09, 6.64, 6.24,
7.02, 7.06, 6.81, 7.74, 6.94, 6.97, 6.51, 6.35, 6.8, 6.79, 7.4,
6.74, 6.71, 7.07, 6.71, 7.59, 7.55, 7.22, 7.16, 7.51, 6.69, 7.75,
7.4, 7.13, 7.76, 7.14, 7.8, 8.08, 7.91, 8.11, 8.53, 7.63, 7.81,
8.18, 7.77, 8.09, 7.51, 7.42, 8.03, 7.71, 7.76, 8.36, 8.53, 8.54,
8.63, 8.3, 7.57, 8.8, 8.78, 8.93, 9.73, 8.53, 9.21, 8.68, 8.63,
8.81, 9.53, 9.02, 8.77, 9.24, 9.04, 9.93, 8.99, 9.02, 8.82, 9.1,
9.07, 9.71, 9.62, 9.95, 9.99, 9.69, 9.59, 9.96, 9.68, 9.85, 10.12,
10.02, 9.86, 10.16, 9.62, 10, 10.75, 10.21, 10.1, 10.13, 10.21,
10.99, 10.63, 10.49, 10.75, 9.92, 10.26, 10.29, 10.43, 10.78,
10.89, 10.79, 11.21, 10.64, 10.15, 10.36, 10.83, 11.08, 10.78,
11.31, 10.72, 10.73, 10.81, 10.6, 10.97, 11.06, 10.34, 10.85,
11.31, 10.62, 11.02, 10.18, 10.62, 10.72, 10.56, 11.37, 10.68,
11.38, 10.93, 7.26, 7.3, 6.65, 6.54, 6.78, 6.66, 6.77, 7.01,
6.43, 6.19, 6.99, 6.97, 6.93, 7.34, 5.85, 5.71, 6.73, 6.75, 6.67,
6.73, 6.77, 6.98, 5.83, 7.02, 6.55, 6.14, 6.93, 6.98, 6.72, 7.66,
6.85, 6.88, 6.41, 6.25, 6.72, 6.7, 7.31, 6.65, 6.62, 6.97, 6.62,
7.52, 7.46, 7.15, 7.08, 7.43, 6.59, 7.65, 7.32, 7.04, 7.68, 7.04,
7.72, 7.99, 7.83, 8.02, 8.45, 7.53, 7.73, 8.09, 7.68, 8, 7.41,
7.33, 7.92, 7.62, 7.68, 8.28, 8.45, 8.45, 8.54, 8.22, 7.48, 8.72,
8.69, 8.87, 9.64, 8.42, 9.12, 8.6, 8.55, 8.72, 9.43, 8.94, 8.66,
9.14, 8.97, 9.84, 8.9, 8.93, 8.72, 9.05, 8.97, 9.62, 9.52, 9.86,
9.9, 9.6, 9.49, 9.86, 9.6, 9.76, 10.04, 9.93, 9.77, 10.07, 9.53,
9.92, 10.67, 10.13, 9.99, 10.04, 10.12, 10.92, 10.54, 10.4, 10.66,
9.81, 10.18, 10.2, 10.34, 10.69, 10.8, 10.73, 11.12, 10.56, 10.07,
10.27, 10.73), rcp85_tg_mean_p10 = c(4.8, 4.62, 4.23, 4.16, 4.63,
4.1, 4.52, 3.43, 3.7, 3.32, 3.66, 4.16, 3.78, 4.57, 3.34, 3.59,
3.75, 4.46, 3.1, 3.74, 3.87, 4.37, 4.07, 4.25, 3.52, 4.52, 4.44,
4.41, 5.22, 4.19, 4.17, 3.52, 3.64, 4.08, 4.46, 3.76, 5, 4.28,
4.06, 5.02, 3.98, 4.03, 3.99, 4.35, 4.43, 3.85, 4.45, 4.21, 4.93,
5.27, 5.3, 4.8, 4.65, 4.83, 5.15, 4.92, 5.74, 5.29, 5.02, 5.03,
4.22, 5.25, 5.06, 4.81, 5.13, 4.66, 5.65, 5.45, 5.28, 4.8, 5.93,
4.99, 5.66, 6.49, 5.85, 6.06, 5.96, 5.66, 6.45, 6.55, 5.84, 5.98,
5.86, 5.96, 5.67, 6.98, 5.69, 6.81, 6.68, 6.44, 6.01, 6.63, 6.85,
6.49, 6.32, 6.69, 7.09, 7.06, 6.88, 7.08, 6.48, 7.13, 6.67, 8.08,
7.87, 7.55, 8.06, 7.47, 7.94, 8.4, 7.61, 7.26, 8.11, 7.85, 8.15,
8.13, 8.24, 8.01, 8.76, 9.52, 8.25, 8.22, 8.17, 8.57, 8.64, 9.6,
9.13, 9.14, 8.84, 8.98, 8.91, 9.41, 9.99, 8.65, 8.97, 8.58, 9.66,
10.01, 9.14, 9.41, 10.12, 10.18, 10.06, 9.73, 9.2, 9.25, 9.4,
10.84, 10.14, 9.97, 11.09, 4.7, 4.51, 4.14, 4.05, 4.54, 4, 4.44,
3.32, 3.58, 3.22, 3.56, 4.07, 3.67, 4.47, 3.24, 3.48, 3.64, 4.36,
2.99, 3.64, 3.75, 4.27, 3.97, 4.16, 3.42, 4.39, 4.33, 4.31, 5.11,
4.08, 4.07, 3.4, 3.52, 3.97, 4.35, 3.65, 4.9, 4.18, 3.96, 4.9,
3.88, 3.93, 3.89, 4.24, 4.34, 3.75, 4.35, 4.1, 4.83, 5.19, 5.21,
4.7, 4.55, 4.73, 5.01, 4.81, 5.63, 5.2, 4.92, 4.94, 4.09, 5.15,
4.95, 4.71, 5.03, 4.56, 5.54, 5.35, 5.18, 4.66, 5.84, 4.89, 5.56,
6.38, 5.74, 5.96, 5.84, 5.53, 6.32, 6.44, 5.73, 5.87, 5.77, 5.84,
5.57, 6.88, 5.6, 6.71, 6.59, 6.31, 5.9, 6.52, 6.76, 6.4, 6.21,
6.56, 6.98, 6.91, 6.78, 6.99, 6.39, 7.03, 6.57, 7.99, 7.78, 7.45,
7.97, 7.37, 7.84, 8.3, 7.51, 7.16, 8.01, 7.75, 8.06, 8.05, 8.14,
7.9, 8.68, 9.43, 8.14, 8.12, 8.08, 8.47, 8.54, 9.49, 9.02, 9.04,
8.74), rcp85_tg_mean_p50 = c(6.02, 5.95, 5.44, 5.65, 5.42, 5.52,
5.44, 5.37, 5.22, 5.21, 5.26, 5.75, 5.6, 5.55, 4.64, 5.19, 5.06,
5.59, 5.36, 5.15, 5.53, 5.69, 5.37, 5.23, 5.42, 5.34, 5.77, 5.69,
5.81, 6.03, 5.25, 5.82, 5.61, 5.09, 6.03, 5.24, 5.77, 5.75, 5.68,
5.81, 5.86, 5.7, 5.03, 5.66, 5.86, 5.35, 5.75, 5.85, 6.14, 6.21,
6.39, 6.03, 6.85, 6.21, 6.26, 6.33, 6.93, 6.95, 6.56, 6.69, 6.33,
6.13, 6.36, 6.65, 6.6, 6.59, 6.9, 7.16, 7.32, 7.37, 6.98, 6.89,
7.72, 7.33, 7.07, 7.2, 7.11, 7.31, 7.95, 7.83, 7.51, 7.3, 7.38,
7.74, 8.36, 8.52, 7.69, 8.26, 8.43, 8.41, 7.89, 7.88, 7.72, 8.61,
7.46, 8.23, 8.22, 8.54, 8.82, 8.92, 8.39, 8.71, 8.47, 9.21, 9.03,
9.12, 9.31, 9.05, 9.38, 9.73, 9.05, 8.87, 9.7, 9.71, 10.05, 9.98,
10.23, 10.28, 10.66, 11.07, 10.19, 10.03, 10.32, 10.31, 10.24,
10.33, 10.6, 10.72, 10.76, 10.75, 10.89, 10.71, 11.9, 11.36,
11.14, 11.39, 11.24, 11.54, 11.11, 11.93, 11.3, 12.24, 11.81,
11.96, 11.9, 11.29, 12.12, 12.59, 12.73, 12.21, 12.84, 5.94,
5.86, 5.39, 5.55, 5.33, 5.42, 5.34, 5.27, 5.11, 5.12, 5.17, 5.66,
5.51, 5.45, 4.54, 5.1, 4.97, 5.5, 5.26, 5.05, 5.43, 5.6, 5.27,
5.14, 5.32, 5.24, 5.67, 5.6, 5.72, 5.94, 5.16, 5.72, 5.53, 4.99,
5.94, 5.14, 5.66, 5.65, 5.59, 5.7, 5.76, 5.61, 4.92, 5.55, 5.77,
5.26, 5.65, 5.74, 6.04, 6.12, 6.3, 5.93, 6.73, 6.12, 6.16, 6.23,
6.84, 6.85, 6.48, 6.59, 6.24, 6.04, 6.27, 6.55, 6.51, 6.5, 6.8,
7.07, 7.23, 7.27, 6.89, 6.79, 7.62, 7.21, 6.97, 7.11, 7.02, 7.23,
7.86, 7.75, 7.42, 7.2, 7.27, 7.64, 8.26, 8.45, 7.59, 8.15, 8.33,
8.32, 7.79, 7.8, 7.63, 8.51, 7.36, 8.13, 8.13, 8.45, 8.74, 8.82,
8.29, 8.63, 8.37, 9.12, 8.92, 9.02, 9.21, 8.96, 9.28, 9.63, 8.96,
8.77, 9.61, 9.62, 9.95, 9.89, 10.14, 10.17, 10.54, 10.98, 10.11,
9.95, 10.24, 10.22, 10.16, 10.24, 10.47, 10.62, 10.66), rcp85_tg_mean_p90 = c(7.35,
7.39, 6.72, 6.63, 6.87, 6.74, 6.86, 7.1, 6.51, 6.28, 7.08, 7.07,
7.02, 7.43, 5.94, 5.8, 6.8, 6.82, 6.71, 6.76, 6.86, 7.04, 5.92,
7.09, 6.64, 6.24, 7.02, 7.06, 6.81, 7.68, 6.93, 6.97, 6.5, 6.36,
6.81, 6.77, 7.41, 6.72, 6.72, 7.08, 6.69, 7.57, 7.55, 7.22, 7.15,
7.5, 6.7, 7.73, 7.4, 7.12, 7.74, 7.14, 7.81, 8.07, 7.89, 8.08,
7.94, 8.35, 7.9, 8.39, 7.48, 7.8, 8.21, 7.74, 8.17, 7.76, 8.12,
8.91, 9.15, 8.36, 8.36, 8.27, 9.07, 8.74, 8.58, 8.42, 8.26, 8.75,
9.46, 8.76, 8.95, 8.68, 8.82, 9.28, 9.72, 9.74, 8.92, 10.03,
9.46, 10.36, 9.94, 9.95, 9.71, 9.6, 10.05, 9.62, 9.64, 9.43,
10.39, 10.4, 10.24, 10.56, 10.91, 10.63, 10.46, 10.96, 10.59,
11.08, 11.49, 11.49, 11.69, 11.43, 11.35, 12.22, 11.46, 11.88,
11.75, 12, 12.06, 12.46, 12.33, 12.29, 11.79, 11.93, 11.81, 12.31,
12.11, 11.97, 12.8, 12.41, 12.96, 12.87, 12.81, 12.79, 13.27,
12.82, 12.97, 12.86, 13.02, 13.15, 13.28, 13.5, 13.55, 13.54,
14.29, 14, 13.74, 14.52, 14.46, 14.91, 14.46, 7.26, 7.3, 6.65,
6.54, 6.78, 6.66, 6.77, 7.01, 6.43, 6.19, 6.99, 6.97, 6.93, 7.34,
5.84, 5.71, 6.73, 6.75, 6.63, 6.66, 6.77, 6.98, 5.83, 7.02, 6.55,
6.14, 6.93, 6.98, 6.72, 7.59, 6.84, 6.88, 6.4, 6.25, 6.72, 6.68,
7.32, 6.62, 6.62, 6.98, 6.6, 7.51, 7.47, 7.15, 7.07, 7.42, 6.6,
7.64, 7.32, 7.02, 7.66, 7.04, 7.72, 7.98, 7.81, 7.99, 7.84, 8.25,
7.81, 8.29, 7.39, 7.7, 8.13, 7.67, 8.07, 7.66, 8.03, 8.81, 9.07,
8.28, 8.28, 8.18, 8.99, 8.66, 8.48, 8.33, 8.16, 8.68, 9.37, 8.66,
8.87, 8.56, 8.74, 9.21, 9.63, 9.67, 8.82, 9.94, 9.35, 10.28,
9.86, 9.86, 9.61, 9.5, 9.96, 9.53, 9.54, 9.34, 10.31, 10.31,
10.16, 10.46, 10.83, 10.54, 10.38, 10.88, 10.51, 10.99, 11.4,
11.4, 11.61, 11.34, 11.26, 12.14, 11.37, 11.79, 11.66, 11.92,
11.98, 12.38, 12.24, 12.2, 11.7, 11.85, 11.72, 12.23, 12.03,
11.87, 12.73)), row.names = c(NA, 280L), class = "data.frame")
Hi I have a question some of you could help me out.
My vector is temperature collected hourly:
a<-c(7.95, 7.8, 7.85, 7.6, 7.1, 5.55, 4.35, 4.1, 7.35, 10.7, 14.2,
17.25, 19.1, 19.8, 20.1, 20.15, 19.9, 18.95, 16.7, 14.4, 13.75,
12.1, 12.3, 11.4, 10.3, 8.55, 7.45, 7.05, 5.6, 5.95, 4.85, 5.3,
9.35, 12.7, 16.15, 19.1, 20.5, 21.05, 21.4, 21.4, 21.35, 20.1,
16.95, 15.8, 15.6, 14.95, 14.15, 13.85)
I want to determine how many events there are which a is above 20 and below 10 for a certain period of time.
Pictorially, this is what I am looking for. Here there are two events (blue and green) where temperature amplitude threshold was achieved. Result should be 2.
============
Another example:
Here temperature was below 10 & above 20 for at least 1 hour, two times (or two events).
Result should be 2.
Data for example 2:
b<-c(20.2, 20.55, 20.85, 21.7, 20.7, 18.7, 17.5, 17.4, 16.65, 17.15,
15.8, 13.85, 12.55, 11.45, 10.2, 9.3, 8.2, 7.4, 7.25, 6.65, 5.9,
4.75, 4.5, 4.15, 4.4, 6.25, 8.1, 10.35, 12.4, 14.3, 15.3, 16.3,
17.25, 17.25, 16.85, 14.45, 12.85, 11.35, 10.2, 9.1, 8.6, 7.35,
5.9, 4.85, 3.65, 3.3, 2.95, 2.65, 2.45, 4.85, 6.45, 8.25, 9.95,
11.1, 12.3, 13.2, 13.95, 14.05, 13.15, 10.35, 8.15, 6.6, 6.3,
6, 7.55, 5.85, 5.05, 4.75, 4.5, 4.75, 4.75, 4.55, 5.15, 8.45,
12.05, 16.35, 18.9, 20.55, 21.6, 21.45, 21.75, 21.15, 20.05,
17.75, 16.5, 18.2, 18.05, 17.95, 17.8, 17.55, 17.25, 16.95, 16.6,
16.35, 16.1, 16.25, 16.4, 17.1, 17.8)
I update my answer using your new example b. I found a solution based on: Find a numeric pattern R .
# Get out of range (10,20)
x = ifelse(b<=10,1,0) # Don't need nested ifelse
# Specify if 2 for upper limit [20,inf)
x[b>=20]=2
z = rle(x)
> z
Run Length Encoding
lengths: int [1:10] 5 10 12 12 14 7 14 3 6 16
values : num [1:10] 2 0 1 0 1 0 1 0 2 0
Like your plot, there is two combinations of going lower limit to upper limit or the other way round: 2-0-1 or 1-0-2. You can do:
ranges = z$values
# This line looks for 0 -groups of T in range (10,20)- and the looks if the group before is 1 and the next 2 (low to up) or 2 and 1 (up to low)
x = as.integer(ranges == 0 & ( (lag(ranges)==2 & lead(ranges)==1) | (lag(ranges)==1 & lead(ranges)==2) ) )
x
[1] 0 1 0 0 0 0 0 1 0 NA
You can sum, omitting the NAs, to return 2:
sum(x, na.rm=TRUE)
You could remove 0s and just look for 1 followed by 2 or the other way round but it is the same concept. If you want to keep the z$lengths to work with them later, you could transform rle() output to a dataframe and adapt the code to mutate a new column.
I am trying to pull data from one data frame to another based on the equivalent of a VLOOKUP table in Excel. I have had a look at the most popular VLOOKUP question in R, but I cannot see how it applies to my specific problem. The key thing is that I don't want to pull all of the columns from the second data frame into my first one - I only want to pull in one column. I'm pretty sure this will be some kind of derivation of a merge function.
Referring to the below data, I am trying to create a new column called df1$Trait1Percentile. This needs to draw from LookupTable$Trait1Percentiles based on a match between df1$Trait1Scores and
LookupTable$Scores.
#Import data.
df1 <- structure(list(JobNumber = c(634L, 21L, 300L, 797L, 1112L, 147L,
1L, 4L, 260L, 194L, 981L, 1110L, 634L, 554L, 213L, 722L, 1036L,
855L, 624L, 1113L, 681L, 547L, 195L, 624L, 546L, 201L, 918L,
1069L, 300L, 294L, 587L, 933L, 918L, 620L, 918L, 298L, 749L,
295L, 635L, 515L, 624L, 147L, 200L, 527L, 800L, 827L, 4L, 568L,
252L, 655L, 559L, 629L, 639L, 933L, 214L, 750L, 1066L, 495L,
1113L, 1L, 1113L, 12L, 561L, 741L, 495L, 981L, 147L, 199L, 629L,
163L, 615L, 294L, 49L, 624L, 260L, 1L, 299L, 193L, 108L, 113L,
426L, 299L, 708L, 749L, 749L, 483L, 935L, 1036L, 295L, 12L, 1113L,
1038L, 4L, 973L, 448L, 295L, 197L, 76L, 1L, 1L), Trait1Score = c(3.89,
4.39, 4.22, 4.21, 3.94, 3.9, 4.58, 4.5, 4.29, 4.47, 4.41, 4.4,
4.14, 4.78, 4.09, 4.58, 4.27, 4.24, 3.96, 3.94, 4.3, 4.07, 4.28,
4.19, 4.57, 4.74, 3.29, 4.23, 3.51, 3.77, 4.46, 5.04, 4.25, 3.92,
3.78, 4.43, 4.12, 4.18, 4.63, 3.25, 3.87, 4.4, 3.83, 4.03, 3.42,
4.9, 4.09, 4.58, 4.29, 4.7, 4.38, 4.61, 4.41, 4.5, 4.6, 4.22,
3.72, 4.34, 4.34, 4.38, 4.15, 4.22, 3.93, 5, 3.81, 4.3, 4.6,
4.96, 4.29, 4.8, 5.05, 3.76, 4.81, 4.77, 4.25, 4.17, 4.75, 4.15,
4.35, 4.23, 5.31, 4.18, 3.67, 3.84, 4.06, 3.66, 3.58, 4.37, 4.43,
4.63, 4.74, 4.79, 5.04, 3.55, 3.64, 4.9, 4.38, 4.01, 4.47, 4.53
), Trait2Score = c(4, 2.94, 3.17, 3.83, 4.22, 3.83, 5.11, 3,
2.83, 2.78, 2.22, 2.22, 4.11, 2.39, 2.22, 2.06, 2.89, 3.61, 3.89,
4.89, 3.78, 4.22, 4.5, 4.39, 1.89, 4.78, 4.56, 3.78, 2.28, 4.61,
2.72, 1.89, 4.44, 4.06, 3.72, 2.44, 3.61, 2.06, 2.17, 6.44, 3.22,
2.78, 4.61, 2.72, 2.83, 2.44, 6.5, 2.28, 2.89, 2.11, 4.44, 2.83,
3, 6.33, 3.11, 3.17, 3.67, 4.5, 2.5, 4.33, 5, 2.89, 3.89, 1.72,
3.33, 4.28, 2.17, 3.17, 2.61, 2.89, 1.22, 3.39, 1.28, 2.61, 2.5,
4.56, 2.89, 4.89, 3.11, 3.5, 1.44, 2.39, 5.33, 3.78, 1.5, 3.44,
5.83, 3.17, 3.78, 2.67, 1.61, 1.83, 4.56, 4.67, 4.61, 2.5, 4.94,
3.94, 4.33, 2.72)), row.names = c(NA, -100L), class = "data.frame")
LookupTable <- structure(list(Scores = c(0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06,
0.07, 0.08, 0.09, 0.1, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17,
0.18, 0.19, 0.2, 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28,
0.29, 0.3, 0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39,
0.4, 0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5,
0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6, 0.61,
0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7, 0.71, 0.72,
0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8, 0.81, 0.82, 0.83,
0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9, 0.91, 0.92, 0.93, 0.94,
0.95, 0.96, 0.97, 0.98, 0.99, 1, 1.01, 1.02, 1.03, 1.04, 1.05,
1.06, 1.07, 1.08, 1.09, 1.1, 1.11, 1.12, 1.13, 1.14, 1.15, 1.16,
1.17, 1.18, 1.19, 1.2, 1.21, 1.22, 1.23, 1.24, 1.25, 1.26, 1.27,
1.28, 1.29, 1.3, 1.31, 1.32, 1.33, 1.34, 1.35, 1.36, 1.37, 1.38,
1.39, 1.4, 1.41, 1.42, 1.43, 1.44, 1.45, 1.46, 1.47, 1.48, 1.49,
1.5, 1.51, 1.52, 1.53, 1.54, 1.55, 1.56, 1.57, 1.58, 1.59, 1.6,
1.61, 1.62, 1.63, 1.64, 1.65, 1.66, 1.67, 1.68, 1.69, 1.7, 1.71,
1.72, 1.73, 1.74, 1.75, 1.76, 1.77, 1.78, 1.79, 1.8, 1.81, 1.82,
1.83, 1.84, 1.85, 1.86, 1.87, 1.88, 1.89, 1.9, 1.91, 1.92, 1.93,
1.94, 1.95, 1.96, 1.97, 1.98, 1.99, 2, 2.01, 2.02, 2.03, 2.04,
2.05, 2.06, 2.07, 2.08, 2.09, 2.1, 2.11, 2.12, 2.13, 2.14, 2.15,
2.16, 2.17, 2.18, 2.19, 2.2, 2.21, 2.22, 2.23, 2.24, 2.25, 2.26,
2.27, 2.28, 2.29, 2.3, 2.31, 2.32, 2.33, 2.34, 2.35, 2.36, 2.37,
2.38, 2.39, 2.4, 2.41, 2.42, 2.43, 2.44, 2.45, 2.46, 2.47, 2.48,
2.49, 2.5, 2.51, 2.52, 2.53, 2.54, 2.55, 2.56, 2.57, 2.58, 2.59,
2.6, 2.61, 2.62, 2.63, 2.64, 2.65, 2.66, 2.67, 2.68, 2.69, 2.7,
2.71, 2.72, 2.73, 2.74, 2.75, 2.76, 2.77, 2.78, 2.79, 2.8, 2.81,
2.82, 2.83, 2.84, 2.85, 2.86, 2.87, 2.88, 2.89, 2.9, 2.91, 2.92,
2.93, 2.94, 2.95, 2.96, 2.97, 2.98, 2.99, 3, 3.01, 3.02, 3.03,
3.04, 3.05, 3.06, 3.07, 3.08, 3.09, 3.1, 3.11, 3.12, 3.13, 3.14,
3.15, 3.16, 3.17, 3.18, 3.19, 3.2, 3.21, 3.22, 3.23, 3.24, 3.25,
3.26, 3.27, 3.28, 3.29, 3.3, 3.31, 3.32, 3.33, 3.34, 3.35, 3.36,
3.37, 3.38, 3.39, 3.4, 3.41, 3.42, 3.43, 3.44, 3.45, 3.46, 3.47,
3.48, 3.49, 3.5, 3.51, 3.52, 3.53, 3.54, 3.55, 3.56, 3.57, 3.58,
3.59, 3.6, 3.61, 3.62, 3.63, 3.64, 3.65, 3.66, 3.67, 3.68, 3.69,
3.7, 3.71, 3.72, 3.73, 3.74, 3.75, 3.76, 3.77, 3.78, 3.79, 3.8,
3.81, 3.82, 3.83, 3.84, 3.85, 3.86, 3.87, 3.88, 3.89, 3.9, 3.91,
3.92, 3.93, 3.94, 3.95, 3.96, 3.97, 3.98, 3.99, 4, 4.01, 4.02,
4.03, 4.04, 4.05, 4.06, 4.07, 4.08, 4.09, 4.1, 4.11, 4.12, 4.13,
4.14, 4.15, 4.16, 4.17, 4.18, 4.19, 4.2, 4.21, 4.22, 4.23, 4.24,
4.25, 4.26, 4.27, 4.28, 4.29, 4.3, 4.31, 4.32, 4.33, 4.34, 4.35,
4.36, 4.37, 4.38, 4.39, 4.4, 4.41, 4.42, 4.43, 4.44, 4.45, 4.46,
4.47, 4.48, 4.49, 4.5, 4.51, 4.52, 4.53, 4.54, 4.55, 4.56, 4.57,
4.58, 4.59, 4.6, 4.61, 4.62, 4.63, 4.64, 4.65, 4.66, 4.67, 4.68,
4.69, 4.7, 4.71, 4.72, 4.73, 4.74, 4.75, 4.76, 4.77, 4.78, 4.79,
4.8, 4.81, 4.82, 4.83, 4.84, 4.85, 4.86, 4.87, 4.88, 4.89, 4.9,
4.91, 4.92, 4.93, 4.94, 4.95, 4.96, 4.97, 4.98, 4.99, 5, 5.01,
5.02, 5.03, 5.04, 5.05, 5.06, 5.07, 5.08, 5.09, 5.1, 5.11, 5.12,
5.13, 5.14, 5.15, 5.16, 5.17, 5.18, 5.19, 5.2, 5.21, 5.22, 5.23,
5.24, 5.25, 5.26, 5.27, 5.28, 5.29, 5.3, 5.31, 5.32, 5.33, 5.34,
5.35, 5.36, 5.37, 5.38, 5.39, 5.4, 5.41, 5.42, 5.43, 5.44, 5.45,
5.46, 5.47, 5.48, 5.49, 5.5, 5.51, 5.52, 5.53, 5.54, 5.55, 5.56,
5.57, 5.58, 5.59, 5.6, 5.61, 5.62, 5.63, 5.64, 5.65, 5.66, 5.67,
5.68, 5.69, 5.7, 5.71, 5.72, 5.73, 5.74, 5.75, 5.76, 5.77, 5.78,
5.79, 5.8, 5.81, 5.82, 5.83, 5.84, 5.85, 5.86, 5.87, 5.88, 5.89,
5.9, 5.91, 5.92, 5.93, 5.94, 5.95, 5.96, 5.97, 5.98, 5.99, 6,
6.01, 6.02, 6.03, 6.04, 6.05, 6.06, 6.07, 6.08, 6.09, 6.1, 6.11,
6.12, 6.13, 6.14, 6.15, 6.16, 6.17, 6.18, 6.19, 6.2, 6.21, 6.22,
6.23, 6.24, 6.25, 6.26, 6.27, 6.28, 6.29, 6.3, 6.31, 6.32, 6.33,
6.34, 6.35, 6.36, 6.37, 6.38, 6.39, 6.4, 6.41, 6.42, 6.43, 6.44,
6.45, 6.46, 6.47, 6.48, 6.49, 6.5, 6.51, 6.52, 6.53, 6.54, 6.55,
6.56, 6.57, 6.58, 6.59, 6.6, 6.61, 6.62, 6.63, 6.64, 6.65, 6.66,
6.67, 6.68, 6.69, 6.7, 6.71, 6.72, 6.73, 6.74, 6.75, 6.76, 6.77,
6.78, 6.79, 6.8, 6.81, 6.82, 6.83, 6.84, 6.85, 6.86, 6.87, 6.88,
6.89, 6.9, 6.91, 6.92, 6.93, 6.94, 6.95, 6.96, 6.97, 6.98, 6.99,
7), Trait1Percentiles = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03,
0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.06, 0.06, 0.06, 0.06,
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
0.06, 0.06, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.13, 0.13, 0.13,
0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.16, 0.19, 0.19, 0.19, 0.19,
0.25, 0.28, 0.35, 0.38, 0.41, 0.5, 0.5, 0.5, 0.5, 0.54, 0.57,
0.57, 0.6, 0.6, 0.66, 0.66, 0.69, 0.69, 0.85, 0.85, 0.91, 1.01,
1.04, 1.07, 1.1, 1.13, 1.2, 1.23, 1.32, 1.42, 1.48, 1.48, 1.67,
1.73, 1.89, 1.98, 2.14, 2.14, 2.14, 2.33, 2.33, 2.52, 2.52, 2.77,
2.77, 3.12, 3.34, 3.46, 3.75, 3.97, 4.16, 4.57, 4.82, 5.1, 5.26,
5.45, 5.61, 5.73, 6.14, 6.36, 6.65, 7.09, 7.43, 7.43, 8.31, 8.31,
9.01, 9.01, 9.51, 9.51, 10.65, 11.15, 11.69, 12.03, 12.6, 13.39,
14.08, 14.61, 14.96, 15.59, 16.5, 17.23, 18.02, 18.8, 19.78,
20.79, 21.57, 22.33, 22.93, 22.93, 25.01, 25.92, 26.9, 26.9,
28.79, 29.83, 31.28, 32.35, 33.45, 34.43, 35.43, 36.91, 37.95,
39.31, 40.88, 42.05, 43.15, 44.22, 45.61, 46.87, 48.22, 49.23,
50.77, 52.03, 52.03, 54.46, 55.81, 56.94, 56.94, 59.37, 60.66,
61.95, 61.95, 64.28, 65.48, 66.96, 68, 68.79, 69.73, 70.55, 71.43,
72.44, 73.48, 74.3, 74.99, 75.81, 76.76, 77.73, 78.46, 78.46,
80.16, 80.79, 81.64, 81.64, 83.28, 83.97, 84.63, 84.63, 85.76,
86.27, 86.99, 87.46, 87.91, 88.35, 88.69, 88.91, 89.32, 89.73,
90.11, 90.43, 90.8, 91.24, 91.5, 91.69, 91.69, 92.44, 92.72,
93.2, 93.2, 93.76, 93.92, 94.17, 94.17, 94.52, 94.83, 95.21,
95.46, 95.72, 95.87, 95.94, 96.19, 96.35, 96.6, 96.85, 96.98,
97.1, 97.1, 97.2, 97.32, 97.32, 97.54, 97.64, 97.76, 97.76, 97.92,
97.98, 98.05, 98.05, 98.24, 98.3, 98.36, 98.39, 98.55, 98.58,
98.68, 98.77, 98.83, 98.87, 98.87, 98.99, 99.06, 99.06, 99.06,
99.06, 99.06, 99.06, 99.15, 99.15, 99.15, 99.28, 99.28, 99.31,
99.31, 99.31, 99.5, 99.53, 99.59, 99.62, 99.62, 99.65, 99.65,
99.65, 99.65, 99.69, 99.69, 99.72, 99.78, 99.78, 99.81, 99.81,
99.81, 99.81, 99.81, 99.81, 99.81, 99.81, 99.81, 99.81, 99.81,
99.81, 99.81, 99.81, 99.81, 99.81, 99.81, 99.81, 99.81, 99.87,
99.87, 99.91, 99.91, 99.94, 99.94, 99.97, 99.97, 99.97, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100), Trait2Percentiles = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.03, 0.03, 0.03,
0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.06, 0.06, 0.06, 0.09, 0.09,
0.09, 0.09, 0.13, 0.13, 0.13, 0.13, 0.19, 0.19, 0.19, 0.22, 0.25,
0.28, 0.31, 0.31, 0.31, 0.41, 0.41, 0.41, 0.41, 0.41, 0.41, 0.57,
0.6, 0.63, 0.69, 0.76, 0.82, 0.82, 0.91, 0.98, 0.98, 1.13, 1.23,
1.23, 1.35, 1.51, 1.54, 1.57, 1.67, 1.67, 1.89, 1.89, 2.08, 2.08,
2.55, 2.55, 2.93, 3.02, 3.15, 3.34, 3.53, 3.94, 4.19, 4.6, 4.85,
5.04, 5.35, 5.57, 6.02, 6.27, 6.65, 7.02, 7.56, 8.06, 8.06, 8.85,
8.85, 9.7, 9.7, 10.55, 10.55, 11.62, 12.38, 12.98, 13.64, 14.3,
14.93, 15.62, 16.35, 17.35, 18.11, 19.15, 20.19, 21.39, 22.52,
23.5, 24.85, 25.61, 26.68, 26.68, 29.13, 29.13, 31.78, 31.78,
34.2, 34.2, 36.91, 38.33, 39.53, 41.35, 42.93, 44.72, 46.52,
48, 49.57, 50.96, 52.41, 54.27, 55.69, 57.64, 58.96, 60.85, 62.2,
64, 65.48, 65.48, 68.31, 69.86, 71.21, 71.21, 73.73, 75.02, 76.54,
77.45, 78.46, 79.78, 80.63, 81.8, 82.8, 83.81, 84.91, 85.86,
86.68, 87.4, 88.16, 88.54, 89.04, 89.86, 90.52, 90.93, 90.93,
91.81, 92.16, 92.47, 92.47, 93.48, 93.8, 94.27, 94.27, 94.68,
95.02, 95.37, 95.59, 95.94, 96.13, 96.41, 96.66, 96.76, 96.79,
97.01, 97.13, 97.32, 97.45, 97.51, 97.57, 97.57, 97.57, 97.95,
98.05, 98.05, 98.24, 98.36, 98.39, 98.39, 98.61, 98.61, 98.74,
98.77, 98.87, 98.93, 98.99, 99.02, 99.12, 99.21, 99.31, 99.4,
99.43, 99.53, 99.59, 99.59, 99.59, 99.59, 99.62, 99.65, 99.65,
99.65, 99.75, 99.75, 99.75, 99.78, 99.81, 99.84, 99.87, 99.87,
99.87, 99.87, 99.87, 99.87, 99.91, 99.91, 99.91, 99.94, 99.94,
99.94, 99.94, 99.94, 99.94, 99.94, 99.97, 99.97, 99.97, 99.97,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100)), class = "data.frame", row.names = c(NA, -701L))
You can use match here :
df1$Trait1Percentile <- LookupTable$Trait1Percentiles[match(df1$Trait1Score, LookupTable$Scores)]
head(df1)
# JobNumber Trait1Score Trait2Score Trait1Percentile
#1 634 3.89 4.00 14.08
#2 21 4.39 2.94 68.00
#3 300 4.22 3.17 46.87
#4 797 4.21 3.83 45.61
#5 1112 3.94 4.22 17.23
#6 147 3.90 3.83 14.61
With merge you need to select relevant columns
merge(df1, LookupTable, by.x = 'Trait1Score', by.y = 'Scores')[1:4]
Similarly in dplyr :
library(dplyr)
inner_join(df1, LookupTable, by = c('Trait1Score' = 'Scores')) %>% select(1:4)
It is necessary to use the data presented here, for the sake of the problem.
I would like to match values from 2 dataframes. however some values are not "matched", and I cannot see why!
I will try to concisely explain my problem.
1) dataframe with theoretical values
#1.1) I have the following vector
Pos<-c(8.75, 9.3, 8.8, 9.6, 9.4, 11, NA, 13, 10.5, 12.31, 11.18, 13.06, 10.71, 12.5, 15.03, 15.26, 13.22, 15.25, 13.03, 15.28, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 9.2, NA, 9.6, NA, 10.93, NA, 11.19, NA, 10.86, 10.3, 9.4, 9.1, 9.1, 9.4, 9.7, 8.9, 9.86, 9.2, 9.2, NA, NA, NA, NA, NA, NA, NA, 10.9, NA, NA, 10.92, 10.69, 9.91, 10.01, NA, 10.66, NA, 10.38, NA, 11.4, 7.4, 7.3, 9, 9.6, NA, NA, 8, 9.3, NA, NA, 9.33, 9.9, 9.9, 11.2, 6.9, 7.3, 7, 8.7, 7.4, 8.6, 7.6, 9.24, 8.59, 8.6, 8.46, NA, 8.21, 9, 6.6, 8.5, 8.5, 10.2, 9.6, 9.55, NA, NA, 7.8, 9.6, NA, NA, 10.5, 11.4, 11.81, 9.7, NA, NA, 7.8, 8.9, NA, NA, NA, 12.29, NA, 11, NA, NA, NA, 11.11, NA, NA, 8.1, 8.1, 8.3, 10.2, NA, NA, 8.2, 11, NA, NA, NA, 8.7, NA, 8.9, NA, 11.3, NA, 12.2, NA, 12.5, 10.76, 14, 11.19, 15.4, NA, NA, 8.9, 10.9, NA, NA, 9.04, 9.74, 9.41, 9.43, 10.96, 10.93, 13.06, 10.31, 11.69, 8.66, 9.11, 8.87, 9.61, 8.99, 9.48, 9.58, 9.26, 9.29, 8.4, 8.5, 8.2, 8.3, 12.1, 8.7, 13.9, 8.8, 7.79, 10.45, 9.56, 9.66, 10.55, 11.76, 9.31, 12.36, 9.33, 10.71, 13.03, 12.36, 11.88, 11.94, 12.83, 13.51, 12.54, 14.29, 11.43, 11.19, 11.4, 9.9, 13.21, 11.1, 12.75, 12.03, 11.55, 10.3, 10.26, 10.31, 8.9, 8.8, 9.12, 10.35, 9.2, 9.3, 8.9, 7.7, 8.51, 8.2, 8.2, 8.54, 8.6, NA, 8, 8.5, 8.84, 8.22, 9.78, 7.8, 7.5, 7.7, 7.7, 9.68, 8.1, 8.21, 7.91, 8.11, 9.21, 9.01, 9.89, 8.2, 8.56, 10.19, 9.1, 9, 10.46, 8.7, 10.16, 8.9, 8.7, 9.6, 7.76, 7.76, 8.51, 10.26, 7.2, 11.71, 11.43, 11.24, 7.3, 9.13, 8.74, 8.81, 8.61, 8.63, 9.43, 8.93, 9.13, 9.33, 7.47, 7.21, 7.71, 8.28, 7.48, NA, 7.44, 8.81, 7.42, 7.25, 6.1, 8.74, 8.51, 6.7, 8.76, 6.2, 7.94, 8.51, 6.8, 13.03, 13.09, 12.9, 13.34, 13.07, 12.02, 12.94, 12, 12.61, 9.96, 8.79, 8.91, 9.2, 8.73, 8.61, 7.89, 8.17, 11.71, 8.99, 11.35, 10.36, 9.67, 8.86, 10.2, 11.17, 12.75, 12.49, 7.6, 9.62, 8.1, 9.93, 12.4, NA, NA, 8.3, 9.95, 7.4, 9.21, 9.34, 10.09, 7.9, 9.64, 7.6, 10.19, 12.65, 10.3, 10.3, 11, 11.66, 16, 11, 12.7, 11, 11.4, 11.49, 12.79, 16.65, NA, 11.75, 12.94, 13.3, 11.3, 9.86, 10.9, 12.08, 11, 9.99, 12.81, 12.36, NA, NA, 7.66, 6.5, 6.3, 6.4, 7, 7.1, 8.48, 6.8, 7.75, 12.97, 12.88, 12.49, 12.59, 12.83, 11.59, 8.9, 13.93, 13.35, 13.63, 14.64, 13.53, 13.64, 13.68, 13.38, 13.97, 12.98, 12.35, 12.89, 9.54, 9.3, 10.16, 10.71, 11.95, 12.03, 9.26, 10.15, 10.26, 6.7, 6.6, 7, 6.3, 7.76, 8.21, 7.7, 7.6, 13.49, 12.2, NA, 12.76, 12.78, 12.5, 13.57, 12.3, 12.84, 15.85, 11.26, 9.4, 11.16, 10.69, 11.43, 10.17, 10.51, 13.27, 11.39, 10.9, 10.54, NA, 10, 11.64, 10.6, 10.1, NA, 11.29, 7.61, 7.3, 7, 9.3, 13.33, 8.01, 8.16, 7.1, 9.91, 8.08, 11.33, 7.4, 10.39, 9, 11.5, 10.68, 8.53, 9.3, 11.19, 15.62, 11.02, 10.3, 9.7, 11.3, 10.5, 10.84, 13.86, 7.9, 7.6, 9.46, 7.9, 7.8, 9.33, 9.79, 7.7, 8.5, 8.3, 8.2, 8.1, 8.1, 10.2, 7.9, 8.3, 9.56, 9.34, 8.6, 9.6, 9.27, 8.1, 11.8, 9.74, 8.9, 8.3, 9.7, 7.6, 7.2, 9.21, 7.8, 7, 7.1, 8.1, 8.85, 9.4, 9.91, 9.44, 10.06, 8.6, 10.2, 10.55, NA, NA, 12.79, NA, NA, 9.75, 13.11, 14.54, NA, 14.36, 10.18, 14, 12.1, 15.26, NA, 10.99, 9.59, 10.9, 10.81, 9.3, 8.2, 8.75, 9.6, 8.9, 11.11, 11, 12, 10.9, 10.96, 8.99, 12.1, 11.76, 12.83, 11.1, 9.12, 8.54, 7.5, 9.01, 10.16, 11.71, 9.43, NA, 8.76, 13.07, 8.73, 8.86, 12.4, 7.9, 16, 11.75, 12.81, 7.1, 11.59, 13.38, 11.95, 7.76, 12.5, 11.43, 11.64, 13.33, 9, 9.7, 7.8, 10.2, 11.8, 7, 10.2, 14.54)
#1.2) Height, is the column to be filled
Pos.table<-data.frame(Pos=Pos,Height=NA)
2) dataframe with theoretical values
#2.1) the whole range of values that "Pos" can get
Source<- seq(0,17,0.01) #possible values that weight can get [0,17]
#2.2)height.0, the adjusted value of Height according to the Loop below
Table.match<- data.frame(Source=Source,Height.0=NA)
# loop for Source (real values)
for (i in 1:dim(Table.match)[1])
{
Table.match[i,"Height.0"] <- -57.5+5*(Table.match[i,"Source"])
}
2) Problem
The following Loop looks for respective matches.
for (i in 1:dim(Pos.table)[1])
{
H.i<-match(Pos.table[i,"Pos"], Table.match[,"Source"], nomatch = 0)
Pos.table[i,"Height"] <-ifelse(H.i,Table.match[H.i,"Height.0"],0)
# Rev.table[i,"Rev.Prot"]<-Rev.table[i,"Rev.Prot"]*Rev.table[i,"Yield"]
}
However, some values ares disregarded. for example, position 15 and 20 (among many others):
# both return NAs
match(15.03, Table.match[,"Source"])
match(15.28, Table.match[,"Source"])
Could you please advice me on how to overcome this problem?
I agree with Nicole that exact comparison between floating numbers should be avoided.
To solve that, I've just added a round() with 2 significant digits in the code:
for (i in 1:dim(Pos.table)[1])
{
H.i<-match(round(Pos.table[i,"Pos"],2), round(Table.match[,"Source"],2), nomatch = 0)
Pos.table[i,"Height"] <-ifelse(H.i,Table.match[H.i,"Height.0"],0)
# Rev.table[i,"Rev.Prot"]<-Rev.table[i,"Rev.Prot"]*Rev.table[i,"Yield"]
}
I guess this solves the problem.
Consider the dataset Kort:
structure(list(V1 = c(-0.03, 0.22, -0.11, -0.01, 0.25, 0.29,
-0.74, 0.23, 0.39, -0.04, 0.18, 0.19, 0.4, 0.21, 0.21, -0.01,
-0.05, 0.02, -0.12, 0.37, -0.07, 0.51, 0.39, 0.14, 0.02, 0.73,
-0.25, 0.44, 0.29), V2 = c(35.39, 34.33, 32.74, 34.72, 33.07,
30.9, 29.89, 31.17, 31.62, 33.13, 30.64, 33.31, 33.61, 34.16,
30.06, 30.06, 31.18, 25.57, 30.52, 32.43, 31.54, 29.6, 34.66,
31.74, 27.22, 41, 32.02, 37.96, 29.25), V3 = c(37.24, 36.77,
37.21, 41.16, 40.3, 42.16, 40.77, 39.59, 37, 38.32, 34.6, 38.1,
36.07, 39.2, 36.97, 38.28, 38.72, 46.81, 39.63, 36, 45.33, 38.72,
36.2, 40.94, 37.7, 42.44, 37.92, 39.87, 37.15), V4 = c(-36L,
-18L, -2L, 20L, 37L, 39L, -7L, 31L, -23L, 32L, 73L, 10L, 14L,
18L, 126L, 98L, 13L, 14L, 15L, 37L, 66L, 3L, -50L, 9L, 6L, -20L,
4L, -26L, -2L), V5 = c(12.4, 10.5, 2.8, 9.5, 9.4, 10.7, 7.5,
14.8, 10.9, 13.5, 11.5, 11.8, 13.6, 8.6, 13.6, 13.1, 14.3, 11.3,
16.1, 14.5, 8.4, 15.4, 13.4, 14, 18.8, 17.4, 16.4, 16, 17.7),
V6 = c(27424L, 25597L, 20968L, 24730L, 25423L, 25801L, 23681L,
29527L, 26228L, 28262L, 27363L, 27134L, 27542L, 24647L, 28260L,
27922L, 29054L, 25650L, 30096L, 29103L, 24112L, 30035L, 28771L,
27818L, 32455L, 29722L, 30508L, 29896L, 31961L), V7 = c(68.8,
70.4, 61.6, 73.5, 71.8, 76.5, 72.7, 75.3, 71.7, 75, 72.9,
73.3, 73.7, 69, 72.7, 74.2, 73.4, 71.2, 76.4, 73, 62.5, 76,
73.7, 74.7, 74.3, 74.8, 74.6, 74.4, 74.4), V8 = c(8.1, 6.8,
11, 5.3, 6.3, 4.1, 5.5, 4, 5.9, 4.3, 5.5, 5.4, 4.2, 8.1,
5.2, 4.8, 4.4, 8.2, 3.8, 5.9, 12.9, 4.3, 5.2, 5, 3.6, 3.8,
4.6, 4.3, 4.5), V9 = c(0.38, 0.15, 0.16, 0.08, 0.12, 0.05,
0.07, 0.04, 0.08, 0.07, 0.13, 0.08, 0.08, 0.26, 0.05, 0.14,
0.05, 0.26, 0.03, 0.18, 0.26, 0.04, 0.04, 0.14, 0.05, 0,
0.02, 0.02, 0.1), V10 = c(9.8, 9.9, 19.4, 7, 9.2, 3, 8.5,
1.1, 3, 2.3, 5.1, 5.6, 1, 22.3, 4.4, 6.2, 2.2, 5.3, 1.5,
5, 18.7, 1.5, 3, 8.9, 1.6, 0, 5.1, 2.1, 3.6), V11 = c(6.3,
7.5, 5.5, 10.2, 5, 9.6, 9.3, 4.8, 4.3, 4.6, 4.1, 5.7, 6.4,
4, 7.2, 4.7, 4.2, 4.5, 7.6, 5.3, 6.2, 4.1, 4.9, 4.1, 5.1,
3.3, 5.4, 5, 5.6), V12 = c(153605L, 152867L, 115972L, 140341L,
139245L, 167038L, 143239L, 179712L, 135273L, 167487L, 160738L,
160648L, 154717L, 118800L, 168954L, 148412L, 147637L, 142615L,
210838L, 161840L, 114310L, 182670L, 160293L, 147747L, 192889L,
191077L, 164107L, 202051L, 192945L)), .Names = c("V1", "V2",
"V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", "V12"
), class = "data.frame", row.names = c(NA, -29L))
Where the response is:
Kort$V12
[1] 153605 152867 115972 140341 139245 167038 143239 179712 135273 167487
[11] 160738 160648 154717 118800 168954 148412 147637 142615 210838 161840
[21] 114310 182670 160293 147747 192889 191077 164107 202051 192945
Doing a box-cox transform, using car::boxcox
boxcox(V12~.,data=Kort,lambda=seq(-4,4,4/10))
yields an optimal parameter of -2. Transforming the response using
car::bcPower
TVP<-bcPower(Kort$V12,lambda=-2)
turns TVP into a vector of constants:
TVP
[1] 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
[20] 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
but box cox transform should be a continuous map!
I don't think this is a bug, there's simply a limit to how many decimal places are printed out. The help file suggests that the calculation is (U^(lambda)-1)/lambda which is pretty close to 1/2 where U is large. You can see that TVP is being calculated correctly with
TVP-0.5
# [1] -2.119138e-11 -2.139650e-11 -3.717610e-11 ...
or
options(digits=20)
TVP
# [1] 0.49999999997880861802 0.49999999997860350431 0.49999999996282390446 ...