How to plot spatial data when grid cell size varies - r

I am trying to make a map of global model predictions with the outline of the world on it.
The data I have cover most of the worlds oceans and the grid cells have a resolution of 4 degrees. This is my desired output: Map of global predictions
This is the code I have tried using:
# Packages
library(rnaturalearth)
library(ggplot2)
library(sp)
library(ggspatial)
# Subset of data (500 rows, 4 columns; Longitude, Latitude, Prediction, Grid cell area)
data <- structure(list(Longitude = c(-10.16956752, 126.1689468, -61.99876686,
-94.00000289, 126.0007703, 170.000033, 82.55726146, -6.495772811,
-149.9999879, -61.99897757, -89.99998854, 2.000011459, 122.1211726,
-2.593090202, -17.99998854, -170.0001433, 121.8262874, 10.21256204,
166.0000115, 158.3111356, 134.3679695, -13.73648415, 170.0000423,
92.91243971, 5.996191823, 62.00001146, -53.99998854, -21.99998854,
125.9910976, 154.0000115, 149.9995576, -77.88731531, -45.99998854,
-33.99998854, -5.878402842, -157.9999885, -169.9929349, 66.00001146,
-138.5853402, -17.99998854, -69.99998854, -56.79830902, 110.0502773,
50.00001146, 39.56890984, -146.0004371, -5.999521688, 18.72192101,
-69.77517581, 21.3289268, 42.00001146, 162.0000115, 165.9995769,
-162.0365539, 170.0000138, 142.0000446, -45.99998854, -165.9999885,
110.0000115, -25.99998854, 6.000011459, -89.99998854, -53.99998854,
50.00187439, 146.0000115, -134.3123896, 165.9543502, -169.9999885,
-81.89972865, 72.42284406, 134.0000115, -170.0197871, -96.6556298,
17.92812, -152.9139565, -9.999988541, -73.99998854, 158.0000115,
54.20603343, 157.9995854, 74.00034763, -157.9999902, 102.4769658,
-73.99191424, 6.000011459, 162.0000177, -10.07971318, -93.99998854,
90.00001146, 153.9999475, 162.0000115, 117.9915582, -33.99998854,
38.00001146, -77.99998854, -81.99998854, 74.00022314, 114.1420482,
-173.9974719, 118.4796001, 178.0206217, 42.00001146, 178, -138.000174,
146.6226982, 165.999998, -49.73916727, -37.99998854, 137.9999465,
38.00001146, -82.04904949, 119.0280587, -169.9999946, 162.855343,
113.9999563, -61.9672714, 50.12191729, -173.9999885, 41.85382837,
130.0000115, 126.5154259, -37.93913378, 62.00001146, 129.9747567,
-161.9999885, -137.9999787, -1.958606869, 165.9999802, 150.1568838,
13.72012633, -37.59668977, 162.0000061, -29.99998854, -177.9999885,
117.9999058, -149.9999885, 114.0000063, 96.91839389, -29.99998854,
-25.99998854, -93.99998854, -29.99998854, 170.0000115, -173.9999885,
142.0105738, -37.9593374, 9.937003613, 145.9958965, -178.0133745,
-78.74777464, -122.0023764, -178.0000115, 162.0000115, -169.9999885,
-141.9999885, 158.0000115, -85.99998854, -15.44057437, 30.01655738,
-161.9999885, 35.50200234, 173.9999943, 5.999766664, 58.00001146,
-14.27262379, 53.59629681, 146.0999455, 53.97807051, 178.3714871,
174.0000115, -173.9999885, 46.05658123, 154.0000115, -150.0013408,
130.3202993, 5.888053059, 70.00001146, -137.9971843, -57.99998854,
-153.9999885, 38.51576452, 5.356846647, -177.9999885, 38.21244542,
22.06404301, -85.99990114, 165.9999621, 42.06833294, 82.38551366,
-69.26776454, -149.9999885, 105.7885139, -162.0000326, 17.01965058,
-74.66124649, -61.84333457, 154.0000125, 170.0162654, 1.824314695,
13.82919973, -166.2490329, 2.000011459, 58.00381545, 162.269909,
70.00001146, 126.2275842, -85.99069255, -161.9999885, -145.9998563,
-110.8963681, -45.6718642, 106.1072479, 150.0124763, 173.9999974,
129.7397288, -53.99998854, -174.0231047, 142.4679548, 6.084793515,
165.951299, 174.0000115, 58.00001146, 54.00001146, -78.41571925,
22.04138072, -87.07057574, -105.9999885, -109.9999885, -37.99998854,
9.440334846, -74.33053694, -65.97004746, -49.99998854, -73.95814747,
-74.74355347, 117.7688064, 78.00001146, 170.0004612, -126.0474866,
-10.14156626, 41.44639846, -65.99998854, 150.0007478, 138.1284291,
166.0000115, -177.9999885, 165.9998751, 122.5803857, -1.832865108,
-78.22084003, 145.9777733, 154.0040729, -57.99998854, -73.99998854,
-17.99998854, 97.66893601, -73.99998854, 58.00448, 158.0000123,
-161.9999831, 10.00001146, -70.11943583, 90.00001146, -93.90742733,
122.0381743, 90.02820747, 114.0000115, 178.0002266, 94.03546334,
-13.99998854, 2.026556417, 170.0816676, 170.0000105, 174.0000115,
-177.9999885, -157.9999885, 134.0000243, -74.01378632, 14.07846074,
4.926681381, 166.0000115, -178.0000102, -18.22132114, -41.76260639,
-165.9999885, -173.9999848, 117.8546849, 13.97253564, 138.0000115,
2.000011459, -57.86039422, -162.000012, -169.9967526, -60.85299577,
-37.53072648, -81.99781314, 138.0875793, 169.8994304, 107.4007518,
-73.99998854, 53.99801704, 46.00001146, 25.98144338, 66.00001146,
114.0000316, 110.0000115, 112.9024546, 177.9564699, -1.999988541,
-82.00000426, 162.0082822, -69.24184951, 101.9020192, -173.9995473,
-49.27150176, 21.92369036, 138.4587056, -66.29757086, -26.0078251,
174.0000131, 133.9889887, 38.16885146, -5.999988541, 133.9391315,
-61.99998854, 8.706431568, -49.8624759, 130.0106889, -13.99998854,
-177.9999885, -1.969664455, 113.1977475, 94.00376755, 82.00293912,
-61.49274336, 78.05701813, 109.8622452, 151.2237393, 154.0000115,
166.0000115, 13.64867252, -90.74983985, -54.5837941, 150.0593517,
-30.00737278, 10.14403385, 165.9999579, 141.99997, -71.49830857,
-57.99636593, 126.0072243, -21.94802182, -109.8907217, -9.999988541,
-61.96780638, -137.9996316, 106.0499776, 158.0000115, -65.08399512,
178, 65.83642764, 125.9426387, -157.9992733, 50.00001146, 38.06053256,
69.99999724, 129.6863334, -166.3675674, -11.16300072, 53.62409287,
137.7173457, 126.0031256, 118.9525785, 154.0010114, -162.0000077,
34.00001146, -5.999988541, -94.32614301, 25.43306958, 142.2242709,
37.21917664, 138.0000115, -49.99998854, -109.999023, 173.6014161,
40.58655549, 14.30990335, -65.58554854, -153.9096668, 121.8307417,
174.0000115, -74.05492837, -17.99998854, -96.82143996, 10.00001146,
165.989779, 54.00001146, 9.648264537, -153.9996769, -1.379131047,
-33.99967232, -13.99998854, -13.99998863, -29.99998854, -157.9993798,
130.3986089, -149.9540973, -66.05141375, -117.9995487, -69.99998854,
-10.08258389, 113.9349012, 50.88704952, -48.62667494, -49.99998854,
-114.0201716, -133.999895, -102.1160738, -174.0044266, 145.728285,
-21.99998854, 138.0034532, -93.99998854, 125.1209634, 133.9987429,
-141.9999885, 133.94514, -75.16942252, 13.30257806, -57.91461701,
58.00001146, 169.9995982, 38.92821472, 70.00001146, 12.76082336,
-77.99998854, -78.00228912, -81.799125, 130.0491847, 46.00091358,
154.117492, -82.0003813, 102.1876905, 50.11824049, 6.000011459,
-41.75455025, -57.99998854, -18.15791357, -53.94267298, -33.99998854,
146.0000115, 142.0000115, 58.78666498, -45.99998854, 174.0000115,
-153.999987, 102.0000115, 125.9624191, 157.9999468, 170.027281,
154.0000115, -57.99998854, -177.994726, 46.00001146, 153.9999931,
-9.999988541, -165.9999925, -89.9340042, -86.0160541, -25.8730291,
58.00001146, 118.0000115, -122.5807138, 46.00001146, 110.0000115,
121.829682, 46.59514055, -157.9999885, -153.9999885, 62.00001146,
-169.9997623, 126.103807, -10.52623514, 173.8637862, 130.0192184,
-18.02951352, -10.00000412, 121.9939471, 154.2592125, -69.35640587,
142.6960182, -153.9999943, -57.89841693, 70.00001146, 101.5055217,
170.0000115, 14.00001146, -86.04945349, 113.4322028, -158.0211136
), Latitude = c(3.820825091, 12.08126747, -55.97816596, 0.022599112,
16.02343874, 0.022652324, 8.210545822, 35.69956109, -7.977383247,
20.02440074, -3.977389136, 68.02261086, 0.21132112, 48.20584876,
56.02261086, -19.97848897, -15.77200491, 39.96397297, -55.97738914,
51.31485551, 42.7205338, 63.73313502, -11.97738132, 19.14158715,
0.020484685, -11.97738914, 56.02261086, 12.02261086, 28.03092594,
-15.97738914, 8.022784025, 31.90009355, 4.022610864, -19.97738914,
3.470593567, 52.02261086, -11.96980917, -19.97738914, 58.63570686,
60.02261086, 36.02261086, -36.03332986, 19.86853597, -7.977389136,
-5.079794602, -15.97762273, 72.02282038, 60.02451001, 20.43051724,
56.88863607, -27.97738914, -51.97738914, -51.98251101, 56.0994785,
12.02263258, 32.02264073, 48.02261086, -39.97738914, -23.97738914,
12.02261086, 72.02261086, 8.022610864, -39.97738914, -23.97920822,
52.02261086, 55.56880819, -15.99570073, 0.022610864, 16.10672797,
19.81627886, 40.02261086, 52.00303707, 27.1872715, -36.06367709,
59.08122925, 64.02261086, -31.97738914, 48.02261086, 15.52636519,
-11.9775079, -7.977528124, -7.97730525, 0.334377082, 24.03404499,
68.02261086, 8.022600292, 56.12108887, 24.02261086, 8.022610864,
8.022581727, -23.97738914, -7.848129941, -31.97738914, 72.02261086,
-51.97738914, -11.97738914, 4.022581755, -8.010086532, -19.9761759,
23.37409625, -35.91559755, -31.97738914, 60.02261086, -19.97753078,
-15.74140829, 20.02262634, 4.188582753, -27.97738914, 8.02189791,
76.02261086, -8.031710615, 38.78875047, 16.02260157, 55.55599452,
12.02268181, 48.082086, 28.08551543, 56.02261086, -24.05829182,
8.022610864, 8.196719853, -20.01786497, -15.97738914, -7.986975327,
-19.97738914, -15.97735921, 60.07070103, 8.022585242, -7.833875605,
39.59151568, -3.200384655, 12.02261958, -7.977389136, -31.97738914,
-11.97749469, 56.02261086, 8.022591881, 15.30078904, 4.022610864,
60.02261086, 4.022610864, 64.02261086, -55.97738914, 0.022610864,
56.02527798, 63.87882091, -15.94729501, -11.98168222, 63.80828291,
4.17958317, 32.02014727, -23.97741025, -27.97738914, 60.02261086,
-11.97738914, -15.97738914, 0.022610864, 25.12307726, 35.30667652,
52.02261086, -20.53611794, -3.977401906, 56.02228724, 0.022610864,
28.3079482, 25.16859735, 44.05525808, -19.96113513, -40.28569378,
-15.97738914, -23.97738914, -0.037988625, 16.02261086, -15.96585368,
39.81030641, 64.19614498, 12.02261086, -7.973236555, 68.02261086,
16.02261086, 20.02052366, 53.42403026, -47.97738914, 68.94567437,
-36.10729177, 4.022489321, 12.02268765, -4.044358874, 15.51366591,
-52.17439793, -19.97738914, -8.252118386, 4.022523768, -32.17125459,
-52.30061977, 68.25610808, 24.02260363, 59.26701564, 37.27072488,
-28.09811868, 68.08853997, 64.02261086, 16.01865467, 60.12532688,
0.022610864, -33.20386499, 16.93217855, -23.97738914, -23.97739795,
27.03071622, -24.85901806, 0.086841557, 43.99971799, 0.022528258,
31.83167869, 8.022610864, 63.3986452, -2.559698337, 42.73623655,
-48.0292721, -31.97738914, -3.977389136, -27.97738914, 8.131263575,
33.25599506, 11.25544918, 16.02261086, 16.02261086, -31.97738914,
43.07891697, -35.77986134, 12.2126783, -35.97738914, 36.00374653,
-39.94361536, -4.13230838, 4.022610864, 56.02295289, 40.00081489,
44.13941424, 16.06104742, 24.02261086, 0.026048965, 43.88233783,
-31.97738914, -35.97738914, 0.02269025, 15.93016831, 36.33475991,
64.08957191, 58.69065802, -11.9821079, -43.97738914, -19.97738914,
8.022610864, 7.886221494, 28.02261086, -19.97425468, 4.022609044,
8.022632834, 76.02261086, -56.35045724, 12.02261086, 27.76247933,
20.11429594, 19.94635242, -11.97738914, 0.022800625, 12.01506492,
52.02261086, 3.990144293, -48.119873, -23.97739031, -47.97738914,
-7.977389136, -15.97738914, 4.02260161, -55.9950546, 55.17829816,
59.78172479, -39.97738914, -27.97709912, 19.91987466, 59.82242042,
-7.977389136, 28.0226185, -19.22686648, 35.75467029, 28.02261086,
60.02261086, 56.24693609, 0.022615635, -15.98239129, 56.91608985,
-16.00820732, 19.99353474, -11.93590796, -43.44797102, 17.39564775,
-27.97738914, -3.97660846, -27.97738914, 72.3771239, 20.02261086,
16.02260062, 8.022610864, -23.92922118, -15.87608439, 0.022610864,
4.02261266, -12.00190948, 43.02433176, 3.940700587, 52.02051602,
0.453746804, 35.80665248, 55.46846217, 60.44655603, 64.01596898,
-23.97739048, -8.021036568, 41.55396512, 0.022610864, -36.03540447,
-47.97738914, 0.18180871, -32.12616329, 28.02527243, 4.022610864,
60.02261086, 3.585780138, -28.19444665, 8.026970975, 4.01869224,
-40.6633516, 7.641169912, -4.189248799, -22.79978441, -35.97738914,
-35.97738914, 68.39801011, 20.72995388, 71.34060756, -39.94398235,
40.03156039, 56.27043197, -7.977302797, 0.022696615, -27.65147886,
12.01992286, -11.98018915, 68.03301665, 23.77432249, 60.02261086,
-44.00407701, -11.9781651, 7.869716893, 24.02261086, -44.63876721,
-43.97738914, 23.57520672, -0.00367899, -19.9766619, -27.97738914,
-20.03795661, -3.977378548, -11.69278291, 63.64955575, 29.33031037,
26.56177022, -36.35002015, 24.02124134, 3.824165017, 48.01687813,
-11.97742276, -31.97738914, 68.02261086, 14.9567938, 39.49419331,
52.14461318, 23.59920469, -39.97738914, 44.02261086, 20.02378979,
-39.82478245, 18.89622815, 33.19261424, 64.03632422, 20.04775234,
7.962805467, -11.97738914, 20.14118871, 36.02261086, 23.88593283,
-7.977389136, -24.0052275, 0.022610864, -4.304016102, -3.977239696,
56.33643088, 64.02229033, 36.02261086, 56.0226104, 36.02261086,
4.02276095, 35.97473352, 59.19366534, 43.43616188, 28.02104594,
28.02261086, 35.94314023, -19.90137354, 7.666766431, -28.51031591,
52.02261086, 24.00874176, -23.97747135, 15.87276871, 60.02116361,
-8.698681751, 60.02261086, 32.01724937, 12.02261086, 39.03817517,
8.023956758, -27.97738914, 36.52433482, -47.86022133, -24.05345441,
51.29963345, 12.02261086, 8.0230886, -17.44438098, 4.022610864,
-11.64166378, -47.97738914, 28.04187363, 12.02387396, -32.82633622,
-11.97100486, -23.86540067, -31.97684268, 8.220758027, 12.50957099,
-7.977389136, -2.362973126, 64.02261086, 11.93486197, 52.02216857,
4.022610864, 56.02261086, -43.97738914, 19.46370663, -27.97738914,
-7.977389136, -19.97738511, -7.977389136, -4.012441622, -3.97733509,
-15.96734551, -19.97738662, 40.02261086, -15.9747231, -31.97738914,
4.022573507, 68.02261086, -11.97740656, 27.83155686, 27.99326446,
67.48803929, -23.97738914, 20.02261086, 35.66604315, -3.977389136,
-27.97738914, 11.9231427, -25.17266991, -11.97738914, -23.97738914,
-23.97738914, -3.977488904, -7.815115494, 32.26364968, -35.8672365,
4.044041471, 68.08721908, -39.97720998, -12.03217659, -32.12480347,
60.72642244, 58.57913449, -15.97736476, -40.14518463, 16.02261086,
-4.507175688, 52.02261086, -31.97738914, 7.959477669, 4.883317505,
19.98977949), y = c(224, 277, 48, 33, 224, 14, 372, 205, 10,
110, 24, 10, 234, 105, 14, 43, 358, 186, 5, 5, 48, 38, 33, 353,
153, 24, 10, 10, 305, 76, 38, 167, 167, 62, 238, 10, 52, 10,
14, 14, 10, 210, 372, 81, 205, 57, 10, 5, 110, 5, 57, 5, 19,
14, 33, 48, 10, 5, 43, 10, 5, 5, 176, 157, 10, 33, 62, 14, 153,
343, 29, 14, 181, 167, 14, 38, 76, 5, 262, 91, 24, 33, 381, 119,
24, 33, 91, 134, 52, 38, 10, 348, 5, 10, 52, 10, 124, 401, 48,
501, 62, 14, 5, 52, 296, 10, 181, 5, 29, 5, 176, 134, 24, 5,
129, 24, 253, 5, 167, 5, 277, 162, 19, 291, 29, 52, 76, 33, 195,
176, 176, 29, 5, 33, 205, 19, 396, 329, 5, 10, 14, 5, 5, 14,
10, 10, 195, 319, 5, 210, 134, 43, 5, 5, 24, 5, 10, 176, 167,
14, 238, 24, 76, 5, 200, 248, 43, 72, 62, 33, 38, 172, 10, 57,
86, 48, 172, 38, 5, 19, 195, 62, 14, 5, 186, 38, 33, 191, 367,
76, 19, 401, 14, 143, 62, 5, 5, 5, 181, 114, 5, 43, 234, 5, 5,
181, 143, 10, 38, 262, 253, 396, 10, 33, 310, 186, 5, 176, 176,
48, 52, 105, 5, 348, 167, 215, 33, 10, 5, 176, 76, 186, 277,
162, 72, 343, 114, 5, 62, 138, 205, 5, 129, 57, 14, 5, 29, 253,
186, 5, 5, 153, 134, 10, 238, 429, 24, 72, 38, 29, 5, 52, 52,
186, 462, 396, 5, 33, 257, 43, 234, 48, 62, 24, 14, 14, 38, 48,
38, 76, 14, 33, 253, 10, 10, 24, 386, 186, 5, 72, 10, 10, 52,
10, 153, 105, 343, 52, 343, 10, 105, 157, 14, 114, 124, 367,
377, 57, 5, 33, 100, 67, 381, 14, 181, 172, 5, 10, 29, 14, 305,
10, 5, 176, 95, 238, 277, 267, 10, 5, 238, 324, 200, 324, 162,
420, 377, 296, 134, 10, 33, 181, 5, 191, 33, 72, 38, 124, 76,
124, 353, 19, 267, 81, 153, 52, 415, 10, 138, 33, 362, 224, 38,
14, 238, 24, 358, 5, 181, 248, 191, 215, 401, 5, 33, 62, 5, 210,
157, 19, 195, 119, 19, 105, 62, 191, 186, 5, 57, 300, 33, 129,
5, 186, 215, 91, 10, 234, 29, 81, 10, 14, 38, 19, 29, 267, 19,
52, 114, 5, 176, 358, 195, 272, 10, 224, 48, 195, 5, 305, 5,
229, 124, 157, 43, 19, 257, 57, 95, 19, 5, 38, 224, 72, 200,
29, 181, 157, 181, 167, 300, 5, 420, 267, 5, 176, 5, 253, 19,
5, 5, 43, 253, 253, 24, 14, 86, 224, 119, 52, 277, 5, 62, 10,
29, 10, 33, 191, 195, 19, 29, 339, 134, 5, 33, 324, 172, 10,
19, 5, 33, 286, 186, 57, 181, 19, 5, 329, 267, 5, 5, 48, 181,
315, 257, 10, 81, 219, 424, 57), AreaKM2 = c(172379.8637, 167621.7823,
111118.7471, 196904.8, 189369.6807, 196896.989, 134153.5568,
71970.27812, 195050.3834, 185116.288, 196444.6159, 74544.74404,
135009.3945, 61775.80703, 111062.6969, 185126.4021, 164516.111,
124340.3488, 111191.639, 68700.83531, 43856.91162, 70108.90819,
192725.4358, 52368.79493, 195854.878, 192730.4345, 111062.6969,
192698.9311, 173434.9048, 189491.6891, 194962.3356, 155420.9957,
196434.0627, 185346.9753, 140635.0673, 122178.9993, 191978.3737,
185346.9753, 23658.60183, 99380.08521, 159993.6391, 42922.78337,
120817.5266, 195050.9669, 19360.96145, 189306.2305, 61499.9413,
56362.06723, 133811.001, 36208.14872, 174407.0449, 122301.2318,
121784.3494, 97667.61516, 192695.2501, 167570.6517, 132676.5593,
151724.8938, 180312.2014, 192698.9312, 61515.16271, 195029.8994,
151724.8937, 180138.6045, 122178.9993, 70469.59767, 182207.8668,
196906.2159, 178430.7004, 35127.21608, 151626.0232, 120088.3123,
34967.02013, 152173.9234, 21689.94623, 87187.46134, 167655.0629,
132676.5593, 137692.4358, 192687.7134, 195008.928, 195034.9023,
37709.23621, 177760.0238, 74544.74403, 195028.7223, 104452.2905,
180250.2648, 195029.8994, 194985.5096, 180312.2015, 168803.3062,
167655.0629, 61515.16273, 122301.2318, 192730.4345, 196367.3912,
154833.4069, 184773.8213, 103635.3051, 154987.4071, 167655.0629,
99379.51582, 185295.6034, 124572.0368, 185291.0797, 166948.1787,
174407.0449, 194937.1264, 48164.88123, 189140.5621, 28961.24096,
189447.4124, 50911.15111, 192688.7448, 114247.0282, 105516.4578,
111062.6969, 166115.7123, 195029.8994, 117024.6708, 179379.4406,
189491.6891, 189395.9156, 185346.9753, 189487.5792, 94441.42754,
195024.8608, 166013.9648, 105637.9472, 106848.1233, 192695.752,
195050.9669, 167655.0629, 192720.1173, 111062.6969, 195027.5475,
56141.14169, 196434.0627, 99380.08521, 196434.0627, 87187.46136,
111191.639, 196906.2158, 109043.8054, 81089.10227, 183386.8841,
192302.1075, 77938.94147, 118711.3761, 167366.3676, 180309.8601,
174407.0449, 99380.08521, 192730.4345, 189491.6891, 196906.2159,
24874.86628, 102407.9819, 122178.9993, 30881.52396, 196442.9135,
111042.0063, 196906.2159, 135534.2336, 66050.24256, 126682.0198,
182764.5526, 114190.7689, 189491.6891, 180312.2014, 190137.6583,
189449.8681, 187971.0012, 121449.9258, 78154.6362, 192698.9312,
194568.6369, 74544.74404, 189449.8681, 108316.5476, 24898.16725,
132791.5122, 28777.04168, 148918.0853, 196418.3023, 192682.0629,
188515.4215, 126131.307, 33183.60174, 185346.9753, 159613.3784,
196421.2717, 82004.78959, 65309.18163, 63338.00515, 180245.5915,
62627.78768, 57720.84009, 157413.4092, 59702.44117, 87187.46136,
189061.1605, 42292.06498, 196906.2159, 61262.26161, 102237.7457,
180312.2015, 180298.0587, 42771.2155, 92334.99481, 182042.8242,
140490.4741, 196664.533, 126823.1072, 195029.8994, 60074.40944,
49109.46533, 50418.56398, 128501.5426, 167655.0629, 196444.6159,
174407.0449, 114886.8676, 59714.23481, 60944.55308, 189449.8681,
189449.8681, 167655.0628, 48354.20928, 130013.1393, 173375.0026,
160083.8017, 156356.2922, 93367.9965, 155830.9163, 196434.0627,
111036.1316, 147924.4383, 130094.4442, 110740.2619, 180250.2648,
196459.0132, 128571.3777, 167655.0629, 160083.8017, 196877.4489,
110073.9451, 72839.48411, 66658.72684, 34400.54341, 191366.9356,
142614.132, 185346.9753, 195029.8994, 153000.4548, 174335.3955,
183445.8281, 196433.8595, 195027.1862, 48164.88123, 86628.41888,
192698.9312, 150234.408, 175199.9653, 177924.5096, 192730.4345,
196878.3235, 187250.6512, 122178.9993, 193078.476, 122110.0691,
180312.0705, 132791.5122, 195050.9669, 189491.6891, 196432.6449,
110092.6632, 47544.9055, 39401.69691, 151724.8938, 174367.861,
163267.6496, 84959.82215, 195050.9669, 174334.3813, 114385.5926,
136121.5477, 174335.3954, 99380.08521, 96012.29654, 196903.8799,
188981.9462, 26900.33901, 144806.3775, 181896.5244, 182719.3605,
61102.65255, 19741.08644, 174407.0449, 196186.7084, 174407.0449,
49160.44088, 185294.9958, 189447.4007, 195029.8994, 78249.88294,
175443.5234, 196906.2159, 196426.4033, 188983.876, 48447.89584,
72293.81391, 120400.3747, 75707.85928, 137471.9858, 60187.19617,
68395.23939, 86832.80395, 180312.0461, 189797.7904, 33511.74556,
196906.2159, 154302.1127, 132791.5122, 66767.84315, 151141.734,
172547.7844, 196434.0627, 99380.08521, 152897.0658, 99664.00085,
193282.3168, 196044.3122, 78806.56556, 145347.3239, 171144.0818,
31242.41669, 160083.8016, 160083.8017, 51851.21776, 74403.31886,
22031.14274, 147071.9195, 150545.6883, 61023.94095, 195042.1262,
196896.4658, 40242.42874, 192254.7086, 190394.1262, 70406.2492,
137426.2258, 99380.08519, 140062.7414, 192633.0614, 176633.5859,
180250.2648, 53869.02935, 142613.3149, 135530.3915, 168519.8947,
185169.7176, 174407.0449, 178691.312, 196443.0624, 148079.4063,
57246.18123, 27932.81263, 40415.31582, 114466.1176, 179818.8947,
93706.47211, 131532.3781, 192726.5206, 167655.0628, 74544.74404,
79470.40371, 67332.54758, 54594.64538, 86527.34867, 151724.8937,
142506.9822, 185109.6548, 87288.26795, 26623.47308, 62365.95905,
40654.42647, 174837.3988, 166235.3165, 192730.4345, 146116.5008,
159993.6391, 72847.57489, 195050.9669, 177538.7456, 196906.2159,
149249.3871, 196397.3296, 57847.69421, 87174.39279, 159993.6391,
111062.6658, 159993.6391, 196334.4073, 113564.9231, 50944.96715,
92400.13042, 174069.8016, 174335.3954, 152177.3195, 177458.1197,
99127.75301, 41868.22708, 122178.9993, 178391.3338, 180294.9067,
173326.9519, 99041.17658, 84287.92399, 99380.08519, 167074.1011,
192698.9312, 23275.03706, 194567.4548, 174407.0449, 110640.4665,
38304.51098, 116760.5063, 32312.56115, 192698.9312, 194912.5765,
30074.00305, 196434.0627, 64801.13889, 132791.5122, 172063.1196,
172932.0266, 95204.73836, 191443.2656, 167294.3229, 167603.8976,
168460.7176, 137621.5109, 195050.9669, 35523.49542, 87187.46136,
176019.8707, 118457.6403, 196434.0627, 111062.6969, 142614.132,
90241.70834, 174407.0449, 195050.9669, 185346.5708, 195050.9669,
186066.4133, 196434.7768, 186583.1961, 185346.692, 151626.0232,
188760.7471, 167655.0629, 196419.9902, 74544.74403, 192727.0388,
156139.2832, 171684.6684, 55093.68422, 180312.2015, 185294.9959,
103656.4268, 196444.6159, 174407.0449, 138519.227, 46366.84254,
192730.4345, 180312.2015, 180312.2015, 196414.8666, 165251.9959,
116955.1283, 132045.4789, 193991.0508, 71828.64499, 151651.5674,
186625.3351, 143211.3547, 43983.46389, 20833.59855, 189482.4773,
137525.1996, 189449.8681, 120134.4853, 122178.9993, 167655.0629,
188336.9525, 89348.85526, 180033.6182)), row.names = c(NA, -500L
), class = "data.frame")
# X and Y limits
x_min <- min(data$Longitude)
x_max <- max(data$Longitude)
y_min <- min(data$Latitude)
y_max <- max(data$Latitude)
# World map
world <- ne_countries(scale = "medium", returnclass = "sf")
# CRF
crf <- CRS(projargs = "+proj=longlat +datum=NAD83 +no_defs")
# Plot
ggplot() +
geom_raster(data = data, aes(x = Longitude, y = Latitude, fill = pred)) +
scale_fill_viridis_c(option = "plasma") +
geom_sf(data = world, fill = alpha("lightgrey", 1), color = "lightgrey") +
coord_sf(xlim=c(x_min, x_max), ylim=c(y_min, y_max), crs = crf, expand = FALSE)
I get this warning when I run the above code:
Error in matrix(NA_character_, nrow = nrow, ncol = ncol) :
invalid 'nrow' value (too large or NA)
In addition: Warning messages:
1: Raster pixels are placed at uneven horizontal intervals and will be shifted. Consider using geom_tile() instead.
2: Raster pixels are placed at uneven vertical intervals and will be shifted. Consider using geom_tile() instead.
3: In f(...) : NAs introduced by coercion to integer range
4: In f(...) : NAs introduced by coercion to integer range
I have tried using geom_tile / geom_stars and rounding my lat/lon values to no avail.
I think the issue might be arising due to the projection used which means the area of a grid cell decreases as you move away from the equator, hence I have also included grid cell area in the data (4th column).
Any help/suggestions would be greatly appreciated! Ideally without interpolating as these are already model predictions.
If you need the full dataset it can be found here: https://github.com/Rawizard/stack_overflow/blob/main/SO_question_data1.RDS

