[R-sig-Geo] How to calculate the integral of the sum of two kernel density functions in R?

Ferra Xu ferra.xu at yahoo.com
Mon Apr 6 21:21:36 CEST 2015


I want to calculate the non-deterministic integral (0 to t) of the sum of two kernel density functions in R. Here is the code that I wrote: 


library(ks) library(rgl)
    zz <- " longitude   latitude    depth   time    magnitude    363.218744  52.38412991 3.04532258  9.241866766 0.838097648363.685488  53.85946071 0.25531946  0.578126952 0.854703175319.732091  32.23166292 4.2952681   1.06539397  0.781598317315.812817  36.98421462 3.27938697  2.295483245 0.769473951119.498255  75.1280934  0.83253364  8.783517513 0.969860223138.588107  169.629965  1.89874255  9.745653758 0.972609198139.158872  169.6452912 3.67211991  15.03850063 0.881092856139.247168  169.3478202 2.85800531  15.26106239 0.80957094565.1234 147.3603323 2.84478705  16.82280753 0.8795771565.55518    147.0377814 2.69845292  20.23640417 0.90054614467.954278   148.6017748 3.41456545  21.21715903 0.96836922766.968587   146.3484298 2.33379707  22.02860946 0.726200657359.087074  42.8644051  2.85850316  23.70757068 0.861703105357.875617  40.08052697 2.90391604  23.84198256 0.828534981358.483231  41.26875168 1.98773836  26.06830006 0.885146048358.218523  40.27344373 0.61176635  28.24887694 0.86766276356.391874   87.82219658 1.18588976  31.82632342 0.76775520656.355593   87.68729915 1.37666954  33.07983844 0.73135585956.274533   88.16157107 1.8140712   33.21245517 0.70299329758.264625   90.59810498 0.71927382  36.35112295 0.717745991296.051213  47.32622119 3.55526758  37.01360264 0.796795711296.428135  47.14235728 0.37586386  40.99409991 0.933842952296.483974  47.11833773 1.63577079  42.59987436 0.800643576149.978704  40.21568522 0.86039359  43.88742522 0.885870584152.913486  41.02065553 1.60530239  44.22730093 0.997865715148.90665   41.08012738 3.93188008  45.45667836 0.894401708346.076543  139.8808064 1.85955423  46.43473917 0.861932746346.414328  139.4533544 1.11412982  47.08968108 0.769676426345.144434  138.4076443 1.90880173  47.41656879 0.921949668347.214491  139.3465751 2.37729226  48.71147851 0.966697704332.753563  192.22991   2.28814891  50.57969649 0.957942288334.317276  189.9511419 3.86455092  50.68494991 0.879118237333.754131  189.9786093 2.97285931  51.75537529 0.896425643369.001883  49.03757175 3.850737    53.25133012 0.974504198373.451478  47.04323055 2.68174275  53.26729487 0.829954924371.270915  48.75191315 1.48038601  53.65752507 0.786928227100.980042  229.3257773 0.34910759  53.95915957 0.88956469185.373887  191.1246659 0.77164573  55.59813846 0.788626552186.209454  190.9779546 1.56667836  57.767241   0.886608637185.124518  191.2527261 0.29458722  60.01364617 0.714260258185.914337  190.2745825 2.52740599  60.61554743 0.99838296967.896372   155.4809072 0.28886827  61.31286449 0.762029245142.43641   227.5860602 4.14452355  62.29628681 0.882209187141.364062  228.3322702 4.1635609   63.89678201 0.804289098347.840723  114.6970218 0.56248091  63.95948138 0.91532112253.564593  174.113376  1.2621932   64.24838467 0.708397989253.984435  174.5656449 1.51828163  65.07176672 0.720052528253.0236    173.9265349 1.23375198  65.94064053 0.978119242231.629097  211.4394495 1.30405063  66.38331454 0.726331528232.328374  210.8879029 2.03407748  66.68961587 0.799719569232.644377  211.2277656 0.97238417  68.10333701 0.857854269232.56663   211.1678557 0.13184224  68.29747246 0.773992637112.295704  228.7487399 2.55766852  70.99561628 0.862869837113.023663  228.1650996 3.59402383  71.87466297 0.934260355112.538871  228.4575775 1.45766956  77.57275212 0.856564714113.814248  228.2794604 3.42350373  78.59894388 0.97958498183.44226   9.61564954  3.18554848  79.43332479 0.744133594183.54304   9.19009803  3.26511022  80.64811426 0.825029711183.268762  9.301064    2.44033259  85.7161253  0.784087865.369079    116.1726895 1.63053766  86.89113268 0.8794300954.970461    116.4974335 2.93526191  87.42088984 0.7109424576.345342    116.6858803 0.89072222  87.63073063 0.7191059515.492747    115.8670653 1.61844848  89.67642448 0.796864752343.385969  193.322461  2.62979605  89.94483446 0.729513559344.352781  193.5268701 4.29402892  90.4154695  0.751013388345.668421  193.0400517 3.1033618   91.93169629 0.81134914344.640169  194.248506  1.57956728  92.33756179 0.711928487167.038399  5.96957805  3.55962707  94.67162694 0.912772286166.987777  6.09536737  3.45933516  96.09079729 0.892402408168.510068  6.14462771  3.46656886  96.53093276 0.752218258167.103837  6.28745699  3.55633497  97.11208455 0.718645583183.30843   45.57593198 0.24446465  98.29165891 0.821997826183.036565  51.9529565  0.52456176  98.81054412 0.838918878183.144323  45.93514679 0.47791706  99.81095514 0.76080229338.977521  169.6576259 2.49761535  100.8103695 0.960864967338.821559  170.0718334 2.66390501  101.5614461 0.879382111337.682972  172.5078824 2.44075925  102.5406185 0.706904061339.414764  168.484822  3.07636237  102.5455371 0.969827931336.702388  194.6364837 3.67393227  103.0440893 0.835883835360.026772  184.3444998 3.62982366  103.3028211 0.717407853361.460234  184.3184775 0.96558388  103.369371  0.731880813247.557142  184.8726881 4.39064255  103.4793669 0.999527587249.630948  186.9021382 3.2051117   104.2020101 0.959898221246.647174  185.7989473 1.89101073  104.2744971 0.884561739248.079025  184.8060252 3.64218348  106.7323829 0.708083345343.213475  214.4993165 0.45176581  107.1307404 0.796756791345.720762  213.6341763 1.63235709  108.0652973 0.83913287345.016305  212.8656785 1.28675708  108.2516714 0.72970517345.462936  213.150121  2.88754323  110.0171049 0.871296443186.053395  7.62715418  2.02691573  112.650555  0.797762528182.455458  4.26944797  2.45740267  114.2678386 0.835147701182.235994  4.39627706  0.26503411  114.7318806 0.873353139409.403475  82.39618618 0.06216757  115.4464072 0.722453292409.052314  82.35559269 0.16314584  115.5665069 0.717202801215.780351  0.08912318  1.39563448  116.2344263 0.790286931214.837434  1.45883443  0.97705113  117.218643  0.856516375461.589113  9.86224616  0.9972708   117.971268  0.868564071459.202929  11.42966468 1.15177071  118.8073097 0.772466431459.088894  15.47868051 2.3559397   121.4164909 0.973816049256.903777  87.43673381 2.04200349  121.5194263 0.947720281257.382498  86.97386698 3.79079014  121.6251341 0.833363765256.697794  87.54199636 2.55432997  121.6996209 0.994618769222.945818  200.9308941 2.84077044  121.7241906 0.873480268222.819858  201.6746837 2.29641155  122.7084014 0.770327049222.130409  201.4562846 1.27840311  123.3858524 0.943177133384.260699  147.0320964 2.79131573  123.5775361 0.835382249381.993241  146.3060028 1.35450405  124.1866165 0.774990751382.080971  147.3370846 3.71228941  125.0491715 0.98663126381.648904  146.8234101 1.83454687  125.0912787 0.742795104474.324216  21.06180692 0.11079926  125.1323202 0.853769015475.098888  18.72632807 0.7689708   125.1400455 0.991577541472.708781  21.18145004 1.53044597  126.206574  0.894496186261.054388  196.4845601 2.85653651  126.4870562 0.884401301261.104845  196.6365273 2.96048717  126.8060731 0.840895112260.23671   197.1606291 2.73990119  128.0764361 0.873334209261.085155  196.6737482 2.69219319  128.5328058 0.97339396812.009559   160.8273903 0.89371542  129.4377345 0.81286608511.849503   160.841293  1.14120928  129.6794689 0.76862943212.345148   160.5244702 1.28818423  129.8214878 0.82705746111.135317   160.7668114 1.65208029  130.7028618 0.78207886631.623888   130.3067685 2.1127655   132.5014859 0.83336975327.170348   129.6318075 1.40646722  132.5051924 0.88825451431.403392   130.7047954 2.29731991  132.8479736 0.86039237629.923974   131.9784448 1.92817547  133.6366443 0.81563264890.074671   59.07610607 1.23271524  134.6497729 0.9620350387.906549   54.82957282 0.65955499  135.3859154 0.7901046289.899048   57.83154064 0.30141145  135.5092119 0.820008915276.323019  6.86787982  1.7776151   135.5215818 0.855317054275.970301  7.91419119  3.21748128  135.6636361 0.718547414275.727285  8.3243395   1.94806409  136.057032  0.769410166276.514291  7.01085361  1.43467439  137.343003  0.735545797330.040302  144.5877302 0.66277381  137.4281315 0.729634011324.893213  146.4365159 0.88234134  137.6037721 0.967082637291.11468   49.33367767 2.47072766  138.7895341 0.710016723293.43458   48.62581312 1.98062144  139.3682232 0.951710498250.764567  198.5016519 0.85846877  140.0507716 0.852180017253.539089  201.132831  1.49089404  140.9996563 0.734114932253.512945  201.0537249 2.4727616   141.0409587 0.847133697253.542791  201.2490377 1.98635162  142.6391569 0.879832516257.795509  216.9359492 0.78064425  145.1118214 0.72706484257.764285  216.4872431 2.80925094  145.1624047 0.99346721257.818943  216.6897931 2.33814072  145.9038514 0.895905632257.774863  216.6051295 2.44114347  145.9367341 0.838339638367.514438  31.4793139  0.78736947  146.1964166 0.959127433367.588968  31.4867048  0.96941256  146.2887533 0.778848722369.492049  30.48020726 2.4794555   150.0217156 0.947188883143.865075  233.3690032 1.67318663  150.4491923 0.798692592144.098544  234.3763707 0.23773927  150.5166887 0.982390159218.32888   216.7083106 2.74041746  151.0604421 0.773232421325.353164  143.7297267 2.96706415  151.763207  0.98713305326.419993  143.5201895 3.63452168  152.3832321 0.853247489325.936613  144.5316032 2.32157168  153.3589663 0.869373958325.810082  144.356522  4.39008693  153.4582455 0.99811483931.910325   124.5598197 2.33494244  153.6579228 0.93129518231.351707   125.4332729 2.30507449  154.0737073 0.79414057631.574903   124.0463483 0.75565309  154.0886993 0.71736178732.54375    122.7135364 2.18675079  154.2861579 0.71322213890.008543   124.8990934 1.97911207  154.3444858 0.94388319190.28441    126.6897782 1.01474716  154.9277926 0.82362198387.735243   125.8623578 3.00304403  155.2644112 0.815248951260.385862  96.59499166 3.96772436  156.995524  0.85693796259.739291  97.04823338 2.61008235  157.5583662 0.96764469260.311284  96.68968926 4.22429753  157.6724828 0.821776192260.229276  95.34657991 2.54473831  157.7533106 0.881316669260.212422  187.7862368 4.31051589  157.9162288 0.728441067260.231256  188.2226327 3.3502332   158.0074879 0.800794383261.545309  191.0387749 3.93897914  158.0767802 0.743452552272.998243  214.0871297 0.58707332  158.0785985 0.773464159272.69374   214.5296828 1.42084384  159.6240869 0.813691498274.126863  212.7891744 0.26609394  159.9565838 0.781095884367.90561   15.41286762 2.95145208  160.5243755 0.76467979367.864872  15.25877454 2.64007335  161.3173639 0.890119333367.649781  19.91579374 3.97005021  161.9414598 0.939914671148.118458  177.8767452 0.7438971   162.0120391 0.762554208147.655051  176.9144773 1.3703557   162.030937  0.968876858148.050091  176.9031162 1.30790609  162.3061767 0.923712841147.649022  175.9846377 0.01212553  162.6304654 0.860915742356.818492  67.21939123 1.76786772  164.2579604 0.990989558355.622664  68.75829882 3.19659511  164.3880109 0.869464951355.844738  68.88550929 4.36517304  164.7900766 0.76530697108.71847   215.0266242 1.98752526  166.393087  0.957328986109.014859  214.6488939 3.20229933  167.6177713 0.958599795109.164433  215.2627269 1.93777216  167.8951075 0.793990658108.131203  215.0862499 2.35653921  168.2758637 0.798095263196.231361  70.49924437 1.97858279  168.4511479 0.952004021197.737244  72.88240664 0.65482871  169.5036831 0.847759836339.257305  120.888616  1.34736322  169.6356306 0.715570745340.781081  117.0187847 2.51971051  169.6790878 0.933405511338.606336  121.5834442 1.80270464  170.3047686 0.828001716338.041063  120.7029464 1.73078138  170.3913998 0.783988564338.99619   35.10033383 2.97337925  170.4444717 0.800052404337.200856  33.51899806 1.09814538  170.613345  0.810081986336.985755  34.21892728 0.24128014  170.8469088 0.938437723220.803763  53.14430613 1.29837937  170.8645578 0.711607236221.630219  52.1940613  1.79796509  171.6146056 0.91800080198.635642   121.5862813 2.9432507   171.8511343 0.96180533898.382482   121.1350972 1.85599348  171.9082703 0.78574443595.692992   121.8135249 2.76335185  172.186239  0.89705477895.455554   121.1536975 3.06349446  172.3154888 0.76956598829.902259   220.4663988 0.032004    172.6212011 0.88658618228.41221    220.079011  0.64563437  172.71164   0.7225371328.608348   219.9848542 0.81174981  173.474008  0.99002676328.837549   219.7118007 0.88414209  176.6881568 0.882990987417.469016  60.71793319 3.13009015  178.4274218 0.815114554420.412299  59.95963986 3.77420788  178.5608842 0.709170932417.835488  61.34888422 3.412701    179.0698474 0.957269676266.303184  148.8628134 4.14332284  179.975606  0.881062956266.264066  149.8292293 4.17012164  180.080254  0.954352122290.584215  94.18249675 2.09946968  180.3264641 0.851384874211.043848  164.8354141 0.38617853  180.4780309 0.702373251394.665914  165.8533229 2.94896243  180.6401804 0.975721953396.948844  164.3185096 2.95721695  181.189757  0.823211439394.152446  166.1863908 2.85283338  181.4459117 0.919709724355.861861  151.482828  1.79907458  181.5735067 0.745331584355.604564  151.2328554 1.42786694  182.0786425 0.944569983355.977914  151.7258945 2.60632787  182.2734472 0.9728807655.193224   70.13457335 1.95421619  182.4681836 0.93338273855.121245   70.26190069 1.7134954   182.7419885 0.927136941466.029024  43.60374817 3.02745392  183.048289  0.821559257466.713681  43.04071215 3.34442703  183.0495735 0.910570324361.343579  38.84946077 4.09011265  183.1337707 0.869638281107.494724  224.061154  0.64225969  184.3347164 0.875420906109.403587  222.7703059 1.02129489  184.5405119 0.80347438108.536435  221.8397702 1.32737534  185.2302114 0.911269972108.966261  223.5009692 0.64308213  185.599194  0.748259104319.258848  64.01371204 4.34421863  185.7281496 0.70045418321.53673   62.05801435 4.02840039  185.9683567 0.868600256320.136879  62.7561261  2.02796518  186.1012058 0.863945237426.321536  79.57156783 1.80279196  186.1655452 0.865248329425.254692  80.84699203 0.53246863  186.351376  0.908361028424.301716  81.17660654 0.05602379  186.4132518 0.978248228424.827296  81.04033515 1.33209986  186.4734765 0.983263093119.807247  187.958236  3.33382966  186.7652667 0.970978999120.210092  189.4849366 2.84489764  187.4376306 0.881231886120.205569  189.1727403 4.18836325  187.4622579 0.977862927247.808605  146.4244449 0.83110586  188.5004803 0.735083878249.294833  144.1907226 1.22128356  188.962155  0.802240452250.204516  146.4749518 1.7944476   189.3051254 0.711019133334.654137  11.15448488 3.46662664  189.8126837 0.861510955333.659862  11.64954098 2.1332473   190.1436423 0.92116864333.427405  10.95652538 2.75561873  191.3076606 0.7543667519.482643    85.46858555 1.15438219  191.3293578 0.8278739989.378643    85.45893223 2.23185957  191.4270999 0.7294503879.333447    84.55839727 1.36554428  192.1174705 0.79096647439.424485  237.4993259 0.59464444  192.4302016 0.934036784438.638525  238.1309002 0.47642958  192.835047  0.861088125439.977322  237.950318  1.51017279  193.0183715 0.93072143438.636146  237.9862702 1.3178479   193.2070887 0.891683094330.410302  62.51368701 2.99926551  193.8201965 0.967944474329.790522  62.17494892 2.86913557  194.8657309 0.718207116329.460954  62.13150555 3.26717085  195.1562923 0.752731464474.563651  69.88329745 4.03771076  195.1657446 0.824899558285.627382  72.98472026 1.95847784  196.1019459 0.921937552286.820235  72.90395374 1.26502975  196.2494223 0.967886438236.897161  134.567653  1.15564329  196.4295919 0.707755992234.806852  133.6690276 0.210115    196.5220457 0.741268909233.53859   132.9487054 2.28750244  196.5612547 0.82722623277.005222  47.68301903 4.36155011  196.6331458 0.929370035277.336998  44.11638043 3.06712344  197.5726448 0.857331156277.800836  43.03107739 3.70528439  197.5837294 0.926344408276.793356  44.16753185 3.20462984  197.9290181 0.750939533165.177531  248.6222625 0.3374573   197.9766126 0.901818102164.463612  249.5902955 1.32364566  198.1684532 0.885595823161.499292  251.3536746 1.75217305  198.2615641 0.702053827440.799878  176.1199082 3.19808685  198.3114499 0.922067657441.798136  176.5611422 2.98843581  198.9449421 0.997520643441.027104  176.7791779 3.08648705  199.572324  0.738454407"
