Site icon R-bloggers

Kernels for everyone!

[This article was first published on MeanMean, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
< svg style="display: none;">< defs id="MathJax_SVG_glyphs">< path stroke-width="1" id="MJMATHI-73" d="M131 289Q131 321 147 354T203 415T300 442Q362 442 390 415T419 355Q419 323 402 308T364 292Q351 292 340 300T328 326Q328 342 337 354T354 372T367 378Q368 378 368 379Q368 382 361 388T336 399T297 405Q249 405 227 379T204 326Q204 301 223 291T278 274T330 259Q396 230 396 163Q396 135 385 107T352 51T289 7T195 -10Q118 -10 86 19T53 87Q53 126 74 143T118 160Q133 160 146 151T160 120Q160 94 142 76T111 58Q109 57 108 57T107 55Q108 52 115 47T146 34T201 27Q237 27 263 38T301 66T318 97T323 122Q323 150 302 164T254 181T195 196T148 231Q131 256 131 289Z">< path stroke-width="1" id="MJMAIN-2C" d="M78 35T78 60T94 103T137 121Q165 121 187 96T210 8Q210 -27 201 -60T180 -117T154 -158T130 -185T117 -194Q113 -194 104 -185T95 -172Q95 -168 106 -156T131 -126T157 -76T173 -3V9L172 8Q170 7 167 6T161 3T152 1T140 0Q113 0 96 17Z">< path stroke-width="1" id="MJMAIN-30" d="M96 585Q152 666 249 666Q297 666 345 640T423 548Q460 465 460 320Q460 165 417 83Q397 41 362 16T301 -15T250 -22Q224 -22 198 -16T137 16T82 83Q39 165 39 320Q39 494 96 585ZM321 597Q291 629 250 629Q208 629 178 597Q153 571 145 525T137 333Q137 175 145 125T181 46Q209 16 250 16Q290 16 318 46Q347 76 354 130T362 333Q362 478 354 524T321 597Z">< path stroke-width="1" id="MJMAIN-2208" d="M84 250Q84 372 166 450T360 539Q361 539 377 539T419 540T469 540H568Q583 532 583 520Q583 511 570 501L466 500Q355 499 329 494Q280 482 242 458T183 409T147 354T129 306T124 272V270H568Q583 262 583 250T568 230H124V228Q124 207 134 177T167 112T231 48T328 7Q355 1 466 0H570Q583 -10 583 -20Q583 -32 568 -40H471Q464 -40 446 -40T417 -41Q262 -41 172 45Q84 127 84 250Z">< path stroke-width="1" id="MJMATHI-44" d="M287 628Q287 635 230 637Q207 637 200 638T193 647Q193 655 197 667T204 682Q206 683 403 683Q570 682 590 682T630 676Q702 659 752 597T803 431Q803 275 696 151T444 3L430 1L236 0H125H72Q48 0 41 2T33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM703 469Q703 507 692 537T666 584T629 613T590 629T555 636Q553 636 541 636T512 636T479 637H436Q392 637 386 627Q384 623 313 339T242 52Q242 48 253 48T330 47Q335 47 349 47T373 46Q499 46 581 128Q617 164 640 212T683 339T703 469Z">< path stroke-width="1" id="MJMAIN-28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z">< path stroke-width="1" id="MJMATHI-66" d="M118 -162Q120 -162 124 -164T135 -167T147 -168Q160 -168 171 -155T187 -126Q197 -99 221 27T267 267T289 382V385H242Q195 385 192 387Q188 390 188 397L195 425Q197 430 203 430T250 431Q298 431 298 432Q298 434 307 482T319 540Q356 705 465 705Q502 703 526 683T550 630Q550 594 529 578T487 561Q443 561 443 603Q443 622 454 636T478 657L487 662Q471 668 457 668Q445 668 434 658T419 630Q412 601 403 552T387 469T380 433Q380 431 435 431Q480 431 487 430T498 424Q499 420 496 407T491 391Q489 386 482 386T428 385H372L349 263Q301 15 282 -47Q255 -132 212 -173Q175 -205 139 -205Q107 -205 81 -186T55 -132Q55 -95 76 -78T118 -61Q162 -61 162 -103Q162 -122 151 -136T127 -157L118 -162Z">< path stroke-width="1" id="MJMAIN-2217" d="M229 286Q216 420 216 436Q216 454 240 464Q241 464 245 464T251 465Q263 464 273 456T283 436Q283 419 277 356T270 286L328 328Q384 369 389 372T399 375Q412 375 423 365T435 338Q435 325 425 315Q420 312 357 282T289 250L355 219L425 184Q434 175 434 161Q434 146 425 136T401 125Q393 125 383 131T328 171L270 213Q283 79 283 63Q283 53 276 44T250 35Q231 35 224 44T216 63Q216 80 222 143T229 213L171 171Q115 130 110 127Q106 124 100 124Q87 124 76 134T64 161Q64 166 64 169T67 175T72 181T81 188T94 195T113 204T138 215T170 230T210 250L74 315Q65 324 65 338Q65 353 74 363T98 374Q106 374 116 368T171 328L229 286Z">< path stroke-width="1" id="MJMATHI-67" d="M311 43Q296 30 267 15T206 0Q143 0 105 45T66 160Q66 265 143 353T314 442Q361 442 401 394L404 398Q406 401 409 404T418 412T431 419T447 422Q461 422 470 413T480 394Q480 379 423 152T363 -80Q345 -134 286 -169T151 -205Q10 -205 10 -137Q10 -111 28 -91T74 -71Q89 -71 102 -80T116 -111Q116 -121 114 -130T107 -144T99 -154T92 -162L90 -164H91Q101 -167 151 -167Q189 -167 211 -155Q234 -144 254 -122T282 -75Q288 -56 298 -13Q311 35 311 43ZM384 328L380 339Q377 350 375 354T369 368T359 382T346 393T328 402T306 405Q262 405 221 352Q191 313 171 233T151 117Q151 38 213 38Q269 38 323 108L331 118L384 328Z">< path stroke-width="1" id="MJMAIN-29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z">< path stroke-width="1" id="MJMAIN-3D" d="M56 347Q56 360 70 367H707Q722 359 722 347Q722 336 708 328L390 327H72Q56 332 56 347ZM56 153Q56 168 72 173H708Q722 163 722 153Q722 140 707 133H70Q56 140 56 153Z">< path stroke-width="1" id="MJSZ2-222B" d="M114 -798Q132 -824 165 -824H167Q195 -824 223 -764T275 -600T320 -391T362 -164Q365 -143 367 -133Q439 292 523 655T645 1127Q651 1145 655 1157T672 1201T699 1257T733 1306T777 1346T828 1360Q884 1360 912 1325T944 1245Q944 1220 932 1205T909 1186T887 1183Q866 1183 849 1198T832 1239Q832 1287 885 1296L882 1300Q879 1303 874 1307T866 1313Q851 1323 833 1323Q819 1323 807 1311T775 1255T736 1139T689 936T633 628Q574 293 510 -5T410 -437T355 -629Q278 -862 165 -862Q125 -862 92 -831T55 -746Q55 -711 74 -698T112 -685Q133 -685 150 -700T167 -741Q167 -789 114 -798Z">< path stroke-width="1" id="MJMAIN-2212" d="M84 237T84 250T98 270H679Q694 262 694 250T679 230H98Q84 237 84 250Z">< path stroke-width="1" id="MJMATHI-64" d="M366 683Q367 683 438 688T511 694Q523 694 523 686Q523 679 450 384T375 83T374 68Q374 26 402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487H491Q506 153 506 145Q506 140 503 129Q490 79 473 48T445 8T417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157Q33 205 53 255T101 341Q148 398 195 420T280 442Q336 442 364 400Q369 394 369 396Q370 400 396 505T424 616Q424 629 417 632T378 637H357Q351 643 351 645T353 664Q358 683 366 683ZM352 326Q329 405 277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q233 26 290 98L298 109L352 326Z">< path stroke-width="1" id="MJMAIN-2E" d="M78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60Z">< path stroke-width="1" id="MJMATHI-3D5" d="M409 688Q413 694 421 694H429H442Q448 688 448 686Q448 679 418 563Q411 535 404 504T392 458L388 442Q388 441 397 441T429 435T477 418Q521 397 550 357T579 260T548 151T471 65T374 11T279 -10H275L251 -105Q245 -128 238 -160Q230 -192 227 -198T215 -205H209Q189 -205 189 -198Q189 -193 211 -103L234 -11Q234 -10 226 -10Q221 -10 206 -8T161 6T107 36T62 89T43 171Q43 231 76 284T157 370T254 422T342 441Q347 441 348 445L378 567Q409 686 409 688ZM122 150Q122 116 134 91T167 53T203 35T237 27H244L337 404Q333 404 326 403T297 395T255 379T211 350T170 304Q152 276 137 237Q122 191 122 150ZM500 282Q500 320 484 347T444 385T405 400T381 404H378L332 217L284 29Q284 27 285 27Q293 27 317 33T357 47Q400 66 431 100T475 170T494 234T500 282Z">< path stroke-width="1" id="MJMATHI-3BC" d="M58 -216Q44 -216 34 -208T23 -186Q23 -176 96 116T173 414Q186 442 219 442Q231 441 239 435T249 423T251 413Q251 401 220 279T187 142Q185 131 185 107V99Q185 26 252 26Q261 26 270 27T287 31T302 38T315 45T327 55T338 65T348 77T356 88T365 100L372 110L408 253Q444 395 448 404Q461 431 491 431Q504 431 512 424T523 412T525 402L449 84Q448 79 448 68Q448 43 455 35T476 26Q485 27 496 35Q517 55 537 131Q543 151 547 152Q549 153 557 153H561Q580 153 580 144Q580 138 575 117T555 63T523 13Q510 0 491 -8Q483 -10 467 -10Q446 -10 429 -4T402 11T385 29T376 44T374 51L368 45Q362 39 350 30T324 12T288 -4T246 -11Q199 -11 153 12L129 -85Q108 -167 104 -180T92 -202Q76 -216 58 -216Z">< path stroke-width="1" id="MJMATHI-3C3" d="M184 -11Q116 -11 74 34T31 147Q31 247 104 333T274 430Q275 431 414 431H552Q553 430 555 429T559 427T562 425T565 422T567 420T569 416T570 412T571 407T572 401Q572 357 507 357Q500 357 490 357T476 358H416L421 348Q439 310 439 263Q439 153 359 71T184 -11ZM361 278Q361 358 276 358Q152 358 115 184Q114 180 114 178Q106 141 106 117Q106 67 131 47T188 26Q242 26 287 73Q316 103 334 153T356 233T361 278Z">< path stroke-width="1" id="MJMATHI-68" d="M137 683Q138 683 209 688T282 694Q294 694 294 685Q294 674 258 534Q220 386 220 383Q220 381 227 388Q288 442 357 442Q411 442 444 415T478 336Q478 285 440 178T402 50Q403 36 407 31T422 26Q450 26 474 56T513 138Q516 149 519 151T535 153Q555 153 555 145Q555 144 551 130Q535 71 500 33Q466 -10 419 -10H414Q367 -10 346 17T325 74Q325 90 361 192T398 345Q398 404 354 404H349Q266 404 205 306L198 293L164 158Q132 28 127 16Q114 -11 83 -11Q69 -11 59 -2T48 16Q48 30 121 320L195 616Q195 629 188 632T149 637H128Q122 643 122 645T124 664Q129 683 137 683Z">< path stroke-width="1" id="MJSZ2-2211" d="M60 948Q63 950 665 950H1267L1325 815Q1384 677 1388 669H1348L1341 683Q1320 724 1285 761Q1235 809 1174 838T1033 881T882 898T699 902H574H543H251L259 891Q722 258 724 252Q725 250 724 246Q721 243 460 -56L196 -356Q196 -357 407 -357Q459 -357 548 -357T676 -358Q812 -358 896 -353T1063 -332T1204 -283T1307 -196Q1328 -170 1348 -124H1388Q1388 -125 1381 -145T1356 -210T1325 -294L1267 -449L666 -450Q64 -450 61 -448Q55 -446 55 -439Q55 -437 57 -433L590 177Q590 178 557 222T452 366T322 544L56 909L55 924Q55 945 60 948Z">< path stroke-width="1" id="MJMATHI-53" d="M308 24Q367 24 416 76T466 197Q466 260 414 284Q308 311 278 321T236 341Q176 383 176 462Q176 523 208 573T273 648Q302 673 343 688T407 704H418H425Q521 704 564 640Q565 640 577 653T603 682T623 704Q624 704 627 704T632 705Q645 705 645 698T617 577T585 459T569 456Q549 456 549 465Q549 471 550 475Q550 478 551 494T553 520Q553 554 544 579T526 616T501 641Q465 662 419 662Q362 662 313 616T263 510Q263 480 278 458T319 427Q323 425 389 408T456 390Q490 379 522 342T554 242Q554 216 546 186Q541 164 528 137T492 78T426 18T332 -20Q320 -22 298 -22Q199 -22 144 33L134 44L106 13Q83 -14 78 -18T65 -22Q52 -22 52 -14Q52 -11 110 221Q112 227 130 227H143Q149 221 149 216Q149 214 148 207T144 186T142 153Q144 114 160 87T203 47T255 29T308 24Z">< path stroke-width="1" id="MJMATHI-77" d="M580 385Q580 406 599 424T641 443Q659 443 674 425T690 368Q690 339 671 253Q656 197 644 161T609 80T554 12T482 -11Q438 -11 404 5T355 48Q354 47 352 44Q311 -11 252 -11Q226 -11 202 -5T155 14T118 53T104 116Q104 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Q21 293 29 315T52 366T96 418T161 441Q204 441 227 416T250 358Q250 340 217 250T184 111Q184 65 205 46T258 26Q301 26 334 87L339 96V119Q339 122 339 128T340 136T341 143T342 152T345 165T348 182T354 206T362 238T373 281Q402 395 406 404Q419 431 449 431Q468 431 475 421T483 402Q483 389 454 274T422 142Q420 131 420 107V100Q420 85 423 71T442 42T487 26Q558 26 600 148Q609 171 620 213T632 273Q632 306 619 325T593 357T580 385Z">< path stroke-width="1" id="MJMATHI-78" d="M52 289Q59 331 106 386T222 442Q257 442 286 424T329 379Q371 442 430 442Q467 442 494 420T522 361Q522 332 508 314T481 292T458 288Q439 288 427 299T415 328Q415 374 465 391Q454 404 425 404Q412 404 406 402Q368 386 350 336Q290 115 290 78Q290 50 306 38T341 26Q378 26 414 59T463 140Q466 150 469 151T485 153H489Q504 153 504 145Q504 144 502 134Q486 77 440 33T333 -11Q263 -11 227 52Q186 -10 133 -10H127Q78 -10 57 16T35 71Q35 103 54 123T99 143Q142 143 142 101Q142 81 130 66T107 46T94 41L91 40Q91 39 97 36T113 29T132 26Q168 26 194 71Q203 87 217 139T245 247T261 313Q266 340 266 352Q266 380 251 392T217 404Q177 404 142 372T93 290Q91 281 88 280T72 278H58Q52 284 52 289Z">< path stroke-width="1" id="MJMATHI-71" d="M33 157Q33 258 109 349T280 441Q340 441 372 389Q373 390 377 395T388 406T404 418Q438 442 450 442Q454 442 457 439T460 434Q460 425 391 149Q320 -135 320 -139Q320 -147 365 -148H390Q396 -156 396 -157T393 -175Q389 -188 383 -194H370Q339 -192 262 -192Q234 -192 211 -192T174 -192T157 -193Q143 -193 143 -185Q143 -182 145 -170Q149 -154 152 -151T172 -148Q220 -148 230 -141Q238 -136 258 -53T279 32Q279 33 272 29Q224 -10 172 -10Q117 -10 75 30T33 157ZM352 326Q329 405 277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q233 26 290 98L298 109L352 326Z">< path stroke-width="1" id="MJMAIN-74" d="M27 422Q80 426 109 478T141 600V615H181V431H316V385H181V241Q182 116 182 100T189 68Q203 29 238 29Q282 29 292 100Q293 108 293 146V181H333V146V134Q333 57 291 17Q264 -10 221 -10Q187 -10 162 2T124 33T105 68T98 100Q97 107 97 248V385H18V422H27Z">< path stroke-width="1" id="MJMAIN-68" d="M41 46H55Q94 46 102 60V68Q102 77 102 91T102 124T102 167T103 217T103 272T103 329Q103 366 103 407T103 482T102 542T102 586T102 603Q99 622 88 628T43 637H25V660Q25 683 27 683L37 684Q47 685 66 686T103 688Q120 689 140 690T170 693T181 694H184V367Q244 442 328 442Q451 442 463 329Q464 322 464 190V104Q464 66 466 59T477 49Q498 46 526 46H542V0H534L510 1Q487 2 460 2T422 3Q319 3 310 0H302V46H318Q379 46 379 62Q380 64 380 200Q379 335 378 343Q372 371 358 385T334 402T308 404Q263 404 229 370Q202 343 195 315T187 232V168V108Q187 78 188 68T191 55T200 49Q221 46 249 46H265V0H257L234 1Q210 2 183 2T145 3Q42 3 33 0H25V46H41Z">< path stroke-width="1" id="MJMAIN-7B" d="M434 -231Q434 -244 428 -250H410Q281 -250 230 -184Q225 -177 222 -172T217 -161T213 -148T211 -133T210 -111T209 -84T209 -47T209 0Q209 21 209 53Q208 142 204 153Q203 154 203 155Q189 191 153 211T82 231Q71 231 68 234T65 250T68 266T82 269Q116 269 152 289T203 345Q208 356 208 377T209 529V579Q209 634 215 656T244 698Q270 724 324 740Q361 748 377 749Q379 749 390 749T408 750H428Q434 744 434 732Q434 719 431 716Q429 713 415 713Q362 710 332 689T296 647Q291 634 291 499V417Q291 370 288 353T271 314Q240 271 184 255L170 250L184 245Q202 239 220 230T262 196T290 137Q291 131 291 1Q291 -134 296 -147Q306 -174 339 -192T415 -213Q429 -213 431 -216Q434 -219 434 -231Z">< path stroke-width="1" id="MJMATHI-61" d="M33 157Q33 258 109 349T280 441Q331 441 370 392Q386 422 416 422Q429 422 439 414T449 394Q449 381 412 234T374 68Q374 43 381 35T402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487Q506 153 506 144Q506 138 501 117T481 63T449 13Q436 0 417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157ZM351 328Q351 334 346 350T323 385T277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q217 26 254 59T298 110Q300 114 325 217T351 328Z">< path stroke-width="1" id="MJMATHI-72" d="M21 287Q22 290 23 295T28 317T38 348T53 381T73 411T99 433T132 442Q161 442 183 430T214 408T225 388Q227 382 228 382T236 389Q284 441 347 441H350Q398 441 422 400Q430 381 430 363Q430 333 417 315T391 292T366 288Q346 288 334 299T322 328Q322 376 378 392Q356 405 342 405Q286 405 239 331Q229 315 224 298T190 165Q156 25 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 114 189T154 366Q154 405 128 405Q107 405 92 377T68 316T57 280Q55 278 41 278H27Q21 284 21 287Z">< path stroke-width="1" id="MJMATHI-6D" d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z">< path stroke-width="1" id="MJMAIN-3A" d="M78 370Q78 394 95 412T138 430Q162 430 180 414T199 371Q199 346 182 328T139 310T96 327T78 370ZM78 60Q78 84 95 102T138 120Q162 120 180 104T199 61Q199 36 182 18T139 0T96 17T78 60Z">< path stroke-width="1" id="MJMATHI-50" d="M287 628Q287 635 230 637Q206 637 199 638T192 648Q192 649 194 659Q200 679 203 681T397 683Q587 682 600 680Q664 669 707 631T751 530Q751 453 685 389Q616 321 507 303Q500 302 402 301H307L277 182Q247 66 247 59Q247 55 248 54T255 50T272 48T305 46H336Q342 37 342 35Q342 19 335 5Q330 0 319 0Q316 0 282 1T182 2Q120 2 87 2T51 1Q33 1 33 11Q33 13 36 25Q40 41 44 43T67 46Q94 46 127 49Q141 52 146 61Q149 65 218 339T287 628ZM645 554Q645 567 643 575T634 597T609 619T560 635Q553 636 480 637Q463 637 445 637T416 636T404 636Q391 635 386 627Q384 621 367 550T332 412T314 344Q314 342 395 342H407H430Q542 342 590 392Q617 419 631 471T645 554Z">< path stroke-width="1" id="MJMATHI-58" d="M42 0H40Q26 0 26 11Q26 15 29 27Q33 41 36 43T55 46Q141 49 190 98Q200 108 306 224T411 342Q302 620 297 625Q288 636 234 637H206Q200 643 200 645T202 664Q206 677 212 683H226Q260 681 347 681Q380 681 408 681T453 682T473 682Q490 682 490 671Q490 670 488 658Q484 643 481 640T465 637Q434 634 411 620L488 426L541 485Q646 598 646 610Q646 628 622 635Q617 635 609 637Q594 637 594 648Q594 650 596 664Q600 677 606 683H618Q619 683 643 683T697 681T738 680Q828 680 837 683H845Q852 676 852 672Q850 647 840 637H824Q790 636 763 628T722 611T698 593L687 584Q687 585 592 480L505 384Q505 383 536 304T601 142T638 56Q648 47 699 46Q734 46 734 37Q734 35 732 23Q728 7 725 4T711 1Q708 1 678 1T589 2Q528 2 496 2T461 1Q444 1 444 10Q444 11 446 25Q448 35 450 39T455 44T464 46T480 47T506 54Q523 62 523 64Q522 64 476 181L429 299Q241 95 236 84Q232 76 232 72Q232 53 261 47Q262 47 267 47T273 46Q276 46 277 46T280 45T283 42T284 35Q284 26 282 19Q279 6 276 4T261 1Q258 1 243 1T201 2T142 2Q64 2 42 0Z">< path stroke-width="1" id="MJMAIN-3C" d="M694 -11T694 -19T688 -33T678 -40Q671 -40 524 29T234 166L90 235Q83 240 83 250Q83 261 91 266Q664 540 678 540Q681 540 687 534T694 519T687 505Q686 504 417 376L151 250L417 124Q686 -4 687 -5Q694 -11 694 -19Z">< path stroke-width="1" id="MJMAIN-2264" d="M674 636Q682 636 688 630T694 615T687 601Q686 600 417 472L151 346L399 228Q687 92 691 87Q694 81 694 76Q694 58 676 56H670L382 192Q92 329 90 331Q83 336 83 348Q84 359 96 365Q104 369 382 500T665 634Q669 636 674 636ZM84 -118Q84 -108 99 -98H678Q694 -104 694 -118Q694 -130 679 -138H98Q84 -131 84 -118Z">< path stroke-width="1" id="MJMAIN-31" d="M213 578L200 573Q186 568 160 563T102 556H83V602H102Q149 604 189 617T245 641T273 663Q275 666 285 666Q294 666 302 660V361L303 61Q310 54 315 52T339 48T401 46H427V0H416Q395 3 257 3Q121 3 100 0H88V46H114Q136 46 152 46T177 47T193 50T201 52T207 57T213 61V578Z">< path stroke-width="1" id="MJMAIN-7D" d="M65 731Q65 745 68 747T88 750Q171 750 216 725T279 670Q288 649 289 635T291 501Q292 362 293 357Q306 312 345 291T417 269Q428 269 431 266T434 250T431 234T417 231Q380 231 345 210T298 157Q293 143 292 121T291 -28V-79Q291 -134 285 -156T256 -198Q202 -250 89 -250Q71 -250 68 -247T65 -230Q65 -224 65 -223T66 -218T69 -214T77 -213Q91 -213 108 -210T146 -200T183 -177T207 -139Q208 -134 209 3L210 139Q223 196 280 230Q315 247 330 250Q305 257 280 270Q225 304 212 352L210 362L209 498Q208 635 207 640Q195 680 154 696T77 713Q68 713 67 716T65 731Z">< path stroke-width="1" id="MJSZ2-7B" d="M547 -643L541 -649H528Q515 -649 503 -645Q324 -582 293 -466Q289 -449 289 -428T287 -200L286 42L284 53Q274 98 248 135T196 190T146 222L121 235Q119 239 119 250Q119 262 121 266T133 273Q262 336 284 449L286 460L287 701Q287 737 287 794Q288 949 292 963Q293 966 293 967Q325 1080 508 1148Q516 1150 527 1150H541L547 1144V1130Q547 1117 546 1115T536 1109Q480 1086 437 1046T381 950L379 940L378 699Q378 657 378 594Q377 452 374 438Q373 437 373 436Q350 348 243 282Q192 257 186 254L176 251L188 245Q211 236 234 223T287 189T340 135T373 65Q373 64 374 63Q377 49 378 -93Q378 -156 378 -198L379 -438L381 -449Q393 -504 436 -544T536 -608Q544 -611 545 -613T547 -629V-643Z">< path stroke-width="1" id="MJSZ2-7D" d="M119 1130Q119 1144 121 1147T135 1150H139Q151 1150 182 1138T252 1105T326 1046T373 964Q378 942 378 702Q378 469 379 462Q386 394 439 339Q482 296 535 272Q544 268 545 266T547 251Q547 241 547 238T542 231T531 227T510 217T477 194Q390 129 379 39Q378 32 378 -201Q378 -441 373 -463Q342 -580 165 -644Q152 -649 139 -649Q125 -649 122 -646T119 -629Q119 -622 119 -619T121 -614T124 -610T132 -607T143 -602Q195 -579 235 -539T285 -447Q286 -435 287 -199T289 51Q294 74 300 91T329 138T390 197Q412 213 436 226T475 244L489 250L472 258Q455 265 430 279T377 313T327 366T293 434Q289 451 289 472T287 699Q286 941 285 948Q279 978 262 1005T227 1048T184 1080T151 1100T129 1109L127 1110Q119 1113 119 1130Z">< path stroke-width="1" id="MJMAIN-5E" d="M112 560L249 694L257 686Q387 562 387 560L361 531Q359 532 303 581L250 627L195 580Q182 569 169 557T148 538L140 532Q138 530 125 546L112 560Z">< path stroke-width="1" id="MJSZ1-2211" d="M61 748Q64 750 489 750H913L954 640Q965 609 976 579T993 533T999 516H979L959 517Q936 579 886 621T777 682Q724 700 655 705T436 710H319Q183 710 183 709Q186 706 348 484T511 259Q517 250 513 244L490 216Q466 188 420 134T330 27L149 -187Q149 -188 362 -188Q388 -188 436 -188T506 -189Q679 -189 778 -162T936 -43Q946 -27 959 6H999L913 -249L489 -250Q65 -250 62 -248Q56 -246 56 -239Q56 -234 118 -161Q186 -81 245 -11L428 206Q428 207 242 462L57 717L56 728Q56 744 61 748Z">< path stroke-width="1" id="MJMAIN-32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z">< path stroke-width="1" id="MJMATHI-63" d="M34 159Q34 268 120 355T306 442Q362 442 394 418T427 355Q427 326 408 306T360 285Q341 285 330 295T319 325T330 359T352 380T366 386H367Q367 388 361 392T340 400T306 404Q276 404 249 390Q228 381 206 359Q162 315 142 235T121 119Q121 73 147 50Q169 26 205 26H209Q321 26 394 111Q403 121 406 121Q410 121 419 112T429 98T420 83T391 55T346 25T282 0T202 -11Q127 -11 81 37T34 159Z">< path stroke-width="1" id="MJMATHI-43" d="M50 252Q50 367 117 473T286 641T490 704Q580 704 633 653Q642 643 648 636T656 626L657 623Q660 623 684 649Q691 655 699 663T715 679T725 690L740 705H746Q760 705 760 698Q760 694 728 561Q692 422 692 421Q690 416 687 415T669 413H653Q647 419 647 422Q647 423 648 429T650 449T651 481Q651 552 619 605T510 659Q484 659 454 652T382 628T299 572T226 479Q194 422 175 346T156 222Q156 108 232 58Q280 24 350 24Q441 24 512 92T606 240Q610 253 612 255T628 257Q648 257 648 248Q648 243 647 239Q618 132 523 55T319 -22Q206 -22 128 53T50 252Z">< path stroke-width="1" id="MJAMS-49" d="M20 666Q20 676 31 683H358Q369 676 369 666Q369 648 331 648Q288 645 282 632Q278 626 278 341Q278 57 282 50Q286 42 295 40T331 35Q369 35 369 16Q369 6 358 -1H31Q20 4 20 16Q20 35 58 35Q84 37 93 39T107 50Q113 60 113 341Q113 623 107 632Q101 645 58 648Q20 648 20 666ZM249 35Q246 40 246 41T244 54T242 83T242 139V341Q242 632 244 639L249 648H140Q146 634 147 596T149 341Q149 124 148 86T140 35H249Z">< path stroke-width="1" id="MJSZ4-7B" d="M661 -1243L655 -1249H622L604 -1240Q503 -1190 434 -1107T348 -909Q346 -897 346 -499L345 -98L343 -82Q335 3 287 87T157 223Q146 232 145 236Q144 240 144 250Q144 265 145 268T157 278Q242 333 288 417T343 583L345 600L346 1001Q346 1398 348 1410Q379 1622 600 1739L622 1750H655L661 1744V1727V1721Q661 1712 661 1710T657 1705T648 1700T630 1690T602 1668Q589 1659 574 1643T531 1593T484 1508T459 1398Q458 1389 458 1001Q458 614 457 605Q441 435 301 316Q254 277 202 251L250 222Q260 216 301 185Q443 66 457 -104Q458 -113 458 -501Q458 -888 459 -897Q463 -944 478 -988T509 -1060T548 -1114T580 -1149T602 -1167Q620 -1183 634 -1192T653 -1202T659 -1207T661 -1220V-1226V-1243Z">< path stroke-width="1" id="MJSZ4-7D" d="M144 1727Q144 1743 146 1746T162 1750H167H183L203 1740Q274 1705 325 1658T403 1562T440 1478T456 1410Q458 1398 458 1001Q459 661 459 624T465 558Q470 526 480 496T502 441T529 395T559 356T588 325T615 301T637 284T654 273L660 269V266Q660 263 660 259T661 250V239Q661 236 661 234T660 232T656 229T649 224Q577 179 528 105T465 -57Q460 -86 460 -123T458 -499V-661Q458 -857 457 -893T447 -955Q425 -1048 359 -1120T203 -1239L183 -1249H168Q150 -1249 147 -1246T144 -1226Q144 -1213 145 -1210T153 -1202Q169 -1193 186 -1181T232 -1140T282 -1081T322 -1000T345 -897Q346 -888 346 -501Q346 -113 347 -104Q359 58 503 184Q554 226 603 250Q504 299 430 393T347 605Q346 614 346 1002Q346 1389 345 1398Q338 1493 288 1573T153 1703Q146 1707 145 1710T144 1727Z">

During my dissertation, I spent a lot of time working on spatial kernel estimates. Where spatial kernel estimates are defined as a convolution of a spatial suppport < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="9.034ex" height="2.509ex" style="vertical-align: -0.671ex;" viewBox="0 -791.3 3889.6 1080.4" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use x="469" y="0" xlink:href="#MJMAIN-2C">< g transform="translate(914,0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30">< use x="2115" y="0" xlink:href="#MJMAIN-2208">< use x="3061" y="0" xlink:href="#MJMATHI-44"> ,

< svg xmlns:xlink="http://www.w3.org/1999/xlink" width="32.164ex" height="5.676ex" style="vertical-align: -2.338ex;" viewBox="0 -1437.2 13848.4 2443.8" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMAIN-28">< use x="389" y="0" xlink:href="#MJMATHI-66">< use x="1162" y="0" xlink:href="#MJMAIN-2217">< use x="1884" y="0" xlink:href="#MJMATHI-67">< use x="2365" y="0" xlink:href="#MJMAIN-29">< use x="2754" y="0" xlink:href="#MJMAIN-28">< g transform="translate(3144,0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30">< use x="4067" y="0" xlink:href="#MJMAIN-29">< use x="4735" y="0" xlink:href="#MJMAIN-3D">< use x="5791" y="0" xlink:href="#MJSZ2-222B">< use x="6902" y="0" xlink:href="#MJMATHI-66">< use x="7453" y="0" xlink:href="#MJMAIN-28">< use x="7842" y="0" xlink:href="#MJMATHI-73">< use x="8312" y="0" xlink:href="#MJMAIN-29">< use x="8701" y="0" xlink:href="#MJMATHI-67">< use x="9182" y="0" xlink:href="#MJMAIN-28">< g transform="translate(9571,0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30">< use x="10717" y="0" xlink:href="#MJMAIN-2212">< use x="11717" y="0" xlink:href="#MJMATHI-73">< use x="12187" y="0" xlink:href="#MJMAIN-29">< use x="12576" y="0" xlink:href="#MJMATHI-64">< use x="13100" y="0" xlink:href="#MJMATHI-73">< use x="13569" y="0" xlink:href="#MJMAIN-2E">
A simple example of this estimate is a Gaussian filter or blur in more common parlance. In the Guassian filter, < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.116ex" height="2.009ex" style="vertical-align: -0.671ex;" viewBox="0 -576.1 480.5 865.1" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-67"> is the normal density function < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.385ex" height="2.509ex" style="vertical-align: -0.671ex;" viewBox="0 -791.3 596.5 1080.4" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-3D5">, with the location parameter < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="6.645ex" height="2.176ex" style="vertical-align: -0.838ex;" viewBox="0 -576.1 2861 936.9" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-3BC">< use x="881" y="0" xlink:href="#MJMAIN-3D">< g transform="translate(1937,0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30"> and scale parameter < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.33ex" height="1.676ex" style="vertical-align: -0.338ex;" viewBox="0 -576.1 572.5 721.6" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-3C3"> equal to the bandwidth < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="1.339ex" height="2.176ex" style="vertical-align: -0.338ex;" viewBox="0 -791.3 576.5 936.9" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-68">.

The kernelDensityEstimates package provides a means to apply these estimates to raster S4 objects from the


raster R package. Each function preserves NA values, and uses OpenMP for acceleration. However, all processing is done in memory, so there is no support for extremely large images.

The rasterKernelEstimates R package is currently not on CRAN, but will be after the documentation is cleaned up. Furthermore, the implementation of the kernels is currently non-optimal, and a fair bit of work can be done to speed them up a bit more. For those interested in helping out, the code is on my github page.

A short example of each function is provided below with some basic performance metrics. All timings are done on a Late 2013 MacBook Pro 13″ with a dual-core 2.4Ghz i5 running R 3.3.1 compiled under gcc 6.1.0.

rasterLocalSums

The function rasterLocalSums calculates a weighted sum of pixel values in a spatial neighborhood defined by the matrix W,

< svg xmlns:xlink="http://www.w3.org/1999/xlink" width="21.865ex" height="6.009ex" style="vertical-align: -3.505ex;" viewBox="0 -1078.4 9414 2587.3" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-66">< use x="550" y="0" xlink:href="#MJMAIN-28">< g transform="translate(940,0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30">< use x="1863" y="0" xlink:href="#MJMAIN-29">< use x="2530" y="0" xlink:href="#MJMAIN-3D">< g transform="translate(3586,0)">< use x="75" y="0" xlink:href="#MJSZ2-2211">< g transform="translate(0,-1117)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="469" y="0" xlink:href="#MJMAIN-2208">< g transform="translate(803,0)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-53">< use transform="scale(0.574)" x="755" y="-243" xlink:href="#MJMAIN-30">< use x="5349" y="0" xlink:href="#MJMATHI-77">< use x="6065" y="0" xlink:href="#MJMAIN-28">< use x="6455" y="0" xlink:href="#MJMATHI-73">< use x="6924" y="0" xlink:href="#MJMAIN-29">< use x="7314" y="0" xlink:href="#MJMATHI-78">< use x="7886" y="0" xlink:href="#MJMAIN-28">< use x="8276" y="0" xlink:href="#MJMATHI-73">< use x="8745" y="0" xlink:href="#MJMAIN-29">< use x="9135" y="0" xlink:href="#MJMAIN-2E">
This is similar to the raster library function focal when fun is not specified. In fact, the results are identical when padding is used in the focal call.

library(raster)
library(rasterKernelEstimates)

set.seed(100)
n <- 500

# create a raster object
r <- raster::raster( matrix(rnorm(n^2),n,n))

# create a weight matrix
W <- raster::focalWeight(r,c(1,0.04),type='Gauss')

# apply the weight with rasterKernelEstimates
run.time <- proc.time()
rLocalKDE1 <- rasterLocalSums(r,W)
print(proc.time() - run.time)

##    user  system elapsed 
##   0.975   0.004   0.325

# apply the weight with the raster packages focal
run.time <- proc.time()
rLocalKDE2 <- raster::focal(r,W,pad=TRUE)
print(proc.time() - run.time)

##    user  system elapsed 
##   0.667   0.008   0.678

# plot original image
plot(r)

# plot the smoothed image
plot(rLocalKDE1)

# print out the max abs difference
print(
sprintf(
  "The maximum absolute difference = %f.",
    max(abs(values(rLocalKDE1) - values(rLocalKDE2)
    ),na.rm = T))
)

## [1] "The maximum absolute difference = 0.000000."

Figure 1: Original image.
Figure 2: Smoothed.

rasterLocalQuantiles

The function rasterLocalQuantiles calculates the < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.865ex" height="2.843ex" style="vertical-align: -0.671ex;" viewBox="0 -934.9 1233.7 1223.9" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-71">< g transform="translate(464,362)">< use transform="scale(0.707)" xlink:href="#MJMAIN-74">< use transform="scale(0.707)" x="389" y="0" xlink:href="#MJMAIN-68"> quantile of pixel values in a spatial neighborhood defined by the matrix W;

< svg xmlns:xlink="http://www.w3.org/1999/xlink" width="53.501ex" height="4.843ex" style="vertical-align: -1.838ex;" viewBox="0 -1293.7 23035 2085" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-66">< use x="550" y="0" xlink:href="#MJMAIN-28">< g transform="translate(940,0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30">< use x="1863" y="0" xlink:href="#MJMAIN-29">< use x="2530" y="0" xlink:href="#MJMAIN-3D">< g transform="translate(3586,0)">< use xlink:href="#MJSZ2-7B">< use x="667" y="0" xlink:href="#MJMATHI-61">< use x="1197" y="0" xlink:href="#MJMATHI-72">< use x="1648" y="0" xlink:href="#MJMATHI-67">< use x="2129" y="0" xlink:href="#MJMATHI-6D">< use x="3007" y="0" xlink:href="#MJMATHI-61">< g transform="translate(3537,0)">< use x="0" y="0" xlink:href="#MJMATHI-78">< g transform="translate(572,-187)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-78">< use transform="scale(0.707)" x="572" y="0" xlink:href="#MJMAIN-28">< use transform="scale(0.707)" x="962" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="1431" y="0" xlink:href="#MJMAIN-29">< use transform="scale(0.707)" x="1821" y="0" xlink:href="#MJMAIN-3A">< use transform="scale(0.707)" x="2099" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="2569" y="0" xlink:href="#MJMAIN-2208">< g transform="translate(2288,0)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-53">< use transform="scale(0.574)" x="755" y="-243" xlink:href="#MJMAIN-30">< use x="7289" y="0" xlink:href="#MJMATHI-50">< use x="8041" y="0" xlink:href="#MJMATHI-72">< g transform="translate(8659,0)">< use x="0" y="0" xlink:href="#MJMAIN-28">< use x="389" y="0" xlink:href="#MJMATHI-58">< use x="1242" y="0" xlink:href="#MJMAIN-28">< use x="1631" y="0" xlink:href="#MJMATHI-73">< use x="2101" y="0" xlink:href="#MJMAIN-29">< use x="2768" y="0" xlink:href="#MJMAIN-3C">< use x="3824" y="0" xlink:href="#MJMATHI-78">< use x="4397" y="0" xlink:href="#MJMAIN-28">< use x="4786" y="0" xlink:href="#MJMATHI-73">< use x="5256" y="0" xlink:href="#MJMAIN-29">< use x="5645" y="0" xlink:href="#MJMAIN-29">< use x="14972" y="0" xlink:href="#MJMAIN-2264">< g transform="translate(15750,0)">< g transform="translate(397,0)">< rect stroke="none" width="450" x="0" y="220">< use x="580" y="714" xlink:href="#MJMATHI-71">< g transform="translate(60,-687)">< use xlink:href="#MJMAIN-31">< use x="500" y="0" xlink:href="#MJMAIN-30">< use x="1001" y="0" xlink:href="#MJMAIN-30">< use x="17890" y="0" xlink:href="#MJMAIN-2C">< use x="18335" y="-1" xlink:href="#MJSZ2-7D">< use x="22756" y="0" xlink:href="#MJMAIN-2C">
where < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="4.88ex" height="2.843ex" style="vertical-align: -0.838ex;" viewBox="0 -863.1 2101 1223.9" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-58">< use x="852" y="0" xlink:href="#MJMAIN-28">< use x="1242" y="0" xlink:href="#MJMATHI-73">< use x="1711" y="0" xlink:href="#MJMAIN-29"> is a random variable on the support of < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.479ex" height="2.509ex" style="vertical-align: -0.671ex;" viewBox="0 -791.3 1067.4 1080.4" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-53">< use transform="scale(0.707)" x="867" y="-213" xlink:href="#MJMAIN-30">, a spatial neighborhood about point < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.145ex" height="2.009ex" style="vertical-align: -0.671ex;" viewBox="0 -576.1 923.4 865.1" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30">.

This result should generally be reproducible using the raster library function focal when fun is specified as quantile. Because quantiles do not necessarily fall exactly on order statistics there is a need to map quantiles between a function of the order statistics. In the rasterLocalQuantiles function this mapping is done to the closest odd-valued-index order statistic. E.g. if the quantile falls between the first and second order statistic. This approach matches the inverse empirical cdf transform used by the type=1 transform in the stats::quantile function. Note that the quantile function applied to raster objects calculates the quantile from all pixels, not local quantiles.

As far as applications go, this function is particularly useful when working with data with a large number of outliers.

library(raster)
library(rasterKernelEstimates)


set.seed(100)
n <- 100

# create a raster object with extreme values
r <- raster::raster( matrix(rcauchy(n^2),n,n))

# create a weight matrix
W <- matrix(1,nrow=3,ncol=3)

# calculate local median
run.time <- proc.time()
rLocalKDE1 <- rasterLocalQuantiles(r,W,q=30)
print(proc.time() - run.time)

##    user  system elapsed
##   0.018   0.001   0.010

# calculate local median
run.time <- proc.time()
rLocalKDE2 <- raster::focal(
  r, W, pad=TRUE,
  fun=function(x) stats::quantile(x,probs=0.3,na.rm=T,type=1),
  padValue=NA)
print(proc.time() - run.time)

##    user  system elapsed
##   1.245   0.010   1.268

# plot boxplot of original image
boxplot(r)

# plot the smoothed image
plot(rLocalKDE1)

# print out the max abs difference
print(
sprintf(
  "The maximum absolute difference = %f.",
    max(abs(values(rLocalKDE1) - values(rLocalKDE2)
    ),na.rm = T))
)

## [1] "The maximum absolute difference = 0.000000."

Figure 3: Box plot of the Cauchy distributed values in the raster r.
Figure 4: Smoothed.

rasterLocalMoments

The function rasterLocalMoments calculates up to the second moment over the weighted matricies Wmu and Wvar,

< svg xmlns:xlink="http://www.w3.org/1999/xlink" width="26.087ex" height="7.176ex" style="vertical-align: -3.005ex;" viewBox="0 -1796 11231.7 3089.6" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-3BC">< use x="79" y="20" xlink:href="#MJMAIN-5E">< use x="603" y="0" xlink:href="#MJMAIN-28">< g transform="translate(993,0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30">< use x="1916" y="0" xlink:href="#MJMAIN-29">< use x="2583" y="0" xlink:href="#MJMAIN-3D">< g transform="translate(3362,0)">< g transform="translate(397,0)">< rect stroke="none" width="450" x="0" y="220">< g transform="translate(60,958)">< use x="0" y="0" xlink:href="#MJSZ1-2211">< g transform="translate(1056,-287)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="469" y="0" xlink:href="#MJMAIN-2208">< g transform="translate(803,0)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-53">< use transform="scale(0.574)" x="755" y="-243" xlink:href="#MJMAIN-30">< g transform="translate(2918,0)">< use x="0" y="0" xlink:href="#MJMATHI-77">< use transform="scale(0.707)" x="1013" y="-213" xlink:href="#MJMATHI-3BC">< use x="4162" y="0" xlink:href="#MJMAIN-28">< use x="4551" y="0" xlink:href="#MJMATHI-73">< use x="5021" y="0" xlink:href="#MJMAIN-29">< use x="5410" y="0" xlink:href="#MJMATHI-78">< use x="5983" y="0" xlink:href="#MJMAIN-28">< use x="6372" y="0" xlink:href="#MJMATHI-73">< use x="6842" y="0" xlink:href="#MJMAIN-29">< g transform="translate(970,-771)">< use x="0" y="0" xlink:href="#MJSZ1-2211">< g transform="translate(1056,-287)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="469" y="0" xlink:href="#MJMAIN-2208">< g transform="translate(803,0)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-53">< use transform="scale(0.574)" x="755" y="-243" xlink:href="#MJMAIN-30">< g transform="translate(2918,0)">< use x="0" y="0" xlink:href="#MJMATHI-77">< use transform="scale(0.707)" x="1013" y="-213" xlink:href="#MJMATHI-3BC">< use x="4162" y="0" xlink:href="#MJMAIN-28">< use x="4551" y="0" xlink:href="#MJMATHI-73">< use x="5021" y="0" xlink:href="#MJMAIN-29">
and
< svg xmlns:xlink="http://www.w3.org/1999/xlink" width="38.725ex" height="7.676ex" style="vertical-align: -3.005ex;" viewBox="0 -2011.3 16673.3 3304.9" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-3C3">< use transform="scale(0.707)" x="810" y="583" xlink:href="#MJMAIN-32">< use x="263" y="462" xlink:href="#MJMAIN-5E">< use x="1026" y="0" xlink:href="#MJMAIN-28">< g transform="translate(1416,0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30">< use x="2339" y="0" xlink:href="#MJMAIN-29">< use x="3006" y="0" xlink:href="#MJMAIN-3D">< g transform="translate(3785,0)">< g transform="translate(397,0)">< rect stroke="none" width="450" x="0" y="220">< g transform="translate(60,958)">< use x="0" y="0" xlink:href="#MJSZ1-2211">< g transform="translate(1056,-287)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="469" y="0" xlink:href="#MJMAIN-2208">< g transform="translate(803,0)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-53">< use transform="scale(0.574)" x="755" y="-243" xlink:href="#MJMAIN-30">< g transform="translate(2918,0)">< use x="0" y="0" xlink:href="#MJMATHI-77">< use transform="scale(0.707)" x="1013" y="-213" xlink:href="#MJMATHI-3C3">< use x="4140" y="0" xlink:href="#MJMAIN-28">< use x="4529" y="0" xlink:href="#MJMATHI-73">< use x="4999" y="0" xlink:href="#MJMAIN-29">< g transform="translate(5388,0)">< use x="0" y="0" xlink:href="#MJMAIN-28">< use x="389" y="0" xlink:href="#MJMATHI-78">< use x="962" y="0" xlink:href="#MJMAIN-28">< use x="1351" y="0" xlink:href="#MJMATHI-73">< use x="1821" y="0" xlink:href="#MJMAIN-29">< use x="2432" y="0" xlink:href="#MJMAIN-2212">< g transform="translate(3433,0)">< use x="0" y="0" xlink:href="#MJMATHI-3BC">< use x="79" y="20" xlink:href="#MJMAIN-5E">< use x="4036" y="0" xlink:href="#MJMAIN-28">< g transform="translate(4426,0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30">< use x="5349" y="0" xlink:href="#MJMAIN-29">< use x="5739" y="0" xlink:href="#MJMAIN-29">< use transform="scale(0.707)" x="8667" y="675" xlink:href="#MJMAIN-32">< g transform="translate(3351,-771)">< use x="0" y="0" xlink:href="#MJSZ1-2211">< g transform="translate(1056,-287)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="469" y="0" xlink:href="#MJMAIN-2208">< g transform="translate(803,0)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-53">< use transform="scale(0.574)" x="755" y="-243" xlink:href="#MJMAIN-30">< g transform="translate(2918,0)">< use x="0" y="0" xlink:href="#MJMATHI-77">< use transform="scale(0.707)" x="1013" y="-213" xlink:href="#MJMATHI-3C3">< use x="4140" y="0" xlink:href="#MJMAIN-28">< use x="4529" y="0" xlink:href="#MJMATHI-73">< use x="4999" y="0" xlink:href="#MJMAIN-29">< use x="16394" y="0" xlink:href="#MJMAIN-2E">
The difference between the mean call here and rasterLocalSums is the normalization of the weights. This ensures the weights sum to one.

library(raster)
library(rasterKernelEstimates)

set.seed(100)
n <- 200

# create a raster object of two populations
r <- raster::raster(
  cbind( matrix(rnorm(n^2),n,n), matrix(rnorm(n^2,2,2),n,n)) )


# create a weight matrix
W <- raster::focalWeight(r,c(1,0.04),type='Gauss')

# calculate the weighted local mean and variance
run.time <- proc.time()
rLocalKDE1 <- rasterLocalMoments(r,W)
print(proc.time() - run.time)

##    user  system elapsed 
##   0.343   0.002   0.102

# plot original image
plot(r)

# plot the weighted mean
plot(rLocalKDE1$mu)

# plot the weighted variance
plot(rLocalKDE1$var)

Figure 5: Original image.
Figure 6: Local weighted mean of the original image.
Figure 7: Local weighted variance of the original image.

rasterLocalCategorical Modes

The function rasterLocalCategoricalModes calculates a categorical mode from a raster image over the weighted spatial neighborhood devined by the matrix W,

< svg xmlns:xlink="http://www.w3.org/1999/xlink" width="42.577ex" height="7.843ex" style="vertical-align: -3.505ex;" viewBox="0 -1867.7 18331.8 3376.7" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-66">< use x="550" y="0" xlink:href="#MJMAIN-28">< g transform="translate(940,0)">< use x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="663" y="-213" xlink:href="#MJMAIN-30">< use x="1863" y="0" xlink:href="#MJMAIN-29">< use x="2530" y="0" xlink:href="#MJMAIN-3D">< g transform="translate(3586,0)">< use xlink:href="#MJSZ4-7B">< use x="806" y="0" xlink:href="#MJMATHI-63">< use x="1517" y="0" xlink:href="#MJMAIN-3A">< use x="2074" y="0" xlink:href="#MJMATHI-6D">< use x="2952" y="0" xlink:href="#MJMATHI-61">< g transform="translate(3482,0)">< use x="0" y="0" xlink:href="#MJMATHI-78">< g transform="translate(572,-155)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-63">< use transform="scale(0.707)" x="433" y="0" xlink:href="#MJMAIN-2208">< g transform="translate(778,0)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-43">< use transform="scale(0.574)" x="881" y="-243" xlink:href="#MJMAIN-30">< g transform="translate(5963,0)">< use xlink:href="#MJSZ4-7B">< g transform="translate(806,0)">< use x="75" y="0" xlink:href="#MJSZ2-2211">< g transform="translate(0,-1117)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="469" y="0" xlink:href="#MJMAIN-2208">< g transform="translate(803,0)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-53">< use transform="scale(0.574)" x="755" y="-243" xlink:href="#MJMAIN-30">< use x="2568" y="0" xlink:href="#MJMATHI-77">< use x="3285" y="0" xlink:href="#MJMAIN-28">< use x="3674" y="0" xlink:href="#MJMATHI-73">< use x="4144" y="0" xlink:href="#MJMAIN-29">< g transform="translate(4533,0)">< use x="0" y="0" xlink:href="#MJAMS-49">< g transform="translate(389,-187)">< use transform="scale(0.707)" x="0" y="0" xlink:href="#MJMATHI-78">< use transform="scale(0.707)" x="572" y="0" xlink:href="#MJMAIN-28">< use transform="scale(0.707)" x="962" y="0" xlink:href="#MJMATHI-73">< use transform="scale(0.707)" x="1431" y="0" xlink:href="#MJMAIN-29">< use transform="scale(0.707)" x="1821" y="0" xlink:href="#MJMAIN-3D">< use transform="scale(0.707)" x="2599" y="0" xlink:href="#MJMATHI-63">< use x="7168" y="-1" xlink:href="#MJSZ4-7D">< use x="13938" y="-1" xlink:href="#MJSZ4-7D">
where < svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.716ex" height="2.509ex" style="vertical-align: -0.671ex;" viewBox="0 -791.3 1169.4 1080.4" role="img" focusable="false">< g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)">< use x="0" y="0" xlink:href="#MJMATHI-43">< use transform="scale(0.707)" x="1011" y="-213" xlink:href="#MJMAIN-30"> is the set of unique categories in r.

library(raster)
library(rasterKernelEstimates)

set.seed(100)
n <- 100

# create a categorical raster
r <- raster::raster(
  matrix( sample(-4:4,size=n^2,replace=T),n,n)
  )


# create a weight matrix
W <- matrix(1,9,9)

# calculate the weighted local mean and variance
run.time <- proc.time()
rLocalKDE1 <- rasterLocalCategoricalModes(r,W)
print(proc.time() - run.time)

##    user  system elapsed 
##   0.030   0.000   0.011

# plot original image
plot(r)

# plot the weighted mean
plot(rLocalKDE1)

Figure 8: Original image.
Figure 9: Local weighted mode of the original image.

To leave a comment for the author, please follow the link and comment on their blog: MeanMean.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.