One approach to solve this would be to perform an interpolation on your data.
Here is an option based on the practical guide found here
Code
Starting by rounding the longitude and latitude with 0 digit. (unique() is to get rid of the duplicate data only 2 rows here)
library(dplyr)
df = data
df = df %>% dplyr::select(c(Longitude, Latitude, prediction))
df = df %>% mutate(across(c(Latitude, Longitude), round, digit = 0)) %>% unique()
Then creating a grid which will be used to interpolate the data:
df_template = expand.grid(Longitude= seq(min(df3$Longitude), max(df3$Longitude), 1), Latitude = seq(min(df3$Latitude), max(df3$Latitude), 1))
> head(df_template)
Longitude Latitude
1 -178 -83
2 -177 -83
3 -176 -83
4 -175 -83
5 -174 -83
6 -173 -83
Rasterizing the grid:
grd_template_raster <- df_template %>% dplyr::mutate(Z = 0) %>% raster::rasterFromXYZ(crs = crf)
Transformation of the data into simple feature:
sf_df <- st_as_sf(df, coords = c("Longitude", "Latitude"), crs = crf)
Model fitting in this case Nearest Neighbour
library(gstat)
fit_NN <- gstat::gstat( # using package {gstat}
formula = prediction ~ 1, # The column `NH4` is what we are interested in
data = as(sf_df, "Spatial"), # using {sf} and converting to {sp}, which is expected
nmax = 10, nmin = 5 # Number of neighboring observations used for the fit
)
Interpolate by running the model the data:
library(raster)
interp_NN = interpolate(grd_template_raster, fit_NN)
Extract the data to build the plot with ggplot:
df_final <- rasterToPoints(interp_NN) %>% as_tibble()
colnames(df_final) <- c("Longitude", "Latitude", "prediction")
Plotting the data:
ggplot() +
geom_raster(data = df_final, aes(x = Longitude, y = Latitude, fill = prediction)) +
scale_fill_viridis_c(option = "plasma") +
geom_sf(data = world, fill = alpha("lightgrey", 1), color = "lightgrey") +
coord_sf(xlim=c(x_min, x_max), ylim=c(y_min, y_max), crs = crf, expand = FALSE)

