#obtenemos los datos desde el sitio de yahoo de disney
NVCRdata <- pdfetch_YAHOO("DIS",from = c("2019-01-01"),to = c("2020-01-01"), interval = '1d')
NVCRdata
##            DIS.open DIS.high DIS.low DIS.close DIS.adjclose DIS.volume
## 2019-01-02   108.10   109.14  107.73    108.97     107.6543    9723500
## 2019-01-03   108.48   108.65  105.94    106.33     105.0462   10594700
## 2019-01-04   107.94   110.75  107.25    109.61     108.2866   10122800
## 2019-01-07   109.91   111.40  109.30    110.56     109.2251    6714700
## 2019-01-08   111.80   112.56  111.17    111.42     110.0748    8730700
## 2019-01-09   111.80   112.80  111.56    112.67     111.3097    5931900
## 2019-01-10   111.99   112.91  111.50    112.80     111.4381    6160100
## 2019-01-11   112.18   112.92  111.73    112.65     111.2899    4819100
## 2019-01-14   111.65   112.70  111.43    112.42     111.0627    6980600
## 2019-01-15   112.30   113.18  110.61    111.76     110.4107    9800900
## 2019-01-16   111.45   112.01  110.81    110.91     109.5709    5899900
## 2019-01-17   110.72   111.26  110.16    111.01     109.6697    6392000
## 2019-01-18   111.86   111.93  110.83    111.04     109.6993    8554700
## 2019-01-22   110.62   111.33  109.75    110.60     109.2647    6986700
## 2019-01-23   111.19   111.84  110.00    111.12     109.7784    6845600
## 2019-01-24   111.00   111.11  109.93    110.55     109.2153    6716800
## 2019-01-25   111.47   111.50  110.66    111.09     109.7487    5706900
## 2019-01-28   110.76   110.99  109.95    110.81     109.4721    6178800
## 2019-01-29   110.65   110.93  110.00    110.90     109.5610    5253700
## 2019-01-30   110.30   110.58  108.96    110.13     108.8003    7853800
## 2019-01-31   110.10   111.54  110.01    111.52     110.1735    8351600
## 2019-02-01   111.97   112.05  110.93    111.30     109.9562    6557700
## 2019-02-04   111.41   111.98  110.68    111.80     110.4502    7037000
## 2019-02-05   112.02   112.74  111.45    112.66     111.2998   13619800
## 2019-02-06   113.85   113.92  111.07    111.41     110.0649   14229000
## 2019-02-07   111.05   111.11  109.81    110.95     109.6104    8658100
## 2019-02-08   110.46   111.54  110.06    111.51     110.1637    6395500
## 2019-02-11   111.77   111.81  109.25    109.44     108.1187   11086400
## 2019-02-12   110.21   110.83  109.15    109.20     107.8816    7610200
## 2019-02-13   109.24   110.48  109.23    110.20     108.8695    6919700
## 2019-02-14   109.85   111.50  109.54    110.66     109.3239    6324300
## 2019-02-15   111.40   112.63  111.03    112.59     111.2306    8197300
## 2019-02-19   112.91   113.77  112.83    113.51     112.1395    8169600
## 2019-02-20   113.60   114.09  113.29    113.68     112.3075    7081500
## 2019-02-21   113.40   114.54  112.87    114.29     112.9101    9480100
## 2019-02-22   114.63   115.77  113.95    115.25     113.8585    8734700
## 2019-02-25   115.62   115.80  113.46    113.59     112.2186    9327700
## 2019-02-26   113.65   114.01  112.85    113.50     112.1296    8956000
## 2019-02-27   113.10   113.51  112.46    112.78     111.4183    7249500
## 2019-02-28   112.90   113.43  112.75    112.84     111.4776    6716700
## 2019-03-01   113.45   114.44  113.45    114.01     112.6335    6996900
## 2019-03-04   114.42   114.42  113.09    114.33     112.9496    6015000
## 2019-03-05   114.23   114.54  113.76    114.00     112.6236    5225100
## 2019-03-06   114.00   115.05  114.00    114.85     113.4633    7352800
## 2019-03-07   114.85   114.90  113.47    114.01     112.6335    6324000
## 2019-03-08   113.46   113.84  112.90    113.81     112.4359    6549100
## 2019-03-11   114.04   115.00  114.03    114.75     113.3645    6952700
## 2019-03-12   114.72   115.30  114.04    114.73     113.3448    9920300
## 2019-03-13   114.84   114.85  113.72    114.09     112.7125   17994500
## 2019-03-14   114.12   114.77  113.93    114.48     113.0978   17409600
## 2019-03-15   114.50   115.48  113.68    114.96     113.5720   14932700
## 2019-03-18   113.03   114.00  112.46    113.12     111.7542   21769800
## 2019-03-19   112.95   113.71  109.80    110.00     108.6719   45384400
## 2019-03-20   110.31   110.88  108.05    109.99     108.6620   35588700
## 2019-03-21   110.10   110.15  108.37    108.66     107.3481   27438900
## 2019-03-22   108.33   109.00  107.51    108.23     106.9233   24133800
## 2019-03-25   108.49   109.07  107.32    107.79     106.4886   18187500
## 2019-03-26   108.40   110.34  108.27    110.14     108.8102   16693100
## 2019-03-27   110.45   111.09  109.73    110.28     108.9485   10794800
## 2019-03-28   110.60   111.27  110.24    110.71     109.3733   11748900
## 2019-03-29   111.56   111.60  110.39    111.03     109.6895   16358800
## 2019-04-01   111.59   112.87  111.38    112.51     111.1516   10785100
## 2019-04-02   113.15   113.31  111.92    111.96     110.6082    9231800
## 2019-04-03   112.70   113.11  112.21    112.52     111.1615   10534900
## 2019-04-04   113.24   114.86  113.03    114.75     113.3645   14378400
## 2019-04-05   114.97   115.13  114.31    115.00     113.6115   10907900
## 2019-04-08   115.00   115.83  114.64    114.96     113.5720   11054200
## 2019-04-09   115.61   117.16  115.28    116.86     115.4491   17340300
## 2019-04-10   117.58   118.03  116.28    117.16     115.7455   14091100
## 2019-04-11   117.73   117.86  116.32    116.60     115.1922   12784500
## 2019-04-12   127.91   130.90  126.36    130.06     128.4897   65253500
## 2019-04-15   131.05   132.70  129.79    132.04     130.4458   32773700
## 2019-04-16   131.75   132.13  129.56    129.90     128.3316   14564300
## 2019-04-17   129.81   132.36  129.28    131.75     130.1593   14253700
## 2019-04-18   131.77   132.87  131.11    132.45     130.8508   11890600
## 2019-04-22   131.90   132.20  131.03    131.68     130.0901   10197500
## 2019-04-23   133.39   134.24  132.65    133.36     131.7499   12159300
## 2019-04-24   133.11   135.75  132.72    135.10     133.4689   11449500
## 2019-04-25   135.36   138.88  134.84    137.24     135.5830   16382500
## 2019-04-26   138.70   140.04  137.51    139.92     138.2307   14167600
## 2019-04-29   142.19   142.37  137.61    139.30     137.6181   25746100
## 2019-04-30   139.12   139.39  136.03    136.97     135.3163   15253100
## 2019-05-01   137.49   138.17  136.24    136.38     134.7334    8906700
## 2019-05-02   135.38   135.69  132.38    134.14     132.5204   16580400
## 2019-05-03   135.22   135.31  133.78    134.33     132.7081    8874400
## 2019-05-06   132.54   135.33  132.30    135.00     133.3701    9955600
## 2019-05-07   135.35   135.97  132.76    133.44     131.8289   10972700
## 2019-05-08   133.50   135.70  133.31    134.99     133.3602   15423500
## 2019-05-09   135.03   136.48  132.84    133.59     131.9771   15297100
## 2019-05-10   133.01   134.32  131.23    134.04     132.4216    9858500
## 2019-05-13   131.50   132.66  130.55    131.34     129.7542   11389900
## 2019-05-14   133.52   134.66  132.05    133.20     131.5918   12001700
## 2019-05-15   132.55   135.21  132.04    134.68     133.0539    9734100
## 2019-05-16   135.29   136.40  134.81    135.50     133.8640    9689400
## 2019-05-17   134.26   135.98  134.03    135.04     133.4096    8964200
## 2019-05-20   134.25   134.40  133.25    133.91     132.2932    8009000
## 2019-05-21   134.57   134.57  133.11    134.09     132.4710    7050300
## 2019-05-22   133.82   134.17  133.39    133.85     132.2339    5093700
## 2019-05-23   132.71   133.55  131.95    132.73     131.1275    6743300
## 2019-05-24   133.10   133.29  132.22    132.79     131.1867    4570100
## 2019-05-28   133.21   134.04  132.59    132.62     131.0188    8938400
## 2019-05-29   131.96   132.15  130.78    131.57     129.9815    7749600
## 2019-05-30   131.88   132.68  131.34    132.20     130.6039    5274000
## 2019-05-31   130.96   132.93  130.78    132.04     130.4458    7420700
## 2019-06-03   132.02   132.95  131.49    132.47     130.8706    7901400
## 2019-06-04   133.45   134.88  132.92    134.82     133.1922    8247500
## 2019-06-05   135.41   136.00  134.94    135.94     134.2987    6842800
## 2019-06-06   136.51   137.44  135.73    137.21     135.5534    6027300
## 2019-06-07   137.60   138.76  137.33    138.04     136.3733    7026300
## 2019-06-10   138.88   138.88  136.07    137.07     135.4151    8479500
## 2019-06-11   137.59   137.75  134.94    135.08     133.4491    6352200
## 2019-06-12   135.09   136.28  134.82    135.72     134.0814    5537000
## 2019-06-13   137.95   141.85  137.61    141.74     140.0287   17939500
## 2019-06-14   142.05   142.95  140.53    141.65     139.9398   11125200
## 2019-06-17   140.81   141.48  139.11    140.97     139.2680    8542700
## 2019-06-18   141.99   143.51  138.97    139.24     137.5589   11231400
## 2019-06-19   139.51   141.07  138.58    140.92     139.2186    6877100
## 2019-06-20   141.98   142.23  139.91    142.02     140.3053    8485800
## 2019-06-21   141.95   142.00  140.00    140.23     138.5369   14150000
## 2019-06-24   140.12   140.47  137.80    139.22     137.5391   10497400
## 2019-06-25   139.02   140.41  138.67    139.94     138.2504   14675400
## 2019-06-26   140.36   140.74  139.51    140.40     138.7048    8842100
## 2019-06-27   141.00   141.74  138.92    139.30     137.6181    6466300
## 2019-06-28   139.41   140.21  138.61    139.64     137.9540   20078800
## 2019-07-01   140.45   141.95  139.22    141.65     139.9398    8972400
## 2019-07-02   141.40   142.86  141.27    142.53     140.8091    7554100
## 2019-07-03   142.70   143.00  142.00    142.98     141.2537    4150900
## 2019-07-05   141.42   142.89  140.70    142.45     141.6016    5596000
## 2019-07-08   142.18   142.23  140.97    141.02     140.1801    4993900
## 2019-07-09   140.06   141.72  139.75    141.61     140.7666    7396300
## 2019-07-10   142.40   144.25  142.01    143.54     142.6851    9533700
## 2019-07-11   144.01   145.34  143.54    143.56     142.7050   10471100
## 2019-07-12   144.15   145.43  144.00    144.88     144.0172    8510000
## 2019-07-15   145.13   145.36  143.97    145.06     144.1961    5289200
## 2019-07-16   144.75   144.99  143.81    144.30     143.4406    5854700
## 2019-07-17   144.60   144.68  142.37    142.57     141.7209    5296400
## 2019-07-18   142.46   142.46  140.17    141.63     140.7865    8031700
## 2019-07-19   142.24   142.24  139.74    139.85     139.0171    6109900
## 2019-07-22   141.25   141.44  140.01    140.84     140.0012    7706400
## 2019-07-23   141.65   142.54  140.27    141.26     140.4187    7514500
## 2019-07-24   141.40   141.59  140.40    141.29     140.4485    5896600
## 2019-07-25   141.40   143.23  141.04    143.21     142.3571    7294400
## 2019-07-26   143.74   145.19  143.05    144.65     143.7885    8139400
## 2019-07-29   145.59   147.15  145.17    146.39     145.5182   11674100
## 2019-07-30   145.76   146.80  144.61    144.93     144.0668    6563100
## 2019-07-31   144.99   145.00  142.34    143.01     142.1583    9710700
## 2019-08-01   143.34   144.53  141.26    141.85     141.0052    8911700
## 2019-08-02   140.99   142.22  139.94    141.71     140.8660    6539100
## 2019-08-05   139.14   140.23  137.03    138.30     137.4763   10669700
## 2019-08-06   140.41   141.95  138.37    141.87     141.0251   18818900
## 2019-08-07   134.93   135.87  132.26    134.86     134.0568   29084500
## 2019-08-08   137.90   138.00  136.01    137.89     137.0688   14054800
## 2019-08-09   137.10   139.24  136.76    138.52     137.6950    8811900
## 2019-08-12   137.31   137.57  135.23    135.75     134.9415    7918900
## 2019-08-13   135.00   137.92  135.00    137.01     136.1940    8145400
## 2019-08-14   135.06   135.15  132.68    132.85     132.0588   11797700
## 2019-08-15   133.50   134.28  132.47    133.41     132.6155    7590100
## 2019-08-16   134.02   135.50  133.83    135.20     134.3948    6353800
## 2019-08-19   136.79   137.62  134.28    135.29     134.4843   11741800
## 2019-08-20   133.33   136.04  133.26    135.13     134.3252    8628000
## 2019-08-21   134.61   136.06  134.05    135.76     134.9515    6325000
## 2019-08-22   136.20   136.67  134.69    136.08     135.2696    6967100
## 2019-08-23   135.51   136.44  131.02    131.67     130.8858   10190900
## 2019-08-26   134.19   134.64  132.55    134.61     133.8083    8093800
## 2019-08-27   135.77   135.99  134.19    134.49     133.6890    6013600
## 2019-08-28   134.06   136.73  133.52    136.55     135.7368    5950500
## 2019-08-29   138.12   138.68  137.42    137.84     137.0191    6532700
## 2019-08-30   138.29   138.50  136.76    137.26     136.4425    5622200
## 2019-09-03   136.37   136.42  135.06    136.31     135.4982    5997400
## 2019-09-04   136.74   138.20  136.39    137.89     137.0688    4285400
## 2019-09-05   138.92   139.27  138.32    138.84     138.0131    5143500
## 2019-09-06   139.13   139.81  138.25    139.55     138.7189    4090000
## 2019-09-09   139.58   140.08  138.48    138.83     138.0032    5458700
## 2019-09-10   138.12   138.44  134.58    135.79     134.9813   10974600
## 2019-09-11   135.56   136.24  134.93    136.19     135.3789    7896400
## 2019-09-12   136.84   138.67  136.54    137.50     136.6811    7333900
## 2019-09-13   137.85   138.22  137.11    138.02     137.1980    5209100
## 2019-09-16   136.29   137.24  135.30    135.80     134.9912    6329300
## 2019-09-17   135.80   136.73  135.67    136.31     135.4982    4726500
## 2019-09-18   136.41   137.07  135.72    136.80     135.9853    6857900
## 2019-09-19   137.00   137.36  133.00    133.30     132.5061   12216100
## 2019-09-20   133.03   133.23  131.61    132.27     131.4823   25231600
## 2019-09-23   131.99   132.89  131.89    132.46     131.6711    6096100
## 2019-09-24   134.01   134.15  131.50    131.97     131.1840   11904000
## 2019-09-25   131.79   133.42  131.22    133.09     132.2974    6523000
## 2019-09-26   133.14   133.30  129.06    131.27     130.4882   11615100
## 2019-09-27   130.10   131.00  128.92    129.96     129.1860    8401300
## 2019-09-30   130.35   130.86  129.82    130.32     129.5439    5532500
## 2019-10-01   130.80   131.78  129.51    129.55     128.7785    5978500
## 2019-10-02   128.51   129.22  127.57    129.14     128.3709    8296100
## 2019-10-03   128.60   129.43  127.54    128.15     127.3868    8498700
## 2019-10-04   128.69   130.44  128.61    130.27     129.4942    6732900
## 2019-10-07   130.27   131.57  129.04    130.90     130.1204    5784100
## 2019-10-08   129.90   130.04  128.37    128.47     127.7049    6526000
## 2019-10-09   129.10   129.85  128.06    129.33     128.5598    5004600
## 2019-10-10   129.22   130.14  128.88    129.34     128.5697    4512000
## 2019-10-11   130.30   130.99  129.84    130.02     129.2457    6559000
## 2019-10-14   130.13   130.30  129.48    129.70     128.9276    3710300
## 2019-10-15   130.01   130.89  129.40    129.76     128.9872    5914700
## 2019-10-16   129.76   131.83  129.70    130.86     130.0807    7610800
## 2019-10-17   132.53   133.44  130.51    132.37     131.5816    8005100
## 2019-10-18   132.37   133.16  130.89    130.89     130.1105    8694000
## 2019-10-21   131.44   131.73  130.02    130.26     129.4842    6257100
## 2019-10-22   133.15   133.68  132.10    132.40     131.6115    9291600
## 2019-10-23   132.45   132.68  130.76    131.13     130.3490    5970800
## 2019-10-24   131.52   131.70  129.63    130.26     129.4842    5746300
## 2019-10-25   130.50   131.76  130.03    130.90     130.1204    6696000
## 2019-10-28   131.45   132.08  130.14    130.53     129.7526    8940300
## 2019-10-29   130.50   130.77  129.44    129.48     128.7089    9305100
## 2019-10-30   129.69   130.31  129.15    129.60     128.8282   10880000
## 2019-10-31   129.53   130.15  128.75    129.92     129.1463    9628300
## 2019-11-01   130.99   132.80  130.51    132.75     131.9594    8507600
## 2019-11-04   134.12   134.14  132.47    132.92     132.1284    7631800
## 2019-11-05   132.77   132.77  130.89    131.45     130.6671    7404500
## 2019-11-06   131.60   131.66  130.66    131.27     130.4882    9132500
## 2019-11-07   132.35   133.70  131.75    132.96     132.1682   18047600
## 2019-11-08   140.22   140.25  136.74    137.96     137.1384   24175900
## 2019-11-11   137.50   138.56  136.21    136.74     135.9256   10138400
## 2019-11-12   138.03   139.34  136.74    138.58     137.7547   17284000
## 2019-11-13   138.58   149.92  136.84    148.72     147.8343   45725400
## 2019-11-14   148.00   150.63  146.28    147.15     146.2736   28393600
## 2019-11-15   147.14   147.20  144.21    144.67     143.8084   13733400
## 2019-11-18   144.75   149.04  143.69    147.65     146.7707   15353100
## 2019-11-19   148.18   149.48  146.80    148.38     147.4963   11504300
## 2019-11-20   148.09   148.99  146.03    146.93     146.0549    9448400
## 2019-11-21   147.16   147.47  146.26    146.90     146.0251    6241400
## 2019-11-22   147.21   149.44  146.68    148.29     147.4068    9639400
## 2019-11-25   148.80   150.21  147.70    149.69     148.7985   11316800
## 2019-11-26   151.75   153.41  150.61    151.64     150.7369   24949900
## 2019-11-27   152.30   152.57  151.15    151.48     150.5779    6155400
## 2019-11-29   151.48   152.47  151.01    151.58     150.6773    6284900
## 2019-12-02   152.94   152.97  149.10    150.62     149.7230   10351000
## 2019-12-03   147.74   149.11  146.87    148.58     147.6951    9273800
## 2019-12-04   149.30   149.33  148.18    148.28     147.3969    7684800
## 2019-12-05   148.69   148.83  147.10    147.44     146.5619    7363300
## 2019-12-06   148.40   148.61  147.18    147.66     146.7806    7084900
## 2019-12-09   147.96   148.51  145.40    146.21     145.3392   11515000
## 2019-12-10   145.26   146.85  145.05    146.10     145.2299    7084100
## 2019-12-11   147.39   147.89  146.76    147.59     146.7110    8253700
## 2019-12-12   147.92   148.93  147.25    147.76     146.8800    7748300
## 2019-12-13   147.43   147.80  146.31    146.38     146.3800    9350100
## 2019-12-16   147.59   148.65  146.55    148.46     148.4600    9134400
## 2019-12-17   148.10   148.46  147.62    147.73     147.7300    8776100
## 2019-12-18   147.77   147.95  146.19    146.26     146.2600    9467800
## 2019-12-19   146.18   146.80  145.32    146.15     146.1500   10243900
## 2019-12-20   147.15   147.88  145.77    146.88     146.8800   12629900
## 2019-12-23   145.91   146.33  144.33    144.68     144.6800    9314000
## 2019-12-24   144.58   145.43  144.45    145.29     145.2900    3508500
## 2019-12-26   145.40   145.86  145.17    145.70     145.7000    4422000
## 2019-12-27   146.05   146.51  145.45    145.75     145.7500    5495300
## 2019-12-30   145.75   145.87  143.40    143.77     143.7700    6602800
## 2019-12-31   143.67   144.77  143.26    144.63     144.6300    5662900
#Obtenemos la columna que es de nuestro interés, la cual será de DIS.close
Novocure <- NVCRdata[,4]
length(Novocure)
## [1] 252
#Convertimos en una serie temporal
tsNovoCure <- ts(Novocure, start = c(2019,1),frequency=365)

