Pro Yearly is on sale from $80 to $50! »

あまぁいRcpp生活

1bf1e3c1b4eceb1e7f43f10dda8d27d3?s=47 teuder
August 19, 2016

 あまぁいRcpp生活

Rの関数をC++で実装することを可能にするパッケージRcppの紹介です。

1bf1e3c1b4eceb1e7f43f10dda8d27d3?s=128

teuder

August 19, 2016
Tweet

Transcript

  1. 55 R 08/06 2 # # #

  2. 3DQQͱ͸ 3ͷؔ਺Λ$ Ͱ࣮૷͢Δ͜ͱΛ Մೳʹ͢Δύοέʔδ

  3. 3DQQΛར༻͍ͯ͠Δ ύοέʔδ਺ ύʔηϯςʔδ       

              Ҏ্ͷύοέʔδͰར༻͞Ε͍ͯΔ ύοέʔδ਺ 
  4. 1 RcppArmadillo 353992 0.0235 2 Rcpp 254575 0.0169 3 ggplot2

    219765 0.0146 4 digest 179117 0.0119 5 stringr 171733 0.0114 6 plyr 170953 0.0113 7 stringi 170182 0.0113 8 scales 167722 0.0111 9 magrittr 159638 0.0106 10 gtable 150171 0.0099 ࠷΋ଟ͘μ΢ϯϩʔυ͞Ε͍ͯΔ ύοέʔδͷͻͱͭ %-਺ ׂ߹ IUUQDSBOMPHTSTUVEJPDPN͔Β ೥݄ʹμ΢ϯϩʔυ͞ΕͨύοέʔδͷϥϯΩϯά
  5. None
  6. )BEMFZWFSTF HHQMPUSFTIBQFQMZSEQMZSUJEZS TUSJOHSMVCSJEBUFSFBESSFBEYMIBWFO IUUSSWFTUYNM T

  7. )BEMFZWFSTF HHQMPUSFTIBQFQMZSEQMZSUJEZS TUSJOHSMVCSJEBUFSFBESSFBEYMIBWFO IUUSSWFTUYNM 3DQQʹґଘ T

  8. 3DQQ 5IF%SJWJOH'PSDFPG )BEMFZWFSTF %JSL&EEFMCVFUUFM 3PNBJO'SBOÇPJT )BEMFZ8JDLIBN

  9. Ͱ΋ʜ $ ͬͯʜ

  10. ࠇຐज़ݴޠ$ ˠ·ͣ͸ศརͳͱ͜Ζ͚ͩษڧ͢Ε͹͍͍ ˠ34UVEJP͕ʢଟগʣΘ͔Γ΍͘͢දࣔͯ͘͠ΕΔ ˠ·͊ɺ൱Ίͳ͍Ͱ͢Ͷʜ ݴޠ࢓༷͕ڊେʹͳͨΊशಘʹֻ͕͔࣌ؒΔ ίϯύΠϧΤϥʔ͕Θ͔Γʹ͍͘ ม਺ͷܕ͕ෳࡶ $ ͷΤϥʔͷଟ͘͸ ʲม਺ͷܕʳͱʲ୅ೖ͢Δ஋ͷܕʳͷෆҰகʹΑΔ

  11. ˞͋͘·Ͱ΋ΠϝʔδͰ͢ E C

  12. E 55

  13. 3DQQͷΠϯετʔϧ ࣗ࡞ͷ3DQQؔ਺Λ࡞੒͢ΔͨΊʹ͸ $ ͷίϯύΠϥΛΠϯετʔϧ͢Δඞཁ͕͋Δ 8JOEPXT 3ͷόʔδϣϯʹ߹ͬͨ3UPPMTΛΠϯετʔϧ͢Δ .BD λʔϛφϧͰ࣍ͷίϚϯυΛଧͭ IUUQTDSBOSQSPKFDUPSHCJOXJOEPXT3UPPMTJOEFYIUNM iUaVW)dW

    WUe ))[ deS 8JOɺ.BDڞʹ3Ͱ [ deS *bSU]S Wd “IUbb”
  14. 3UPPMTͷΠϯετʔϧ جຊతʹ͸ରԠ͢ΔҰ൪৽͍͠'SP[FOόʔδϣϯΛ Πϯετʔϧ͢Ε͹ྑ͍

  15. 3UPPMTͷΠϯετʔϧ Πϯετʔϧ࣌ʹ1"5)ͷઃఆʹνΣοΫ͢Δ ʢίϯύΠϥ͕Πϯετʔϧ͞Εͨ৔ॴΛ3ʹڭ͑ΔͨΊʣ ͜͜ʂ

  16. 3UPPMTͷΠϯετʔϧ : jd* WeW g #H; B# P- !=6ooHca cS_

    [ WdooIooI)/*/*-ooT[ ooi207 U6ooIeaa dooT[ 7 U6ooIeaa do o_[ hR20ooT[ 7v wu : djdeW_ # '' )g# Ld[ Tf[ e)[ dbWUd* =GDD = RA==9U6oIeaa do_[ hR20oT[ oARRn-* O =GDD = RD GRNI;HH I9U6+Ieaa d+_[ hR20+T[ +**+ [TWiWU+ UU+i42R20)h20) _[ h/.+0*5*/+ ea)hcSbbWc*WiW v w cWSV _aVW 6 bad[i UU gWcd[a 0*5*/ i42R20)bad[i)dW ( <f[ e Tj E[ AN)N20 bca WUe ֬ೝͷͨΊ3Ͱ࣍ͷίϚϯυΛೖྗ͠ ԼͷΑ͏ͳग़ྗ͕ಘΒΕͨΒ0,
  17. E 55 3DQQͷίʔυΛॻ͘৔߹ʹ͸ ͷར༻Λڧ͘ਪ঑͠·͢ ίʔυิ׬΍Τϥʔද͕ࣔ਌੾ͳͷͰ

  18. E 55 ϑΝΠϧ/FX'JMF$ 'JMF ద౰ͳ໊લͰϑΝΠϧʹอଘͯ͠4PVSDFΛԡ͢ʂ

  19. E 55 : IUbb66dafcUW=bb # a]jaI*Ubb# : e[_Wd ha 0.

    P- 40 : e[_Wd ha U -(.(/ P- . 0 2 ϑΝΠϧ/FX'JMF$ 'JMF ద౰ͳ໊લͰϑΝΠϧʹอଘͯ͠4PVSDFΛԡ͢ʂ 3DQQͰ࡞੒ͨؔ͠਺͸௨ৗͷ3ؔ਺ͱಉ༷ʹ࢖༻Ͱ͖Δ
  20. E 55 // Rcpp Λར༻͢ΔͨΊʹඞཁͳϔομϑΝΠϧ #include <Rcpp.h> // ͜ΕΛهड़͠ͳ͍৔߹͸ Rcpp::NumericVector

    // ͱॻ͔ͳ͍ͱ͍͚ͳ͍ using namespace Rcpp; // ͜ͷهड़ͷ௚Լͷؔ਺͕R͔Βར༻ՄೳʹͳΔ // [[Rcpp::export]] // ؔ਺ͷఆٛ NumericVector timesTwo(NumericVector x) { return x * 2; } /* C++ ίʔυதʹRͷίʔυΛຒΊࠐΊΔ ίϯύΠϧ௚ޙʹࣗಈͰ࣮ߦ͞ΕΔ */ /*** R timesTwo(42) */
  21. 3ͱ$ ͷॻ͖ํͷҧ͍ ϕΫτϧͳͲͷཁૉ൪߸͸͔Β ม਺΍ؔ਺ʢͷฦ஋ʣͷσʔλܕΛࢦఆ͢Δඞཁ͕͋Δ จͷ࠷ޙʹඞਢ ม਺໊΍ؔ਺໊ʹϐϦΦυ͸࢖͑ͳ͍ ஋ͷ୅ೖ͸ͷΈ ؔ਺ͷฦ஋ʹSFUVSOඞਢ

  22. df_I 8) Xf Ue[a i l df_ 8) , Xac

    ii [ i l df_ 8) df_ ' ii m cWefc df_ m ϕϯνϚʔΫ ࣮਺ϕΫτϧͷ߹ܭ஋ΛٻΊΔ
  23. ϕϯνϚʔΫ ++ PPIUbb66Wibace VafT W df_IUbb Ff_Wc[UMWUeac i l [

    e 9 i* W e 7 VafT W df_ 9 ,*,7 Xac [ e [9,7 [ 8 7 ''[ l df_ 9 df_ ' iP[ 7 m cWefc df_7 m ࣮਺ϕΫτϧͷ߹ܭ஋ΛٻΊΔ
  24. ϕϯνϚʔΫ ࣮਺ϕΫτϧͷ߹ܭ஋ΛٻΊΔ ++ PPIUbb66b f [ d Ubb-- ++ PPIUbb66Wibace

    VafT W df_IUbb=bb-- Ff_Wc[UMWUeac i l VafT W df_ 9 ,*,7 Xac VafT W ii 6 i l df_ 9 df_ ' ii7 m cWefc df_7 m
  25. ϕϯνϚʔΫ ࣮਺ϕΫτϧͷ߹ܭ஋ΛٻΊΔ ++ PPIUbb66Wibace VafT W df_IUbb f_ Ff_Wc[UMWUeac i

    l cWefc df_ i 7 m
  26. ϕϯνϚʔΫ : cTW U _Sc]66TW U _Sc] ' df_I i

    ( ' df_ i ( ' df_IUbb i ( ' df_IUbb=bb-- i ( ' df_IUbb f Sc i ( ' Ua f_ d9U !eWde!( !cWb [USe[a d!( !W SbdWV!( !cW Se[gW! ( ' acVWc 9 FLDD( ' cWb [USe[a d9-,, eWde cWb [USe[a d W SbdWV cW Se[gW - df_I i -,, 1,*045 .30*/53 . df_ i -,, ,*.0/ -*/.- / df_IUbb i -,, ,*-5. -*,0/ 0 df_IUbb=bb-- i -,, ,*.,. -*,54 1 df_IUbb f_ i -,, ,*-40 -*,,, 3DQQ͸$ݴޠͰ࣮૷͞Εͨ3ͷؔ਺ΑΓ଎͍৔߹͕͋Δ
  27. 3DQQͷσʔλߏ଄ %BUBGSBNFɹ-JTUɹ4 7FDUPSɹ.BUSJY ୯ʹಉ͡Α͏ͳσʔλߏ଄͕ఆٛ͞Ε͍ͯΔͷͰ͸ͳ͘ 3ͷϝϞϦʹ͋ΔΦϒδΣΫτΛ௚઀ૢ࡞Ͱ͖Δ

  28. 3DQQͷσʔλܕ ̋̋.BUSJY ஋ܕ 3 ϕΫτϧ 3DQQ ϕΫτϧ 3DQQ ߦྻ 3DQQ

    εΧϥʔ $  εΧϥʔ ࿦ཧ MPHJBM -PHJDBM7FDUPS -PHJDBM.BUSJY  CPPM ੔਺ JOUFHFS *OUFHFS7FDUPS *OUFHFS.BUSJY  JOU ࣮਺ OVNFSJD /VNFSJD7FDUPS /VNFSJD.BUSJY  EPVCMF ෳૉ਺ DPNQMFY $PNQMFY7FDUPS $PNQMFY.BUSJY 3DPNQMFY DPNQMFY จࣈྻ DIBSBDUFS $IBSBDUFS7FDUPS $IBSBDUFS.BUSJY 4USJOH TUSJOH ೔෇ %BUF %BUF7FDUPS  %BUF  ೔࣌ 104*9DU %BUFUJNF7FDUPS  %BUFUJNF UJNF@U
  29. @ 3DQQͰ͸ ͍ۤ$ Λ؁͘͢ΔͨΊʹ 3ͱಉ༷ʹ࢖͑Δ ؔ਺΍ԋࢉࢠ͕ఆٛ͞Ε͍ͯΔ

  30. 4VHBSؔ਺ ベクター関係 head, tail, rep_each, rep_len, rep, rev, seq_along, seq_len,

    cumsum, diff, pmin, pmax 確率分布 d/q/p/r 全てのRの提供する確率分布 数学関数 abs, acos, asin, atan, beta, ceil, ceiling, choose,cos, cosh, exp, expm1, factorial, floor, gamma, lbeta, lchoose, lfactorial, lgamma, log, log10, log1p, pentagamma, psigamma, round, signif, sin, sinh, sqrt, tan, tanh, tetragamma, trigamma, trunc ཁ໿౷ܭྔ NFBO NJO NBY TVN TE WBS ஋ͷݕࡧ NBUDI TFMG@NBUDI XIJDI@NBY XIJDI@NJO ॏෳͷ஋ EVQMJDBUFE VOJRVF BQQMZؔ਺ MBQQMZ TBQQMZ NBQQMZ ࿦ཧ஋ JGFMTF BMM BOZ
  31. 4VHBSԋࢉࢠ ϕΫτϧϕΫτϧ i'jpi)jpi jpi+j ϕΫτϧεΧϥʔ .'ip.)ip. ipi+. ࢛ଇԋࢉ

  32. ϕΫτϧϕΫτϧ i99jpi 9jpi:jpi:9j ϕΫτϧεΧϥʔ .99ip. 9ip.:ipi:9. 4VHBSԋࢉࢠ ൺֱԋࢉ

  33. ϕΫτϧͷ࡞੒ ++ g 8) cWb ,( -, Ff_Wc[UMWUeac g -,

    7 ++ g 8) U -(.(/ ++ PPIUbb66b f [ d Ubb-- Ff_Wc[UMWUeac g 9 l-(.(/m7 ++ g 8) U i9-( j9.( k9/ Ff_Wc[UMWUeac g 9 Ff_Wc[UMWUeac66UcWSeW FS_WV "i", - ( FS_WV "j" 9. ( RP"k" 9/ 7
  34. ϕΫτϧཁૉ΁ͷΞΫηε ࣮਺ɾ੔਺ɾจࣈྻɾ࿦ཧϕΫτϧͰΞΫηε Ff_Wc[UMWUeac [ 9 l,(.(0m7 C eW WcMWUeac [

    9 l,(.(0m7 = ScSUeWcMWUeac [ 9 l!i!(!j!(!k!m7 Da [US MWUeac [ 9 g:,7 Ff_Wc[UMWUeac c 9 gP[ 7 gP[ 9 gS 7
  35. ߦྻͷ࡞੒ ++ _ 8) _Sec[i ,( cah 9 .( Ua

    9 / Ff_Wc[UESec[i _ .( / 7 ++ _ 8) _Sec[i g( cah 9 .( Ua 9 / Ff_Wc[UESec[i _ .( / ( g*TW [ 7
  36. ߦྻͷཁૉ΁ͷΞΫηε ++ , . _ , ( . 7 ++

    , _ , ( R 7 ++ . _ R ( . 7 ++ ,s- .s/ _ IS W ,(- ( IS W .(/ 7
  37. σʔλϑϨʔϜͷ࡞੒ >SeS cS_W VX 9 >SeS cS_W66UcWSeW g-(g.(g/ 7 ++

    { >SeS cS_W66UcWSeW FS_WV ! -!( g- ( FS_WV ! .! 9g. ( RP! /! 9g/ 7
  38. σʔλϑϨʔϜͷཁૉ΁ͷΞΫηε ࣮਺ɾ੔਺ɾจࣈྻɾ࿦ཧϕΫτϧͰΞΫηε >SeS cS_W VX- 9 VXPg 7 VXPg 9

    VX.7 Ff_Wc[UMWUeac g- 9 VXP, 7 VXP!i! 9 g.7 ࣮਺ɾ੔਺ɾจࣈྻεΧϥʔͰΞΫηε ΧϥϜͷ஋ʹ͸g-Λհͯ͠ΞΫηε͢Δ VXP(- ΍VXP- P- ͳͲ͸ବ໨
  39. D[de D 9 D[de66UcWSeW g-(g.(g/ 7 ++ { D[de66UcWSeW FS_WV

    ! -!( g- ( FS_WV ! .! 9g. ( RP! /! 9g/ 7 ˞W W Wͷܕ΍αΠζ͸ԿͰ΋ྑ͍ Ϧετͷ࡞੒
  40. Ϧετͷཁૉ΁ͷΞΫηε ࣮਺ɾ੔਺ɾจࣈྻɾ࿦ཧϕΫτϧͰΞΫηε D[de D- 9 DPg 7 DPg 9 D.7

    Ff_Wc[UMWUeac g- 9 DP, 7 DP!i! 9 g.7 ࣮਺ɾ੔਺ɾจࣈྻεΧϥʔͰΞΫηε Ϧετཁૉͷཁૉʹ͸g-Λհͯ͠ΞΫηε͢Δ
  41. MWUeac M M* W e ++ M* S_Wd ++ ESec[i

    E E* cah ++ E* Ua ++ cah S_Wd E ++ Ua S_Wd E ++ >SeS cS_W > > * W e ++ > * S_Wd ++ > *Seec !cah* S_Wd! ++ D[de D D* W e ++ D* S_Wd ++ ϕΫτϧͷ௕͞΍ཁૉ໊ͳͲ
  42. ଐੑ஋ MWUeac M M*Seec ! S_Wd! ++ ESec[i E7 E*Seec

    !V[_! 9 C eW WcMWUeac66UcWSeW ( 7 E*Seec !V[_ S_Wd! 9 D[de66UcWSeW ( 7 >SeS cS_W > 7 > *Seec ! S_Wd! ++ > *Seec !cah* S_Wd! ++ D[de D7 D*Seec ! S_Wd! ++
  43. 3ͷؔ਺Λݺͼग़͢ f Ue[a X !c ac_! 7 Ff_Wc[UMWUeac cWd 9

    X 1( FS_WV !_WS !(- ( FS_WV !dV!(. 7 g[ca _W e W g 9 g[ca _W e66 S_WdbSUWRW g ! r ! 7 f Ue[a X 9 W gP! ! 7 ͜ͷํ๏Ͱύοέʔδؔ਺Λݺͼग़͢৔߹͸ MJCSBSZ ύοέʔδ໊ ͢Δඞཁ͕͋Δ ͪ͜Β͸MJCSBSZ ύοέʔδ໊ ͢Δඞཁ͕ͳ͍
  44. 55 R 3DQQΛ࢖͑͹ 3ͷίʔυΛ$ ʹஔ͖׵͑Δ͜ͱ΋Մೳ ࣮ͨͩ૷͕໘౗ͳͷ͸ࣄ࣮ͳͷͰ ॲཧͷϘτϧωοΫʹͳ͍ͬͯΔ෦෼ʹ ద༻͢Δͷ͕ޮՌత

  45. 3DQQֶशͷͨΊʹ 3DQQ(BMMFSZ IUUQHBMMFSZSDQQPSH 3DQQ/PUF IUUQTUBUSNFSDQQOPUF *OUSPEVDUJPOUP3DQQʢ೔ຊޠʣ IUUQTXXXHJUCPPLDPNCPPLUFVEFSJOUSPEVDUJPOUPSDQQEFUBJMTKB 3ݴޠపఈղઆ ʢڞཱग़൛ʣ 4MBDLͷSXBLBMBOHʹ3DQQ࣭໰෦԰͋Γ·͢

    IUUQTSXBLBMBOHTMBDLDPNNFTTBHFTSDQQ
  46. $ ֶशͷͨΊʹ 1SPHSBNNJOH1MBDF1MVT IUUQQQQMBCTBLVSBOFKQ1SPHSBNNJOH1MBDF1MVTDQQJOEFYIUNM ୔ࢁ͋Γ͗͢ΔͷͰ͚̍ͭͩ

  47. + 1

  48. None
  49. 3DQQͷ஫ҙ఺ ίϯύΠϧͨ͠3DQQؔ਺͸3%BUBʹอଘͰ͖ͳ͍ ৽͍͠3ͷηογϣϯͷ౓ʹ࠶ίϯύΠϧ͢Δ ίϯύΠϧࡁΈؔ਺Λอଘ͍ͨ͠৔߹ʹ͸ ࣗ࡞ύοέʔ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ͷҾ਺ͷҰ෦ͷཁૉΛมߋ͢Δέʔε
  59. ++ g- IUbb X } y ++ ga[V X Ff_Wc[UMWUeac

    g. l tttt m : X g- 3DQQͷ஫ҙ఺ ϝϞϦʔ g- -(.(/ g. ؔ਺ͷҾ਺ͷҰ෦ͷཁૉΛมߋ͢Δέʔε
  60. ++ X g. ~ ++ ga[V X Ff_Wc[UMWUeac g. l

    g.P, 957 m : X g- : bc[ e g- P- 5 . / 3DQQͷ஫ҙ఺ ϝϞϦʔ g- 5(.(/ g. ؔ਺ͷҾ਺ͷҰ෦ͷཁૉΛมߋ͢Δέʔε
  61. 3ͷDPQZPONPEJGZ ϝϞϦʔ " g- ~ g- 8) U -(.(/ g-

    -(.(/
  62. ϝϞϦʔ g- -(.(/ g. 3ͷDPQZPONPEJGZ " g- g. ~ g.

    8) g-
  63. ϝϞϦʔ g- -(.(/ g. 5(.(/ " g. ~ g.P- 8)

    5 3ͷDPQZPONPEJGZ
  64. /"ͷѻ͍ 4VHBSؔ਺ɾԋࢉࢠ͸ศརͳ͚ͩͰ͸ͳ҆͘શͰ͋Δ ඪ४$ ʹ͸/"͸ఆٛ͞Ε͍ͯͳ͍ͷͰ 3ͷ/"஋Λ͏·͘ѻ͑ͳ͍ 4VHBSؔ਺ɾԋࢉࢠ͸3ͱಉ༷ͷ݁ՌΛฦ͢

  65. ඪ४$ ͷJOUಉ࢜ͷԋࢉࢠ ++ PPIUbb66Wibace C eW WcMWUeac SVVR[ eW Wc

    C eW WcMWUeac i( C eW WcMWUeac j l [ e 9 i* W e 7 C eW WcMWUeac k 7 Xac [ e [9,7 [8 7 ''[ l kP[ 9 iP[ ' jP[ 7 ++ [ e ' [ e m cWefc k7 m : i'j P- F; . : SVVR[ eW Wc i(j P- ).-0304/203 . : i 8) U - ( - : j 8) U F;( -