Related

How to make spiked rather than rectangular columns with ggplot

I'm using ggplot to display some data. The machine that generates it produces a plot that looks like this. You'll notice each individual blue peak is a spike, with a sharp top.
I've recreated this with ggplot, but is there a way to get geom_col, or another function, to produce a spike for each peak?
data <- data.frame(rpt = c(100.803333333333, 101.783333333333, 102.733333333333, 103.653333333333,
104.64, 105.566666666667, 106.526666666667, 107.46, 108.36, 109.33,
110.303333333333, 111.276666666667, 112.223333333333, 113.136666666667,
114.186666666667, 115.21, 116.2, 117.153333333333, 118.18, 119.17,
120.16, 121.153333333333, 122.163333333333, 123.086666666667,
124.046666666667, 125.003333333333, 125.956666666667, 126.946666666667,
127.866666666667, 128.82, 129.736666666667, 130.7, 131.633333333333,
132.6, 133.573333333333, 134.51, 135.453333333333, 136.43, 137.376666666667,
138.356666666667, 139.306666666667, 140.256666666667, 141.213333333333,
142.166666666667, 143.16, 144.086666666667, 145.046666666667,
146.01, 147.013333333333, 147.946666666667, 148.913333333333,
149.853333333333, 150.86, 151.803333333333, 152.746666666667,
153.693333333333, 154.673333333333, 155.66, 156.576666666667,
157.53, 158.486666666667, 159.443333333333, 160.403333333333,
161.403333333333, 162.33, 164.223333333333, 165.226666666667,
166.123333333333, 167.023333333333, 168.103333333333, 168.936666666667,
170.026666666667, 170.9, 171.85, 172.62, 173.61, 174.716666666667,
175.713333333333, 176.64, 177.57, 178.466666666667, 180.913333333333,
181.256666666667, 182.286666666667, 183.32, 184.283333333333,
185.246666666667, 186.136666666667, 187.146666666667, 188.16,
188.823333333333, 190.036666666667, 190.983333333333, 191.613333333333,
191.93, 192.323333333333, 192.84, 193.91, 194.866666666667, 195.706666666667,
199.523333333333, 200.563333333333, 201.326666666667, 202.293333333333,
203.346666666667, 204.893333333333, 206.943333333333, 208.223333333333,
208.846666666667, 209.68, 210.723333333333, 211.476666666667,
212.023333333333, 212.363333333333, 214.71, 216.576666666667,
216.97, 219.883333333333, 221.806666666667, 222.62, 223.526666666667,
224.436666666667, 225.35, 226.273333333333, 227.056666666667,
228.22, 229.013333333333, 232.076666666667, 238.52, 240.806666666667,
245.48, 248.49, 251.136666666667, 256.523333333333, 258.646666666667,
260.856666666667, 265.93, 268.88, 270.963333333333, 283.38, 285.2,
286.223333333333, 288.49, 294.926666666667),
height = c(119, 127, 132, 139, 136, 136, 140, 161, 162, 194, 239, 278,
370, 288, 434, 361, 286, 232, 213, 221, 238, 244, 266, 295, 306,
325, 358, 420, 497, 670, 838, 1104, 1451, 1743, 2018, 2170, 2226,
2058, 1777, 1464, 1158, 916, 702, 604, 540, 535, 554, 543, 517,
490, 434, 365, 322, 315, 312, 293, 272, 281, 279, 293, 297, 286,
253, 222, 170, 111, 69, 49, 29, 39, 33, 25, 24, 23, 16, 19, 18,
17, 24, 16, 19, 12, 20, 16, 19, 17, 19, 13, 16, 16, 17, 17, 16,
11, 19, 11, 18, 19, 17, 16, 14, 18, 14, 14, 11, 15, 10, 10, 11,
15, 11, 10, 10, 10, 11, 10, 10, 12, 10, 15, 14, 24, 27, 30, 22,
11, 12, 10, 10, 10, 10, 11, 10, 11, 10, 10, 12, 12, 15, 11, 10,
11, 10, 12))
ggplot(data, aes(x=rpt, y=height)) +
geom_col(position="identity")
Here's an approach where I add a zero value halfway in between each original value.
library(tidyverse)
data %>%
uncount(2, .id = "id") %>% # repeat each row
mutate(rpt = if_else(id == 2, (rpt + lead(rpt))/2, rpt), # space in between
height = if_else(id == 2, 0, height)) %>% # zero out every other row
ggplot(aes(x=rpt, y=height)) +
geom_line()

