<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/semiparametric-regression-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://mathewanalytics.com/2018/03/05/semiparametric-regression-in-r/"> R – Mathew Analytics</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A. INTRODUCTION</p>
<p>When building statistical models, the goal is to define a compact and parsimonious mathematical representation of some data generating process. Many of these techniques require that one make assumptions about the data or how the analysis is specified. For example, Auto Regressive Integrated Moving Average (ARIMA) models require that the time series is weakly stationary or can be made so. Furthermore, ARIMA assumes that the data has no deterministic time trends, the variance of the error term is constant, and so forth. Assumptions are generally a good thing, but there are definitely situations in which one wants to free themselves from such “constraints.”</p>
<p>In the context of evaluating relationships between one or more target variables and a set of explanatory variables, semiparametric regression is one such technique that provides the user with some flexibility in modeling complex data without maintaining stringent assumptions. With semiparametric regression, the goal is to develop a properly specified model that integrates the simplicity of parametric estimation with the flexibility provided by nonparametric splines.</p>
<p>B. LINEAR REGRESSION</p>
<p>A core requirement of both linear and generalized linear regression is that the user must define a set of explanatory variables and functional form that describes the relationship between the predictors and response variable. So when the goal is to build a linear regression model that explains which social and political characteristics within a country account for different levels of macroeconomic performance, we must first specify a number of variables that may explain our target variable. Furthermore, we must specify the functional form of the relationship. In both linear and generalized linear regression, this refers to how we characterize and “encode” each of the explanatory variables. So if we include the log of yearly precipitation rate as<br>
a predictor such that <img alt="Y = B0 + B1*log(precipitation) " title="Y = B0 + B1*log(precipitation) " src="https://s0.wp.com/latex.php?latex=Y+%3D+B0+%2B+B1%2Alog%28precipitation%29+&bg=ffffff&%23038;fg=555555&%23038;s=0" class="latex">, the model would nonlinear in the variables (log(precipitation)) and linear in the parameters (<img alt="B1 " title="B1 " src="https://s0.wp.com/latex.php?latex=B1+&bg=ffffff&%23038;fg=555555&%23038;s=0" class="latex">). Linearity in the variables is not an assumption of linear or generalized linear regression. However, ignoring it could result in a mispecified model with incorrect estimates.</p>
<p>C. REGRESSION SPLINES</p>
<p>One way to ensure that the model is properly specified is through the use of nonparametric splines. Instead of assuming that we know the functional form for a regression model, the user would essentially estimate the appropriate functional form from the data. Compared to a traditional parametric model whereby we estimate one global value that represents the relationship between <img alt="X " title="X " src="https://s0.wp.com/latex.php?latex=X+&bg=ffffff&%23038;fg=555555&%23038;s=0" class="latex"> and <img alt="Y " title="Y " src="https://s0.wp.com/latex.php?latex=Y+&bg=ffffff&%23038;fg=555555&%23038;s=0" class="latex">, we would instead have a series of local estimates that characterize the relationship across different values of <img alt="X " title="X " src="https://s0.wp.com/latex.php?latex=X+&bg=ffffff&%23038;fg=555555&%23038;s=0" class="latex">. There are many different variations of splines that can utilized, including b-splines, natural splines, cubic splines, and so forth. However, numerous studies have shown that restricted cubic splines with three to five knots tend to perform best across a wide array of domains.</p>
<p>In our previous model where the goal was to identify how macroeconomic performance is affected by weather/precipitation, the linear regression model would look like <img alt="economy = B0 + B1*precipitation " title="economy = B0 + B1*precipitation " src="https://s0.wp.com/latex.php?latex=economy+%3D+B0+%2B+B1%2Aprecipitation+&bg=ffffff&%23038;fg=555555&%23038;s=0" class="latex">. After incorporating a spline for the explanatory variable, we can now characterize the functional form of the model as <img alt="economy = B0 + f(precipitation) " title="economy = B0 + f(precipitation) " src="https://s0.wp.com/latex.php?latex=economy+%3D+B0+%2B+f%28precipitation%29+&bg=ffffff&%23038;fg=555555&%23038;s=0" class="latex"> whereby <img alt="f " title="f " src="https://s0.wp.com/latex.php?latex=f+&bg=ffffff&%23038;fg=555555&%23038;s=0" class="latex"> characterized the relationship between <img alt="X " title="X " src="https://s0.wp.com/latex.php?latex=X+&bg=ffffff&%23038;fg=555555&%23038;s=0" class="latex"> and <img alt="Y " title="Y " src="https://s0.wp.com/latex.php?latex=Y+&bg=ffffff&%23038;fg=555555&%23038;s=0" class="latex"> conditional on <img alt="X " title="X " src="https://s0.wp.com/latex.php?latex=X+&bg=ffffff&%23038;fg=555555&%23038;s=0" class="latex">. An important thing to note when developing semiparametric models is that although it allow users flexibility by incorporating a smoothing term, the estimation process still requires some of the core assumptions in OLS and GLM, namely that the data keeps the requirements of independence, normally distributed errors, and constant error variance.</p>
<p>Before we look at how to implement semiparametric regression, it is worth noting that these types of models are often referred to as either general or generalized additive models. Furthermore, semiparametric variations of other regression models are available such as semiparametric quantile regression and even semiparametric nonlinear regression.</p>
<p>D. R EXAMPLE</p>
<p>For this post, I’m going to stick with the gam function in the mgcv package because it is usually a good starting point. Taking the previous use case, let’s create some data and construct a linear regression model that regresses economy as a function of weather. Given that the data is from a random distribution, we obviously find a weak relationship between the two variables.</p>
<pre>
mydat = data.frame(economy = rnorm(100),
                   weather = rnorm(100))
mod = lm(economy ~ weather, data=mydat)
summary(mod)
plot(economy ~ weather, data = mydat, cex = .8)
abline(mod, col=&quot;red&quot;)
</pre>
<p>Let us say that we know based on experience, theory, or other sources that linearity in the variables is a poor specification in this domain case. One thing that could be done is to incorporate a spline to create local estimates for our explanatory variable within the linear regression.</p>
<pre>
mod &lt;- lm(economy ~ bs(weather, knots = 3), data=mydat)
summary(mod)
plot(economy ~ weather, data = mydat, cex = .8)
lines(mydat$weather, predict(mod), lwd=2, col='red')
</pre>
<p>The same model could also be examined using gam. Take note of the edf value, which represents how much the explanatory variable is smoothed.</p>
<p>library(mgcv)<br>
mod &lt;-gam(economy ~ s(weather, bs = &quot;cr&quot;, k = 3),<br>
data = mydat,<br>
family = gaussian)<br>
summary(mod)<br>
plot(mod)</p>
<p>This example data is pretty silly, but this should give you some things to investigate in terms of learning how semiparametric models can help improve the inferences that you are examining.</p>
<p> </p>
<p><strong>Have questions, comments, interesting work, etc? Feel free to contact me at mathewanalytics@gmail.com</strong></p>
<p><strong>Up next will be a series of posts on causal inference, a topic that I’ve been trying to get a better understanding of over the past month.</strong></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://mathewanalytics.com/2018/03/05/semiparametric-regression-in-r/"> R – Mathew Analytics</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/EEZHBx1XfuI" height="1"></a></div>