#Graficamos
plot(tsNovoCure)

#Se calculan las diferencias de la serie de datos con logaritmo
l_NovoCure<-diff(log(tsNovoCure))
plot(l_NovoCure)

#Calculo parámetros iniciales manera 1
Delta <- 1/365
alpha <- mean(l_NovoCure)/Delta
sigma <- sqrt(var(l_NovoCure)/Delta)
mu <- alpha +0.5*sigma^2
x0<-tsNovoCure[1]

#Calculo parámetros iniciales manera 2
x <- tsNovoCure
gBm <- setModel(drift="mu*x", diffusion="sigma*x", xinit=x0)
## Warning in yuima.warn("Solution variable (lhs) not specified. Trying to use state variables."): 
## YUIMA: Solution variable (lhs) not specified. Trying to use state variables.
mod <- setYuima(model=gBm, data=setData(tsNovoCure, delta=Delta))
set.seed(123)
fit <- qmle(mod, start=list(mu=0, sigma=1),
            lower=list(mu=0.1, sigma=0.1),
            upper=list(mu=100, sigma=10))
summary(fit)
## Quasi-Maximum likelihood estimation
## 
## Call:
## qmle(yuima = mod, start = list(mu = 0, sigma = 1), lower = list(mu = 0.1, 
##     sigma = 0.1), upper = list(mu = 100, sigma = 10))
## 
## Coefficients:
##        Estimate Std. Error
## sigma 0.2727174 0.01223059
## mu    0.4480226 0.32886867
## 
## -2 log L: 1021.129
#comparación
coef(fit)
##     sigma        mu 
## 0.2727174 0.4480226
sigma
##           DIS.close
## DIS.close 0.2677428
mu
##           DIS.close
## DIS.close 0.4475313
#Sigma 0.2727174 0.4480226 