Plotly animation in R; frames are not in correct order, mix up in frames

The Data:
dput(LifeExpCH$Age)
c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
111)
> dput(LifeExpCH$Die)
c(380, 16, 11, 9, 9, 8, 7, 7, 7, 8, 7, 7, 8, 8, 8, 10, 11, 13,
16, 19, 20, 20, 21, 20, 19, 21, 20, 21, 23, 24, 25, 27, 30, 31,
35, 37, 41, 44, 48, 52, 57, 63, 70, 76, 84, 94, 104, 115, 129,
143, 159, 176, 195, 215, 237, 258, 283, 307, 334, 363, 392, 424,
458, 495, 534, 578, 624, 677, 734, 798, 869, 952, 1044, 1149,
1271, 1411, 1569, 1750, 1955, 2184, 2440, 2723, 3032, 3363, 3711,
4064, 4404, 4710, 4952, 5106, 5143, 5041, 4795, 4408, 3908, 3342,
2753, 2190, 1679, 1243, 888, 612, 406, 259, 158, 93, 51, 27,
13, 5, 2, 1)
I want to create a plotly animation in R. The animation is not working as intended. There is a mix up in the frames. Frames 100:109 are at the start. May I ask for some help, how to get the frames in the right order?
Here is the code:
library(plotly)
library(dplyr)
library(purrr)
LifeExpCH %>%
split(.$Age) %>% accumulate(~bind_rows(.x, .y)) %>%
set_names(0:111) %>%
bind_rows(.id = "frame") %>%
plot_ly(x = ~Age, y = ~Die) %>%
add_lines(frame = ~frame, showlegend = FALSE)

