<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/simultaneous-intervals-for-derivatives-of-smooths-revisited/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.fromthebottomoftheheap.net/2017/03/21/simultaneous-intervals-for-derivatives-of-smooths/"> From the Bottom of the Heap - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>
Eighteen months ago <a target="_blank" rel="nofollow" href="http://www.fromthebottomoftheheap.net/2016/12/15/simultaneous-interval-revisited/">I screwed up</a>! I’d written a <a target="_blank" rel="nofollow" href="http://www.fromthebottomoftheheap.net/2014/06/16/simultaneous-confidence-intervals-for-derivatives/">post</a> in which I described the use of simulation from the posterior distribution of a fitted GAM to derive simultaneous confidence intervals for the derivatives of a penalized spline. It was a nice post that attracted some interest. It was also wrong. In December I corrected the first part of that mistake by illustrating one approach to compute an actual simultaneous interval, but only for the fitted smoother. At the time I thought that the approach I outlined would translate to the derivatives but I was being lazy then Christmas came and went and I was back to teaching — you know how it goes. Anyway, in this post I hope to finally rectify my past stupidity and show how the approach used to generate simultaneous intervals from the December 2016 post can be applied to the derivatives of a spline.
</p>
<p>
If you haven’t read the December 2016 post I suggest you do so as there I explain this:
</p>
<p>
<span>[<br>
\begin{align}<br>
\mathbf{\hat{f}_g} &amp;amp;\pm m_{1 – \alpha} \begin{bmatrix}<br>
\widehat{\mathrm{st.dev}} (\hat{f}(g_1) – f(g_1)) \\<br>
\widehat{\mathrm{st.dev}} (\hat{f}(g_2) – f(g_2)) \\<br>
\vdots \\<br>
\widehat{\mathrm{st.dev}} (\hat{f}(g_M) – f(g_M)) \\<br>
\end{bmatrix}<br>
\end{align}<br>
]</span>
</p>
<p>
This equation states that the critical value for a 100(1 – <span>()</span>)% simultaneous interval is given by the 100(1 – <span>()</span>)% quantile of the distribution of the standard errors of deviations of the fitted values from the true values of the smoother. We don’t know this distribution, so we generated realizations from it using simulation, and used the empirical quantiles of the simulated distribution to give the appropriate critical value <span>(m)</span> with which to calculate the simultaneous interval. In that post I worked my way through some R code to show how you can calculate this for a fitted spline.
</p>
<p>
To keep this post relatively short, I won’t rehash the discussion of the code used to compute the critical value <span>(m)</span>. I also won’t cover in detail how these derivatives are computed. We use finite differences and the general approach is explained in an <a target="_blank" rel="nofollow" href="http://www.fromthebottomoftheheap.net/2014/05/15/identifying-periods-of-change-with-gams/">older post</a>. I don’t recommend you use the code in that post for real data analysis, however. Whilst I was putting together this post I re-wrote the derivative code as well as that for computing point-wise and simultaneous intervals and started a new R package <strong>tsgam</strong>. <strong>tsgam</strong> is is <a target="_blank" rel="nofollow" href="http://github.com/gavinsimpson/tsgam">available on GitHub</a> and we’ll use it here. Note this package isn’t even at version 0.1 yet, but the code for derivatives and intervals has been through several iterations now and works well whenever I have tested it.
</p>
<p>
Assuming you have the <strong>devtools</strong> package installed, you can install <strong>tsgam</strong> using
</p>
<figure>
<pre><code data-lang="r"><span>devtools</span><span>::</span><span>install_github</span><span>(</span><span>&quot;gavinsimpson/tsgam&quot;</span><span>)</span></code></pre>
</figure>
<p>
As example data, I’ll again use the strontium isotope data set included in the <strong>SemiPar</strong> package, and which is extensively analyzed in the monograph <em>Semiparametric Regression</em> <span data-cites="Ruppert2003-pt">(Ruppert et al., 2003)</span>. First, load the packages we’ll need as well as the data, which is data set <code>fossil</code>. If you don’t have <strong>SemiPar</strong> installed, install it using <code>install.packages(“SemiPar”)</code> before proceeding
</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>&quot;mgcv&quot;</span><span>)</span><span> </span><span># fit the GAM
</span><span>library</span><span>(</span><span>&quot;tsgam&quot;</span><span>)</span><span> </span><span># code for derivatives &amp; intervals
</span><span>library</span><span>(</span><span>&quot;ggplot2&quot;</span><span>)</span><span> </span><span># package for nice plots
</span><span>theme_set</span><span>(</span><span>theme_bw</span><span>())</span><span> </span><span># simpler theme for the plots
</span><span>data</span><span>(</span><span>fossil</span><span>,</span><span> </span><span>package</span><span> </span><span>=</span><span> </span><span>&quot;SemiPar&quot;</span><span>)</span><span> </span><span>#</span><span> </span><span>load</span><span> </span><span>the</span><span> </span><span>data</span></code></pre>
</figure>
<p>
The <code>fossil</code> data set includes two variables and is a time series of strontium isotope measurements on samples from a sediment core. The data are shown below using <code>ggplot()</code>
</p>
<figure>
<pre><code data-lang="r"><span>ggplot</span><span>(</span><span>fossil</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>age</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>strontium.ratio</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>()</span><span> </span><span>+</span><span> </span><span>scale_x_reverse</span><span>()</span></code></pre>
</figure>
<figure>
<img alt="The strontium isotope example data used in the post" src="https://i2.wp.com/www.fromthebottomoftheheap.net/assets/img/posts/simultaneous-intervals-for-derivatives-of-smooths-plot-fossil-data-1.png?w=456" data-recalc-dims="1">
<figcaption>
The strontium isotope example data used in the post<br>
</figcaption>
</figure>
<p>
The aim of the analysis of these data is to model how the measured strontium isotope ratio changed through time, using a GAM to estimate the clearly non-linear change in the response. As time is the complement of sediment age, we should probably model this on that time scale, especially if you wanted to investigate residual temporal auto-correlation. This requires creating a new variable <code>negAge</code> for modelling purposes only
</p>
<figure>
<pre><code data-lang="r"><span>fossil</span><span> </span><span>&lt;-</span><span> </span><span>transform</span><span>(</span><span>fossil</span><span>,</span><span> </span><span>negAge</span><span> </span><span>=</span><span> </span><span>-</span><span>age</span><span>)</span></code></pre>
</figure>
<p>
As per the previous post a reasonable GAM for these data is fitted using <strong>mgcv</strong> and <code>gam()</code>
</p>
<figure>
<pre><code data-lang="r"><span>m</span><span> </span><span>&lt;-</span><span> </span><span>gam</span><span>(</span><span>strontium.ratio</span><span> </span><span>~</span><span> </span><span>s</span><span>(</span><span>negAge</span><span>,</span><span> </span><span>k</span><span> </span><span>=</span><span> </span><span>20</span><span>),</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>fossil</span><span>,</span><span> </span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;REML&quot;</span><span>)</span></code></pre>
</figure>
<p>
Having fitted the model we should do some evaluation of it but I’m going to skip that here and move straight to computing the derivative of the fitted spline and a simultaneous interval for it. First we set some constants that we can refer to throughout the rest of the post
</p>
<figure>
<pre><code data-lang="r"><span>## parameters for testing
</span><span>UNCONDITIONAL</span><span> </span><span>&lt;-</span><span> </span><span>FALSE</span><span> </span><span># unconditional or conditional on estimating smooth params?
</span><span>N</span><span> </span><span>&lt;-</span><span> </span><span>10000</span><span> </span><span># number of posterior draws
</span><span>n</span><span> </span><span>&lt;-</span><span> </span><span>500</span><span> </span><span># number of newdata values
</span><span>EPS</span><span> </span><span>&lt;-</span><span> </span><span>1e-07</span><span> </span><span>#</span><span> </span><span>finite</span><span> </span><span>difference</span></code></pre>
</figure>
<p>
To facilitate checking that this interval has the correct coverage properties I’m going to fix the locations where we’ll evaluate the derivative, calculating the vector of values to predict at once only. Normally you wouldn’t need to do this just to compute the derivatives and associated confidence intervals — you would just need to set the number of values <code>n</code> over the range of the predictors you want — and if you have a model with several splines it is probably easier to let <strong>tsgam</strong> handle this part for you.
</p>
<figure>
<pre><code data-lang="r"><span>## where are we going to predict at?
</span><span>newd</span><span> </span><span>&lt;-</span><span> </span><span>with</span><span>(</span><span>fossil</span><span>,</span><span> </span><span>data.frame</span><span>(</span><span>negAge</span><span> </span><span>=</span><span> </span><span>seq</span><span>(</span><span>min</span><span>(</span><span>negAge</span><span>),</span><span> </span><span>max</span><span>(</span><span>negAge</span><span>),</span><span> </span><span>length</span><span> </span><span>=</span><span> </span><span>n</span><span>)))</span></code></pre>
</figure>
<p>
The <code>fderiv()</code> function in <strong>tsgam</strong> computes the first derivative of any splines in the supplied GAM<a target="_blank" rel="nofollow" href="http://www.fromthebottomoftheheap.net/2017/03/21/simultaneous-intervals-for-derivatives-of-smooths/#fn1"><sup>1</sup></a> or you can request derivatives for a specified smooth term. As we have only a single smooth term in the model, we simply pass in the model and the data frame of locations at which to evaluate the derivative
</p>
<figure>
<pre><code data-lang="r"><span>fd</span><span> </span><span>&lt;-</span><span> </span><span>fderiv</span><span>(</span><span>m</span><span>,</span><span> </span><span>newdata</span><span> </span><span>=</span><span> </span><span>newd</span><span>,</span><span> </span><span>eps</span><span> </span><span>=</span><span> </span><span>EPS</span><span>,</span><span> </span><span>unconditional</span><span> </span><span>=</span><span> </span><span>UNCONDITIONAL</span><span>)</span></code></pre>
</figure>
<p>
(we set <code>eps = EPS</code>, so we have the same grid shift later in the post when checking coverage properties, and don’t account for the uncertainty due to estimating the smoothness parameters (<code>unconditional = FALSE</code>), normally you can leave these at the defaults). The object returned by <code>fderiv()</code>
</p>
<figure>
<pre><code data-lang="r"><span>str</span><span>(</span><span>fd</span><span>,</span><span> </span><span>max</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">List of 6
 $ derivatives  :List of 1
 $ terms        : chr &quot;negAge&quot;
 $ model        :List of 52
  ..- attr(*, &quot;class&quot;)= chr [1:3] &quot;gam&quot; &quot;glm&quot; &quot;lm&quot;
 $ eps          : num 1e-07
 $ eval         :'data.frame':  500 obs. of  1 variable:
 $ unconditional: logi FALSE
 - attr(*, &quot;class&quot;)= chr &quot;fderiv&quot;</code></pre>
</figure>
<p>
contains a component <code>derivatives</code> that contains the evaluated derivatives for all or the selected smooth terms. The other components include a copy of the fitted model and some additional parameters that are required for the confidence intervals. Confidence intervals for the derivatives are computed using the <code>confint()</code> method. The <code>type</code> argument specifies whether point-wise or simultaneous intervals are required. For the latter, the number of simulations to draw is required via <code>nsim</code>
</p>
<figure>
<pre><code data-lang="r"><span>set.seed</span><span>(</span><span>42</span><span>)</span><span> </span><span># set the seed to make this repeatable </span><span>sint</span><span> </span><span>&lt;-</span><span> </span><span>confint</span><span>(</span><span>fd</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;simultaneous&quot;</span><span>,</span><span> </span><span>nsim</span><span> </span><span>=</span><span> </span><span>N</span><span>)</span></code></pre>
</figure>
<p>
To make it easier to work with the results I wrote the <code>confint()</code> method so that it returned the confidence interval as a tidy data frame suitable for plotting with <strong>ggplot2</strong>. <code>sint</code> is a data frame with an identifier for which smooth term each row relates to (<code>term</code>), plus columns containing the estimated (<code>est</code>) derivative and the lower and upper confidence interval
</p>
<figure>
<pre><code data-lang="r"><span>head</span><span>(</span><span>sint</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">    term     lower      est    upper
1 negAge -0.000053 -6.1e-06 0.000041
2 negAge -0.000053 -6.1e-06 0.000041
3 negAge -0.000053 -6.1e-06 0.000040
4 negAge -0.000052 -6.1e-06 0.000040
5 negAge -0.000052 -6.1e-06 0.000040
6 negAge -0.000051 -6.0e-06 0.000039</code></pre>
</figure>
<p>
The estimated derivative plus its 95% simultaneous confidence interval are shown below
</p>
<figure>
<pre><code data-lang="r"><span>ggplot</span><span>(</span><span>cbind</span><span>(</span><span>sint</span><span>,</span><span> </span><span>age</span><span> </span><span>=</span><span> </span><span>-</span><span>newd</span><span>$</span><span>negAge</span><span>),</span><span>
</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>age</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>est</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_hline</span><span>(</span><span>aes</span><span>(</span><span>yintercept</span><span> </span><span>=</span><span> </span><span>0</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_ribbon</span><span>(</span><span>aes</span><span>(</span><span>ymin</span><span> </span><span>=</span><span> </span><span>lower</span><span>,</span><span> </span><span>ymax</span><span> </span><span>=</span><span> </span><span>upper</span><span>),</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.2</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>scale_x_reverse</span><span>()</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;First derivative&quot;</span><span>,</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Age&quot;</span><span>)</span></code></pre>
</figure>
<figure>
<img alt="Estimated first derivative of the spline fitted to the strontium isotope data. The grey band shows the 95% simultaneous interval." src="https://i1.wp.com/www.fromthebottomoftheheap.net/assets/img/posts/simultaneous-intervals-for-derivatives-of-smooths-plot-sint-1.png?w=456" data-recalc-dims="1">
<figcaption>
Estimated first derivative of the spline fitted to the strontium isotope data. The grey band shows the 95% simultaneous interval.<br>
</figcaption>
</figure>
<p>
So far so good.
</p>
<p>
Having thought about how to apply the theory outlined in the previous post, it seems that all we need to do to apply it to derivatives is to make the assumption that <em>the estimate of the first derivative is unbiased</em> and hence we can proceed as we did in the previous post by computing <code>BUdiff</code> using a multivariate normal with zero mean vector and the Bayesian covariance matrix of the model coefficients. Where the version for derivatives differs is that we use a prediction matrix for the derivatives instead of for the fitted spline. This prediction matrix is created as follows
</p>
<ol type="1">
<li>
generate a prediction matrix from the current model for the locations in <code>newd</code>,
</li>
<li>
generate a second prediction matrix as before but for slightly shifted locations <code>newd + eps</code>
</li>
<li>
difference these two prediction matrices yielding the prediction matrix for the first differences <code>Xp</code>
</li>
<li>
for each smooth in turn
<ol type="1">
<li>
create a zero matrix, <code>Xi</code>, of the same dimensions as the prediction matrices
</li>
<li>
fill in the columns of <code>Xi</code> that relate to the current smooth using the values of the same columns from <code>Xp</code>
</li>
<li>
multiply <code>Xi</code> by the vector of model coefficients to yield predicted first differences
</li>
<li>
calculate the standard error of these predictions
</li>
</ol>
</li>
</ol>
<p>
The matrix <code>Xi</code> is supplied for each smooth term in the <code>derivatives</code> component of the object returned by <code>fderiv()</code>.
</p>
<p>
Once I’d grokked this one basic assumption about the unbiasedness of the first derivative, the rest of the translation of the method to derivatives fell into place. As we are using finite differences, we may be a little biased in estimating the first derivatives, but this can be reduced by makes <code>eps</code> smaller, thought the default probably suffices.
</p>
<p>
To see the detail of how this is done, look at the source code for <code>tsgam:::simultaneous</code>, which apart from a bit of renaming of objects follows closely the code in the <a target="_blank" rel="nofollow" href="http://www.fromthebottomoftheheap.net/2016/12/15/simultaneous-interval-revisited/">previous post</a>.
</p>
<p>
Having computed the purported simultaneous interval for the derivatives of the trend, we should do what I didn’t do in the original posts about these intervals and go and look at the coverage properties of the generated interval.
</p>
<p>
To do that I’m going to simulate a large number, <code>N</code>, of draws from the posterior distribution of the model. Each of these draws is a fitted spline that includes the uncertainty in the estimated model coefficients. Note that I’m not including a correction here for the uncertainty due to smoothing parameters being estimated — you can set <code>unconditional = TRUE</code> throughout (or change <code>UNCONDITIONAL</code> above) to include this extra uncertainty if you wish.
</p>
<figure>
<pre><code data-lang="r"><span>Vb</span><span> </span><span>&lt;-</span><span> </span><span>vcov</span><span>(</span><span>m</span><span>,</span><span> </span><span>unconditional</span><span> </span><span>=</span><span> </span><span>UNCONDITIONAL</span><span>)</span><span>
</span><span>set.seed</span><span>(</span><span>24</span><span>)</span><span>
</span><span>sims</span><span> </span><span>&lt;-</span><span> </span><span>MASS</span><span>::</span><span>mvrnorm</span><span>(</span><span>N</span><span>,</span><span> </span><span>mu</span><span> </span><span>=</span><span> </span><span>coef</span><span>(</span><span>m</span><span>),</span><span> </span><span>Sigma</span><span> </span><span>=</span><span> </span><span>Vb</span><span>)</span><span>
</span><span>X</span><span>0</span><span> </span><span>&lt;-</span><span> </span><span>predict</span><span>(</span><span>m</span><span>,</span><span> </span><span>newd</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;lpmatrix&quot;</span><span>)</span><span>
</span><span>newd</span><span> </span><span>&lt;-</span><span> </span><span>newd</span><span> </span><span>+</span><span> </span><span>EPS</span><span>
</span><span>X</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>predict</span><span>(</span><span>m</span><span>,</span><span> </span><span>newd</span><span>,</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;lpmatrix&quot;</span><span>)</span><span>
</span><span>Xp</span><span> </span><span>&lt;-</span><span> </span><span>(</span><span>X</span><span>1</span><span> </span><span>-</span><span> </span><span>X</span><span>0</span><span>)</span><span> </span><span>/</span><span> </span><span>EPS</span><span>
</span><span>derivs</span><span> </span><span>&lt;-</span><span> </span><span>Xp</span><span> </span><span>%*%</span><span> </span><span>t</span><span>(</span><span>sims</span><span>)</span></code></pre>
</figure>
<p>
The code above basically makes a large number of draws from the model posterior and applies the steps of the algorithm outlined above to generate <code>derivs</code>, a matrix containing 10000 draws from the posterior distribution of the model derivatives. Our simultaneous interval should entirely contain about 95% of these posterior draws. Note that a draw here refers to the entire set of evaluations of the first derivative for each posterior draw from the model. The plot below shows 50 such draws (lines)
</p>
<figure>
<pre><code data-lang="r"><span>set.seed</span><span>(</span><span>2</span><span>)</span><span>
</span><span>matplot</span><span>(</span><span>derivs</span><span>[,</span><span> </span><span>sample</span><span>(</span><span>N</span><span>,</span><span> </span><span>50</span><span>)],</span><span> </span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;l&quot;</span><span>,</span><span> </span><span>lty</span><span> </span><span>=</span><span> </span><span>&quot;solid&quot;</span><span>)</span></code></pre>
</figure>
<figure>
<img alt="50 draws from the posterior distribution of the first derivative of the fitted spline." src="https://i1.wp.com/www.fromthebottomoftheheap.net/assets/img/posts/simultaneous-intervals-for-derivatives-of-smooths-plot-sample-of-derivs-1.png?w=456" data-recalc-dims="1">
<figcaption>
50 draws from the posterior distribution of the first derivative of the fitted spline.<br>
</figcaption>
</figure>
<p>
and 95% of the 10000 draws (lines) should lie <em>entirely</em> within the simultaneous interval if it has the right coverage properties. Put the other way, only 5% of the draws (lines) should ever venture outside the limits of the interval.
</p>
<p>
To check this is the case, we reuse the the <code>inCI()</code> function that checks if a draw lies entirely within the interval or not
</p>
<figure>
<pre><code data-lang="r"><span>inCI</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>x</span><span>,</span><span> </span><span>upr</span><span>,</span><span> </span><span>lwr</span><span>)</span><span> </span><span>{</span><span>
</span><span>all</span><span>(</span><span>x</span><span> </span><span>>=</span><span> </span><span>lwr</span><span> </span><span>&amp;</span><span> </span><span>x</span><span> </span><span>&lt;=</span><span> </span><span>upr</span><span>)</span><span>
</span><span>}</span></code></pre>
</figure>
<p>
As each <em>column</em> of <code>derivs</code> contains a different draw, we want to apply <code>inCI()</code> to each column in turn
</p>
<figure>
<pre><code data-lang="r"><span>fitsInCI</span><span> </span><span>&lt;-</span><span> </span><span>with</span><span>(</span><span>sint</span><span>,</span><span> </span><span>apply</span><span>(</span><span>derivs</span><span>,</span><span> </span><span>2L</span><span>,</span><span> </span><span>inCI</span><span>,</span><span> </span><span>upr</span><span> </span><span>=</span><span> </span><span>upper</span><span>,</span><span> </span><span>lwr</span><span> </span><span>=</span><span> </span><span>lower</span><span>))</span></code></pre>
</figure>
<p>
<code>inCI()</code> returns a <code>TRUE</code> if all the points that make up the line representing a single posterior draw lie within the interval and <code>FALSE</code> otherwise, therefore we can sum up the <code>TRUE</code>s (recall that a <code>TRUE == 1</code> and a <code>FALSE == 0</code>) and divide by the number of draws to get an estimate of the coverage properties of the interval. If we do this for our interval
</p>
<figure>
<pre><code data-lang="r"><span>sum</span><span>(</span><span>fitsInCI</span><span>)</span><span> </span><span>/</span><span> </span><span>length</span><span>(</span><span>fitsInCI</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">[1] 0.95</code></pre>
</figure>
<p>
we see that the interval includes 95% of the 10000 draws. Which, you’ll agree is pretty close to the desired coverage of 95%.
</p>
<p>
That’s it for this post; whilst the signs are encouraging that these simultaneous intervals have the required coverage properties, I’ve only looked at them for a simple single-term GAM, and only for a response that is conditionally distributed Gaussian. I also haven’t looked at anything other than the coverage at an expected 95%. If you do use this in your work, please do check that the interval is working as anticipated. If you do discover problems, please let me know either in the comments below or via email. The next task is to start thinking about extending these ideas to work with a wider range of GAMs that <strong>mgcv</strong> can fit, include location-scale models and models with factor-smooth interactions.
</p>
<h2>
References<br>
</h2>
<div>
<div>
<p>
Ruppert, D., Wand, M. P., and Carroll, R. J. (2003). <em>Semiparametric regression</em>. Cambridge University Press.
</p>
</div>
</div>
<section>
<hr>
<ol>
<li>
<p>
<code>fderiv()</code> currently works for smooths of a single variable fitted using <code>gam()</code> or <code>gamm()</code>. It hasn’t been tested with the location-scale extended families in newer versions of <strong>mgcv</strong> and I doubt it will work with them currently.<a target="_blank" rel="nofollow" href="http://www.fromthebottomoftheheap.net/2017/03/21/simultaneous-intervals-for-derivatives-of-smooths/#fnref1">↩</a>
</p>
</li>
</ol>
</section>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.fromthebottomoftheheap.net/2017/03/21/simultaneous-intervals-for-derivatives-of-smooths/"> From the Bottom of the Heap - R</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/g0B5KvMb56k"></a></div>