y1 <- read.table(text=zz, header = TRUE)
zzz <- " longitude  latitude    depth   time    magnitude277.728371  139.925845  0.75103658  26.40786514 0.943718276426.087586  168.903095  0.2957441   0.241456485 0.759137864331.549444  74.168092   0.55140397  66.51363095 0.77617643393.078983   78.588053   0.15328453  104.9418546 0.834896464492.359229  11.082291   0.08173915  111.3391451 0.87479811986.85704    42.34973    0.23081904  152.8098572 0.878111793128.038949  73.935782   0.66160123  157.8933315 0.990100773295.300125  1.935765    0.49789785  159.9134319 0.842815655294.688309  1.024583    0.44789667  165.7092358 0.886545275221.246937  151.217171  0.6337224   167.6213491 0.885163617111.240376  156.04214   0.55752237  171.2039395 0.88527352625.929383   136.975153  0.0271747   172.6574772 0.812214826415.726989  158.482975  0.37340509  184.3767148 0.9083717473.921877   60.031908   0.15224511  189.6429637 0.7914032288.811256    124.676545  0.26806101  193.7498013 0.813638308"y2 <- read.table(text=zzz, header = TRUE)
evpts1 <-do.call(expand.grid,  lapply(y1,quantile, prob=c(.1,.15,.2,.25,.3,.35,.4,.45,.5,.55,.6,.65,.7,.75,.8, .85,.9,.95)))hat1 <- kde(y1, eval.points= evpts1)
evpts2 <-do.call(expand.grid,  lapply(y2,quantile, prob=c(.1,.15,.2,.25,.3,.35,.4,.45,.5,.55,.6,.65,.7,.75,.8, .85,.9,.95)))hat2 <- kde(y2, eval.points= evpts2)
str(hat1)str(hat2)
integrand1 <- function(p){hat1(p,y1)+hat2(p,y2)}
Vintegrand <- Vectorize(integrand1)
integrate( Vintegrand, lower = 0, upper = t)


But it gives me error. I appreciate any help in advance.

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list