ggvis barchart using dates as x axis

I have been switching over from ggplot to ggvis when working with shiny apps. I have figured out a lot but am really stumped when it comes to bar graphs. I have a timeseries with dates and values. I simply want bars instead of points for each value (I would ideally like to be able to plot multiple semi-transparent bars if anyone has had success there please share) but here I wanted to get one bar working.
Works with layer_points()
df %>% ggvis(~date, ~x) %>% layer_points() %>% scale_datetime("x")
Doesnt work with layer_bars()
df %>% ggvis(~date, ~x) %>% layer_bars() %>% scale_datetime("x")
Data I am using...
structure(list(date = structure(c(7680, 7687, 7694, 7701, 7708,
7715, 7722, 7729, 7736, 7743, 7750, 7757, 7764, 7771, 7778, 7785,
7792, 7799, 7806, 7813, 7820, 7827, 7834, 7841, 7848, 7855, 7862,
7869, 7876, 7883, 7890, 7897, 7904, 7911, 7918, 7925, 7932, 7939,
7946, 7953, 7960, 7967, 7974, 7981, 7988, 7995, 8002, 8009, 8016,
8023, 8030, 8037, 8044, 8051, 8058, 8065, 8072, 8079, 8086, 8093,
8100, 8107, 8114, 8121, 8128, 8135, 8142, 8149, 8156, 8163, 8170,
8177, 8184, 8191, 8198, 8205, 8212, 8219, 8226, 8233, 8240, 8247,
8254, 8261, 8268, 8275, 8282, 8289, 8296, 8303, 8310, 8317, 8324,
8331, 8338, 8345, 8352, 8359, 8366, 8373, 8380, 8387, 8394, 8401,
8408, 8415, 8422, 8429, 8436, 8443, 8450, 8457, 8464, 8471, 8478,
8485, 8492, 8499, 8506, 8513, 8520, 8527, 8534, 8541, 8548, 8555,
8562, 8569, 8576, 8583, 8590, 8597, 8604, 8611, 8618, 8625, 8632,
8639, 8646, 8653, 8660, 8667, 8674, 8681, 8688, 8695, 8702, 8709,
8716, 8723, 8730, 8737, 8744, 8751, 8758, 8765, 8772, 8779, 8786,
8793, 8800, 8807, 8814, 8821, 8828, 8835, 8842, 8849, 8856, 8863,
8870, 8877, 8884, 8891, 8898, 8905, 8912, 8919, 8926, 8933, 8940,
8947, 8954, 8961, 8968, 8975, 8982, 8989, 8996, 9003, 9010, 9017,
9024, 9031, 9038, 9045, 9052, 9059, 9066, 9073), class = "Date"),
x = c(-0.034038302, 0.122310949, -0.002797319, 0.026515253,
0.039961798, 0.034473263, 0.00549937, -0.024125944, 0.000132490000000001,
0.011038357, -0.02135072, 0.030663311, -0.008915551, 0.004855042,
0.01563688, -0.007397493, 0.013569146, -0.004968811, -0.00250391,
0.014624532, 0.036937453, -0.023685917, 0.018921356, -0.003066779,
-0.009217771, 0.005317513, 0.010378968, 0.001580798, -0.015085972,
-0.000121644000000001, 0.020468644, 0.007925229, 0.007721276,
-0.003123545, -0.018317891, -0.014900591, 0.003260844, -0.001565358,
-0.014833886, 0.00366766, 0.014297139, -0.00725552, 0.012207931,
0.024035152, -0.024195095, -0.0043564, 0.000847468, 0.033031596,
0.023685033, 0.025143071, 0.046264348, 0.038285177, -0.009180356,
-0.01630399, -0.010131294, -0.009939386, -0.007620427, 0.013062259,
0.009912238, 0.000192973, -0.01683559, -0.002627549, 0.019836063,
-0.019946159, -0.020124331, 0.012921737, 0.034604405, -0.020774015,
0.00334805, 0.002271156, -0.018676732, 0.019160923, -0.01945997,
-0.014342636, -0.004867796, -0.010002446, -0.004372991, 0.023164369,
0.019824112, -0.00321832, -0.015785746, 0.040836652, 0.00148831,
0.012084485, -0.009603897, -0.004642148, -0.008399234, 0.010463218,
0.000256571000000001, -0.01978405, -0.003439498, -0.015669975,
0.026180724, 0.020373255, 0.019160773, 0.00692683, 0.010215506,
0.010861939, 0.012041143, 0.025734568, -0.004828156, 0.006914552,
-0.00720089, -0.000538489999999999, -0.008479448, 0.022926604,
0.002131842, -0.003688597, 0.025325639, -0.009562293, -0.024336741,
0.012907537, 0.004339383, 0.010744364, -0.013058765, -0.003672014,
-0.023887493, 0.01062259, 0.02088054, -0.035249878, -0.001462821,
0.01904368, -0.001308787, 0.009203217, 0.019856479, 0.011296979,
0.010039545, -0.01559142, 0.006083419, -0.017958978, -0.007488063,
0.01236649, -0.004459064, -0.004375386, 0.025500722, 0.005557851,
0.008444321, 0.002827649, 0.020320308, 0.031611803, -0.010199803,
-0.009425874, 0.007942729, -2.59379999999999e-05, 0.016669077,
-0.011666062, 0.022835386, -0.025599107, 0.013562535, -0.018365192,
0.018148786, 0.016649144, -0.009530455, 0.012996597, 0.002034778,
-0.005926478, -0.004897238, -0.004419719, 0.010848926, -0.006039757,
-0.030287605, 0.019221837, 0.001808161, -0.009566133, 0.005009292,
0.005365023, -0.004879922, -0.024637933, -0.0186584, 0.004786059,
-0.008245254, -0.000106243, -0.001714888, -0.017804006, -0.021200061,
0.003812757, 0.021940886, 0.002270448, -0.015417493, -0.045754612,
-0.003468442, -0.006242659, 0.022383824, -0.018753927, 0.008577571,
0.008655048, 0.02374636, 0.029522811, 0.009946946, 0.015419714,
-0.016714623, -0.014616188, 0.019670855, -0.038979063, 0.020491563,
-0.009640674, 0.046051144, -0.021434575, 0.000190443999999998,
-0.029013969), id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112,
113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124,
125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136,
137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148,
149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172,
173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184,
185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
197, 198, 199, 200)), .Names = c("date", "x", "id"), row.names = 53:252, class = "data.frame")
Set format df$date as character:
df$date <- as.character(df$date)
and then:
df %>% ggvis(~date, ~x) %>% layer_bars()