#Sigma 0.2677428
#mu 0.4475313

gbm_vec <- function(nsim = 10000, t = 25, mu = 0, sigma = 0.1, S0 = 100, dt = 1./365) {
  # matrix of random draws - one for each day for each simulation
  epsilon <- matrix(rnorm(t*nsim), ncol = nsim, nrow = t)  
  # get GBM and convert to price paths
  gbm <- exp((mu - sigma * sigma / 2) * dt + sigma * epsilon * sqrt(dt))
  gbm <- apply(rbind(rep(S0, nsim), gbm), 2, cumprod)
  return(gbm)
}

gBm
## 
## Diffusion process
## Number of equations: 1
## Number of Wiener noises: 1
## Parametric model with 2 parameters
valores_simulados <- simulate(gBm, true.parameter = list(mu=mu, sigma=sigma))
## Warning in yuima.warn("'delta' (re)defined."): 
## YUIMA: 'delta' (re)defined.
plot(valores_simulados)

#PROBAR CON FORMA 1 Y FORMA 2
nsim <- 1000
t <- 365
mu <- 0.4475313
sigma <- 0.2677428
S0 <- 108.97
dt = 1/365
gbm <- gbm_vec(nsim, t, mu, sigma, S0, dt)

gbm_df <- as.data.frame(gbm) %>%
  mutate(ix = 1:nrow(gbm)) %>%
  pivot_longer(-ix, names_to = 'sim', values_to = 'price')
gbm_df %>%
  ggplot(aes(x=ix, y=price, color=sim)) +
  geom_line() +
  theme(legend.position = 'none')

data.frame(price = gbm[259, ]) %>%
  ggplot(aes(x = price)) +
  geom_histogram(aes(y = ..density..), binwidth = 0.1) +
  geom_density() + 
  ggtitle('terminal price distribution')

D <- gbm[259, ] %>%
  density()

D$x[which.max(D$y)] 
## [1] 147.6409