title: Derivative of a smoothed curve
body: <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th></th>
  <th>point</th>
  <th>doy</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>0.6917529</td>
  <td>49</td>
  </tr>
  <tr>
  <td>2</td>
  <td>0.6917501</td>
  <td>49</td>
  </tr>
  <tr>
  <td>3</td>
  <td>0.6917529</td>
  <td>49</td>
  </tr>
  <tr>
  <td>4</td>
  <td>0.6917501</td>
  <td>49</td>
  </tr>
  <tr>
  <td>5</td>
  <td>0.2857624</td>
  <td>59</td>
  </tr>
  <tr>
  <td>6</td>
  <td>0.2857624</td>
  <td>59</td>
  </tr>
  <tr>
  <td>7</td>
  <td>0.1809273</td>
  <td>109</td>
  </tr>
  <tr>
  <td>8</td>
  <td>0.1808737</td>
  <td>109</td>
  </tr>
  <tr>
  <td>9</td>
  <td>0.1809273</td>
  <td>109</td>
  </tr>
  <tr>
  <td>10</td>
  <td>0.1808737</td>
  <td>109</td>
  </tr>
  <tr>
  <td>11</td>
  <td>0.3589439</td>
  <td>119</td>
  </tr>
  <tr>
  <td>12</td>
  <td>0.3588334</td>
  <td>119</td>
  </tr>
  <tr>
  <td>13</td>
  <td>0.3589439</td>
  <td>119</td>
  </tr>
  <tr>
  <td>14</td>
  <td>0.3588334</td>
  <td>119</td>
  </tr>
  <tr>
  <td>15</td>
  <td>0.3589439</td>
  <td>119</td>
  </tr>
  <tr>
  <td>16</td>
  <td>0.3588334</td>
  <td>119</td>
  </tr>
  <tr>
  <td>17</td>
  <td>0.3362660</td>
  <td>119</td>
  </tr>
  <tr>
  <td>18</td>
  <td>0.3365558</td>
  <td>119</td>
  </tr>
  <tr>
  <td>19</td>
  <td>0.7751922</td>
  <td>139</td>
  </tr>
  <tr>
  <td>20</td>
  <td>0.7751910</td>
  <td>139</td>
  </tr>
  <tr>
  <td>21</td>
  <td>0.7751922</td>
  <td>139</td>
  </tr>
  <tr>
  <td>22</td>
  <td>0.7751910</td>
  <td>139</td>
  </tr>
  <tr>
  <td>23</td>
  <td>0.8126297</td>
  <td>149</td>
  </tr>
  <tr>
  <td>24</td>
  <td>0.8126118</td>
  <td>149</td>
  </tr>
  <tr>
  <td>25</td>
  <td>0.8126297</td>
  <td>149</td>
  </tr>
  <tr>
  <td>26</td>
  <td>0.8126118</td>
  <td>149</td>
  </tr>
  <tr>
  <td>27</td>
  <td>0.8251105</td>
  <td>159</td>
  </tr>
  <tr>
  <td>28</td>
  <td>0.8251142</td>
  <td>159</td>
  </tr>
  <tr>
  <td>29</td>
  <td>0.8251105</td>
  <td>159</td>
  </tr>
  <tr>
  <td>30</td>
  <td>0.8251142</td>
  <td>159</td>
  </tr>
  <tr>
  <td>31</td>
  <td>0.8403495</td>
  <td>189</td>
  </tr>
  <tr>
  <td>32</td>
  <td>0.8403497</td>
  <td>189</td>
  </tr>
  <tr>
  <td>33</td>
  <td>0.8403495</td>
  <td>189</td>
  </tr>
  <tr>
  <td>34</td>
  <td>0.8403497</td>
  <td>189</td>
  </tr>
  <tr>
  <td>35</td>
  <td>0.7746058</td>
  <td>196</td>
  </tr>
  <tr>
  <td>36</td>
  <td>0.7746463</td>
  <td>196</td>
  </tr>
  <tr>
  <td>37</td>
  <td>0.7746058</td>
  <td>196</td>
  </tr>
  <tr>
  <td>38</td>
  <td>0.7746463</td>
  <td>196</td>
  </tr>
  <tr>
  <td>39</td>
  <td>0.6160177</td>
  <td>226</td>
  </tr>
  <tr>
  <td>40</td>
  <td>0.6160177</td>
  <td>226</td>
  </tr>
  <tr>
  <td>41</td>
  <td>0.5651071</td>
  <td>239</td>
  </tr>
  <tr>
  <td>42</td>
  <td>0.5651133</td>
  <td>239</td>
  </tr>
  <tr>
  <td>43</td>
  <td>0.5651071</td>
  <td>239</td>
  </tr>
  <tr>
  <td>44</td>
  <td>0.5651133</td>
  <td>239</td>
  </tr>
  <tr>
  <td>45</td>
  <td>0.3736361</td>
  <td>249</td>
  </tr>
  <tr>
  <td>46</td>
  <td>0.3736361</td>
  <td>249</td>
  </tr>
  <tr>
  <td>47</td>
  <td>0.3366151</td>
  <td>256</td>
  </tr>
  <tr>
  <td>48</td>
  <td>0.3365827</td>
  <td>256</td>
  </tr>
  <tr>
  <td>49</td>
  <td>0.3366151</td>
  <td>256</td>
  </tr>
  <tr>
  <td>50</td>
  <td>0.3365827</td>
  <td>256</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Using the above data, i am trying to apply GAM smoothing curve to the point column and also plot the smoothed curve with the derivative of the smooth on the same plot. I tried the following</p>
  <pre><code>ggplot(df, aes(doy)) +
      geom_line(aes(y=point),col='blue') +
      geom_smooth(aes(y=point),method = &quot;gam&quot;, formula = y ~s(x,bs='cr'), size = 1, se = F, colour = &quot;blue&quot;)
  }
  </code></pre>
  <p>This got me only the smoothed curve and then i tried getting the values of the smoothed curve using:</p>
  <pre><code>library(mgcv)
  mod = gam(point~s(n=doy,bs='cr'),data=df)
  df$fitted = fitted(mod)
  </code></pre>
  <p>But i'm having issues getting and plotting the derivative as i have used:</p>
  <pre><code>derivatives(mod)
  
  # And
  fderiv(mod)
  
  
  # Lastly
  first_diff = function(x,y){
    vec=vector(length=length(x))
    vec[1]=0
    for (i in 2:length(x)){
      vec[i]=(y[i]-y[i-1])/(x[i]-x[i-1])
    }
    return(vec)
  }
  </code></pre>
  <p>I was unable to come up with the derivative (rate of change of points with the day of the year) for the curve</p>