Getting the error "level sets of factors are different" when running a for loop

I have the following 3 tables:
AggData <- structure(list(Path = c("NonBrand", "Brand", "NonBrand,NonBrand",
"Brand,Brand", "NonBrand,NonBrand,NonBrand", "Brand,Brand,Brand",
"Brand,NonBrand", "NonBrand,Brand", "NonBrand,NonBrand,NonBrand,NonBrand",
"Brand,Brand,Brand,Brand", "NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"Brand,Brand,Brand,Brand,Brand", "Brand,Brand,NonBrand", "NonBrand,Brand,Brand",
"Brand,NonBrand,NonBrand", "NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"NonBrand,NonBrand,Brand", "Brand,NonBrand,Brand", "NonBrand,Brand,NonBrand",
"NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"Brand,Brand,Brand,Brand,Brand,Brand", "NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"NonBrand,Brand,Brand,Brand", "NonBrand,NonBrand,NonBrand,Brand",
"Brand,Brand,Brand,NonBrand", "Brand,Brand,Brand,Brand,Brand,Brand,Brand",
"Brand,NonBrand,NonBrand,NonBrand", "NonBrand,NonBrand,Brand,Brand",
"Brand,Brand,NonBrand,NonBrand", "Brand,NonBrand,Brand,Brand",
"NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"Brand,Brand,NonBrand,Brand", "NonBrand,Brand,NonBrand,NonBrand",
"Brand,Brand,Brand,Brand,Brand,Brand,Brand,Brand", "NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"NonBrand,NonBrand,Brand,NonBrand", "Brand,NonBrand,NonBrand,Brand",
"NonBrand,Brand,Brand,Brand,Brand", "NonBrand,NonBrand,NonBrand,NonBrand,Brand",
"Brand,NonBrand,Brand,NonBrand", "NonBrand,Brand,Brand,NonBrand",
"Brand,Brand,Brand,Brand,NonBrand", "Brand,NonBrand,NonBrand,NonBrand,NonBrand",
"Brand,Brand,Brand,Brand,Brand,Brand,Brand,Brand,Brand", "NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand",
"Brand,NonBrand,Brand,Brand,Brand", "NonBrand,Brand,NonBrand,Brand",
"Brand,Brand,Brand,NonBrand,Brand", "NonBrand,NonBrand,Brand,Brand,Brand",
"NonBrand,NonBrand,NonBrand,Brand,Brand", "Brand,Brand,NonBrand,Brand,Brand",
"Brand,Brand,Brand,NonBrand,NonBrand", "Brand,Brand,Brand,Brand,Brand,Brand,Brand,Brand,Brand,Brand",
"NonBrand,NonBrand,NonBrand,Brand,NonBrand", "Brand,Brand,NonBrand,NonBrand,NonBrand",
"NonBrand,Brand,Brand,Brand,Brand,Brand", "NonBrand,Brand,NonBrand,NonBrand,NonBrand",
"NonBrand,NonBrand,Brand,NonBrand,NonBrand", "NonBrand,NonBrand,NonBrand,NonBrand,NonBrand,Brand",
"Brand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand", "Brand,Brand,Brand,Brand,Brand,NonBrand",
"NonBrand,Brand,Brand,NonBrand,NonBrand", "Brand,NonBrand,NonBrand,Brand,Brand",
"NonBrand,NonBrand,NonBrand,NonBrand,Brand,Brand", "NonBrand,NonBrand,Brand,Brand,Brand,Brand",
"NonBrand,NonBrand,NonBrand,NonBrand,Brand,NonBrand", "NonBrand,NonBrand,Brand,NonBrand,Brand",
"Brand,NonBrand,NonBrand,Brand,NonBrand", "NonBrand,NonBrand,NonBrand,Brand,Brand,Brand",
"NonBrand,Brand,Brand,NonBrand,Brand", "Brand,NonBrand,NonBrand,NonBrand,NonBrand,Brand",
"Brand,Brand,NonBrand,NonBrand,NonBrand,NonBrand,NonBrand", "Brand,Brand,Brand,Brand,NonBrand,NonBrand,NonBrand"
), click_count = c(1799265, 874478, 198657, 128159, 45728, 30172,
20520, 17815, 16718, 9479, 6554, 3722, 3561, 3408, 3391, 3366,
3256, 2526, 1846, 1708, 1682, 1013, 951, 899, 881, 782, 780,
703, 642, 625, 615, 601, 453, 442, 414, 407, 362, 343, 313, 284,
281, 281, 271, 269, 268, 229, 223, 218, 215, 212, 204, 162, 161,
158, 155, 145, 132, 130, 115, 103, 102, 86, 77, 77, 72, 68, 68,
67, 58, 52, 32, 18, 18), conv_count = c(30938, 19652, 7401, 3803,
2014, 1072, 1084, 981, 652, 379, 230, 166, 205, 246, 254, 93,
239, 104, 112, 51, 76, 23, 66, 81, 55, 29, 62, 57, 50, 37, 17,
33, 38, 17, 8, 41, 33, 30, 24, 16, 26, 18, 16, 17, 7, 21, 10,
8, 27, 23, 11, 13, 6, 15, 14, 16, 8, 10, 6, 6, 11, 11, 8, 9,
8, 8, 9, 7, 7, 6, 6, 6, 7), CR = c(0.0171947989873643, 0.0224728352228415,
0.0372551684561833, 0.0296740767328085, 0.0440430370888733, 0.0355296301206417,
0.0528265107212476, 0.0550659556553466, 0.0389998803684651, 0.0399831205823399,
0.0350930729325603, 0.0445996775926921, 0.057568098848638, 0.0721830985915493,
0.0749041580654674, 0.0276292335115865, 0.0734029484029484, 0.0411718131433096,
0.0606717226435536, 0.0298594847775176, 0.0451843043995244, 0.0227048371174729,
0.0694006309148265, 0.0901001112347052, 0.0624290578887628, 0.0370843989769821,
0.0794871794871795, 0.0810810810810811, 0.0778816199376947, 0.0592,
0.0276422764227642, 0.0549084858569052, 0.0838852097130243, 0.0384615384615385,
0.0193236714975845, 0.100737100737101, 0.0911602209944751, 0.0874635568513119,
0.0766773162939297, 0.0563380281690141, 0.0925266903914591, 0.0640569395017794,
0.0590405904059041, 0.0631970260223048, 0.0261194029850746, 0.091703056768559,
0.0448430493273543, 0.036697247706422, 0.125581395348837, 0.108490566037736,
0.053921568627451, 0.0802469135802469, 0.0372670807453416, 0.0949367088607595,
0.0903225806451613, 0.110344827586207, 0.0606060606060606, 0.0769230769230769,
0.0521739130434783, 0.058252427184466, 0.107843137254902, 0.127906976744186,
0.103896103896104, 0.116883116883117, 0.111111111111111, 0.117647058823529,
0.132352941176471, 0.104477611940299, 0.120689655172414, 0.115384615384615,
0.1875, 0.333333333333333, 0.388888888888889)), .Names = c("Path",
"click_count", "conv_count", "CR"), row.names = c(NA, -73L), class = "data.frame")
another one here:
breakVector <- structure(list(breakVector = structure(c(1L, 1L), .Label = "NonBrand", class = "factor"),
CR = c(0.461541302855402, 0.538458697144598)), .Names = c("breakVector",
"CR"), row.names = c(NA, -2L), class = "data.frame")
and:
FinalTable <- structure(list(autribution_category = structure(c(2L, 1L), .Label = c("Brand",
"NonBrand"), class = "factor"), attributed_result = c(0, 0)), .Names = c("autribution_category",
"attributed_result"), row.names = 1:2, class = "data.frame")
when I run the following command:
if (FinalTable [2,1] == breakVector[1,1]) {
FinalTable$attributed_result[2] <- FinalTable$attributed_result[2] +
breakVector[1,2] * AggData$conv_count[3];
break}
I get the following error:
Error in Ops.factor(FinalTable[2, 1], breakVector[1, 1]) :
level sets of factors are different
This is pretty weird, since both values that im comparing are factors, I don't see any reason why R cant compare the two levels?
FinalTable[2,1] and breakVector[1,1] do not have the same levels:
> FinalTable[2,1]
[1] Brand
Levels: Brand NonBrand
> breakVector[1,1]
[1] NonBrand
Levels: NonBrand
This is easily fixed by using
breakVector[,1] <- factor(breakVector[,1], levels=c("Brand", "NonBrand"))
or, more generally
breakVector[,1] <- factor(breakVector[,1], levels=levels(FinalTable[,1]))
Perhaps, it will better compare both variables like a string:
if (as.character(FinalTable [2,1]) == as.character(breakVector[1,1])) {
FinalTable$attributed_result[2] <- FinalTable$attributed_result[2] +
breakVector[1,2] * AggData$conv_count[3];
break}

Create new dataset removing variables with high inflation factors

I have a dataset of environmental variables I would like to use for a GLMM. I am using the corvif function from the AED package (http://www.highstat.com/Book2/AED_1.0.zip) to identify and remove variables with high inflation factors.
Instead of removing one variable at a time manually from my dataset with a GVIF values > 3 (highest value removed first), I would like to know how to write a loop to accomplish this task automatically with the result being a new dataset with only the remaining variables (i.e. those with GVIF values < 3).
Any suggestions for how to approach this problem for a new R user?
Here is my sample data:
WW_Covs <- structure(list(Latitude = c(62.4419, 67.833333, 65.95, 63.72935,
60.966667, 60.266667, 55.660455, 62.216667, 61.3, 61.4, 62.084139,
55.662566, 64.48508, 63.208354, 62.87591, 62.70856, 62.64009,
63.79488, 59.55, 62.84206), BIO_02 = c(87, 82, 75, 70, 77, 70,
59, 84, 84, 79, 85, 60, 91, 87, 74, 74, 76, 70, 76, 74), BIO_03 = c(26,
23, 25, 26, 25, 24, 25, 25, 26, 25, 26, 26, 24, 25, 24, 25, 25,
25, 26, 24), BIO_04 = c(8443, 9219, 7594, 6939, 7928, 7593, 6160,
8317, 8167, 7972, 8323, 6170, 9489, 8578, 7814, 7680, 7904, 7149,
7445, 7803), BIO_05 = c(201, 169, 151, 166, 194, 210, 202, 205,
204, 186, 205, 200, 200, 195, 170, 154, 180, 166, 219, 170),
BIO_06 = c(-131, -183, -144, -102, -107, -75, -26, -119,
-113, -120, -120, -28, -169, -143, -131, -142, -124, -111,
-72, -129), BIO_08 = c(128, 109, 85, 78, 122, 145, 153, 134,
130, 126, 132, 152, 120, 119, 115, 98, 124, 104, 147, 115
), BIO_09 = c(-31, -81, -16, 13, -60, -6, 25, -25, -25, -70,
-25, 23, -56, -39, -47, -60, -39, 8, 0, -46), BIO_12 = c(667,
481, 760, 970, 645, 557, 645, 666, 652, 674, 670, 670, 568,
598, 650, 734, 620, 868, 571, 658), BIO_13 = c(78, 77, 96,
109, 85, 70, 67, 77, 84, 93, 78, 68, 72, 78, 93, 99, 90,
96, 72, 93), BIO_15 = c(23, 40, 25, 21, 36, 30, 21, 24, 28,
34, 24, 22, 28, 29, 34, 32, 36, 22, 30, 34), BIO_19 = c(147,
85, 180, 236, 108, 119, 154, 149, 135, 118, 148, 162, 117,
119, 120, 141, 111, 204, 111, 122)), .Names = c("Latitude",
"BIO_02", "BIO_03", "BIO_04", "BIO_05", "BIO_06", "BIO_08", "BIO_09",
"BIO_12", "BIO_13", "BIO_15", "BIO_19"), row.names = c(1:20), class = "data.frame")
Sample code:
library(AED)
WW_Final <- corvif(WW_Covs)
test <- corvif(WW_Covs])
test[order(-test$GVIF), ]
if(test$GVIF[1,] > 3, # this is where I get stuck...
Here is an algorithm for doing this. I illustrate with the built-in dataset longley, and I also use function vif in package car, rather than using package AED:
It's not pretty, and should be wrapped inside a function, but I leave that as an exercise for the interested reader.
The code:
library(car)
dat <- longley
cutoff <- 2
flag <- TRUE
while(flag){
fit <- lm(Employed ~ ., data=dat)
vfit <- vif(fit)
if(max(vfit) > cutoff){
dat <- dat[, -which.max(vfit)]
} else {
flag <- FALSE
}
}
print(fit)
print(vfit)
The output:
Call:
lm(formula = Employed ~ ., data = dat)
Coefficients:
(Intercept) Unemployed Armed.Forces
50.66281 0.02265 0.02847
Unemployed Armed.Forces
1.032501 1.032501

Resources