<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Regression on Stats and R</title>
    <link>https://statsandr.com/tags/regression/</link>
    <description>Recent content in Regression on Stats and R</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <lastBuildDate>Tue, 30 Jan 2024 00:00:00 +0000</lastBuildDate>
    
	<atom:link href="https://statsandr.com/tags/regression/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Binary logistic regression in R</title>
      <link>https://statsandr.com/blog/binary-logistic-regression-in-r/</link>
      <pubDate>Tue, 30 Jan 2024 00:00:00 +0000</pubDate>
      
      <guid>https://statsandr.com/blog/binary-logistic-regression-in-r/</guid>
      <description>

&lt;div id=&#34;TOC&#34;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#introduction&#34; id=&#34;toc-introduction&#34;&gt;Introduction&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#linear-versus-logistic-regression&#34; id=&#34;toc-linear-versus-logistic-regression&#34;&gt;Linear versus logistic regression&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#univariable-versus-multivariable-logistic-regression&#34; id=&#34;toc-univariable-versus-multivariable-logistic-regression&#34;&gt;Univariable versus multivariable logistic regression&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#data&#34; id=&#34;toc-data&#34;&gt;Data&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#binary-logistic-regression-in-r&#34; id=&#34;toc-binary-logistic-regression-in-r&#34;&gt;Binary logistic regression in R&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#univariable-binary-logistic-regression&#34; id=&#34;toc-univariable-binary-logistic-regression&#34;&gt;Univariable binary logistic regression&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#quantitative-independent-variable&#34; id=&#34;toc-quantitative-independent-variable&#34;&gt;Quantitative independent variable&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#qualitative-independent-variable&#34; id=&#34;toc-qualitative-independent-variable&#34;&gt;Qualitative independent variable&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#multivariable-binary-logistic-regression&#34; id=&#34;toc-multivariable-binary-logistic-regression&#34;&gt;Multivariable binary logistic regression&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#interaction&#34; id=&#34;toc-interaction&#34;&gt;Interaction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#model-selection&#34; id=&#34;toc-model-selection&#34;&gt;Model selection&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#quality-of-a-model&#34; id=&#34;toc-quality-of-a-model&#34;&gt;Quality of a model&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#validity-of-the-predictions&#34; id=&#34;toc-validity-of-the-predictions&#34;&gt;Validity of the predictions&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#accuracy&#34; id=&#34;toc-accuracy&#34;&gt;Accuracy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#sensitivity-and-specificity&#34; id=&#34;toc-sensitivity-and-specificity&#34;&gt;Sensitivity and specificity&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#auc-and-roc-curve&#34; id=&#34;toc-auc-and-roc-curve&#34;&gt;AUC and ROC curve&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#reporting-results&#34; id=&#34;toc-reporting-results&#34;&gt;Reporting results&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#gtsummary-package&#34; id=&#34;toc-gtsummary-package&#34;&gt;{gtsummary} package&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#finalfit-package&#34; id=&#34;toc-finalfit-package&#34;&gt;{finalfit} package&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#conditions-of-application&#34; id=&#34;toc-conditions-of-application&#34;&gt;Conditions of application&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#conclusion&#34; id=&#34;toc-conclusion&#34;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

&lt;p&gt;&lt;img src=&#34;images/binary-logistic-regression-in-r.jpeg&#34; style=&#34;width:100.0%&#34; /&gt;&lt;/p&gt;
&lt;div id=&#34;introduction&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Introduction&lt;/h1&gt;
&lt;p&gt;Regression is a common tool in statistics to test and quantify relationships between variables.&lt;/p&gt;
&lt;p&gt;The two most common regressions are linear and logistic regressions. A &lt;strong&gt;linear&lt;/strong&gt; regression is used when the dependent variable is &lt;strong&gt;quantitative&lt;/strong&gt;, whereas a &lt;strong&gt;logistic&lt;/strong&gt; regression is used when the dependent variable is &lt;strong&gt;qualitative&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Both linear and logistic regressions are divided into different types. Before detailing them, let’s first recap of which type a variable can be:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A quantitative variable measures a quantity, the values it can take are numbers. It is divided into:
&lt;ul&gt;
&lt;li&gt;discrete: the values it can take are countable and have a finite number of possibilities (the values are often integers, for example the number of children), and&lt;/li&gt;
&lt;li&gt;continuous: the values it can take are not countable and have an infinite number of possibilities (the values are usually with decimals, or at least decimals are technically possible, for example the weight).&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;A qualitative variable (also known as categorical) is not numerical and its values fit into categories. It is also divided into two types:
&lt;ul&gt;
&lt;li&gt;nominal: no ordering is possible or implied in the categories (for example the sex), and&lt;/li&gt;
&lt;li&gt;ordinal: an order is implied in the categories (for example the health status, such as poor/reasonable/good).&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A &lt;strong&gt;binary&lt;/strong&gt; variable, also known as dichotomous, is a special case of qualitative nominal variable when there are &lt;strong&gt;only two categories&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;See more details and examples about &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/&#34;&gt;variable types&lt;/a&gt; if needed.&lt;/p&gt;
&lt;p&gt;Now that the types of a variable is clear, let’s summarize the different types of regression:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Linear regression:
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Simple&lt;/strong&gt; linear regression is used when the goal is to estimate the relationship between a quantitative continuous dependent variable (also often called outcome or response variable) and &lt;strong&gt;only one&lt;/strong&gt; independent variable (also often called explanatory variable, covariate or predictor) of any type.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multiple&lt;/strong&gt; linear regression is used when the goal is to estimate the relationship between a quantitative continuous dependent variable and &lt;strong&gt;two or more&lt;/strong&gt; independent variables (again, of any type).&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;Logistic regression:
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Binary&lt;/strong&gt; logistic regression is used when the goal is to estimate the relationship between a &lt;strong&gt;binary dependent variable&lt;/strong&gt; (= two outcomes), and one or more independent variables (of any type).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multinomial&lt;/strong&gt; logistic regression is used when the goal is to estimate the relationship between a &lt;strong&gt;nominal dependent variable&lt;/strong&gt; with three or more &lt;em&gt;unordered&lt;/em&gt; outcomes, and one or more independent variables (of any type).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ordinal&lt;/strong&gt; logistic regression is used when the goal is to estimate the relationship between an &lt;strong&gt;ordinal dependent variable&lt;/strong&gt; with three or more &lt;em&gt;ordered&lt;/em&gt; outcomes, and one or more independent variables (of any type).&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Note that there exists another type of regression; the Poisson regression. This type of regression is used when the goal is to estimate the relationship between a dependent variable which is in the form of count data (number of occurrences of an event of interest over a given period of time or space, e.g., &lt;span class=&#34;math inline&#34;&gt;\(0, 1, 2, \ldots\)&lt;/span&gt;), and one or more independent variables.&lt;/p&gt;
&lt;p&gt;Logistic regressions and poisson regressions are both part of a broader type of model called &lt;strong&gt;generalized linear models&lt;/strong&gt; (abbreviated as &lt;strong&gt;GLM&lt;/strong&gt;). The name “generalized linear models” comes from the fact that these models allow to “generalize” the classic linear model. Indeed, it can be used in many situations, for example when analyzing a dependent variable which is not necessarily quantitative continuous or when residuals are not normally distributed (which are prerequisites for a linear model).&lt;/p&gt;
&lt;p&gt;Linear regression and its application in R have already been presented in this &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/&#34;&gt;post&lt;/a&gt;. It is now time to present the logistic regression.&lt;/p&gt;
&lt;p&gt;Binary logistic regression being the most common and the easiest one to interpret among the different types of logistic regression, this post will focus only on the binary logistic regression. Other types of regression (multinomial &amp;amp; ordinal logistic regressions, as well as Poisson regressions are left for future posts).&lt;/p&gt;
&lt;p&gt;In this post, we will first explain when a logistic regression is more appropriate than a linear regression. We will then show how to perform a binary logistic regression in R, and how to interpret and report results. We will also present some plots in order to visualize results. Finally, we will cover the topics of model selection, quality of fit and underlying assumptions of a binary logistic regression. We will try to keep this tutorial as applied as possible by focusing on the applications in R and the interpretations. Mathematical details will be as concise as possible.&lt;/p&gt;
&lt;div id=&#34;linear-versus-logistic-regression&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Linear versus logistic regression&lt;/h2&gt;
&lt;p&gt;We know that a linear regression is a convenient way to estimate the relationship between a quantitative continuous dependent variable, and one or more independent variables (of any type).&lt;/p&gt;
&lt;p&gt;For instance, suppose we would like to estimate the relationship between two quantitative variables, &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt;. Using the ordinary least squares method (the most common estimator used in linear regression), we obtain the following regression line:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/binary-logistic-regression-in-r/index_files/figure-html/unnamed-chunk-1-1.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Now suppose we are interested in estimating the impact of age on whether or not a patient has a certain disease. Age is considered as a quantitative continuous variable, while having the disease is binary (a patient is either ill or healthy).&lt;/p&gt;
&lt;p&gt;Visually, we could have something like this:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/binary-logistic-regression-in-r/index_files/figure-html/unnamed-chunk-2-1.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;If we fit a regression line (using the ordinary least square method) to the points, we obtain the following plot:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/binary-logistic-regression-in-r/index_files/figure-html/unnamed-chunk-3-1.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;We see that the regression line goes below 0 and above 1 with respect to the &lt;span class=&#34;math inline&#34;&gt;\(y\)&lt;/span&gt;-axis. Since the dependent variable &lt;code&gt;disease&lt;/code&gt; cannot take values below 0 (= healthy) nor above 1 (= ill), it is obvious that a linear regression is not appropriate for these data!&lt;/p&gt;
&lt;p&gt;In addition to this limitation, the assumptions of normality and homoscedasticity, which are required in linear regression, are clearly not appropriate with these data since the dependent variable is binary and follows a Binomial distribution! R will not stop you from performing a linear regression on binary data, but this will produce a model of little interest.&lt;/p&gt;
&lt;p&gt;This is where a logistic regression becomes handy as it takes into consideration these limitations.&lt;/p&gt;
&lt;p&gt;Applied to our example, here is how the points are fitted using a binary logistic regression:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/binary-logistic-regression-in-r/index_files/figure-html/unnamed-chunk-4-1.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;It is clear that this model is more appropriate.&lt;/p&gt;
&lt;p&gt;The curve (known as a sigmoid) is obtained via a transformation of the predicted values. There are several possible choices for the link function, which aim is to constrain predicted values to be within the range of observed values.&lt;/p&gt;
&lt;p&gt;The most widely used in practice is the logit function, which relates the probability of occurrence of an event (bounded between 0 and 1) to the linear combination of independent variables. The logit function also turns out to be the canonical link function for a Bernoulli or Binomial distribution. This transformation ensures that no matter in which range the &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; values are located, &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt; will only take numbers between 0 and 1.&lt;/p&gt;
&lt;p&gt;One could say that the fitted values (= represented by the blue curve) taking values between 0 and 1 also does not seem to make sense since a patient can only be healthy or ill (and thus the dependent variable can only take the value 0 or 1, respectively). However, in a binary logistic regression it is not the outcome no disease/disease that is directly modeled, but the &lt;em&gt;likelihood&lt;/em&gt; that a patient has the disease or not given his or her characteristics. This likelihood will be framed in terms of a &lt;a href=&#34;https://statsandr.com/blog/the-9-concepts-and-formulas-in-probability-that-every-data-scientist-should-know/&#34;&gt;probability&lt;/a&gt; to observe or not the disease in a patient, which is indeed included between 0 and 1, or 0% and 100%.&lt;/p&gt;
&lt;p&gt;More generally, with a logistic regression we would like to &lt;strong&gt;model how the probability of success varies with the independent variables&lt;/strong&gt; and determine whether or not these changes are statistically significant. We are actually going to model the logarithm of the odds, and the logistic regression model will be written as follows:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\begin{align}
\log(odds(success)) &amp;amp;= logit(\pi) \\
&amp;amp;= \log\left(\frac{\pi}{1 - \pi}\right) \\
&amp;amp;= \beta_0 + \beta_1 X_1 + \cdots + \beta_p X_p
\end{align}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;where &lt;span class=&#34;math inline&#34;&gt;\(\pi\)&lt;/span&gt; &lt;span class=&#34;math inline&#34;&gt;\((0 \le \pi \le 1)\)&lt;/span&gt; is the probability of an event happening (success) and denoted &lt;span class=&#34;math inline&#34;&gt;\(\pi = P(success)\)&lt;/span&gt;. We find the values of &lt;span class=&#34;math inline&#34;&gt;\(\hat{\beta}_0\)&lt;/span&gt;, &lt;span class=&#34;math inline&#34;&gt;\(\hat{\beta}_1\)&lt;/span&gt;, &lt;span class=&#34;math inline&#34;&gt;\(\ldots\)&lt;/span&gt;, &lt;span class=&#34;math inline&#34;&gt;\(\hat{\beta}_p\)&lt;/span&gt;, which are used as estimates for &lt;span class=&#34;math inline&#34;&gt;\(\beta_0\)&lt;/span&gt;, &lt;span class=&#34;math inline&#34;&gt;\(\beta_1\)&lt;/span&gt;, &lt;span class=&#34;math inline&#34;&gt;\(\ldots\)&lt;/span&gt;, &lt;span class=&#34;math inline&#34;&gt;\(\beta_p\)&lt;/span&gt;, using the maximum likelihood method. This method is one of several methods used in statistics to estimate parameters of a mathematical model. The goal of the estimator is to estimate the parameters &lt;span class=&#34;math inline&#34;&gt;\(\beta_0\)&lt;/span&gt;, &lt;span class=&#34;math inline&#34;&gt;\(\beta_1\)&lt;/span&gt;, &lt;span class=&#34;math inline&#34;&gt;\(\ldots\)&lt;/span&gt;, &lt;span class=&#34;math inline&#34;&gt;\(\beta_p\)&lt;/span&gt; which maximize the log likelihood function. Different algorithms have been established over the years for this non-linear optimization, but this is beyond the scope of the post.&lt;/p&gt;
&lt;p&gt;Logistic regressions are very common in the medical field, for example to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;estimate the risk factors associated with a disease or a harmful condition,&lt;/li&gt;
&lt;li&gt;predict the risk of developing a disease based on a patient’s characteristics, or&lt;/li&gt;
&lt;li&gt;determine the most important biological factors associated with a specific disease or condition.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;However, logistic regressions are used in many other domains, for instance in:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;banking sector: estimate a debtor’s creditworthiness based on his or her profile (income, assets, liabilities, etc.),&lt;/li&gt;
&lt;li&gt;marketing: estimate a customer’s propensity to buy a product or service based on his or her profile (age, sex, salary, previous purchases, etc.),&lt;/li&gt;
&lt;li&gt;sports: estimate the probability of a player winning against another player as a function of the characteristics of the two opponents,&lt;/li&gt;
&lt;li&gt;politics: answer the question “Would a citizen vote for our political party at the next elections?”&lt;/li&gt;
&lt;li&gt;etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;univariable-versus-multivariable-logistic-regression&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Univariable versus multivariable logistic regression&lt;/h2&gt;
&lt;p&gt;Now that it is more clear when a binary logistic regression should be used, we show how to perform one in R. We start by presenting univariable binary logistic regressions, and then multivariable binary logistic regressions.&lt;/p&gt;
&lt;p&gt;Remember that in both cases, the dependent variable must be a qualitative variable with two outcomes (hence the name &lt;em&gt;binary&lt;/em&gt; logistic regression). The difference between a univariable and multivariable binary logistic regression lies in the fact that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;for a &lt;strong&gt;univariable&lt;/strong&gt; binary logistic regression, there is only &lt;strong&gt;one independent variable&lt;/strong&gt;, while&lt;/li&gt;
&lt;li&gt;for a &lt;strong&gt;multivariable&lt;/strong&gt; binary logistic regression, there are &lt;strong&gt;two ore more independent variables&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It is true that the term “univariable” may be confusing here because there are two variables in the model (i.e., one dependent variable and one independent variable). However, it is called univariable binary logistic regression to indicate that only one independent variable is considered in the model, as opposed to multivariable binary logistic regression where several independent variables are considered in the model.&lt;/p&gt;
&lt;p&gt;To draw a parallel with linear regression:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a univariable binary logistic regression is the equivalent of a simple linear regression, whereas&lt;/li&gt;
&lt;li&gt;a multivariable binary logistic regression is the equivalent of a multiple linear regression&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;when the dependent variable is binary instead of quantitative continuous. This is the reason a univariable binary logistic regression is sometimes called a simple binary logistic regression and a multivariable binary logistic regression sometimes called a multiple binary logistic regression.&lt;/p&gt;
&lt;p&gt;The terms univaria&lt;em&gt;ble&lt;/em&gt;/multivaria&lt;em&gt;ble&lt;/em&gt; should not be confused with univaria&lt;em&gt;te&lt;/em&gt;/multivaria&lt;em&gt;te&lt;/em&gt;. The number of dependent variables characterizes the model as univariate or multivariate; univariate refers to a model with only one dependent variable, while multivariate refers to a model that simultaneously predicts more than one dependent variable. Usually, the intent is to differentiate models based on the number of independent variables. This distinction is made thanks to the terms univariable and multivariable. Multivariable refers to a model relating multiple predictor variables to a dependent variable, whereas univariable refers to a model relating one single independent variable to a dependent variable.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;data&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Data&lt;/h1&gt;
&lt;p&gt;For these illustrations, we use the “Heart Disease” dataset, available from the &lt;code&gt;{kmed}&lt;/code&gt; R package. This data frame consists of 14 variables, of which only 5 of them are kept for this post:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;&lt;code&gt;age&lt;/code&gt;: age in years&lt;/li&gt;
&lt;li&gt;&lt;code&gt;sex&lt;/code&gt;: sex (FALSE = female, TRUE = male)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cp&lt;/code&gt;: chest pain type (1 = typical angina, 2 = atypical angina, 3 = non-anginal pain, 4 = asymptomatic)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;thalach&lt;/code&gt;: maximum heart rate achieved&lt;/li&gt;
&lt;li&gt;&lt;code&gt;class&lt;/code&gt;: diagnosis of heart disease (divided into 4 classes)&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# import and rename dataset
library(kmed)
dat &amp;lt;- heart

# select variables
library(dplyr)
dat &amp;lt;- dat |&amp;gt;
  select(
    age,
    sex,
    cp,
    thalach,
    class
  )

# print dataset&amp;#39;s structure
str(dat)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## &amp;#39;data.frame&amp;#39;:	297 obs. of  5 variables:
##  $ age    : num  63 67 67 37 41 56 62 57 63 53 ...
##  $ sex    : logi  TRUE TRUE TRUE TRUE FALSE TRUE ...
##  $ cp     : Factor w/ 4 levels &amp;quot;1&amp;quot;,&amp;quot;2&amp;quot;,&amp;quot;3&amp;quot;,&amp;quot;4&amp;quot;: 1 4 4 3 2 2 4 4 4 4 ...
##  $ thalach: num  150 108 129 187 172 178 160 163 147 155 ...
##  $ class  : int  0 2 1 0 0 0 3 0 2 1 ...
##  - attr(*, &amp;quot;na.action&amp;quot;)= &amp;#39;omit&amp;#39; Named int [1:6] 88 167 193 267 288 303
##   ..- attr(*, &amp;quot;names&amp;quot;)= chr [1:6] &amp;quot;88&amp;quot; &amp;quot;167&amp;quot; &amp;quot;193&amp;quot; &amp;quot;267&amp;quot; ...&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that the pipe operator &lt;code&gt;|&amp;gt;&lt;/code&gt; and the &lt;code&gt;{dplyr}&lt;/code&gt; package is used to select variables. See &lt;a href=&#34;https://statsandr.com/blog/introduction-to-data-manipulation-in-r-with-dplyr/&#34;&gt;more data manipulation techniques&lt;/a&gt; using this package if you are interested.&lt;/p&gt;
&lt;p&gt;For greater readability, we rename the variables &lt;code&gt;cp&lt;/code&gt;, &lt;code&gt;thalach&lt;/code&gt; and &lt;code&gt;class&lt;/code&gt; with more informative names:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# rename variables
dat &amp;lt;- dat |&amp;gt;
  rename(
    chest_pain = cp,
    max_heartrate = thalach,
    heart_disease = class
  )&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We transform the variables &lt;code&gt;sex&lt;/code&gt; and &lt;code&gt;chest_pain&lt;/code&gt; into &lt;a href=&#34;https://statsandr.com/blog/data-types-in-r/#factor&#34;&gt;factor&lt;/a&gt; and set the labels accordingly:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# recode sex
dat$sex &amp;lt;- factor(dat$sex,
  levels = c(FALSE, TRUE),
  labels = c(&amp;quot;female&amp;quot;, &amp;quot;male&amp;quot;)
)

# recode chest_pain
dat$chest_pain &amp;lt;- factor(dat$chest_pain,
  levels = 1:4,
  labels = c(&amp;quot;typical angina&amp;quot;, &amp;quot;atypical angina&amp;quot;, &amp;quot;non-anginal pain&amp;quot;, &amp;quot;asymptomatic&amp;quot;)
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For a binary logistic regression in R, it is recommended that all the qualitative variables are transformed into factors.&lt;/p&gt;
&lt;p&gt;In our case, &lt;code&gt;heart_disease&lt;/code&gt; (our dependent variable) is currently encoded as &lt;a href=&#34;https://statsandr.com/blog/data-types-in-r/#integer&#34;&gt;integer&lt;/a&gt; with values ranging from 0 to 4. Therefore, we first classify it into 2 classes by setting 0 for 0 values and 1 for non-0 values, using the &lt;code&gt;ifelse()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# recode heart_disease into 2 classes
dat$heart_disease &amp;lt;- ifelse(dat$heart_disease == 0,
  0,
  1
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We then transform it into a factor and set the labels accordingly using the &lt;code&gt;factor()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# set labels for heart_disease
dat$heart_disease &amp;lt;- factor(dat$heart_disease,
  levels = c(0, 1),
  labels = c(&amp;quot;no disease&amp;quot;, &amp;quot;disease&amp;quot;)
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Keep in mind the order of the levels for your dependent variable, as it will have an impact on the interpretations. In R, the first level given by &lt;code&gt;levels()&lt;/code&gt; is always taken as the reference level.&lt;/p&gt;
&lt;p&gt;In our case, the first level is the absence of the disease and the second level is the presence of the disease:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;levels(dat$heart_disease)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;no disease&amp;quot; &amp;quot;disease&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This means that when we will build the models, we will estimate the impact of the independent variable(s) on the &lt;strong&gt;presence&lt;/strong&gt; of the disease (and not the absence!).&lt;/p&gt;
&lt;p&gt;This is the reason that, for dependent variables of the type no/yes, false/true, absence/presence of a condition, etc. it is recommended to set the level no, false, absence of the condition, etc. as the reference level. It is indeed usually easier to interpret the impact of an independent variable on the presence of a condition/disease than the opposite.&lt;/p&gt;
&lt;p&gt;If you want to switch the reference level, this can be done with the &lt;code&gt;relevel()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;Here is a preview of the final data frame and some basic &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/&#34;&gt;descriptive statistics&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# print first 6 observations
head(dat)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##   age    sex       chest_pain max_heartrate heart_disease
## 1  63   male   typical angina           150    no disease
## 2  67   male     asymptomatic           108       disease
## 3  67   male     asymptomatic           129       disease
## 4  37   male non-anginal pain           187    no disease
## 5  41 female  atypical angina           172    no disease
## 6  56   male  atypical angina           178    no disease&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# basic descriptive statistics
summary(dat)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##       age            sex                 chest_pain  max_heartrate  
##  Min.   :29.00   female: 96   typical angina  : 23   Min.   : 71.0  
##  1st Qu.:48.00   male  :201   atypical angina : 49   1st Qu.:133.0  
##  Median :56.00                non-anginal pain: 83   Median :153.0  
##  Mean   :54.54                asymptomatic    :142   Mean   :149.6  
##  3rd Qu.:61.00                                       3rd Qu.:166.0  
##  Max.   :77.00                                       Max.   :202.0  
##     heart_disease
##  no disease:160  
##  disease   :137  
##                  
##                  
##                  
## &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The data frame is now ready to be analyzed further through univariable and multivariable binary logistic regressions.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;binary-logistic-regression-in-r&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Binary logistic regression in R&lt;/h1&gt;
&lt;div id=&#34;univariable-binary-logistic-regression&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Univariable binary logistic regression&lt;/h2&gt;
&lt;p&gt;As mentioned above, we start with a univariable binary logistic regression, that is, a binary logistic regression with only one independent variable.&lt;/p&gt;
&lt;p&gt;In R, a binary logistic regression can be done with the &lt;code&gt;glm()&lt;/code&gt; function and the &lt;code&gt;family = &#34;binomial&#34;&lt;/code&gt; argument. Similar to linear regression, the formula used inside the function must be written as &lt;code&gt;dependent variable ~ independent variable&lt;/code&gt; (in this order!).&lt;/p&gt;
&lt;p&gt;While the dependent variable must be categorical with two levels, the independent variable can be of any type. However, interpretations differ depending on whether the independent variable is qualitative or quantitative.&lt;/p&gt;
&lt;p&gt;For completeness, we illustrate this type of regression with both a quantitative and a qualitative independent variable, starting with a quantitative independent variable.&lt;/p&gt;
&lt;div id=&#34;quantitative-independent-variable&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Quantitative independent variable&lt;/h3&gt;
&lt;p&gt;Suppose we want to estimate the impact of a patient’s age on the presence of heart disease. In this case, &lt;code&gt;age&lt;/code&gt; is our independent variable and &lt;code&gt;heart_disease&lt;/code&gt; is our dependent variable:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# save model
m1 &amp;lt;- glm(heart_disease ~ age,
  data = dat,
  family = &amp;quot;binomial&amp;quot;
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Results of the model is saved under the object &lt;code&gt;m1&lt;/code&gt;. Again, similar to linear regression, results can be accessed thanks to the &lt;code&gt;summary()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# print results
summary(m1)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## Call:
## glm(formula = heart_disease ~ age, family = &amp;quot;binomial&amp;quot;, data = dat)
## 
## Coefficients:
##             Estimate Std. Error z value Pr(&amp;gt;|z|)    
## (Intercept) -3.05122    0.76862  -3.970  7.2e-05 ***
## age          0.05291    0.01382   3.829 0.000128 ***
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 409.95  on 296  degrees of freedom
## Residual deviance: 394.25  on 295  degrees of freedom
## AIC: 398.25
## 
## Number of Fisher Scoring iterations: 4&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The most important results in this output are displayed in the table after &lt;code&gt;Coefficients&lt;/code&gt;. The bottom part of the output summarizes the distribution of the deviance residuals. In a nutshell, deviance residuals measure how well the observations fit the model. The closer the residual to 0, the better the fit of the observation.&lt;/p&gt;
&lt;p&gt;Within the &lt;code&gt;Coefficients&lt;/code&gt; table, we focus on the first and last columns (the other two columns correspond to the standard error and the test statistic, which are both used to compute the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the column &lt;code&gt;Estimate&lt;/code&gt; corresponds to the coefficients &lt;span class=&#34;math inline&#34;&gt;\(\hat{\beta}_0\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(\hat{\beta}_1\)&lt;/span&gt;, and&lt;/li&gt;
&lt;li&gt;the column &lt;code&gt;Pr(&amp;gt;|z|)&lt;/code&gt; corresponds to the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-values.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;R performs a &lt;a href=&#34;https://statsandr.com/blog/hypothesis-test-by-hand/&#34;&gt;hypothesis test&lt;/a&gt; for each coefficient, that is, &lt;span class=&#34;math inline&#34;&gt;\(H_0: \beta_j = 0\)&lt;/span&gt; versus &lt;span class=&#34;math inline&#34;&gt;\(H_1: \beta_j \neq 0\)&lt;/span&gt; for &lt;span class=&#34;math inline&#34;&gt;\(j = 0, 1\)&lt;/span&gt; via the Wald test, and print the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-values in the last column. We can thus compare these &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-values to the chosen significance level (usually &lt;span class=&#34;math inline&#34;&gt;\(\alpha = 0.05\)&lt;/span&gt;) to conclude whether or not each of the coefficient is significantly different from 0. The lower the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value, the more evidence that the coefficient is different from 0. This is similar to linear regression.&lt;/p&gt;
&lt;p&gt;Coefficients are slightly harder to interpret in logistic regression than in linear regression because the relationship between dependent and independent variables is not linear.&lt;/p&gt;
&lt;p&gt;Let’s first interpret the coefficient of age, &lt;span class=&#34;math inline&#34;&gt;\(\hat{\beta}_1\)&lt;/span&gt;, which is the most important coefficient of the two.&lt;/p&gt;
&lt;p&gt;First, since the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value of the test on the coefficient for age is &amp;lt; 0.05, we conclude that it is significantly different from 0, which means that age is significantly associated with the presence of heart disease (at the 5% significance level). Note that if the test was not significant (i.e., the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value &lt;span class=&#34;math inline&#34;&gt;\(\ge \alpha\)&lt;/span&gt;), we would refrain from interpreting the coefficient since it means that, based on the data at hand, we are unable to conclude that age is associated with the presence of heart disease in the population.&lt;/p&gt;
&lt;p&gt;Second, remember that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;when &lt;span class=&#34;math inline&#34;&gt;\(\beta_1 = 0\)&lt;/span&gt;, &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt; are independent,&lt;/li&gt;
&lt;li&gt;when &lt;span class=&#34;math inline&#34;&gt;\(\beta_1 &amp;gt; 0\)&lt;/span&gt;, the probability that &lt;span class=&#34;math inline&#34;&gt;\(Y = 1\)&lt;/span&gt; &lt;em&gt;increases&lt;/em&gt; with &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt;, and&lt;/li&gt;
&lt;li&gt;when &lt;span class=&#34;math inline&#34;&gt;\(\beta_1 &amp;lt; 0\)&lt;/span&gt;, the probability that &lt;span class=&#34;math inline&#34;&gt;\(Y = 1\)&lt;/span&gt; &lt;em&gt;decreases&lt;/em&gt; with &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In our context, we have:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;when &lt;span class=&#34;math inline&#34;&gt;\(\hat{\beta}_1 = 0\)&lt;/span&gt;, the probability of developing a heart disease is &lt;em&gt;independent&lt;/em&gt; of the age,&lt;/li&gt;
&lt;li&gt;when &lt;span class=&#34;math inline&#34;&gt;\(\hat{\beta}_1 &amp;gt; 0\)&lt;/span&gt;, the probability of developing a heart disease &lt;em&gt;increases&lt;/em&gt; with age, and&lt;/li&gt;
&lt;li&gt;when &lt;span class=&#34;math inline&#34;&gt;\(\hat{\beta}_1 &amp;lt; 0\)&lt;/span&gt;, the probability of developing a heart disease &lt;em&gt;decreases&lt;/em&gt; with age.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here we have &lt;span class=&#34;math inline&#34;&gt;\(\hat{\beta}_1 =\)&lt;/span&gt; 0.053 &lt;span class=&#34;math inline&#34;&gt;\(&amp;gt; 0\)&lt;/span&gt;, so we already know that the older the patient, the more likely he or she is to develop a heart disease. This makes sense.&lt;/p&gt;
&lt;p&gt;Now that we know the direction of the relationship, we would like to &lt;strong&gt;quantify&lt;/strong&gt; this relationship. This is easily done thanks to odds ratios (OR). OR are found by taking the exponential of the coefficients. In R, the exponential is done thanks to the &lt;code&gt;exp()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;OR can be interpreted as follows: the OR is the multiplicative change in the odds in favor of &lt;span class=&#34;math inline&#34;&gt;\(Y = 1\)&lt;/span&gt; when &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; increases by 1 unit.&lt;/p&gt;
&lt;p&gt;Applied to our context, we compute the OR for the age by computing &lt;span class=&#34;math inline&#34;&gt;\(\exp(\hat{\beta}_1) =\)&lt;/span&gt; exp(0.053).&lt;/p&gt;
&lt;p&gt;Using R, this gives:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# OR for age
exp(coef(m1)[&amp;quot;age&amp;quot;])&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##      age 
## 1.054331&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Based on this result, we can say that an extra year of age increases the odds (that is, the chance) of developing a heart disease by a factor of 1.054.&lt;/p&gt;
&lt;p&gt;Therefore, the odds of developing a heart disease increases by (1.054 - 1) &lt;span class=&#34;math inline&#34;&gt;\(\times\)&lt;/span&gt; 100 = 5.4% when a patient becomes one year older.&lt;/p&gt;
&lt;p&gt;To sum up:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;when the coefficient &lt;span class=&#34;math inline&#34;&gt;\(\hat{\beta}_1 = 0 \Rightarrow\)&lt;/span&gt; OR &lt;span class=&#34;math inline&#34;&gt;\(= \exp(\hat{\beta}_1) = 1 \Rightarrow P(Y = 1)\)&lt;/span&gt; is &lt;em&gt;independent&lt;/em&gt; of &lt;span class=&#34;math inline&#34;&gt;\(X \Rightarrow\)&lt;/span&gt; there is &lt;em&gt;no&lt;/em&gt; relationship between &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt;,&lt;/li&gt;
&lt;li&gt;when the coefficient &lt;span class=&#34;math inline&#34;&gt;\(\hat{\beta}_1 &amp;gt; 0 \Rightarrow\)&lt;/span&gt; OR &lt;span class=&#34;math inline&#34;&gt;\(= \exp(\hat{\beta}_1) &amp;gt; 1 \Rightarrow P(Y = 1)\)&lt;/span&gt; &lt;em&gt;increases&lt;/em&gt; with &lt;span class=&#34;math inline&#34;&gt;\(X \Rightarrow\)&lt;/span&gt; there is a &lt;em&gt;positive&lt;/em&gt; relationship between &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt;, and&lt;/li&gt;
&lt;li&gt;when the coefficient &lt;span class=&#34;math inline&#34;&gt;\(\hat{\beta}_1 &amp;lt; 0 \Rightarrow\)&lt;/span&gt; OR &lt;span class=&#34;math inline&#34;&gt;\(= \exp(\hat{\beta}_1) &amp;lt; 1 \Rightarrow P(Y = 1)\)&lt;/span&gt; &lt;em&gt;decreases&lt;/em&gt; with &lt;span class=&#34;math inline&#34;&gt;\(X \Rightarrow\)&lt;/span&gt; there is a &lt;em&gt;negative&lt;/em&gt; relationship between &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We now interpret the intercept &lt;span class=&#34;math inline&#34;&gt;\(\hat{\beta}_0\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;First, we look at the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value of the test on the intercept. This &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value being &amp;lt; 0.05, we conclude that the intercept is significantly different from 0 (at the 5% significance level).&lt;/p&gt;
&lt;p&gt;Second, similar to linear regression, in order to obtain an interpretation of the intercept, we need to find a situation in which the other coefficient, &lt;span class=&#34;math inline&#34;&gt;\(\beta_1\)&lt;/span&gt;, vanishes.&lt;/p&gt;
&lt;p&gt;In our case, it happens when a patient is 0 year old. We may or may not need to interpret results in a such a situation, and in many situations the interpretation does not make sense, so it is more a hypothetical interpretation. However, again for completeness we show how to interpret the intercept. Note that it is also possible to center the numeric variable so that the intercept has a more meaningful interpretation. This, however, goes beyond the scope of the post.&lt;/p&gt;
&lt;p&gt;For a patient aged 0 year, the odds of developing a heart disease is &lt;span class=&#34;math inline&#34;&gt;\(\exp(\hat{\beta}_0) =\)&lt;/span&gt; exp(-3.051) = 0.047. When interpreting an intercept, it often makes more sense to interpret it as the probability that &lt;span class=&#34;math inline&#34;&gt;\(Y = 1\)&lt;/span&gt;, which can be computed as follows:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\frac{\exp(\hat{\beta}_0)}{1 + \exp(\hat{\beta}_0)}.\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In our case, it corresponds to the probability that a patient of age 0 develops a heart disease, which is equal to:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# prob(heart disease) for age = 0
exp(coef(m1)[1]) / (1 + exp(coef(m1)[1]))&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## (Intercept) 
##  0.04516478&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This means that, if we trust our model, a newborn is expected to develops a heart disease with a probability of 4.52%.&lt;/p&gt;
&lt;p&gt;For your information, a confidence interval can be computed for any of the OR using the &lt;code&gt;confint()&lt;/code&gt; function. For example, a 95% confidence interval for the OR for age:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 95% CI for the OR for age
exp(confint(m1,
  parm = &amp;quot;age&amp;quot;
))&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##    2.5 %   97.5 % 
## 1.026699 1.083987&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Remember than when evaluating an OR, the null value is 1, not 0. An OR of 1 in this study would mean that there is no association between the age and the presence of heart disease. If the 95% confidence interval of the OR does not include 1, we conclude that there is a significant association between the age and the presence of heart disease. On the contrary, if it includes 1, we do not reject the hypothesis that there is no association between age and the presence of heart disease.&lt;/p&gt;
&lt;p&gt;In our case, the 95% CI does not include 1, so we conclude, at the 5% significance level, that there is a significant association between age and the presence of heart disease.&lt;/p&gt;
&lt;p&gt;You will notice that it is the same conclusion than with the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value. This is normal, it will always be the case:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;if the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value &amp;lt; 0.05, 1 will not be included in the 95% CI, and&lt;/li&gt;
&lt;li&gt;if the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value &lt;span class=&#34;math inline&#34;&gt;\(\ge\)&lt;/span&gt; 0.05, 1 will be included in the 95% CI.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This means that you can choose whether you draw your conclusion about the association between the two variables based on the 95% CI or the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value.&lt;/p&gt;
&lt;p&gt;Estimating the relationship between variables is the main reason for building models. Another goal is to predict the dependent variable based on newly observed values of the independent variable(s). This can be done with the &lt;code&gt;predict()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;Suppose we would like to predict the probability of developing a heart disease for a patient aged 30 years old:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# predict probability to develop heart disease
pred &amp;lt;- predict(m1,
  newdata = data.frame(age = c(30)),
  type = &amp;quot;response&amp;quot;
)

# print prediction
pred&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##         1 
## 0.1878525&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It is predicted that a 30-year-old patient has a 18.79% chance of developing a heart disease.&lt;/p&gt;
&lt;p&gt;Note that if you would like to construct a confidence interval for this prediction, it can be done by adding the &lt;code&gt;se = TRUE&lt;/code&gt; argument in the &lt;code&gt;predict()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# predict probability to develop heart disease
pred &amp;lt;- predict(m1,
  newdata = data.frame(age = c(30)),
  type = &amp;quot;response&amp;quot;,
  se = TRUE
)

# print prediction
pred$fit&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##         1 
## 0.1878525&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 95% confidence interval for the prediction
lower &amp;lt;- pred$fit - (qnorm(0.975) * pred$se.fit)
upper &amp;lt;- pred$fit + (qnorm(0.975) * pred$se.fit)
c(lower, upper)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##          1          1 
## 0.07873357 0.29697138&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you are a frequent reader of the blog, you are probably know that I like visualizations. The &lt;code&gt;plot_model()&lt;/code&gt; function available in the &lt;code&gt;{sjPlot}&lt;/code&gt; R package does a good job of visualizing results of the model:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# load package
library(sjPlot)

# plot
plot_model(m1,
  type = &amp;quot;pred&amp;quot;,
  terms = &amp;quot;age&amp;quot;
) +
  labs(y = &amp;quot;Prob(heart disease)&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/binary-logistic-regression-in-r/index_files/figure-html/unnamed-chunk-19-1.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;For those of you who are familiar with the &lt;a href=&#34;https://statsandr.com/blog/graphics-in-r-with-ggplot2/&#34;&gt;&lt;code&gt;{ggplot2}&lt;/code&gt; package&lt;/a&gt;, you will have noticed that the function accepts layers from the &lt;code&gt;{ggplot2}&lt;/code&gt; package. Note also that this function works with other types of model (such as linear models).&lt;/p&gt;
&lt;p&gt;The above plot shows the probability of developing a heart disease in function of age, and confirms results found above:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We see that the probability of developing a heart disease increases with age (which was expected given that the OR for the coefficient of age is &amp;gt; 1),&lt;/li&gt;
&lt;li&gt;and we also see that the probability of developing a heart disease for a 30-year-old patient is slightly below 20%.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;qualitative-independent-variable&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Qualitative independent variable&lt;/h3&gt;
&lt;p&gt;Suppose now that we are interested in estimating the relationship between the probability of developing a heart disease and the sex (which is a qualitative variable).&lt;/p&gt;
&lt;p&gt;Recall that when the independent variable was quantitative, &lt;span class=&#34;math inline&#34;&gt;\(\exp(\hat{\beta}_1)\)&lt;/span&gt; was the multiplicative change in the odds in favor of &lt;span class=&#34;math inline&#34;&gt;\(Y = 1\)&lt;/span&gt; as &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; increases by 1 unit.&lt;/p&gt;
&lt;p&gt;With &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; being the sex, the only unit increase possible is from 0 to 1 (or from 1 to 2 if sex is encoded as a factor), so we can write an interpretation in terms of female/male:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(\exp(\hat{\beta}_1)\)&lt;/span&gt; is the multiplicative change of the odds in favor of &lt;span class=&#34;math inline&#34;&gt;\(Y = 1\)&lt;/span&gt; as a female becomes a male.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Again, keep in mind what is the order of the level for the variable sex. In our case, the level female comes before the level male:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# levels for sex
levels(dat$sex)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;female&amp;quot; &amp;quot;male&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So it is indeed the multiplicative change of the odds in favor of &lt;span class=&#34;math inline&#34;&gt;\(Y = 1\)&lt;/span&gt; &lt;strong&gt;as a female becomes a male&lt;/strong&gt;. If the level male came before the level female in our dataset, it would have been the opposite.&lt;/p&gt;
&lt;p&gt;You will concede that it is rather strange to interpret odds as a female becomes a male, or vice versa. Therefore, it is better to say:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(\exp(\hat{\beta}_1)\)&lt;/span&gt; is the multiplicative change of the odds in favor of &lt;span class=&#34;math inline&#34;&gt;\(Y = 1\)&lt;/span&gt; for &lt;strong&gt;males versus females&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In our case, we obtain the following results:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# save model
m2 &amp;lt;- glm(heart_disease ~ sex,
  data = dat,
  family = &amp;quot;binomial&amp;quot;
)

# print results
summary(m2)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## Call:
## glm(formula = heart_disease ~ sex, family = &amp;quot;binomial&amp;quot;, data = dat)
## 
## Coefficients:
##             Estimate Std. Error z value Pr(&amp;gt;|z|)    
## (Intercept)  -1.0438     0.2326  -4.488 7.18e-06 ***
## sexmale       1.2737     0.2725   4.674 2.95e-06 ***
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 409.95  on 296  degrees of freedom
## Residual deviance: 386.12  on 295  degrees of freedom
## AIC: 390.12
## 
## Number of Fisher Scoring iterations: 4&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# OR for sex
exp(coef(m2)[&amp;quot;sexmale&amp;quot;])&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##  sexmale 
## 3.573933&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which can be interpreted as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value of the test on the coefficient for sex is &amp;lt; 0.05, so we conclude that the sex is significantly associated with the presence of heart disease (at the 5% significance level).&lt;/li&gt;
&lt;li&gt;Moreover, when looking at the coefficient for sex, &lt;span class=&#34;math inline&#34;&gt;\(\hat{\beta}_1 =\)&lt;/span&gt; 1.274, we can say that:
&lt;ul&gt;
&lt;li&gt;For males, the odds of developing a heart disease is multiplied by a factor of exp(1.274) = 3.574 compared to females.&lt;/li&gt;
&lt;li&gt;In other words, the odds of developing a heart disease for males are 3.574 times the odds for females.&lt;/li&gt;
&lt;li&gt;This means that, the odds of developing a heart disease are (3.574 - 1) &lt;span class=&#34;math inline&#34;&gt;\(\times\)&lt;/span&gt; 100 = 257.4% higher for males than for females.&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These results again make sense, as it known that men are more likely to have a heart disease than women.&lt;/p&gt;
&lt;p&gt;The interpretation of the intercept &lt;span class=&#34;math inline&#34;&gt;\(\hat{\beta}_0 =\)&lt;/span&gt; -1.044 is similar than in the previous section in the sense that it gives the probability of developing a heart disease when the other coefficient, &lt;span class=&#34;math inline&#34;&gt;\(\beta_1\)&lt;/span&gt;, is equal to 0.&lt;/p&gt;
&lt;p&gt;In our case, &lt;span class=&#34;math inline&#34;&gt;\(\beta_1 = 0\)&lt;/span&gt; simply means that the patient is a female. Therefore, the probability of developing a heart disease for a woman is:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# prob(disease) for sex = female
exp(coef(m2)[1]) / (1 + exp(coef(m2)[1]))&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## (Intercept) 
##   0.2604167&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The avid reader will notice that a univariable binary logistic regression with a qualitative independent variable will lead to the same conclusion than a &lt;a href=&#34;https://statsandr.com/blog/chi-square-test-of-independence-in-r/&#34;&gt;Chi-square test of independence&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;chisq.test(table(dat$heart_disease, dat$sex))&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## 	Pearson&amp;#39;s Chi-squared test with Yates&amp;#39; continuity correction
## 
## data:  table(dat$heart_disease, dat$sex)
## X-squared = 21.852, df = 1, p-value = 2.946e-06&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Based on this test, we reject the null hypothesis of independence between the two variables and we thus conclude that there is a significant association between the sex and the presence of heart disease (&lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value &amp;lt; 0.001).&lt;/p&gt;
&lt;p&gt;The advantage of a univariable binary logistic regression over a Chi-square test of independence is that it not only tests whether or not there is a significant association between the two variables, but it also &lt;strong&gt;estimates the direction and strength of this relationship&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;As for a univariable logistic regression with a quantitative independent variable, predictions can also be made with the &lt;code&gt;predict()&lt;/code&gt; function. Suppose we would like to predict the probability of developing a heart disease for a male:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# predict probability to develop heart disease
pred &amp;lt;- predict(m2,
  newdata = data.frame(sex = c(&amp;quot;male&amp;quot;)),
  type = &amp;quot;response&amp;quot;
)

# print prediction
pred&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##         1 
## 0.5572139&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Based on this model, it is predicted that a male patient has 55.72% chance of developing a heart disease.&lt;/p&gt;
&lt;p&gt;We can also visualize these results thanks to the &lt;code&gt;plot_model()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# plot
plot_model(m2,
  type = &amp;quot;pred&amp;quot;,
  terms = &amp;quot;sex&amp;quot;
) +
  labs(y = &amp;quot;Prob(heart disease)&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/binary-logistic-regression-in-r/index_files/figure-html/unnamed-chunk-25-1.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The points correspond to the predicted probabilities, and the bars correspond to their confidence intervals.&lt;/p&gt;
&lt;p&gt;The plot of the results in terms of probabilities confirms what was found above, that is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;women are less likely to develop a heart disease than men,&lt;/li&gt;
&lt;li&gt;the probability that a woman develops a heart disease is expected to be slightly above 25%, and&lt;/li&gt;
&lt;li&gt;the probability that a man develops a heart disease is expected to be around 55%.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;multivariable-binary-logistic-regression&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Multivariable binary logistic regression&lt;/h2&gt;
&lt;p&gt;The interpretation of the coefficients in multivariable logistic regression is similar to the interpretation in univariable regression, except that this time it estimates the multiplicative change in the odds in favor of &lt;span class=&#34;math inline&#34;&gt;\(Y = 1\)&lt;/span&gt; when &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; increases by 1 unit, &lt;strong&gt;while the other independent variables remain unchanged&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;This is similar to multiple linear regression, where a coefficient gives the expected change of &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt; for an increase of 1 unit of &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt;, while keeping all other variables constant.&lt;/p&gt;
&lt;p&gt;The main advantages of using a multivariable logistic regression compared to a univariable logistic regression are to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;consider the simultaneous (rather than isolated) effect of independent variables,&lt;/li&gt;
&lt;li&gt;take into account potential confounding and/or interaction effects, and&lt;/li&gt;
&lt;li&gt;improve predictions.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For this illustration, suppose we would like to estimate the relationship between heart disease and all variables present in the data frame, that is, age, sex, chest pain type and maximum heart rate achieved:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# save model
m3 &amp;lt;- glm(heart_disease ~ .,
  data = dat,
  family = &amp;quot;binomial&amp;quot;
)

# print results
summary(m3)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## Call:
## glm(formula = heart_disease ~ ., family = &amp;quot;binomial&amp;quot;, data = dat)
## 
## Coefficients:
##                             Estimate Std. Error z value Pr(&amp;gt;|z|)    
## (Intercept)                -0.060150   1.962091  -0.031 0.975544    
## age                         0.042814   0.019009   2.252 0.024302 *  
## sexmale                     1.686330   0.349352   4.827 1.39e-06 ***
## chest_painatypical angina  -0.120481   0.641396  -0.188 0.851000    
## chest_painnon-anginal pain -0.124331   0.571093  -0.218 0.827658    
## chest_painasymptomatic      1.963723   0.548877   3.578 0.000347 ***
## max_heartrate              -0.030326   0.007975  -3.802 0.000143 ***
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 409.95  on 296  degrees of freedom
## Residual deviance: 275.26  on 290  degrees of freedom
## AIC: 289.26
## 
## Number of Fisher Scoring iterations: 5&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that the formula &lt;code&gt;heart_disease ~ .&lt;/code&gt; is a shortcut to include all variables present in the data frame in the model as independent variables, except &lt;code&gt;heart_disease&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Based on the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-values displayed in the last column of the coefficients table, we conclude that, at the 5% significance level, age, sex and maximum heart rate achieved are all significantly associated with heart disease (&lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-values &amp;lt; 0.05).&lt;/p&gt;
&lt;p&gt;For the variables &lt;code&gt;age&lt;/code&gt;, &lt;code&gt;sex&lt;/code&gt; and &lt;code&gt;max_heartrate&lt;/code&gt;, there is only one &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value (the result of the test on the nullity of the coefficient).&lt;/p&gt;
&lt;p&gt;For the variable &lt;code&gt;chest_pain&lt;/code&gt;, 3 &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-values are displayed. This is normal: similar to linear regression when a categorical variable with more than two levels is included in the model, one test is performed for each comparison between the reference level and the other levels.&lt;/p&gt;
&lt;p&gt;In our case, the reference level for the variable &lt;code&gt;chest_pain&lt;/code&gt; is &lt;code&gt;typical angina&lt;/code&gt; as it comes first:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;levels(dat$chest_pain)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;typical angina&amp;quot;   &amp;quot;atypical angina&amp;quot;  &amp;quot;non-anginal pain&amp;quot; &amp;quot;asymptomatic&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Therefore, a test is performed for the comparison between:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;&lt;code&gt;typical angina&lt;/code&gt; and &lt;code&gt;atypical angina&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;&lt;code&gt;typical angina&lt;/code&gt; and &lt;code&gt;non-anginal pain&lt;/code&gt;, and&lt;/li&gt;
&lt;li&gt;&lt;code&gt;typical angina&lt;/code&gt; and &lt;code&gt;asymptomatic&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;But here we are not interested in comparing levels of the variable chest pain, we would like to test the overall effect of chest pain on heart disease. For this, we are going to compare two models via a likelihood ratio test (LRT):&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;a model which includes all the variables of interest &lt;strong&gt;and&lt;/strong&gt; the variable &lt;code&gt;chest_pain&lt;/code&gt;, and&lt;/li&gt;
&lt;li&gt;the exact same model but which &lt;strong&gt;excludes&lt;/strong&gt; the variable &lt;code&gt;chest_pain&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The first one is referred as the full or complete model, whereas the second one is referred as the reduced model.&lt;/p&gt;
&lt;p&gt;We compare these two models with the &lt;code&gt;anova()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# save reduced model
m3_reduced &amp;lt;- glm(heart_disease ~ age + sex + max_heartrate,
  data = dat,
  family = &amp;quot;binomial&amp;quot;
)

# compare reduced with full model
anova(m3_reduced, m3,
  test = &amp;quot;LRT&amp;quot;
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Analysis of Deviance Table
## 
## Model 1: heart_disease ~ age + sex + max_heartrate
## Model 2: heart_disease ~ age + sex + chest_pain + max_heartrate
##   Resid. Df Resid. Dev Df Deviance  Pr(&amp;gt;Chi)    
## 1       293     325.12                          
## 2       290     275.26  3    49.86 8.558e-11 ***
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice that the reduced model must come before the complete model in the &lt;code&gt;anova()&lt;/code&gt; function. The null hypothesis of this test is that the two models are equivalent.&lt;/p&gt;
&lt;p&gt;At the 5% significance level (see the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value at the right of the R output), we reject the null hypothesis and we conclude that the complete model is significantly better than the reduced model at explaining the presence of heart disease. This means that chest pain is significantly associated with heart disease (which was expected since the comparison between &lt;code&gt;typical angina&lt;/code&gt; and &lt;code&gt;asymptomatic&lt;/code&gt; was found to be significant).&lt;/p&gt;
&lt;p&gt;A comparison of two models via the LRT will be shown again later, when discussing about interactions.&lt;/p&gt;
&lt;p&gt;Now that we have shown that all four independent variables were significantly associated with the presence of heart disease, we can interpret the coefficients in order to know the direction of the relationships and most importantly, quantify the strength of these relationships.&lt;/p&gt;
&lt;p&gt;Like univariable binary logistic regression, it is easier to interpret these relationships through OR. But this time, we also print the 95% CI of the OR in addition to the OR (rounded to 3 decimals) so that we can easily see which ones are significantly different from 1:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# OR and 95% CI
round(exp(cbind(OR = coef(m3), confint(m3))), 3)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##                               OR 2.5 % 97.5 %
## (Intercept)                0.942 0.020 44.353
## age                        1.044 1.006  1.084
## sexmale                    5.400 2.776 10.971
## chest_painatypical angina  0.886 0.252  3.191
## chest_painnon-anginal pain 0.883 0.293  2.814
## chest_painasymptomatic     7.126 2.509 22.030
## max_heartrate              0.970 0.955  0.985&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;From the OR and their 95% CI computed above, we conclude that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Age: the odds of having a heart disease are multiplied by a factor of 1.04 for each one-unit increase in age, all else being equal.&lt;/li&gt;
&lt;li&gt;Sex: the odds of developing a heart disease for males are 5.4 times the odds for females, all else being equal.&lt;/li&gt;
&lt;li&gt;Chest pain: the odds of developing a heart disease for people suffering from chest pain of the type “asymptomatic” are 7.13 times the odds for people suffering from chest pain of the type “typical angina”, all else being equal. We refrain from interpreting the other comparisons as they are not significant at the 5% significance level (1 is included in their 95% CI).&lt;/li&gt;
&lt;li&gt;Maximum heart rate achieved: the odds of having a heart disease are multiplied by a factor of 0.97 for each one-unit increase in maximum heart rate achieved, all else being equal.&lt;/li&gt;
&lt;li&gt;Intercept: we also refrain from interpreting the intercept as it is not significantly different from 0 at the 5% significance level (1 is included in the 95% CI).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you are interested in printing only the OR for which the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value of the coefficient is &amp;lt; 0.05, here is the code:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;exp(coef(m3))[coef(summary(m3))[, &amp;quot;Pr(&amp;gt;|z|)&amp;quot;] &amp;lt; 0.05]&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##                    age                sexmale chest_painasymptomatic 
##              1.0437437              5.3996293              7.1258054 
##          max_heartrate 
##              0.9701289&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Remember that we can always write the interpretations in terms of the percentage increase/decrease in odds with the formula &lt;span class=&#34;math inline&#34;&gt;\((OR - 1) \times 100\)&lt;/span&gt;, where OR corresponds to the odds ratio.&lt;/p&gt;
&lt;p&gt;For instance, for the maximum heart rate achieved, the OR = 0.97, so the interpretation becomes: the odds of developing a heart disease increases by (0.97 &lt;span class=&#34;math inline&#34;&gt;\(- 1) \times 100 =\)&lt;/span&gt; -3% for each one-unit increase in maximum heart rate achieved, which is equivalent to say that the odds of developing a heart disease &lt;em&gt;decreases&lt;/em&gt; by 3% for each one-unit increase in maximum heart rate achieved.&lt;/p&gt;
&lt;p&gt;For illustrative purposes, suppose now that we would like to predict the probability that a new patient develops a heart disease. Suppose that this patient is a 32-year-old woman, suffering from chest pain of the type non-anginal and she achieved a maximum heart rate of 150. The probability that she develops a heart disease is:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# create data frame of new patient
new_patient &amp;lt;- data.frame(
  age = 32,
  sex = &amp;quot;female&amp;quot;,
  chest_pain = &amp;quot;non-anginal pain&amp;quot;,
  max_heartrate = 150
)

# predict probability to develop heart disease
pred &amp;lt;- predict(m3,
  newdata = new_patient,
  type = &amp;quot;response&amp;quot;
)

# print prediction
pred&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##          1 
## 0.03345948&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If we trust our model, the probability that this new patient will develop a heart disease is predicted to be 3.35%.&lt;/p&gt;
&lt;p&gt;We can also visualize the results thanks to the &lt;code&gt;plot_model()&lt;/code&gt; function, three effects at the same time:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;effect of age, sex and chest pain type on the predicted probability of developing a heart disease, and&lt;/li&gt;
&lt;li&gt;effect of maximum heart rate achieved, sex and chest pain type on the predicted probability of developing a heart disease.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 1. age, sex and chest pain on prob of disease
plot_model(m3,
  type = &amp;quot;pred&amp;quot;,
  terms = c(&amp;quot;age&amp;quot;, &amp;quot;chest_pain&amp;quot;, &amp;quot;sex&amp;quot;),
  ci.lvl = NA # remove confidence bands
) +
  labs(y = &amp;quot;Prob(heart disease)&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/binary-logistic-regression-in-r/index_files/figure-html/unnamed-chunk-32-1.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# 2. max heart rate, chest pain and sex on prob of disease
plot_model(m3,
  type = &amp;quot;pred&amp;quot;,
  terms = c(&amp;quot;max_heartrate&amp;quot;, &amp;quot;chest_pain&amp;quot;, &amp;quot;sex&amp;quot;),
  ci.lvl = NA # remove confidence bands
) +
  labs(y = &amp;quot;Prob(heart disease)&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/binary-logistic-regression-in-r/index_files/figure-html/unnamed-chunk-32-2.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;For more clarity in the plots, confidence bands are removed thanks to &lt;code&gt;ci.lvl = NA&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;These plots confirm results obtained above, that is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;there is a &lt;em&gt;positive&lt;/em&gt; relationship between age and the presence of heart disease,&lt;/li&gt;
&lt;li&gt;there is a &lt;em&gt;negative&lt;/em&gt; relationship between maximum heart rate achieved and the presence of heart disease,&lt;/li&gt;
&lt;li&gt;the odds of developing a heart disease is higher for patients suffering from chest pain of the type asymptomatic and similar for the 3 other types of chest pain, and&lt;/li&gt;
&lt;li&gt;the odds of developing a heart disease is higher for males than for females.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;interaction&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Interaction&lt;/h2&gt;
&lt;p&gt;In the previous sections, potential interaction effects were omitted.&lt;/p&gt;
&lt;p&gt;An interaction occurs when the relationship between an independent variable and the outcome variable depends on the value or the level taken by another independent variable. On the contrary, if the relationship between an independent variable and the dependent variable remains unchanged no matter the value taken by another independent variable, we cannot conclude that there is an interaction effect.&lt;/p&gt;
&lt;p&gt;In our case, there would be an interaction if for example the relationship between age and heart disease depends on the sex. There would be an interaction, for instance, if the relationship between age and heart disease was positive for females, and negative for males, or vice versa. Or if the relationship between age and heart disease was much stronger or much weaker for females than for males.&lt;/p&gt;
&lt;p&gt;Let’s see if there is an interaction between age and sex, and more importantly, whether or not this interaction is significant. For this, we need to build two models:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;one model containing only the main effects, so without the interaction, and&lt;/li&gt;
&lt;li&gt;one model containing the main effects &lt;strong&gt;and&lt;/strong&gt; the interaction.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# save model without interaction
m4 &amp;lt;- glm(heart_disease ~ age + sex,
  data = dat,
  family = &amp;quot;binomial&amp;quot;
)

# save model with interaction
m4_inter &amp;lt;- glm(heart_disease ~ age * sex,
  data = dat,
  family = &amp;quot;binomial&amp;quot;
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We first assess the interaction visually via the &lt;code&gt;plot_model()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# plot
plot_model(m4_inter,
  type = &amp;quot;pred&amp;quot;,
  terms = c(&amp;quot;age&amp;quot;, &amp;quot;sex&amp;quot;),
  ci.lvl = NA # remove confidence bands
) +
  labs(y = &amp;quot;Prob(heart disease)&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/binary-logistic-regression-in-r/index_files/figure-html/unnamed-chunk-34-1.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Since the two curves of the predicted probabilities are relatively similar and follow the same pattern, the relationship between age and the presence of heart disease does not seem to depend on the sex, indicating that there may indeed be no interaction. However, we would like to test it more formally via a statistical test.&lt;/p&gt;
&lt;p&gt;For this, we can compare the two models (the one without compared to the one with interaction) with a likelihood ratio test (LRT), using the &lt;code&gt;anova()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;anova(m4, m4_inter,
  test = &amp;quot;LRT&amp;quot;
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Analysis of Deviance Table
## 
## Model 1: heart_disease ~ age + sex
## Model 2: heart_disease ~ age * sex
##   Resid. Df Resid. Dev Df Deviance Pr(&amp;gt;Chi)
## 1       294     364.43                     
## 2       293     364.23  1  0.20741   0.6488&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Remember that it is always the reduced model as the first argument in the &lt;code&gt;anova()&lt;/code&gt; function, and then the more complex model as the second argument.&lt;/p&gt;
&lt;p&gt;The test confirms what we supposed based on the plot: at the 5% significance level, we do not reject the null hypothesis that the two models are equivalent. Since the only difference between the two models is that an interaction term is added in the complete model, we do not reject the hypothesis that there is no interaction between the age and the sex (&lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value = 0.649).&lt;/p&gt;
&lt;p&gt;This conclusion could have also been obtained more simply with the &lt;code&gt;drop1()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;drop1(m4_inter,
  test = &amp;quot;LRT&amp;quot;
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Single term deletions
## 
## Model:
## heart_disease ~ age * sex
##         Df Deviance    AIC     LRT Pr(&amp;gt;Chi)
## &amp;lt;none&amp;gt;       364.23 372.23                 
## age:sex  1   364.43 370.43 0.20741   0.6488&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As you can see, this gives the same &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value of 0.649.&lt;/p&gt;
&lt;p&gt;In practice, a non-significant interaction is removed from the model before interpreting its results. In our case, only the main effects of age and sex would remain in the model.&lt;/p&gt;
&lt;p&gt;This leads us to model selection, or which variables should be included in our final model. This is discussed in the next section.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;model-selection&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Model selection&lt;/h2&gt;
&lt;p&gt;In practice, we often have several models, corresponding to the different combinations of independent variables and their interactions. Finding the best model is not easy.&lt;/p&gt;
&lt;p&gt;In general, the best practice is to obtain a final model that is as parsimonious as possible, that is, with as few parameters as possible. A parsimonious model is easier to interpret and generalize, and also more powerful from a statistical point of view. On the other hand, it should not be too simple so that it still captures the variations or patterns in the data. In general, while more variables are often better than one, too many is often worse than a few.&lt;/p&gt;
&lt;p&gt;The two most common approaches to obtain a final model are the following:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;Adjust the model by removing the main effects and their interactions which are not significant with respect to their &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-values, obtained by testing the nullity of the corresponding coefficients using a statistical test such as the likelihood ratio test or the Wald test. If there are several main effects or interactions which are not significant, interactions must be removed before removing any main effect. Moreover, it is recommended to remove interactions and independent variables one by one (starting with the one with the highest &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value), as removing a variable or an interaction may make another variable or interaction that was initially non-significant significant.&lt;/li&gt;
&lt;li&gt;Adjust the model by using AIC (Akaike Information Criterion) or BIC (Bayesian Information Criterion). These procedures allow to select the best model (according to AIC or BIC) by finding an equilibrium between simplicity and complexity. These selection processes usually lead to a final model with as few parameters as possible, but which captures as much information in the data as possible. Note that only models with the same dependent variable can be compared using AIC or BIC. Models with different dependent variables cannot be compared using these criteria.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The first method requires that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the underlying assumptions are valid,&lt;/li&gt;
&lt;li&gt;the sample size is sufficiently large, and&lt;/li&gt;
&lt;li&gt;the models are nested (i.e., the complete model includes at least all the variables included in the reduced model).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Moreover, the second method can be used with widely used criteria when selecting variables, and more importantly, in a completely autonomous way in R.&lt;/p&gt;
&lt;p&gt;For this reason, the second method is recommended and more often used in practice.&lt;/p&gt;
&lt;p&gt;This second method, referred as the stepwise selection, is divided into 3 types:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;backward selection: we start from the most complete model (containing all independent variables and usually also their interactions), and the interactions/main effects are deleted at each step until the model cannot be improved,&lt;/li&gt;
&lt;li&gt;forward selection: we start from the most basic model containing only the intercept, and the independent variables/interactions are added at each step until the model cannot be improved, or&lt;/li&gt;
&lt;li&gt;mixed selection: we apply both the backward &lt;strong&gt;and&lt;/strong&gt; forward selection to determine the best model according to the desired criterion.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We show how to select the best model according to AIC using the mixed stepwise selection, illustrated with all variables present in the data frame as independent variables and all possible second order interactions:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# save initial model
m5 &amp;lt;- glm(heart_disease ~ (age + sex + chest_pain + max_heartrate)^2,
  data = dat,
  family = &amp;quot;binomial&amp;quot;
)

# select best model according to AIC using mixed selection
m5_final &amp;lt;- step(m5,
  direction = &amp;quot;both&amp;quot;, # both = mixed selection
  trace = FALSE # do not display intermediate steps
)

# display results of final model
summary(m5_final)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## Call:
## glm(formula = heart_disease ~ age + sex + chest_pain + max_heartrate + 
##     age:max_heartrate, family = &amp;quot;binomial&amp;quot;, data = dat)
## 
## Coefficients:
##                              Estimate Std. Error z value Pr(&amp;gt;|z|)    
## (Intercept)                19.4386591  8.1904201   2.373 0.017628 *  
## age                        -0.3050017  0.1414986  -2.156 0.031122 *  
## sexmale                     1.7055353  0.3507149   4.863 1.16e-06 ***
## chest_painatypical angina  -0.0086463  0.6547573  -0.013 0.989464    
## chest_painnon-anginal pain -0.0590333  0.5844000  -0.101 0.919538    
## chest_painasymptomatic      1.9724490  0.5649516   3.491 0.000481 ***
## max_heartrate              -0.1605658  0.0540933  -2.968 0.002994 ** 
## age:max_heartrate           0.0023314  0.0009433   2.471 0.013457 *  
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 409.95  on 296  degrees of freedom
## Residual deviance: 268.60  on 289  degrees of freedom
## AIC: 284.6
## 
## Number of Fisher Scoring iterations: 5&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;According to the AIC (which is the default criterion when using the &lt;code&gt;step()&lt;/code&gt; function), the best model is the one including:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;age&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;&lt;code&gt;sex&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;&lt;code&gt;chest_pain&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;&lt;code&gt;max_heartrate&lt;/code&gt;, and&lt;/li&gt;
&lt;li&gt;the interaction between &lt;code&gt;age&lt;/code&gt; and &lt;code&gt;max_heartrate&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For your information, you can also easily compare models manually using AIC or the pseudo-&lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt; with the &lt;code&gt;tab_model()&lt;/code&gt; function, also available in the &lt;code&gt;{sjPlot}&lt;/code&gt; R package:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;tab_model(m3, m4, m5_final,
  show.ci = FALSE, # remove CI
  show.aic = TRUE, # display AIC
  p.style = &amp;quot;numeric_stars&amp;quot; # display p-values and stars
)&lt;/code&gt;&lt;/pre&gt;
&lt;table style=&#34;border-collapse:collapse; border:none;&#34;&gt;
&lt;tr&gt;
&lt;th style=&#34;border-top: double; text-align:center; font-style:normal; font-weight:bold; padding:0.2cm;  text-align:left; &#34;&gt;
 
&lt;/th&gt;
&lt;th colspan=&#34;2&#34; style=&#34;border-top: double; text-align:center; font-style:normal; font-weight:bold; padding:0.2cm; &#34;&gt;
heart disease
&lt;/th&gt;
&lt;th colspan=&#34;2&#34; style=&#34;border-top: double; text-align:center; font-style:normal; font-weight:bold; padding:0.2cm; &#34;&gt;
heart disease
&lt;/th&gt;
&lt;th colspan=&#34;2&#34; style=&#34;border-top: double; text-align:center; font-style:normal; font-weight:bold; padding:0.2cm; &#34;&gt;
heart disease
&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34; text-align:center; border-bottom:1px solid; font-style:italic; font-weight:normal;  text-align:left; &#34;&gt;
Predictors
&lt;/td&gt;
&lt;td style=&#34; text-align:center; border-bottom:1px solid; font-style:italic; font-weight:normal;  &#34;&gt;
Odds Ratios
&lt;/td&gt;
&lt;td style=&#34; text-align:center; border-bottom:1px solid; font-style:italic; font-weight:normal;  &#34;&gt;
p
&lt;/td&gt;
&lt;td style=&#34; text-align:center; border-bottom:1px solid; font-style:italic; font-weight:normal;  &#34;&gt;
Odds Ratios
&lt;/td&gt;
&lt;td style=&#34; text-align:center; border-bottom:1px solid; font-style:italic; font-weight:normal;  &#34;&gt;
p
&lt;/td&gt;
&lt;td style=&#34; text-align:center; border-bottom:1px solid; font-style:italic; font-weight:normal;  &#34;&gt;
Odds Ratios
&lt;/td&gt;
&lt;td style=&#34; text-align:center; border-bottom:1px solid; font-style:italic; font-weight:normal;  col7&#34;&gt;
p
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:left; &#34;&gt;
(Intercept)
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
0.94 &lt;sup&gt;&lt;/sup&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
0.976
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
0.01 &lt;sup&gt;***&lt;/sup&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
&lt;strong&gt;&amp;lt;0.001&lt;/strong&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
276759408.51 &lt;sup&gt;*&lt;/sup&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  col7&#34;&gt;
&lt;strong&gt;0.018&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:left; &#34;&gt;
age
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
1.04 &lt;sup&gt;*&lt;/sup&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
&lt;strong&gt;0.024&lt;/strong&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
1.07 &lt;sup&gt;***&lt;/sup&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
&lt;strong&gt;&amp;lt;0.001&lt;/strong&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
0.74 &lt;sup&gt;*&lt;/sup&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  col7&#34;&gt;
&lt;strong&gt;0.031&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:left; &#34;&gt;
sex [male]
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
5.40 &lt;sup&gt;***&lt;/sup&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
&lt;strong&gt;&amp;lt;0.001&lt;/strong&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
4.47 &lt;sup&gt;***&lt;/sup&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
&lt;strong&gt;&amp;lt;0.001&lt;/strong&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
5.50 &lt;sup&gt;***&lt;/sup&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  col7&#34;&gt;
&lt;strong&gt;&amp;lt;0.001&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:left; &#34;&gt;
chest pain [atypical&lt;br&gt;angina]
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
0.89 &lt;sup&gt;&lt;/sup&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
0.851
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
0.99 &lt;sup&gt;&lt;/sup&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  col7&#34;&gt;
0.989
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:left; &#34;&gt;
chest pain [non-anginal&lt;br&gt;pain]
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
0.88 &lt;sup&gt;&lt;/sup&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
0.828
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
0.94 &lt;sup&gt;&lt;/sup&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  col7&#34;&gt;
0.920
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:left; &#34;&gt;
chest pain [asymptomatic]
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
7.13 &lt;sup&gt;***&lt;/sup&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
&lt;strong&gt;&amp;lt;0.001&lt;/strong&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
7.19 &lt;sup&gt;***&lt;/sup&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  col7&#34;&gt;
&lt;strong&gt;&amp;lt;0.001&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:left; &#34;&gt;
max heartrate
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
0.97 &lt;sup&gt;***&lt;/sup&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
&lt;strong&gt;&amp;lt;0.001&lt;/strong&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
0.85 &lt;sup&gt;**&lt;/sup&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  col7&#34;&gt;
&lt;strong&gt;0.003&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:left; &#34;&gt;
age × max heartrate
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  &#34;&gt;
1.00 &lt;sup&gt;*&lt;/sup&gt;
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:center;  col7&#34;&gt;
&lt;strong&gt;0.013&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:left; padding-top:0.1cm; padding-bottom:0.1cm; border-top:1px solid;&#34;&gt;
Observations
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; padding-top:0.1cm; padding-bottom:0.1cm; text-align:left; border-top:1px solid;&#34; colspan=&#34;2&#34;&gt;
297
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; padding-top:0.1cm; padding-bottom:0.1cm; text-align:left; border-top:1px solid;&#34; colspan=&#34;2&#34;&gt;
297
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; padding-top:0.1cm; padding-bottom:0.1cm; text-align:left; border-top:1px solid;&#34; colspan=&#34;2&#34;&gt;
297
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:left; padding-top:0.1cm; padding-bottom:0.1cm;&#34;&gt;
R&lt;sup&gt;2&lt;/sup&gt; Tjur
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; padding-top:0.1cm; padding-bottom:0.1cm; text-align:left;&#34; colspan=&#34;2&#34;&gt;
0.393
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; padding-top:0.1cm; padding-bottom:0.1cm; text-align:left;&#34; colspan=&#34;2&#34;&gt;
0.142
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; padding-top:0.1cm; padding-bottom:0.1cm; text-align:left;&#34; colspan=&#34;2&#34;&gt;
0.409
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; text-align:left; padding-top:0.1cm; padding-bottom:0.1cm;&#34;&gt;
AIC
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; padding-top:0.1cm; padding-bottom:0.1cm; text-align:left;&#34; colspan=&#34;2&#34;&gt;
289.263
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; padding-top:0.1cm; padding-bottom:0.1cm; text-align:left;&#34; colspan=&#34;2&#34;&gt;
370.435
&lt;/td&gt;
&lt;td style=&#34; padding:0.2cm; text-align:left; vertical-align:top; padding-top:0.1cm; padding-bottom:0.1cm; text-align:left;&#34; colspan=&#34;2&#34;&gt;
284.599
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan=&#34;7&#34; style=&#34;font-style:italic; border-top:double black; text-align:right;&#34;&gt;
* p&amp;lt;0.05   ** p&amp;lt;0.01   *** p&amp;lt;0.001
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;Pseudo-&lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt; is a generalization of the coefficient of determination &lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt; often used in linear regression to judge the quality of a model. Like the &lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt; in linear regression, the pseudo-&lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt; varies from 0 to 1, and can be interpreted as the percentage of the null deviance explained by the independent variable(s). The higher the pseudo-&lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt; and the lower the AIC, the better the model.&lt;/p&gt;
&lt;p&gt;Note that there are several pseudo-&lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt;, such as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Likelihood ratio &lt;span class=&#34;math inline&#34;&gt;\(R^2_{L}\)&lt;/span&gt;,&lt;/li&gt;
&lt;li&gt;Cox and Snell &lt;span class=&#34;math inline&#34;&gt;\(R^2_{CS}\)&lt;/span&gt;,&lt;/li&gt;
&lt;li&gt;Nagelkerke &lt;span class=&#34;math inline&#34;&gt;\(R^2_{N}\)&lt;/span&gt;,&lt;/li&gt;
&lt;li&gt;McFadden &lt;span class=&#34;math inline&#34;&gt;\(R^2_{McF}\)&lt;/span&gt;, and&lt;/li&gt;
&lt;li&gt;Tjur &lt;span class=&#34;math inline&#34;&gt;\(R^2_{T}\)&lt;/span&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;code&gt;tab_model()&lt;/code&gt; function gives the Tjur &lt;span class=&#34;math inline&#34;&gt;\(R^2_{T}\)&lt;/span&gt; by default.&lt;/p&gt;
&lt;p&gt;Based on the AIC and the Tjur &lt;span class=&#34;math inline&#34;&gt;\(R^2_{T}\)&lt;/span&gt;, the last model is considered as the best one among the 3 considered.&lt;/p&gt;
&lt;p&gt;Note that, even though a model is deemed the best one among the ones you have considered (based on one or several criteria), it does not necessarily mean that it fits the data well. There are several methods to check the quality of a model and to check if it is appropriate for the data at hand. This is the topic of the next section.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;quality-of-a-model&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Quality of a model&lt;/h2&gt;
&lt;p&gt;Usually, the goal of building a model is to be able to predict, as precisely as possible, the response variable for new data.&lt;/p&gt;
&lt;p&gt;In the next sections, we present some measures to judge the quality of a model, starting with the easiest and most intuitive one, followed by two widely used in the medical domain, and finally two other metrics common in the field of machine learning.&lt;/p&gt;
&lt;div id=&#34;validity-of-the-predictions&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Validity of the predictions&lt;/h3&gt;
&lt;div id=&#34;accuracy&#34; class=&#34;section level4&#34;&gt;
&lt;h4&gt;Accuracy&lt;/h4&gt;
&lt;p&gt;A good way to judge the accuracy of a model is to monitor its performance on new data and count how often it predicts the correct outcome.&lt;/p&gt;
&lt;p&gt;Unfortunately, when we have access to new data, we often do not know the real outcome and we cannot therefore check if the model does a good job in predicting the outcome. The trick is to:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;train the model on the initial data frame,&lt;/li&gt;
&lt;li&gt;test the model on the exact same data (just like if it was a complete different data frame for which we do not know the outcome), and then&lt;/li&gt;
&lt;li&gt;compare the predictions made by the model to the real outcomes.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To illustrate this process, we take the model built in the previous section and test it on the initial data frame.&lt;/p&gt;
&lt;p&gt;Moreover, suppose that if the probability for the patient to develop a heart disease is below 50%, we consider that the predicted outcome is the absence of the disease, otherwise the predicted outcome is the presence of the disease.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# create a vector of predicted probabilities
preds &amp;lt;- predict(m5_final,
  newdata = select(dat, -heart_disease), # remove real outcomes
  type = &amp;quot;response&amp;quot;
)

# if probability &amp;lt; threshold, patient is considered not to have the disease
preds_outcome &amp;lt;- ifelse(preds &amp;lt; 0.5,
  0,
  1
)

# transform predictions into factor and set labels
preds_outcome &amp;lt;- factor(preds_outcome,
  levels = c(0, 1),
  labels = c(&amp;quot;no disease&amp;quot;, &amp;quot;disease&amp;quot;)
)

# compare observed vs. predicted outcome
tab &amp;lt;- table(dat$heart_disease, preds_outcome,
  dnn = c(&amp;quot;observed&amp;quot;, &amp;quot;predicted&amp;quot;)
)

# print results
tab&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##             predicted
## observed     no disease disease
##   no disease        132      28
##   disease            33     104&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;From the contingency table of the predicted and observed outcomes, we see that the model:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;correctly predicted the absence of the disease for 132 patients,&lt;/li&gt;
&lt;li&gt;incorrectly predicted the presence of the disease for 28 patients,&lt;/li&gt;
&lt;li&gt;incorrectly predicted the absence of the disease for 33 patients, and&lt;/li&gt;
&lt;li&gt;correctly predicted the presence of the disease for 104 patients.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The percentage of correct predictions, referred as the accuracy, is the sum of the correct predictions divided by the total number of predictions:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;accuracy &amp;lt;- sum(diag(tab)) / sum(tab)
accuracy&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.7946128&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This model has an accuracy of 79.5%.&lt;/p&gt;
&lt;p&gt;Although accuracy is the most intuitive and easiest way to measure a model’s predictive performance, it has some drawbacks, notably because we have to choose an &lt;em&gt;arbitrary&lt;/em&gt; threshold beyond which we classify a new observation as 1 or 0. A more detailed discussion about this can be found on Frank Harrell’s &lt;a href=&#34;https://hbiostat.org/blog/post/classification/index.html&#34; target=&#34;_blank&#34;&gt;blog&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;In this illustration, we chose 50% as the threshold beyond which a patient was considered as having the disease. Nonetheless, we could have chosen another threshold and the results would have been different!&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;sensitivity-and-specificity&#34; class=&#34;section level4&#34;&gt;
&lt;h4&gt;Sensitivity and specificity&lt;/h4&gt;
&lt;p&gt;If you work in the medical field, or if your research is related to medical sciences, you have probably already heard about sensitivity and specificity.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;sensitivity&lt;/strong&gt; of a classifier, also referred as the recall, measures the ability of a classifier to detect the condition when the condition is present. In our case, it is the percentage of diseased people who are correctly identified as having the disease. Formally, we have:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[ Sensitivity = \frac{\text{True positives}}{\text{True positives} + \text{False negatives}},\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;where true positives are people correctly diagnosed as ill and false negatives are people incorrectly diagnosed as healthy.&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;specificity&lt;/strong&gt; of a classifier measures the ability of a classifier to correctly exclude the condition when the condition is absent. In our case, it is the percentage of healthy people who are correctly identified as not having the disease. Formally, we have:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[Specificity = \frac{\text{True negatives}}{\text{True negatives} + \text{False positives}},\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;where true negatives are people correctly diagnosed as healthy and false positives are people incorrectly diagnosed as ill.&lt;/p&gt;
&lt;p&gt;In R, sensitivity and specificity can be computed as follows:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# sensitivity
sensitivity &amp;lt;- tab[2, 2] / (tab[2, 2] + tab[2, 1])
sensitivity&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.7591241&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# specificity
specificity &amp;lt;- tab[1, 1] / (tab[1, 1] + tab[1, 2])
specificity&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.825&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With our model, we obtain:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;sensitivity = 75.9%, and&lt;/li&gt;
&lt;li&gt;specificity = 82.5%.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The closer the sensitivity and the specificity are to 100%, the better the model.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;auc-and-roc-curve&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;AUC and ROC curve&lt;/h3&gt;
&lt;p&gt;We have already seen that the better the quality of the model, the better the predictions.&lt;/p&gt;
&lt;p&gt;Another common and less arbitrary way to judge the quality of a model is by computing the AUC (Area Under the Curve) and plotting the ROC (Receiver Operating Characteristic) curve.&lt;/p&gt;
&lt;p&gt;This can be achieved easily thanks to the &lt;code&gt;{pROC}&lt;/code&gt; package:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# load package
library(pROC)

# save roc object
res &amp;lt;- roc(heart_disease ~ fitted(m5_final),
  data = dat
)

# plot ROC curve
ggroc(res, legacy.axes = TRUE)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/binary-logistic-regression-in-r/index_files/figure-html/unnamed-chunk-42-1.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# print AUC
res$auc&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Area under the curve: 0.87&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As the &lt;code&gt;ggroc()&lt;/code&gt; function works with layers from the &lt;code&gt;{ggplot2}&lt;/code&gt; package, we can print the AUC directly in the title of the plot of the ROC curve:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# plot ROC curve with AUC in title
ggroc(res, legacy.axes = TRUE) +
  labs(title = paste0(&amp;quot;AUC = &amp;quot;, round(res$auc, 2)))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/binary-logistic-regression-in-r/index_files/figure-html/unnamed-chunk-43-1.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;These two quality metrics can be interpreted as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;in the plot, the closer the ROC curve is to the upper left-hand corner, the better the model, and&lt;/li&gt;
&lt;li&gt;the closer the AUC is to 1, the better the model.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Based on the ROC curve and the AUC, we can say that this model is good to very good. This means that the model is appropriate for these data, and that it can be useful to predict whether or not a patient will develop a heart disease!&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;reporting-results&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Reporting results&lt;/h2&gt;
&lt;p&gt;As we have seen before, odds ratios are useful when reporting results of binary logistic regressions.&lt;/p&gt;
&lt;p&gt;Computing these odds ratios together with the confidence intervals is not particularly difficult. However, presenting them in a table for a publication or a report can quickly become time consuming, in particular if you have many models and many independent variables.&lt;/p&gt;
&lt;p&gt;Luckily, there are two packages which saved me a lot of time and which I use almost every time I need to report results of a logistic regression.&lt;/p&gt;
&lt;p&gt;The first package, called &lt;code&gt;{gtsummary}&lt;/code&gt; is useful to report results of one regression at a time. The second one is the &lt;code&gt;{finalfit}&lt;/code&gt; package.&lt;a href=&#34;#fn1&#34; class=&#34;footnote-ref&#34; id=&#34;fnref1&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; This packages is more appropriate if you need to report results of several regressions at a time.&lt;/p&gt;
&lt;div id=&#34;gtsummary-package&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;{gtsummary} package&lt;/h3&gt;
&lt;p&gt;Here is an example with one of the models built previously:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# load package
library(gtsummary)

# print table of results
tbl_regression(m5_final, exponentiate = TRUE)&lt;/code&gt;&lt;/pre&gt;
&lt;div id=&#34;hsdigyayxw&#34; style=&#34;padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;&#34;&gt;
&lt;style&gt;#hsdigyayxw table {
  font-family: system-ui, &#39;Segoe UI&#39;, Roboto, Helvetica, Arial, sans-serif, &#39;Apple Color Emoji&#39;, &#39;Segoe UI Emoji&#39;, &#39;Segoe UI Symbol&#39;, &#39;Noto Color Emoji&#39;;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#hsdigyayxw thead, #hsdigyayxw tbody, #hsdigyayxw tfoot, #hsdigyayxw tr, #hsdigyayxw td, #hsdigyayxw th {
  border-style: none;
}

#hsdigyayxw p {
  margin: 0;
  padding: 0;
}

#hsdigyayxw .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#hsdigyayxw .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#hsdigyayxw .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#hsdigyayxw .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#hsdigyayxw .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#hsdigyayxw .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#hsdigyayxw .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#hsdigyayxw .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#hsdigyayxw .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#hsdigyayxw .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#hsdigyayxw .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#hsdigyayxw .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#hsdigyayxw .gt_spanner_row {
  border-bottom-style: hidden;
}

#hsdigyayxw .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#hsdigyayxw .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#hsdigyayxw .gt_from_md &gt; :first-child {
  margin-top: 0;
}

#hsdigyayxw .gt_from_md &gt; :last-child {
  margin-bottom: 0;
}

#hsdigyayxw .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#hsdigyayxw .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#hsdigyayxw .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#hsdigyayxw .gt_row_group_first td {
  border-top-width: 2px;
}

#hsdigyayxw .gt_row_group_first th {
  border-top-width: 2px;
}

#hsdigyayxw .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#hsdigyayxw .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#hsdigyayxw .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#hsdigyayxw .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#hsdigyayxw .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#hsdigyayxw .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#hsdigyayxw .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#hsdigyayxw .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#hsdigyayxw .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#hsdigyayxw .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#hsdigyayxw .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#hsdigyayxw .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#hsdigyayxw .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#hsdigyayxw .gt_left {
  text-align: left;
}

#hsdigyayxw .gt_center {
  text-align: center;
}

#hsdigyayxw .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#hsdigyayxw .gt_font_normal {
  font-weight: normal;
}

#hsdigyayxw .gt_font_bold {
  font-weight: bold;
}

#hsdigyayxw .gt_font_italic {
  font-style: italic;
}

#hsdigyayxw .gt_super {
  font-size: 65%;
}

#hsdigyayxw .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#hsdigyayxw .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#hsdigyayxw .gt_indent_1 {
  text-indent: 5px;
}

#hsdigyayxw .gt_indent_2 {
  text-indent: 10px;
}

#hsdigyayxw .gt_indent_3 {
  text-indent: 15px;
}

#hsdigyayxw .gt_indent_4 {
  text-indent: 20px;
}

#hsdigyayxw .gt_indent_5 {
  text-indent: 25px;
}

#hsdigyayxw .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#hsdigyayxw div.Reactable &gt; div.rt-table &gt; div.rt-thead &gt; div.rt-tr.rt-tr-group-header &gt; div.rt-th-group:after {
  height: 0px !important;
}
&lt;/style&gt;
&lt;table class=&#34;gt_table&#34; data-quarto-disable-processing=&#34;false&#34; data-quarto-bootstrap=&#34;false&#34;&gt;
  &lt;thead&gt;
    &lt;tr class=&#34;gt_col_headings&#34;&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_left&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;label&#34;&gt;&lt;span class=&#39;gt_from_md&#39;&gt;&lt;strong&gt;Characteristic&lt;/strong&gt;&lt;/span&gt;&lt;/th&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_center&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;estimate&#34;&gt;&lt;span class=&#39;gt_from_md&#39;&gt;&lt;strong&gt;OR&lt;/strong&gt;&lt;/span&gt;&lt;span class=&#34;gt_footnote_marks&#34; style=&#34;white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/span&gt;&lt;/th&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_center&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;conf.low&#34;&gt;&lt;span class=&#39;gt_from_md&#39;&gt;&lt;strong&gt;95% CI&lt;/strong&gt;&lt;/span&gt;&lt;span class=&#34;gt_footnote_marks&#34; style=&#34;white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/span&gt;&lt;/th&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_center&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;p.value&#34;&gt;&lt;span class=&#39;gt_from_md&#39;&gt;&lt;strong&gt;p-value&lt;/strong&gt;&lt;/span&gt;&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody class=&#34;gt_table_body&#34;&gt;
    &lt;tr&gt;&lt;td headers=&#34;label&#34; class=&#34;gt_row gt_left&#34;&gt;age&lt;/td&gt;
&lt;td headers=&#34;estimate&#34; class=&#34;gt_row gt_center&#34;&gt;0.74&lt;/td&gt;
&lt;td headers=&#34;conf.low&#34; class=&#34;gt_row gt_center&#34;&gt;0.55, 0.96&lt;/td&gt;
&lt;td headers=&#34;p.value&#34; class=&#34;gt_row gt_center&#34;&gt;0.031&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;label&#34; class=&#34;gt_row gt_left&#34;&gt;sex&lt;/td&gt;
&lt;td headers=&#34;estimate&#34; class=&#34;gt_row gt_center&#34;&gt;&lt;br /&gt;&lt;/td&gt;
&lt;td headers=&#34;conf.low&#34; class=&#34;gt_row gt_center&#34;&gt;&lt;br /&gt;&lt;/td&gt;
&lt;td headers=&#34;p.value&#34; class=&#34;gt_row gt_center&#34;&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;label&#34; class=&#34;gt_row gt_left&#34;&gt;    female&lt;/td&gt;
&lt;td headers=&#34;estimate&#34; class=&#34;gt_row gt_center&#34;&gt;—&lt;/td&gt;
&lt;td headers=&#34;conf.low&#34; class=&#34;gt_row gt_center&#34;&gt;—&lt;/td&gt;
&lt;td headers=&#34;p.value&#34; class=&#34;gt_row gt_center&#34;&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;label&#34; class=&#34;gt_row gt_left&#34;&gt;    male&lt;/td&gt;
&lt;td headers=&#34;estimate&#34; class=&#34;gt_row gt_center&#34;&gt;5.50&lt;/td&gt;
&lt;td headers=&#34;conf.low&#34; class=&#34;gt_row gt_center&#34;&gt;2.82, 11.2&lt;/td&gt;
&lt;td headers=&#34;p.value&#34; class=&#34;gt_row gt_center&#34;&gt;&lt;0.001&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;label&#34; class=&#34;gt_row gt_left&#34;&gt;chest_pain&lt;/td&gt;
&lt;td headers=&#34;estimate&#34; class=&#34;gt_row gt_center&#34;&gt;&lt;br /&gt;&lt;/td&gt;
&lt;td headers=&#34;conf.low&#34; class=&#34;gt_row gt_center&#34;&gt;&lt;br /&gt;&lt;/td&gt;
&lt;td headers=&#34;p.value&#34; class=&#34;gt_row gt_center&#34;&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;label&#34; class=&#34;gt_row gt_left&#34;&gt;    typical angina&lt;/td&gt;
&lt;td headers=&#34;estimate&#34; class=&#34;gt_row gt_center&#34;&gt;—&lt;/td&gt;
&lt;td headers=&#34;conf.low&#34; class=&#34;gt_row gt_center&#34;&gt;—&lt;/td&gt;
&lt;td headers=&#34;p.value&#34; class=&#34;gt_row gt_center&#34;&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;label&#34; class=&#34;gt_row gt_left&#34;&gt;    atypical angina&lt;/td&gt;
&lt;td headers=&#34;estimate&#34; class=&#34;gt_row gt_center&#34;&gt;0.99&lt;/td&gt;
&lt;td headers=&#34;conf.low&#34; class=&#34;gt_row gt_center&#34;&gt;0.27, 3.65&lt;/td&gt;
&lt;td headers=&#34;p.value&#34; class=&#34;gt_row gt_center&#34;&gt;&gt;0.9&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;label&#34; class=&#34;gt_row gt_left&#34;&gt;    non-anginal pain&lt;/td&gt;
&lt;td headers=&#34;estimate&#34; class=&#34;gt_row gt_center&#34;&gt;0.94&lt;/td&gt;
&lt;td headers=&#34;conf.low&#34; class=&#34;gt_row gt_center&#34;&gt;0.30, 3.07&lt;/td&gt;
&lt;td headers=&#34;p.value&#34; class=&#34;gt_row gt_center&#34;&gt;&gt;0.9&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;label&#34; class=&#34;gt_row gt_left&#34;&gt;    asymptomatic&lt;/td&gt;
&lt;td headers=&#34;estimate&#34; class=&#34;gt_row gt_center&#34;&gt;7.19&lt;/td&gt;
&lt;td headers=&#34;conf.low&#34; class=&#34;gt_row gt_center&#34;&gt;2.44, 22.8&lt;/td&gt;
&lt;td headers=&#34;p.value&#34; class=&#34;gt_row gt_center&#34;&gt;&lt;0.001&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;label&#34; class=&#34;gt_row gt_left&#34;&gt;max_heartrate&lt;/td&gt;
&lt;td headers=&#34;estimate&#34; class=&#34;gt_row gt_center&#34;&gt;0.85&lt;/td&gt;
&lt;td headers=&#34;conf.low&#34; class=&#34;gt_row gt_center&#34;&gt;0.76, 0.94&lt;/td&gt;
&lt;td headers=&#34;p.value&#34; class=&#34;gt_row gt_center&#34;&gt;0.003&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;label&#34; class=&#34;gt_row gt_left&#34;&gt;age * max_heartrate&lt;/td&gt;
&lt;td headers=&#34;estimate&#34; class=&#34;gt_row gt_center&#34;&gt;1.00&lt;/td&gt;
&lt;td headers=&#34;conf.low&#34; class=&#34;gt_row gt_center&#34;&gt;1.00, 1.00&lt;/td&gt;
&lt;td headers=&#34;p.value&#34; class=&#34;gt_row gt_center&#34;&gt;0.013&lt;/td&gt;&lt;/tr&gt;
  &lt;/tbody&gt;
  
  &lt;tfoot class=&#34;gt_footnotes&#34;&gt;
    &lt;tr&gt;
      &lt;td class=&#34;gt_footnote&#34; colspan=&#34;4&#34;&gt;&lt;span class=&#34;gt_footnote_marks&#34; style=&#34;white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/span&gt; &lt;span class=&#39;gt_from_md&#39;&gt;OR = Odds Ratio, CI = Confidence Interval&lt;/span&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tfoot&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;What I like with this package is its ease of use, and the fact that all results are nicely formatted in a table. This is a very good starting point when I need to create a table for a publication or a report, for one regression at a time.&lt;/p&gt;
&lt;p&gt;The second package becomes interesting when you need to report results for several models at once.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;finalfit-package&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;{finalfit} package&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;{finalfit}&lt;/code&gt; package allows to report odds ratios, their confidence intervals and the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-values in a very efficient way. Moreover, it is quite easy to do so for many regressions at the same time.&lt;/p&gt;
&lt;p&gt;Let me present the package by reporting results of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;all univariable binary logistic regressions that are possible with the variables available in the data frame,&lt;/li&gt;
&lt;li&gt;a multivariable binary logistic regression that includes all variables available in the data frame, and&lt;/li&gt;
&lt;li&gt;a multivariable binary logistic regression that includes only some of the variables present in the data frame.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We start with all the possible univariable binary logistic regressions:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# load packages
library(tidyverse)
library(gt)
library(finalfit)

# set dependent and independent variables
dependent &amp;lt;- &amp;quot;heart_disease&amp;quot;
independent &amp;lt;- c(&amp;quot;age&amp;quot;, &amp;quot;sex&amp;quot;, &amp;quot;chest_pain&amp;quot;, &amp;quot;max_heartrate&amp;quot;)

# save results of univariable logistic regressions
glmuni &amp;lt;- dat |&amp;gt;
  glmuni(dependent, independent) |&amp;gt;
  fit2df(
    explanatory_name = &amp;quot;Variables&amp;quot;,
    estimate_name = &amp;quot;Crude OR&amp;quot;,
    estimate_suffix = &amp;quot; (95% CI)&amp;quot;
  )

# print results
glmuni |&amp;gt;
  gt()&lt;/code&gt;&lt;/pre&gt;
&lt;div id=&#34;ewvlblrgtf&#34; style=&#34;padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;&#34;&gt;
&lt;style&gt;#ewvlblrgtf table {
  font-family: system-ui, &#39;Segoe UI&#39;, Roboto, Helvetica, Arial, sans-serif, &#39;Apple Color Emoji&#39;, &#39;Segoe UI Emoji&#39;, &#39;Segoe UI Symbol&#39;, &#39;Noto Color Emoji&#39;;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#ewvlblrgtf thead, #ewvlblrgtf tbody, #ewvlblrgtf tfoot, #ewvlblrgtf tr, #ewvlblrgtf td, #ewvlblrgtf th {
  border-style: none;
}

#ewvlblrgtf p {
  margin: 0;
  padding: 0;
}

#ewvlblrgtf .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#ewvlblrgtf .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#ewvlblrgtf .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#ewvlblrgtf .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#ewvlblrgtf .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#ewvlblrgtf .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#ewvlblrgtf .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#ewvlblrgtf .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#ewvlblrgtf .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#ewvlblrgtf .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#ewvlblrgtf .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#ewvlblrgtf .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#ewvlblrgtf .gt_spanner_row {
  border-bottom-style: hidden;
}

#ewvlblrgtf .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#ewvlblrgtf .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#ewvlblrgtf .gt_from_md &gt; :first-child {
  margin-top: 0;
}

#ewvlblrgtf .gt_from_md &gt; :last-child {
  margin-bottom: 0;
}

#ewvlblrgtf .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#ewvlblrgtf .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#ewvlblrgtf .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#ewvlblrgtf .gt_row_group_first td {
  border-top-width: 2px;
}

#ewvlblrgtf .gt_row_group_first th {
  border-top-width: 2px;
}

#ewvlblrgtf .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#ewvlblrgtf .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#ewvlblrgtf .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#ewvlblrgtf .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#ewvlblrgtf .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#ewvlblrgtf .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#ewvlblrgtf .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#ewvlblrgtf .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#ewvlblrgtf .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#ewvlblrgtf .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#ewvlblrgtf .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#ewvlblrgtf .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#ewvlblrgtf .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#ewvlblrgtf .gt_left {
  text-align: left;
}

#ewvlblrgtf .gt_center {
  text-align: center;
}

#ewvlblrgtf .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#ewvlblrgtf .gt_font_normal {
  font-weight: normal;
}

#ewvlblrgtf .gt_font_bold {
  font-weight: bold;
}

#ewvlblrgtf .gt_font_italic {
  font-style: italic;
}

#ewvlblrgtf .gt_super {
  font-size: 65%;
}

#ewvlblrgtf .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#ewvlblrgtf .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#ewvlblrgtf .gt_indent_1 {
  text-indent: 5px;
}

#ewvlblrgtf .gt_indent_2 {
  text-indent: 10px;
}

#ewvlblrgtf .gt_indent_3 {
  text-indent: 15px;
}

#ewvlblrgtf .gt_indent_4 {
  text-indent: 20px;
}

#ewvlblrgtf .gt_indent_5 {
  text-indent: 25px;
}

#ewvlblrgtf .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#ewvlblrgtf div.Reactable &gt; div.rt-table &gt; div.rt-thead &gt; div.rt-tr.rt-tr-group-header &gt; div.rt-th-group:after {
  height: 0px !important;
}
&lt;/style&gt;
&lt;table class=&#34;gt_table&#34; data-quarto-disable-processing=&#34;false&#34; data-quarto-bootstrap=&#34;false&#34;&gt;
  &lt;thead&gt;
    &lt;tr class=&#34;gt_col_headings&#34;&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_left&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;Variables&#34;&gt;Variables&lt;/th&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_left&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;Crude-OR-(95%-CI)&#34;&gt;Crude OR (95% CI)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody class=&#34;gt_table_body&#34;&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;age&lt;/td&gt;
&lt;td headers=&#34;Crude OR (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;1.05 (1.03-1.08, p&amp;lt;0.001)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;sexmale&lt;/td&gt;
&lt;td headers=&#34;Crude OR (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;3.57 (2.12-6.18, p&amp;lt;0.001)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;chest_painatypical angina&lt;/td&gt;
&lt;td headers=&#34;Crude OR (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;0.51 (0.16-1.66, p=0.255)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;chest_painnon-anginal pain&lt;/td&gt;
&lt;td headers=&#34;Crude OR (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;0.63 (0.23-1.86, p=0.384)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;chest_painasymptomatic&lt;/td&gt;
&lt;td headers=&#34;Crude OR (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;6.04 (2.39-16.76, p&amp;lt;0.001)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;max_heartrate&lt;/td&gt;
&lt;td headers=&#34;Crude OR (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;0.96 (0.94-0.97, p&amp;lt;0.001)&lt;/td&gt;&lt;/tr&gt;
  &lt;/tbody&gt;
  
  
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;A few remarks regarding this code:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;glmuni()&lt;/code&gt; is used because we want to run univariable GLM.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;explanatory_name = &#34;Variables&#34;&lt;/code&gt; is used to rename the first column (by default it is “explanatory”).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;estimate_name = &#34;Crude OR&#34;&lt;/code&gt; is used to rename the second column and inform the reader that we are in the univariable case. In the univariable case, OR are often called crude OR because they are not adjusted for the effects of the other independent variables.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;estimate_suffix = &#34; (95% CI)&#34;&lt;/code&gt; is used to specify that it is the 95% confidence intervals which are inside the parentheses.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;gt()&lt;/code&gt; layer at the end of the code is not compulsory. It is just to make the output appears in a nice table instead of the usual format of R outputs. See more information about the &lt;code&gt;{gt}&lt;/code&gt; package in its &lt;a href=&#34;https://gt.rstudio.com/&#34; target=&#34;_blank&#34;&gt;documentation&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here is how to report results of a multivariable binary logistic regression which includes all variables:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# save results of full model
glmmulti_full &amp;lt;- dat |&amp;gt;
  glmmulti(dependent, independent) |&amp;gt;
  fit2df(
    explanatory_name = &amp;quot;Variables&amp;quot;,
    estimate_name = &amp;quot;Adjusted OR - full model&amp;quot;,
  )

# print results
glmmulti_full |&amp;gt;
  gt()&lt;/code&gt;&lt;/pre&gt;
&lt;div id=&#34;vtrqkpqzfa&#34; style=&#34;padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;&#34;&gt;
&lt;style&gt;#vtrqkpqzfa table {
  font-family: system-ui, &#39;Segoe UI&#39;, Roboto, Helvetica, Arial, sans-serif, &#39;Apple Color Emoji&#39;, &#39;Segoe UI Emoji&#39;, &#39;Segoe UI Symbol&#39;, &#39;Noto Color Emoji&#39;;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#vtrqkpqzfa thead, #vtrqkpqzfa tbody, #vtrqkpqzfa tfoot, #vtrqkpqzfa tr, #vtrqkpqzfa td, #vtrqkpqzfa th {
  border-style: none;
}

#vtrqkpqzfa p {
  margin: 0;
  padding: 0;
}

#vtrqkpqzfa .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#vtrqkpqzfa .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#vtrqkpqzfa .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#vtrqkpqzfa .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#vtrqkpqzfa .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#vtrqkpqzfa .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#vtrqkpqzfa .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#vtrqkpqzfa .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#vtrqkpqzfa .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#vtrqkpqzfa .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#vtrqkpqzfa .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#vtrqkpqzfa .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#vtrqkpqzfa .gt_spanner_row {
  border-bottom-style: hidden;
}

#vtrqkpqzfa .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#vtrqkpqzfa .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#vtrqkpqzfa .gt_from_md &gt; :first-child {
  margin-top: 0;
}

#vtrqkpqzfa .gt_from_md &gt; :last-child {
  margin-bottom: 0;
}

#vtrqkpqzfa .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#vtrqkpqzfa .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#vtrqkpqzfa .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#vtrqkpqzfa .gt_row_group_first td {
  border-top-width: 2px;
}

#vtrqkpqzfa .gt_row_group_first th {
  border-top-width: 2px;
}

#vtrqkpqzfa .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#vtrqkpqzfa .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#vtrqkpqzfa .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#vtrqkpqzfa .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#vtrqkpqzfa .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#vtrqkpqzfa .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#vtrqkpqzfa .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#vtrqkpqzfa .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#vtrqkpqzfa .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#vtrqkpqzfa .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#vtrqkpqzfa .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#vtrqkpqzfa .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#vtrqkpqzfa .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#vtrqkpqzfa .gt_left {
  text-align: left;
}

#vtrqkpqzfa .gt_center {
  text-align: center;
}

#vtrqkpqzfa .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#vtrqkpqzfa .gt_font_normal {
  font-weight: normal;
}

#vtrqkpqzfa .gt_font_bold {
  font-weight: bold;
}

#vtrqkpqzfa .gt_font_italic {
  font-style: italic;
}

#vtrqkpqzfa .gt_super {
  font-size: 65%;
}

#vtrqkpqzfa .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#vtrqkpqzfa .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#vtrqkpqzfa .gt_indent_1 {
  text-indent: 5px;
}

#vtrqkpqzfa .gt_indent_2 {
  text-indent: 10px;
}

#vtrqkpqzfa .gt_indent_3 {
  text-indent: 15px;
}

#vtrqkpqzfa .gt_indent_4 {
  text-indent: 20px;
}

#vtrqkpqzfa .gt_indent_5 {
  text-indent: 25px;
}

#vtrqkpqzfa .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#vtrqkpqzfa div.Reactable &gt; div.rt-table &gt; div.rt-thead &gt; div.rt-tr.rt-tr-group-header &gt; div.rt-th-group:after {
  height: 0px !important;
}
&lt;/style&gt;
&lt;table class=&#34;gt_table&#34; data-quarto-disable-processing=&#34;false&#34; data-quarto-bootstrap=&#34;false&#34;&gt;
  &lt;thead&gt;
    &lt;tr class=&#34;gt_col_headings&#34;&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_left&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;Variables&#34;&gt;Variables&lt;/th&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_left&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;Adjusted-OR---full-model&#34;&gt;Adjusted OR - full model&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody class=&#34;gt_table_body&#34;&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;age&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - full model&#34; class=&#34;gt_row gt_left&#34;&gt;1.04 (1.01-1.08, p=0.024)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;sexmale&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - full model&#34; class=&#34;gt_row gt_left&#34;&gt;5.40 (2.78-10.97, p&amp;lt;0.001)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;chest_painatypical angina&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - full model&#34; class=&#34;gt_row gt_left&#34;&gt;0.89 (0.25-3.19, p=0.851)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;chest_painnon-anginal pain&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - full model&#34; class=&#34;gt_row gt_left&#34;&gt;0.88 (0.29-2.81, p=0.828)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;chest_painasymptomatic&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - full model&#34; class=&#34;gt_row gt_left&#34;&gt;7.13 (2.51-22.03, p&amp;lt;0.001)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;max_heartrate&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - full model&#34; class=&#34;gt_row gt_left&#34;&gt;0.97 (0.95-0.99, p&amp;lt;0.001)&lt;/td&gt;&lt;/tr&gt;
  &lt;/tbody&gt;
  
  
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;A few remarks regarding this code:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;glmmulti()&lt;/code&gt; is used because we want to run multivariable GLM.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;estimate_name = &#34;Adjusted OR - full model&#34;&lt;/code&gt; is used to remind the reader that we are in the multivariable case with all variables included. In the multivariable case, OR are often called adjusted OR because they are adjusted for the effects of the other independent variables.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here is how to report results of a multivariable binary logistic regression which includes only a selection of variables:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# select the variables to be included in the final model
independent_final &amp;lt;- c(&amp;quot;age&amp;quot;, &amp;quot;sex&amp;quot;, &amp;quot;chest_pain&amp;quot;)

# save results of final model
glmmulti_final &amp;lt;- dat |&amp;gt;
  glmmulti(dependent, independent_final) |&amp;gt;
  fit2df(
    explanatory_name = &amp;quot;Variables&amp;quot;,
    estimate_name = &amp;quot;Adjusted OR - final model&amp;quot;,
    estimate_suffix = &amp;quot; (95% CI)&amp;quot;
  )

# print results
glmmulti_final |&amp;gt;
  gt()&lt;/code&gt;&lt;/pre&gt;
&lt;div id=&#34;klgxvctcfp&#34; style=&#34;padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;&#34;&gt;
&lt;style&gt;#klgxvctcfp table {
  font-family: system-ui, &#39;Segoe UI&#39;, Roboto, Helvetica, Arial, sans-serif, &#39;Apple Color Emoji&#39;, &#39;Segoe UI Emoji&#39;, &#39;Segoe UI Symbol&#39;, &#39;Noto Color Emoji&#39;;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#klgxvctcfp thead, #klgxvctcfp tbody, #klgxvctcfp tfoot, #klgxvctcfp tr, #klgxvctcfp td, #klgxvctcfp th {
  border-style: none;
}

#klgxvctcfp p {
  margin: 0;
  padding: 0;
}

#klgxvctcfp .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#klgxvctcfp .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#klgxvctcfp .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#klgxvctcfp .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#klgxvctcfp .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#klgxvctcfp .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#klgxvctcfp .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#klgxvctcfp .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#klgxvctcfp .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#klgxvctcfp .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#klgxvctcfp .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#klgxvctcfp .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#klgxvctcfp .gt_spanner_row {
  border-bottom-style: hidden;
}

#klgxvctcfp .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#klgxvctcfp .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#klgxvctcfp .gt_from_md &gt; :first-child {
  margin-top: 0;
}

#klgxvctcfp .gt_from_md &gt; :last-child {
  margin-bottom: 0;
}

#klgxvctcfp .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#klgxvctcfp .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#klgxvctcfp .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#klgxvctcfp .gt_row_group_first td {
  border-top-width: 2px;
}

#klgxvctcfp .gt_row_group_first th {
  border-top-width: 2px;
}

#klgxvctcfp .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#klgxvctcfp .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#klgxvctcfp .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#klgxvctcfp .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#klgxvctcfp .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#klgxvctcfp .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#klgxvctcfp .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#klgxvctcfp .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#klgxvctcfp .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#klgxvctcfp .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#klgxvctcfp .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#klgxvctcfp .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#klgxvctcfp .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#klgxvctcfp .gt_left {
  text-align: left;
}

#klgxvctcfp .gt_center {
  text-align: center;
}

#klgxvctcfp .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#klgxvctcfp .gt_font_normal {
  font-weight: normal;
}

#klgxvctcfp .gt_font_bold {
  font-weight: bold;
}

#klgxvctcfp .gt_font_italic {
  font-style: italic;
}

#klgxvctcfp .gt_super {
  font-size: 65%;
}

#klgxvctcfp .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#klgxvctcfp .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#klgxvctcfp .gt_indent_1 {
  text-indent: 5px;
}

#klgxvctcfp .gt_indent_2 {
  text-indent: 10px;
}

#klgxvctcfp .gt_indent_3 {
  text-indent: 15px;
}

#klgxvctcfp .gt_indent_4 {
  text-indent: 20px;
}

#klgxvctcfp .gt_indent_5 {
  text-indent: 25px;
}

#klgxvctcfp .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#klgxvctcfp div.Reactable &gt; div.rt-table &gt; div.rt-thead &gt; div.rt-tr.rt-tr-group-header &gt; div.rt-th-group:after {
  height: 0px !important;
}
&lt;/style&gt;
&lt;table class=&#34;gt_table&#34; data-quarto-disable-processing=&#34;false&#34; data-quarto-bootstrap=&#34;false&#34;&gt;
  &lt;thead&gt;
    &lt;tr class=&#34;gt_col_headings&#34;&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_left&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;Variables&#34;&gt;Variables&lt;/th&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_left&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;Adjusted-OR---final-model-(95%-CI)&#34;&gt;Adjusted OR - final model (95% CI)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody class=&#34;gt_table_body&#34;&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;age&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - final model (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;1.07 (1.03-1.11, p&amp;lt;0.001)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;sexmale&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - final model (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;5.52 (2.88-11.07, p&amp;lt;0.001)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;chest_painatypical angina&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - final model (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;0.82 (0.24-2.82, p=0.743)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;chest_painnon-anginal pain&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - final model (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;0.93 (0.32-2.90, p=0.903)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;chest_painasymptomatic&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - final model (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;9.48 (3.47-28.53, p&amp;lt;0.001)&lt;/td&gt;&lt;/tr&gt;
  &lt;/tbody&gt;
  
  
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;Note that you have to manually select the variables (the package will not choose for you unfortunately). The selection could be done preliminary thanks to a stepwise procedure for example.&lt;/p&gt;
&lt;p&gt;Now the most interesting part of this package:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;we can combine all these results together,&lt;/li&gt;
&lt;li&gt;in addition to some descriptive statistics for each level of the dependent variable!&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here are all results combined together and displayed in a table:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# save descriptive statistics
summary &amp;lt;- dat |&amp;gt;
  summary_factorlist(dependent, independent, fit_id = TRUE)

# save results of regressions
output &amp;lt;- summary |&amp;gt;
  finalfit_merge(glmuni) |&amp;gt;
  finalfit_merge(glmmulti_full) |&amp;gt;
  finalfit_merge(glmmulti_final)

# print all results
output |&amp;gt;
  dplyr::select(-fit_id, -index) |&amp;gt;
  dplyr::rename(
    Variables = label,
    &amp;quot; &amp;quot; = levels
  ) |&amp;gt;
  gt()&lt;/code&gt;&lt;/pre&gt;
&lt;div id=&#34;szrjblstrk&#34; style=&#34;padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;&#34;&gt;
&lt;style&gt;#szrjblstrk table {
  font-family: system-ui, &#39;Segoe UI&#39;, Roboto, Helvetica, Arial, sans-serif, &#39;Apple Color Emoji&#39;, &#39;Segoe UI Emoji&#39;, &#39;Segoe UI Symbol&#39;, &#39;Noto Color Emoji&#39;;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#szrjblstrk thead, #szrjblstrk tbody, #szrjblstrk tfoot, #szrjblstrk tr, #szrjblstrk td, #szrjblstrk th {
  border-style: none;
}

#szrjblstrk p {
  margin: 0;
  padding: 0;
}

#szrjblstrk .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#szrjblstrk .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#szrjblstrk .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#szrjblstrk .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#szrjblstrk .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#szrjblstrk .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#szrjblstrk .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#szrjblstrk .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#szrjblstrk .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#szrjblstrk .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#szrjblstrk .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#szrjblstrk .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#szrjblstrk .gt_spanner_row {
  border-bottom-style: hidden;
}

#szrjblstrk .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#szrjblstrk .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#szrjblstrk .gt_from_md &gt; :first-child {
  margin-top: 0;
}

#szrjblstrk .gt_from_md &gt; :last-child {
  margin-bottom: 0;
}

#szrjblstrk .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#szrjblstrk .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#szrjblstrk .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#szrjblstrk .gt_row_group_first td {
  border-top-width: 2px;
}

#szrjblstrk .gt_row_group_first th {
  border-top-width: 2px;
}

#szrjblstrk .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#szrjblstrk .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#szrjblstrk .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#szrjblstrk .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#szrjblstrk .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#szrjblstrk .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#szrjblstrk .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#szrjblstrk .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#szrjblstrk .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#szrjblstrk .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#szrjblstrk .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#szrjblstrk .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#szrjblstrk .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#szrjblstrk .gt_left {
  text-align: left;
}

#szrjblstrk .gt_center {
  text-align: center;
}

#szrjblstrk .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#szrjblstrk .gt_font_normal {
  font-weight: normal;
}

#szrjblstrk .gt_font_bold {
  font-weight: bold;
}

#szrjblstrk .gt_font_italic {
  font-style: italic;
}

#szrjblstrk .gt_super {
  font-size: 65%;
}

#szrjblstrk .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#szrjblstrk .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#szrjblstrk .gt_indent_1 {
  text-indent: 5px;
}

#szrjblstrk .gt_indent_2 {
  text-indent: 10px;
}

#szrjblstrk .gt_indent_3 {
  text-indent: 15px;
}

#szrjblstrk .gt_indent_4 {
  text-indent: 20px;
}

#szrjblstrk .gt_indent_5 {
  text-indent: 25px;
}

#szrjblstrk .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#szrjblstrk div.Reactable &gt; div.rt-table &gt; div.rt-thead &gt; div.rt-tr.rt-tr-group-header &gt; div.rt-th-group:after {
  height: 0px !important;
}
&lt;/style&gt;
&lt;table class=&#34;gt_table&#34; data-quarto-disable-processing=&#34;false&#34; data-quarto-bootstrap=&#34;false&#34;&gt;
  &lt;thead&gt;
    &lt;tr class=&#34;gt_col_headings&#34;&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_left&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;Variables&#34;&gt;Variables&lt;/th&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_left&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;a-&#34;&gt; &lt;/th&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_right&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;no-disease&#34;&gt;no disease&lt;/th&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_right&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;disease&#34;&gt;disease&lt;/th&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_left&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;Crude-OR-(95%-CI)&#34;&gt;Crude OR (95% CI)&lt;/th&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_left&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;Adjusted-OR---full-model&#34;&gt;Adjusted OR - full model&lt;/th&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_left&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;Adjusted-OR---final-model-(95%-CI)&#34;&gt;Adjusted OR - final model (95% CI)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody class=&#34;gt_table_body&#34;&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;age&lt;/td&gt;
&lt;td headers=&#34; &#34; class=&#34;gt_row gt_left&#34;&gt;Mean (SD)&lt;/td&gt;
&lt;td headers=&#34;no disease&#34; class=&#34;gt_row gt_right&#34;&gt;52.6 (9.6)&lt;/td&gt;
&lt;td headers=&#34;disease&#34; class=&#34;gt_row gt_right&#34;&gt;56.8 (7.9)&lt;/td&gt;
&lt;td headers=&#34;Crude OR (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;1.05 (1.03-1.08, p&amp;lt;0.001)&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - full model&#34; class=&#34;gt_row gt_left&#34;&gt;1.04 (1.01-1.08, p=0.024)&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - final model (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;1.07 (1.03-1.11, p&amp;lt;0.001)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;sex&lt;/td&gt;
&lt;td headers=&#34; &#34; class=&#34;gt_row gt_left&#34;&gt;female&lt;/td&gt;
&lt;td headers=&#34;no disease&#34; class=&#34;gt_row gt_right&#34;&gt;71 (44.4)&lt;/td&gt;
&lt;td headers=&#34;disease&#34; class=&#34;gt_row gt_right&#34;&gt;25 (18.2)&lt;/td&gt;
&lt;td headers=&#34;Crude OR (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;-&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - full model&#34; class=&#34;gt_row gt_left&#34;&gt;-&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - final model (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;-&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;&lt;/td&gt;
&lt;td headers=&#34; &#34; class=&#34;gt_row gt_left&#34;&gt;male&lt;/td&gt;
&lt;td headers=&#34;no disease&#34; class=&#34;gt_row gt_right&#34;&gt;89 (55.6)&lt;/td&gt;
&lt;td headers=&#34;disease&#34; class=&#34;gt_row gt_right&#34;&gt;112 (81.8)&lt;/td&gt;
&lt;td headers=&#34;Crude OR (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;3.57 (2.12-6.18, p&amp;lt;0.001)&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - full model&#34; class=&#34;gt_row gt_left&#34;&gt;5.40 (2.78-10.97, p&amp;lt;0.001)&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - final model (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;5.52 (2.88-11.07, p&amp;lt;0.001)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;chest_pain&lt;/td&gt;
&lt;td headers=&#34; &#34; class=&#34;gt_row gt_left&#34;&gt;typical angina&lt;/td&gt;
&lt;td headers=&#34;no disease&#34; class=&#34;gt_row gt_right&#34;&gt;16 (10.0)&lt;/td&gt;
&lt;td headers=&#34;disease&#34; class=&#34;gt_row gt_right&#34;&gt;7 (5.1)&lt;/td&gt;
&lt;td headers=&#34;Crude OR (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;-&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - full model&#34; class=&#34;gt_row gt_left&#34;&gt;-&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - final model (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;-&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;&lt;/td&gt;
&lt;td headers=&#34; &#34; class=&#34;gt_row gt_left&#34;&gt;atypical angina&lt;/td&gt;
&lt;td headers=&#34;no disease&#34; class=&#34;gt_row gt_right&#34;&gt;40 (25.0)&lt;/td&gt;
&lt;td headers=&#34;disease&#34; class=&#34;gt_row gt_right&#34;&gt;9 (6.6)&lt;/td&gt;
&lt;td headers=&#34;Crude OR (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;0.51 (0.16-1.66, p=0.255)&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - full model&#34; class=&#34;gt_row gt_left&#34;&gt;0.89 (0.25-3.19, p=0.851)&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - final model (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;0.82 (0.24-2.82, p=0.743)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;&lt;/td&gt;
&lt;td headers=&#34; &#34; class=&#34;gt_row gt_left&#34;&gt;non-anginal pain&lt;/td&gt;
&lt;td headers=&#34;no disease&#34; class=&#34;gt_row gt_right&#34;&gt;65 (40.6)&lt;/td&gt;
&lt;td headers=&#34;disease&#34; class=&#34;gt_row gt_right&#34;&gt;18 (13.1)&lt;/td&gt;
&lt;td headers=&#34;Crude OR (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;0.63 (0.23-1.86, p=0.384)&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - full model&#34; class=&#34;gt_row gt_left&#34;&gt;0.88 (0.29-2.81, p=0.828)&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - final model (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;0.93 (0.32-2.90, p=0.903)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;&lt;/td&gt;
&lt;td headers=&#34; &#34; class=&#34;gt_row gt_left&#34;&gt;asymptomatic&lt;/td&gt;
&lt;td headers=&#34;no disease&#34; class=&#34;gt_row gt_right&#34;&gt;39 (24.4)&lt;/td&gt;
&lt;td headers=&#34;disease&#34; class=&#34;gt_row gt_right&#34;&gt;103 (75.2)&lt;/td&gt;
&lt;td headers=&#34;Crude OR (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;6.04 (2.39-16.76, p&amp;lt;0.001)&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - full model&#34; class=&#34;gt_row gt_left&#34;&gt;7.13 (2.51-22.03, p&amp;lt;0.001)&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - final model (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;9.48 (3.47-28.53, p&amp;lt;0.001)&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Variables&#34; class=&#34;gt_row gt_left&#34;&gt;max_heartrate&lt;/td&gt;
&lt;td headers=&#34; &#34; class=&#34;gt_row gt_left&#34;&gt;Mean (SD)&lt;/td&gt;
&lt;td headers=&#34;no disease&#34; class=&#34;gt_row gt_right&#34;&gt;158.6 (19.0)&lt;/td&gt;
&lt;td headers=&#34;disease&#34; class=&#34;gt_row gt_right&#34;&gt;139.1 (22.7)&lt;/td&gt;
&lt;td headers=&#34;Crude OR (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;0.96 (0.94-0.97, p&amp;lt;0.001)&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - full model&#34; class=&#34;gt_row gt_left&#34;&gt;0.97 (0.95-0.99, p&amp;lt;0.001)&lt;/td&gt;
&lt;td headers=&#34;Adjusted OR - final model (95% CI)&#34; class=&#34;gt_row gt_left&#34;&gt;-&lt;/td&gt;&lt;/tr&gt;
  &lt;/tbody&gt;
  
  
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;A few remarks regarding this code:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;summary_factorlist(dependent, independent, fit_id = TRUE)&lt;/code&gt; is used to compute the descriptive statistics by group of the dependent variable.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;finalfit_merge()&lt;/code&gt; is used to merge results together.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;dplyr::select(-fit_id, -index)&lt;/code&gt; is used to remove unnecessary columns.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;dplyr::rename(Variables = label, &#34; &#34; = levels)&lt;/code&gt; is used to renames some columns.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And finally, a few remarks regarding the resulting table:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The first column gives the name of the variables.&lt;/li&gt;
&lt;li&gt;The second column specifies:
&lt;ul&gt;
&lt;li&gt;for qualitative variables: the levels&lt;/li&gt;
&lt;li&gt;for quantitative variables: that it is the mean and the standard deviation (SD) which will be computed in the next two columns&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;The third and fourth columns give the descriptive statistics for each level of the dependent variable:
&lt;ul&gt;
&lt;li&gt;for qualitative variables: the number of cases, and in parentheses the frequencies by column&lt;/li&gt;
&lt;li&gt;for quantitative variables: the mean, and in parentheses the standard deviation&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;The last three columns give the OR, and in parentheses the 95% CI of the OR and the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value (for the univariable and the two multivariable models, respectively).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For your convenience, here is the full code so you can copy paste it easily in case you want to reproduce the process:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# load packages
library(tidyverse)
library(gt)
library(finalfit)

# set variables
dependent &amp;lt;- &amp;quot;heart_disease&amp;quot;
independent &amp;lt;- c(&amp;quot;age&amp;quot;, &amp;quot;sex&amp;quot;, &amp;quot;chest_pain&amp;quot;, &amp;quot;max_heartrate&amp;quot;)
independent_final &amp;lt;- c(&amp;quot;age&amp;quot;, &amp;quot;sex&amp;quot;, &amp;quot;chest_pain&amp;quot;)

# save descriptive statistics
summary &amp;lt;- dat |&amp;gt;
  summary_factorlist(dependent, independent, fit_id = TRUE)

# save results of univariable logistic regressions
glmuni &amp;lt;- dat |&amp;gt;
  glmuni(dependent, independent) |&amp;gt;
  fit2df(
    explanatory_name = &amp;quot;Variables&amp;quot;,
    estimate_name = &amp;quot;Crude OR&amp;quot;,
    estimate_suffix = &amp;quot; (95% CI)&amp;quot;
  )

# save results of full model
glmmulti_full &amp;lt;- dat |&amp;gt;
  glmmulti(dependent, independent) |&amp;gt;
  fit2df(
    explanatory_name = &amp;quot;Variables&amp;quot;,
    estimate_name = &amp;quot;Adjusted OR - full model&amp;quot;,
  )

# save results of final model
glmmulti_final &amp;lt;- dat |&amp;gt;
  glmmulti(dependent, independent_final) |&amp;gt;
  fit2df(
    explanatory_name = &amp;quot;Variables&amp;quot;,
    estimate_name = &amp;quot;Adjusted OR - final model&amp;quot;,
    estimate_suffix = &amp;quot; (95% CI)&amp;quot;
  )

# save merged results
output &amp;lt;- summary |&amp;gt;
  finalfit_merge(glmuni) |&amp;gt;
  finalfit_merge(glmmulti_full) |&amp;gt;
  finalfit_merge(glmmulti_final)

# print all results
output |&amp;gt;
  dplyr::select(-fit_id, -index) |&amp;gt;
  dplyr::rename(
    Variables = label,
    &amp;quot; &amp;quot; = levels
  ) |&amp;gt;
  gt()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Last but not least, the &lt;code&gt;or_plot()&lt;/code&gt; function, also available from the &lt;code&gt;{finalfit}&lt;/code&gt; package, is useful to visualize all odds ratio and their 95% confidence intervals:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dat |&amp;gt; or_plot(dependent, independent,
  table_text_size = 3.5 # reduce text size
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/binary-logistic-regression-in-r/index_files/figure-html/unnamed-chunk-50-1.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Here is how to read this plot:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The squares represent the OR, and the whiskers their 95% CI.&lt;/li&gt;
&lt;li&gt;When the 95% CI crosses the vertical dashed line, it means that the OR is not significantly different from 1 (at the 5% significance level). In these cases, we cannot reject the hypothesis of no association with the dependent variable.&lt;/li&gt;
&lt;li&gt;When the 95% CI does not cross the vertical dashed line, it means that the OR is significantly different from 1. In these cases:
&lt;ul&gt;
&lt;li&gt;if the square is located to the right of the vertical dashed line, there is a positive relationship between the outcome and the independent variable (known as a risk factor), and&lt;/li&gt;
&lt;li&gt;if the square is located to the left of the vertical dashed line, there is a negative relationship between the outcome and the independent variable (known as a protective factor).&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The plot confirms what was obtained above:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;age is a risk factor for heart disease,&lt;/li&gt;
&lt;li&gt;maximum heart rate achieved is a protective factor for heart disease, and&lt;/li&gt;
&lt;li&gt;being a male and suffering from asymptomatic chest pain are both risk factors of heart disease.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Be careful that sometimes the square is too big to see the whiskers of the 95% CI. This is the case for the variables &lt;code&gt;max_heartrate&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt;. In these cases, it is better to check the significance of the OR thanks to their 95% CI or the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-values printed in parentheses.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;conditions-of-application&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Conditions of application&lt;/h2&gt;
&lt;p&gt;For results to be valid and interpretable, a binary logistic regression requires:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;the dependent variable to be binary,&lt;/li&gt;
&lt;li&gt;independence of the observations: no repeated measurements or matched data, otherwise generalize linear mixed effect models (GLMM) should be used,&lt;/li&gt;
&lt;li&gt;linearity of continuous independent variables and the log-odds outcome: take age and heart disease as an example. If heart disease is more frequent or less frequent as age rises, the model will work well. However, if children and the elderly are at high risk of having a heart disease, but those in middle years are not, then the relationship is not linear, or not monotonic, meaning that the response does not only go in one direction,&lt;/li&gt;
&lt;li&gt;a sufficiently large sample size (for confidence intervals and hypothesis tests to be valid), and&lt;/li&gt;
&lt;li&gt;no multicollinearity: independent variables should not be highly correlated with each other, otherwise coefficients and OR can become unstable.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Here is how to verify each of them:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;this is obvious; check if the dependent variable has indeed only two levels,&lt;/li&gt;
&lt;li&gt;this is often not tested formally, but verified through the design of the experiment,&lt;/li&gt;
&lt;li&gt;quantitative independent variables should have a linear relationship between their log-odds and their observed values. A visual check is sufficient, see below with age, maximum heart rate achieved and model &lt;code&gt;m3&lt;/code&gt; as example:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# linearity to the log-odds?
dat |&amp;gt;
  dplyr::select(age, max_heartrate) |&amp;gt;
  mutate(log_odds = predict(m3)) |&amp;gt;
  pivot_longer(-log_odds) |&amp;gt;
  ggplot(aes(log_odds, value)) +
  geom_point() +
  geom_smooth(method = &amp;quot;lm&amp;quot;) +
  facet_wrap(~name)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/binary-logistic-regression-in-r/index_files/figure-html/unnamed-chunk-51-1.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;ol start=&#34;4&#34; style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;in practice, it is recommended to have at least 10 times as many events as parameters in the model, and&lt;/li&gt;
&lt;li&gt;the variance inflation factors (VIF) is a well known measure of multicollinearity. It should be below 10 or 5, depending on the field of research. VIF can be computed with the &lt;code&gt;vif()&lt;/code&gt; function, available in the &lt;code&gt;{car}&lt;/code&gt; package:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# load package
library(car)

# compute VIF for model m3
vif(m3)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##                   GVIF Df GVIF^(1/(2*Df))
## age           1.205246  1        1.097837
## sex           1.155071  1        1.074742
## chest_pain    1.113010  3        1.018005
## max_heartrate 1.143125  1        1.069170&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;conclusion&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;Thanks for reading.&lt;/p&gt;
&lt;p&gt;In this relatively long and detailed post, we covered several important points about binary logistic regression. First, when to use such models and what is the difference with linear models, how to implement it in R, and how to interpret and report results. We ended by discussing about model selection, how to judge the quality of fit of a logistic regression, and its underlying assumptions.&lt;/p&gt;
&lt;p&gt;I now hope that (univariable and multivariable) binary logistic regressions in R no longer hold any secrets for you.&lt;/p&gt;
&lt;p&gt;As always, if you have a question or a suggestion related to the topic covered in this article, please add it as a comment so other readers can benefit from the discussion.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;footnotes footnotes-end-of-document&#34;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&#34;fn1&#34;&gt;&lt;p&gt;Thanks to Claire from DellaData.fr for introducing me to this package.&lt;a href=&#34;#fnref1&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Multiple linear regression made simple</title>
      <link>https://statsandr.com/blog/multiple-linear-regression-made-simple/</link>
      <pubDate>Mon, 04 Oct 2021 00:00:00 +0000</pubDate>
      
      <guid>https://statsandr.com/blog/multiple-linear-regression-made-simple/</guid>
      <description>

&lt;div id=&#34;TOC&#34;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#introduction&#34; id=&#34;toc-introduction&#34;&gt;Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#simple-linear-regression-reminder&#34; id=&#34;toc-simple-linear-regression-reminder&#34;&gt;Simple linear regression: reminder&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#principle&#34; id=&#34;toc-principle&#34;&gt;Principle&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#equation&#34; id=&#34;toc-equation&#34;&gt;Equation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#interpretations-of-coefficients-widehatbeta&#34; id=&#34;toc-interpretations-of-coefficients-widehatbeta&#34;&gt;Interpretations of coefficients &lt;span class=&#34;math inline&#34;&gt;\(\widehat\beta\)&lt;/span&gt;&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#another-interpretation-of-the-intercept&#34; id=&#34;toc-another-interpretation-of-the-intercept&#34;&gt;Another interpretation of the intercept&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#significance-of-the-relationship&#34; id=&#34;toc-significance-of-the-relationship&#34;&gt;Significance of the relationship&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#correlation-does-not-imply-causation&#34; id=&#34;toc-correlation-does-not-imply-causation&#34;&gt;Correlation does not imply causation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#conditions-of-application&#34; id=&#34;toc-conditions-of-application&#34;&gt;Conditions of application&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#visualizations&#34; id=&#34;toc-visualizations&#34;&gt;Visualizations&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#multiple-linear-regression&#34; id=&#34;toc-multiple-linear-regression&#34;&gt;Multiple linear regression&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#principle-1&#34; id=&#34;toc-principle-1&#34;&gt;Principle&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#equation-1&#34; id=&#34;toc-equation-1&#34;&gt;Equation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#interpretations-of-coefficients-widehatbeta-1&#34; id=&#34;toc-interpretations-of-coefficients-widehatbeta-1&#34;&gt;Interpretations of coefficients &lt;span class=&#34;math inline&#34;&gt;\(\widehat\beta\)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#conditions-of-application-1&#34; id=&#34;toc-conditions-of-application-1&#34;&gt;Conditions of application&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#how-to-choose-a-good-linear-model&#34; id=&#34;toc-how-to-choose-a-good-linear-model&#34;&gt;How to choose a good linear model?&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#p-value-associated-to-the-model&#34; id=&#34;toc-p-value-associated-to-the-model&#34;&gt;&lt;span class=&#34;math inline&#34;&gt;\(P\)&lt;/span&gt;-value associated to the model&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#coefficient-of-determination-r2&#34; id=&#34;toc-coefficient-of-determination-r2&#34;&gt;Coefficient of determination &lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#parsimony&#34; id=&#34;toc-parsimony&#34;&gt;Parsimony&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#visualizations-1&#34; id=&#34;toc-visualizations-1&#34;&gt;Visualizations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#to-go-further&#34; id=&#34;toc-to-go-further&#34;&gt;To go further&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#print-models-parameters&#34; id=&#34;toc-print-models-parameters&#34;&gt;Print model’s parameters&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#automatic-reporting&#34; id=&#34;toc-automatic-reporting&#34;&gt;Automatic reporting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#predictions&#34; id=&#34;toc-predictions&#34;&gt;Predictions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#linear-hypothesis-tests&#34; id=&#34;toc-linear-hypothesis-tests&#34;&gt;Linear hypothesis tests&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#overall-effect-of-categorical-variables&#34; id=&#34;toc-overall-effect-of-categorical-variables&#34;&gt;Overall effect of categorical variables&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#interaction&#34; id=&#34;toc-interaction&#34;&gt;Interaction&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#summary&#34; id=&#34;toc-summary&#34;&gt;Summary&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#references&#34; id=&#34;toc-references&#34;&gt;References&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

&lt;p&gt;&lt;img src=&#34;images/multiple-linear-regression.jpeg&#34; style=&#34;width:100.0%&#34; /&gt;&lt;/p&gt;
&lt;div id=&#34;introduction&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Introduction&lt;/h1&gt;
&lt;p&gt;Remember that &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/&#34;&gt;descriptive statistics&lt;/a&gt; is a branch of statistics that allows to describe your data at hand.&lt;/p&gt;
&lt;p&gt;Inferential statistics (with the popular &lt;a href=&#34;https://statsandr.com/blog/hypothesis-test-by-hand/&#34;&gt;hypothesis tests&lt;/a&gt; and confidence intervals) is another branch of statistics that allows to make inferences, that is, to draw conclusions about a population based on a &lt;a href=&#34;https://statsandr.com/blog/what-is-the-difference-between-population-and-sample/&#34;&gt;sample&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The last branch of statistics is about &lt;strong&gt;modeling the relationship between two or more variables&lt;/strong&gt;.&lt;a href=&#34;#fn1&#34; class=&#34;footnote-ref&#34; id=&#34;fnref1&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; The most common statistical tool to describe and evaluate the link between variables is linear regression.&lt;/p&gt;
&lt;p&gt;There are two types of linear regression:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;&lt;strong&gt;Simple linear regression&lt;/strong&gt; is a statistical approach that allows to assess the linear relationship between two &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/#quantitative&#34;&gt;quantitative variables&lt;/a&gt;. More precisely, it enables the relationship to be quantified and its significance to be evaluated.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multiple linear regression&lt;/strong&gt; is a generalization of simple linear regression, in the sense that this approach makes it possible to evaluate the linear relationships between a response variable (quantitative) and several explanatory variables (quantitative or &lt;a href=&#34;https://statsandr.com/blog/variable-types-and-examples/#qualitative&#34;&gt;qualitative&lt;/a&gt;).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In the real world, multiple linear regression is used more frequently than simple linear regression. This is mostly the case because:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Multiple linear regression allows to evaluate the relationship between two variables, while &lt;strong&gt;controlling for the effect&lt;/strong&gt; (i.e., removing the effect) &lt;strong&gt;of other variables&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;With data collection becoming easier, more variables can be included and taken into account when analyzing data.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Multiple linear regression being such a powerful statistical tool, I would like to present it so that everyone understands it, and perhaps even use it when deemed necessary. However, I cannot afford to write about multiple linear regression without first presenting simple linear regression.&lt;/p&gt;
&lt;p&gt;So after a reminder about the principle and the interpretations that can be drawn from a simple linear regression, I will illustrate how to perform multiple linear regression in R. I will also show, in the context of multiple linear regression, how to interpret the output and discuss about its conditions of application. I will then conclude the article by presenting more advanced topics directly linked to linear regression.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;simple-linear-regression-reminder&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Simple linear regression: reminder&lt;/h1&gt;
&lt;p&gt;Simple linear regression is an asymmetric procedure in which:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;one of the variable is considered the response or the variable to be explained. It is also called &lt;strong&gt;dependent variable&lt;/strong&gt;, and is represented on the &lt;span class=&#34;math inline&#34;&gt;\(y\)&lt;/span&gt;-axis&lt;/li&gt;
&lt;li&gt;the other variable is the explanatory or also called &lt;strong&gt;independent variable&lt;/strong&gt;, and is represented on the &lt;span class=&#34;math inline&#34;&gt;\(x\)&lt;/span&gt;-axis&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Simple linear regression allows to &lt;strong&gt;evaluate the existence of a &lt;em&gt;linear&lt;/em&gt; relationship between two variables&lt;/strong&gt; and to quantify this link. Note that linearity is a strong assumption in linear regression in the sense that it tests and quantifies whether the two variables are &lt;em&gt;linearly&lt;/em&gt; dependent.&lt;/p&gt;
&lt;p&gt;What makes linear regression a powerful statistical tool is that it allows to &lt;strong&gt;quantify by what quantity the response/dependent variable varies when the explanatory/independent variable increases by one unit&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;This concept is key in linear regression and helps to answer the following questions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Is there a link between the amount spent in advertising and the sales during a certain period?&lt;/li&gt;
&lt;li&gt;Is the number of years of schooling valued, in financial terms, in the first job?&lt;/li&gt;
&lt;li&gt;Will an increase in tobacco taxes reduce its consumption?&lt;/li&gt;
&lt;li&gt;What is the most likely price of an apartment, depending on the area?&lt;/li&gt;
&lt;li&gt;Does a person’s reaction time to a stimulus depend on gender?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Simple linear regression can be seen as an extension to the &lt;a href=&#34;https://statsandr.com/blog/anova-in-r/&#34;&gt;analysis of variance (ANOVA)&lt;/a&gt; and the &lt;a href=&#34;https://statsandr.com/blog/student-s-t-test-in-r-and-by-hand-how-to-compare-two-groups-under-different-scenarios/&#34;&gt;Student’s t-test&lt;/a&gt;. ANOVA and t-test allow to compare groups in terms of a quantitative variable—2 groups for t-test and 3 or more groups for ANOVA.&lt;a href=&#34;#fn2&#34; class=&#34;footnote-ref&#34; id=&#34;fnref2&#34;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;For these tests, the independent variable, that is, the grouping variable forming the different groups to compare must be a qualitative variable. Linear regression is an extension because in addition to be used to compare groups, it is also used with quantitative independent variables (which is not possible with t-test and ANOVA).&lt;/p&gt;
&lt;p&gt;In this article, we are interested in assessing whether there is a linear relationship between the distance traveled with a gallon of fuel and the weight of cars. For this example, we use the &lt;code&gt;mtcars&lt;/code&gt; dataset (preloaded in R).&lt;/p&gt;
&lt;p&gt;The dataset includes fuel consumption and 10 aspects of automotive design and performance for 32 automobiles:&lt;a href=&#34;#fn3&#34; class=&#34;footnote-ref&#34; id=&#34;fnref3&#34;&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;&lt;code&gt;mpg&lt;/code&gt; Miles/(US) gallon (with a gallon &lt;span class=&#34;math inline&#34;&gt;\(\approx\)&lt;/span&gt; 3.79 liters)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cyl&lt;/code&gt; Number of cylinders&lt;/li&gt;
&lt;li&gt;&lt;code&gt;disp&lt;/code&gt; Displacement (cu.in.)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;hp&lt;/code&gt; Gross horsepower&lt;/li&gt;
&lt;li&gt;&lt;code&gt;drat&lt;/code&gt; Rear axle ratio&lt;/li&gt;
&lt;li&gt;&lt;code&gt;wt&lt;/code&gt; Weight (1000 lbs, with 1000 lbs &lt;span class=&#34;math inline&#34;&gt;\(\approx\)&lt;/span&gt; 453.59 kg)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;qsec&lt;/code&gt; 1/4 mile time (with 1/4 mile &lt;span class=&#34;math inline&#34;&gt;\(\approx\)&lt;/span&gt; 402.34 meters)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;vs&lt;/code&gt; Engine (0 = V-shaped, 1 = straight)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;am&lt;/code&gt; Transmission (0 = automatic, 1 = manual)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gear&lt;/code&gt; Number of forward gears&lt;/li&gt;
&lt;li&gt;&lt;code&gt;carb&lt;/code&gt; Number of carburetors&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dat &amp;lt;- mtcars

library(ggplot2)
ggplot(dat, aes(x = wt, y = mpg)) +
  geom_point() +
  labs(
    y = &amp;quot;Miles per gallon&amp;quot;,
    x = &amp;quot;Car&amp;#39;s weight (1000 lbs)&amp;quot;
  ) +
  theme_minimal()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/index_files/figure-html/unnamed-chunk-1-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The &lt;a href=&#34;https://statsandr.com/blog/graphics-in-r-with-ggplot2/#scatter-plot&#34;&gt;scatterplot&lt;/a&gt; above shows that there seems to be a &lt;strong&gt;negative relationship between the distance traveled with a gallon of fuel and the weight of a car&lt;/strong&gt;. This makes sense, as the heavier the car, the more fuel it consumes and thus the fewer miles it can drive with a gallon.&lt;/p&gt;
&lt;p&gt;This is already a good overview of the relationship between the two variables, but a simple linear regression with the miles per gallon as dependent variable and the car’s weight as independent variable goes further. It will tell us by &lt;strong&gt;how many miles the distance varies, on average, when the weight varies by one unit&lt;/strong&gt; (1000 lbs in this case). This is possible thanks to the regression line.&lt;/p&gt;
&lt;div id=&#34;principle&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Principle&lt;/h2&gt;
&lt;p&gt;The principle of simple linear regression is to &lt;strong&gt;find the line&lt;/strong&gt; (i.e., determine its equation) &lt;strong&gt;which passes as close as possible to the observations&lt;/strong&gt;, that is, the set of points formed by the pairs &lt;span class=&#34;math inline&#34;&gt;\((x_i, y_i)\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;In the first step, there are many potential lines. Three of them are plotted:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/index_files/figure-html/unnamed-chunk-2-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;To find the line which passes as close as possible to all the points, we take the square of the vertical distance between each point and each potential line. Note that we take the square of the distances to make sure that a negative gap (i.e., a point below the line) is not compensated by a positive gap (i.e., a point above the line). The line which passes closest to the set of points is the one which &lt;strong&gt;&lt;em&gt;minimizes&lt;/em&gt;&lt;/strong&gt; &lt;strong&gt;the sum of these squared distances&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The resulting regression line is presented in blue in the following plot, and the dashed gray lines represent the vertical distance between the points and the fitted line. These vertical distances between each observed point and the fitted line determined by the least squares method are called the &lt;strong&gt;residuals&lt;/strong&gt; of the linear regression model and denoted &lt;span class=&#34;math inline&#34;&gt;\(\epsilon\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/index_files/figure-html/unnamed-chunk-3-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;By definition, there is no other line with a smaller total distance between the points and the line. This method is called the least squares method, or &lt;strong&gt;OLS&lt;/strong&gt; for &lt;strong&gt;ordinary least squares&lt;/strong&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;equation&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Equation&lt;/h2&gt;
&lt;p&gt;The regression model can be written in the form of the equation:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[Y = \beta_0 + \beta_1 X + \epsilon\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt; the dependent variable&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; the independent variable&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(\beta_0\)&lt;/span&gt; the intercept (the mean value of &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt; when &lt;span class=&#34;math inline&#34;&gt;\(x = 0\)&lt;/span&gt;), also sometimes denoted &lt;span class=&#34;math inline&#34;&gt;\(\alpha\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(\beta_1\)&lt;/span&gt; the slope (the expected increase in &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt; when &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; increases by one unit)&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(\epsilon\)&lt;/span&gt; the residuals (the error term of mean 0 which describes the variations of &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt; not captured by the model, also referred as the noise)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When we determine the line which passes closest to all the points (we say that we fit a line to the observed data), we actually &lt;strong&gt;estimate the unknown parameters &lt;span class=&#34;math inline&#34;&gt;\(\beta_0\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(\beta_1\)&lt;/span&gt;&lt;/strong&gt; based on the data at hand. Remember from your geometry classes, to draw a line you only need two parameters—the intercept and the slope.&lt;/p&gt;
&lt;p&gt;These estimates (and thus the blue line shown in the previous scatterplot) can be computed by hand with the following formulas:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\begin{align}
\widehat\beta_1 &amp;amp;= \frac{\sum^n_{i = 1} (x_i - \bar{x})(y_i - \bar{y})}{\sum^n_{i = 1}(x_i - \bar{x})^2} \\
&amp;amp;= \frac{\left(\sum^n_{i = 1}x_iy_i\right) - n\bar{x}\bar{y}}{\sum^n_{i = 1}(x_i - \bar{x})^2}
\end{align}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;and&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\widehat\beta_0 = \bar{y} - \widehat\beta_1 \bar{x}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;with &lt;span class=&#34;math inline&#34;&gt;\(\bar{x}\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(\bar{y}\)&lt;/span&gt; denoting the sample mean of &lt;span class=&#34;math inline&#34;&gt;\(x\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(y\)&lt;/span&gt;, respectively.&lt;/p&gt;
&lt;p&gt;(If you struggle to compute &lt;span class=&#34;math inline&#34;&gt;\(\widehat\beta_0\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(\widehat\beta_1\)&lt;/span&gt; by hand, see this &lt;a href=&#34;https://statsandr.com/blog/a-shiny-app-for-simple-linear-regression-by-hand-and-in-r/&#34;&gt;Shiny app&lt;/a&gt; which helps you to easily find these estimates based on your data.)&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;interpretations-of-coefficients-widehatbeta&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Interpretations of coefficients &lt;span class=&#34;math inline&#34;&gt;\(\widehat\beta\)&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;The &lt;strong&gt;intercept &lt;span class=&#34;math inline&#34;&gt;\(\widehat\beta_0\)&lt;/span&gt;&lt;/strong&gt; is the &lt;strong&gt;mean value of the dependent variable &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt; when the independent variable &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; takes the value 0&lt;/strong&gt;. Its estimation has no interest in evaluating whether there is a linear relationship between two variables. It has, however, an interest if you want to know what the mean value of &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt; could be when &lt;span class=&#34;math inline&#34;&gt;\(x = 0\)&lt;/span&gt;.&lt;a href=&#34;#fn4&#34; class=&#34;footnote-ref&#34; id=&#34;fnref4&#34;&gt;&lt;sup&gt;4&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The &lt;strong&gt;slope &lt;span class=&#34;math inline&#34;&gt;\(\widehat\beta_1\)&lt;/span&gt;&lt;/strong&gt;, on the other hand, corresponds to the expected &lt;strong&gt;variation of &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt; when &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; varies by one unit&lt;/strong&gt;. It tells us two important informations:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;The &lt;strong&gt;sign of the slope&lt;/strong&gt; indicates the &lt;strong&gt;direction of the line&lt;/strong&gt;—a positive slope (&lt;span class=&#34;math inline&#34;&gt;\(\widehat\beta_1 &amp;gt; 0\)&lt;/span&gt;) indicates that there is a positive relationship between the two variables of interest (they vary in the same direction), whereas a negative slope (&lt;span class=&#34;math inline&#34;&gt;\(\widehat\beta_1 &amp;lt; 0\)&lt;/span&gt;) means that there is a negative relationship between the two variables (they vary in opposite directions).&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;value of the slope&lt;/strong&gt; provides information on the &lt;strong&gt;speed of evolution&lt;/strong&gt; of the variable &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt; as a function of the variable &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt;. The larger the slope in absolute value, the larger the expected variation of &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt; for each unit of &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt;. Note, however, that a large value does not necessarily mean that the relationship is statistically significant (more on that in the section about &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/#significance-of-the-relationship&#34;&gt;significance of the relationship&lt;/a&gt;).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This is similar to the &lt;a href=&#34;https://statsandr.com/blog/correlation-coefficient-and-correlation-test-in-r/&#34;&gt;correlation coefficient&lt;/a&gt;, which gives information about the direction and the strength of the relationship between two variables.&lt;/p&gt;
&lt;p&gt;To perform a linear regression in R, we use the &lt;code&gt;lm()&lt;/code&gt; function (which stands for linear model). The function requires to set the dependent variable first then the independent variable, separated by a tilde (&lt;code&gt;~&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Applied to our example of weight and car’s consumption, we have:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;model &amp;lt;- lm(mpg ~ wt, data = dat)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;summary()&lt;/code&gt; function gives the results of the model:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;summary(model)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## Call:
## lm(formula = mpg ~ wt, data = dat)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.5432 -2.3647 -0.1252  1.4096  6.8727 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(&amp;gt;|t|)    
## (Intercept)  37.2851     1.8776  19.858  &amp;lt; 2e-16 ***
## wt           -5.3445     0.5591  -9.559 1.29e-10 ***
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## Residual standard error: 3.046 on 30 degrees of freedom
## Multiple R-squared:  0.7528,	Adjusted R-squared:  0.7446 
## F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In practice, we usually check the conditions of application &lt;em&gt;before&lt;/em&gt; interpreting the coefficients (because if they are not respected, results may be biased).&lt;/p&gt;
&lt;p&gt;In this article, however, I present the interpretations before testing the conditions because the point is to show how to interpret the results, and less about finding a valid model.&lt;/p&gt;
&lt;p&gt;The results can be summarized as follows (see the column &lt;code&gt;Estimate&lt;/code&gt; in the table &lt;code&gt;Coefficients&lt;/code&gt;):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The intercept &lt;span class=&#34;math inline&#34;&gt;\(\widehat\beta_0 =\)&lt;/span&gt; 37.29 indicates that, for a hypothetical car weighting 0 lbs, we can expect, on average, a consumption of 37.29 miles/gallon. This interpretation is shown for illustrative purposes, but as a car weighting 0 lbs is impossible, the interpretation has no meaning. In practice, we would therefore refrain from interpreting the intercept in this case. See another interpretation of the intercept when the independent variable is centered around its mean in this &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/#another-interpretation-of-the-intercept&#34;&gt;section&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The slope &lt;span class=&#34;math inline&#34;&gt;\(\widehat\beta_1 =\)&lt;/span&gt; -5.34 indicates that:
&lt;ul&gt;
&lt;li&gt;There is a &lt;strong&gt;negative relationship&lt;/strong&gt; between the weight and the distance a car can drive with a gallon (this was expected given the negative trend of the points in the scatterplot shown previously).&lt;/li&gt;
&lt;li&gt;But more importantly, a slope of -5.34 means that, for an increase of one unit in the weight (that is, an increase of 1000 lbs), the number of miles per gallon decreases, on average, by 5.34 units. In other words, &lt;strong&gt;for an increase of 1000 lbs, the number of miles/gallon decreases, on average, by 5.34&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div id=&#34;another-interpretation-of-the-intercept&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Another interpretation of the intercept&lt;/h3&gt;
&lt;p&gt;Another useful interpretation of the intercept is when the independent variable is centered around its mean. In this case, the intercept is interpreted as the mean value of &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt; for individuals who have a value of &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; equal to the mean of &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Let’s see it in practice.&lt;/p&gt;
&lt;p&gt;We first center the &lt;code&gt;wt&lt;/code&gt; variable around the mean then rerun a simple linear model with this new variable:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dat_centered &amp;lt;- dat

dat_centered$wt_centered &amp;lt;- dat$wt - mean(dat$wt)

mod_centered &amp;lt;- lm(mpg ~ wt_centered,
  data = dat_centered
)

summary(mod_centered)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## Call:
## lm(formula = mpg ~ wt_centered, data = dat_centered)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.5432 -2.3647 -0.1252  1.4096  6.8727 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(&amp;gt;|t|)    
## (Intercept)  20.0906     0.5384  37.313  &amp;lt; 2e-16 ***
## wt_centered  -5.3445     0.5591  -9.559 1.29e-10 ***
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## Residual standard error: 3.046 on 30 degrees of freedom
## Multiple R-squared:  0.7528,	Adjusted R-squared:  0.7446 
## F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Based on the results, we see that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The slope has not changed, the interpretation is the same than without the centering (which makes sense since the regression line has simply been shifted to the right or left).&lt;/li&gt;
&lt;li&gt;More importantly, the intercept is now &lt;span class=&#34;math inline&#34;&gt;\(\widehat\beta_0 =\)&lt;/span&gt; 20.09, so we can expect, on average, a consumption of 20.09 miles/gallon for a car with an average weight (the mean of weight is 3.22 so 3220 lbs).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This centering is particularly interesting:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;when the continuous independent variable has &lt;strong&gt;no&lt;/strong&gt; meaningful value of 0 (which is the case here as a car with a weight of 0 lbs is not meaningful), or&lt;/li&gt;
&lt;li&gt;when interpreting the intercept is important.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Note that centering does not have to be done around the mean only. The independent variable can also be centered at some value that is actually in the range of the data. The exact value you center on does not matter as long it’s meaningful and within the range of data (it is not recommended to center it on a value that is not in the range of the data because we are not sure about the type of relationship between the two variables outside that range).&lt;/p&gt;
&lt;p&gt;For our example, we may find that choosing the lowest value or the highest value of weight is the best option. So it’s up to us to decide the weight at which it’s most meaningful to interpret the intercept.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;significance-of-the-relationship&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Significance of the relationship&lt;/h2&gt;
&lt;p&gt;As mentioned earlier, the &lt;strong&gt;value of the slope does not&lt;/strong&gt;, by itself, make it possible to &lt;strong&gt;assess the significance of the linear relationship&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;In other words, a slope different from 0 does not necessarily mean it is &lt;em&gt;significantly&lt;/em&gt; different from 0, so it does not mean that there is a &lt;strong&gt;significant&lt;/strong&gt; relationship between the two variables in the population. There could be a slope of 10 that is not significant, and a slope of 2 that is significant.&lt;/p&gt;
&lt;p&gt;Significance of the relationship also depends on the variability of the slope, which is measured by its standard error and generally noted &lt;span class=&#34;math inline&#34;&gt;\(se(\widehat\beta_1)\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Without going too much into details, to assess the significance of the linear relationship, we divide the slope by its standard error. This ratio is the test statistic and follows a Student distribution with &lt;span class=&#34;math inline&#34;&gt;\(n - 2\)&lt;/span&gt; degrees of freedom:&lt;a href=&#34;#fn5&#34; class=&#34;footnote-ref&#34; id=&#34;fnref5&#34;&gt;&lt;sup&gt;5&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[T_{n - 2} = \frac{\widehat\beta_1}{se(\widehat\beta_1)}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;For a bilateral test, the null and alternative hypotheses are:&lt;a href=&#34;#fn6&#34; class=&#34;footnote-ref&#34; id=&#34;fnref6&#34;&gt;&lt;sup&gt;6&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(H_0 : \beta_1 = 0\)&lt;/span&gt; (there is no (linear) relationship between the two variables)&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(H_1 : \beta_1 \ne 0\)&lt;/span&gt; (there is a (linear) relationship between the two variables)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Roughly speaking, if this ratio is greater than 2 in absolute value then the slope is significantly different from 0, and therefore the relationship between the two variables is significant (and in that case it is positive or negative depending on the sign of the estimate &lt;span class=&#34;math inline&#34;&gt;\(\widehat\beta_1\)&lt;/span&gt;).&lt;/p&gt;
&lt;p&gt;The standard error and the test statistic are shown in the column &lt;code&gt;Std. Error&lt;/code&gt; and &lt;code&gt;t value&lt;/code&gt; in the table &lt;code&gt;Coefficients&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Fortunately, R gives a more precise and easier way to assess to the significance of the relationship. The information is provided in the column &lt;code&gt;Pr(&amp;gt;|t|)&lt;/code&gt; of the &lt;code&gt;Coefficients&lt;/code&gt; table. This is the &lt;a href=&#34;https://statsandr.com/blog/student-s-t-test-in-r-and-by-hand-how-to-compare-two-groups-under-different-scenarios/#a-note-on-p-value-and-significance-level-alpha&#34;&gt;&lt;em&gt;p&lt;/em&gt;-value&lt;/a&gt; of the test. As for any &lt;a href=&#34;https://statsandr.com/blog/what-statistical-test-should-i-do/&#34;&gt;statistical test&lt;/a&gt;, if the &lt;em&gt;p&lt;/em&gt;-value is greater than or equal to the significance level (usually &lt;span class=&#34;math inline&#34;&gt;\(\alpha = 0.05\)&lt;/span&gt;), we do not reject the null hypothesis, and if the &lt;em&gt;p&lt;/em&gt;-value is lower than the significance level, we reject the null hypothesis.&lt;/p&gt;
&lt;p&gt;If we do not reject the null hypothesis, we do not reject the hypothesis of no relationship between the two variables (because we do not reject the hypothesis of a slope of 0). On the contrary, if we reject the null hypothesis of no relationship, we can conclude that there is a significant linear relationship between the two variables.&lt;/p&gt;
&lt;p&gt;In our example, the &lt;em&gt;p&lt;/em&gt;-value = 1.29e-10 &amp;lt; 0.05 so we reject the null hypothesis at the significance level &lt;span class=&#34;math inline&#34;&gt;\(\alpha = 5\%\)&lt;/span&gt;. We therefore conclude that there is a &lt;strong&gt;significant relationship between a car’s weight and its fuel consumption&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Tip:&lt;/em&gt; In order to make sure I interpret only parameters that are significant, I tend to first check the significance of the parameters thanks to the &lt;em&gt;p&lt;/em&gt;-values, and then interpret the estimates accordingly. For completeness, note that the test is also performed on the intercept. The &lt;em&gt;p&lt;/em&gt;-value being smaller than 0.05, we also conclude that the intercept is significantly different from 0.&lt;/p&gt;
&lt;div id=&#34;correlation-does-not-imply-causation&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Correlation does not imply causation&lt;/h3&gt;
&lt;p&gt;Be careful that a significant relationship between two variables does not necessarily mean that there is an influence of one variable on the other or that there is a causal effect between these two variables!&lt;/p&gt;
&lt;p&gt;A significant relationship between &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt; can appear in several cases:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; causes &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt; causes &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;a third variable cause &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;a combination of these three reasons&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A statistical model alone cannot establish a causal link between two variables. Demonstrating causality between two variables is more complex and requires, among others, a specific experimental design, the repeatability of the results over time, as well as various samples.&lt;/p&gt;
&lt;p&gt;This is the reason you will often read “&lt;a href=&#34;https://statsandr.com/blog/correlation-coefficient-and-correlation-test-in-r/#correlation-does-not-imply-causation&#34;&gt;Correlation does not imply causation&lt;/a&gt;” and linear regression follows the same principle.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;conditions-of-application&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Conditions of application&lt;/h2&gt;
&lt;p&gt;Unfortunately, linear regression cannot be used in all situations.&lt;/p&gt;
&lt;p&gt;In addition to the requirement that the dependent variable must be a continuous quantitative variables, simple linear regression requires that the data satisfy the following conditions:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;&lt;strong&gt;Linearity:&lt;/strong&gt; The relationship between the two variables should be linear (at least roughly). For this reason it is always necessary to represent graphically the data with a scatterplot before performing a simple linear regression.&lt;a href=&#34;#fn7&#34; class=&#34;footnote-ref&#34; id=&#34;fnref7&#34;&gt;&lt;sup&gt;7&lt;/sup&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/index_files/figure-html/unnamed-chunk-7-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;ol start=&#34;2&#34; style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;&lt;strong&gt;Independence:&lt;/strong&gt; Observations must be independent. It is the sampling plan and the experimental design that usually provide information on this condition. If the data come from different individuals or experimental units, they are usually independent. On the other hand, if the same individuals are measured at different periods, the data are probably not independent.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Normality of the residuals:&lt;/strong&gt; For large sample sizes, confidence intervals and tests on the coefficients are (approximately) valid whether the error follows a &lt;a href=&#34;https://statsandr.com/blog/do-my-data-follow-a-normal-distribution-a-note-on-the-most-widely-used-distribution-and-how-to-test-for-normality-in-r/&#34;&gt;normal distribution&lt;/a&gt; or not (a consequence of the central limit theorem, see more in &lt;span class=&#34;citation&#34;&gt;Ernst and Albers (&lt;a href=&#34;#ref-ernst2017regression&#34;&gt;2017&lt;/a&gt;)&lt;/span&gt; and &lt;span class=&#34;citation&#34;&gt;Lumley et al. (&lt;a href=&#34;#ref-lumley2002importance&#34;&gt;2002&lt;/a&gt;)&lt;/span&gt;)! For small sample sizes, residuals should follow a normal distribution. This condition can be tested visually (via a &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/#qq-plot&#34;&gt;QQ-plot&lt;/a&gt; and/or a &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/#histogram&#34;&gt;histogram&lt;/a&gt;), or more formally (via the &lt;a href=&#34;https://statsandr.com/blog/do-my-data-follow-a-normal-distribution-a-note-on-the-most-widely-used-distribution-and-how-to-test-for-normality-in-r/#normality-test&#34;&gt;Shapiro-Wilk test&lt;/a&gt; for instance).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Homoscedasticity of the residuals:&lt;/strong&gt; The variance of the errors should be constant. There is a lack of homoscedasticity when the dispersion of the residuals increases with the predicted values (fitted values). This condition can be tested visually (by plotting the standardized residuals vs. the fitted values) or more formally (via the Breusch-Pagan test).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No influential points:&lt;/strong&gt; If the data contain &lt;a href=&#34;https://statsandr.com/blog/outliers-detection-in-r/&#34;&gt;outliers&lt;/a&gt;, it is essential to identify them so that they &lt;strong&gt;do not&lt;/strong&gt;, on their own, &lt;strong&gt;influence&lt;/strong&gt; the results of the regression. Note that an outlier is not an issue &lt;em&gt;per se&lt;/em&gt; if the point is in the alignment of the regression line for example because it does not influence the regression line. It becomes a problem in the context of linear regression if it influences in a substantial manner the estimates (and in particular the slope of the regression line). This can be tackled by identifying outliers (via the Cook’s distance&lt;a href=&#34;#fn8&#34; class=&#34;footnote-ref&#34; id=&#34;fnref8&#34;&gt;&lt;sup&gt;8&lt;/sup&gt;&lt;/a&gt; or the leverage index&lt;a href=&#34;#fn9&#34; class=&#34;footnote-ref&#34; id=&#34;fnref9&#34;&gt;&lt;sup&gt;9&lt;/sup&gt;&lt;/a&gt; for instance), and comparing the results with and without the potential outliers. Do the results remain the same with the two approaches? If yes, outliers are not really an issue in this case. If results are much different, you can use the Theil-Sen estimator, robust regression or quantile regression which are all more robust to outliers.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;em&gt;Tip:&lt;/em&gt; I remember the first 4 conditions thanks to the acronym “LINE”, for Linearity, Independence, Normality and Equality of variance.&lt;/p&gt;
&lt;p&gt;If any of the condition is not met, the tests and the conclusions could be erroneous so it is best to avoid using and interpreting the model. If this is the case, sometimes the conditions can be met by transforming the data (e.g., logarithmic transformation, square or square root, Box-Cox transformation, etc.) or by adding a quadratic or cubic (or even a higher-order polynomial) term to the model.&lt;/p&gt;
&lt;p&gt;If it does not help, it could be worth thinking about removing some variables or adding other variables, or even considering other types of models such as non-linear models.&lt;/p&gt;
&lt;p&gt;Keep in mind that in practice, &lt;strong&gt;conditions of application should be verified before drawing any conclusion&lt;/strong&gt; based on the model. I refrain here from testing the conditions on our data because it will be covered in details in the context of multiple linear regression (see this &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/#conditions-of-application-1&#34;&gt;section&lt;/a&gt;).&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;visualizations&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Visualizations&lt;/h2&gt;
&lt;p&gt;If you are a frequent reader of the blog, you may know that I like to draw (simple but efficient) &lt;a href=&#34;https://statsandr.com/tags/visualization/&#34;&gt;visualizations&lt;/a&gt; to illustrate my statistical analyses. Linear regression is not an exception.&lt;/p&gt;
&lt;p&gt;There are numerous ways to visualize the relationship between the two variables of interest, but the easiest one I found so far is via the &lt;code&gt;visreg()&lt;/code&gt; function from the package of the same name:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(visreg)
visreg(model)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/index_files/figure-html/unnamed-chunk-8-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;I like this approach for its simplicity—only a single line of code.&lt;/p&gt;
&lt;p&gt;However, other elements could be displayed on the regression plot (for example the regression equation and the &lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt;). This can easily be done with the &lt;code&gt;stat_regline_equation()&lt;/code&gt; and &lt;code&gt;stat_cor()&lt;/code&gt; functions from the &lt;code&gt;{ggpubr}&lt;/code&gt; package:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# load necessary libraries
library(ggpubr)

# create plot with regression line, regression equation and R^2
ggplot(dat, aes(x = wt, y = mpg)) +
  geom_smooth(method = &amp;quot;lm&amp;quot;) +
  geom_point() +
  stat_regline_equation(label.x = 3, label.y = 32) + # for regression equation
  stat_cor(aes(label = after_stat(rr.label)), label.x = 3, label.y = 30) + # for R^2
  theme_minimal()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/index_files/figure-html/unnamed-chunk-9-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;multiple-linear-regression&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Multiple linear regression&lt;/h1&gt;
&lt;p&gt;Now that you understand the principle behind simple linear regression and you know how to interpret the results, it is time to discuss about multiple linear regression.&lt;/p&gt;
&lt;p&gt;We also start with the underlying principle of multiple linear regression, then show how to interpret the results, how to test the conditions of application and finish with more advanced topics.&lt;/p&gt;
&lt;div id=&#34;principle-1&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Principle&lt;/h2&gt;
&lt;p&gt;Multiple linear regression is a generalization of simple linear regression, in the sense that this approach makes it possible to relate one variable with &lt;strong&gt;several variables&lt;/strong&gt; through a linear function in its parameters.&lt;/p&gt;
&lt;p&gt;Multiple linear regression is used to assess the relationship between two variables &lt;strong&gt;while taking into account the effect of other variables&lt;/strong&gt;. By taking into account the effect of other variables, we cancel out the effect of these other variables in order to &lt;strong&gt;isolate&lt;/strong&gt; and measure the relationship between the two variables of interest. This point is the main difference with simple linear regression.&lt;/p&gt;
&lt;p&gt;To illustrate how to perform a multiple linear regression in R, we use the same dataset than the one used for simple linear regression (&lt;code&gt;mtcars&lt;/code&gt;). Below a short preview:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;head(dat)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We have seen that there is a significant and negative linear relationship between the distance a car can drive with a gallon and its weight (&lt;span class=&#34;math inline&#34;&gt;\(\widehat\beta_1 =\)&lt;/span&gt; -5.34, &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value &amp;lt; 0.001).&lt;/p&gt;
&lt;p&gt;However, one may wonder whether there are not in reality other factors that could explain a car’s fuel consumption.&lt;/p&gt;
&lt;p&gt;To explore this, we can visualize the relationship between a car’s fuel consumption (&lt;code&gt;mpg&lt;/code&gt;) together with its weight (&lt;code&gt;wt&lt;/code&gt;), horsepower (&lt;code&gt;hp&lt;/code&gt;) and displacement (&lt;code&gt;disp&lt;/code&gt;) (engine displacement is the combined swept (or displaced) volume of air resulting from the up-and-down movement of pistons in the cylinders, usually the higher the more powerful the car):&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ggplot(dat) +
  aes(x = wt, y = mpg, colour = hp, size = disp) +
  geom_point() +
  scale_color_gradient() +
  labs(
    y = &amp;quot;Miles per gallon&amp;quot;,
    x = &amp;quot;Weight (1000 lbs)&amp;quot;,
    color = &amp;quot;Horsepower&amp;quot;,
    size = &amp;quot;Displacement&amp;quot;
  ) +
  theme_minimal()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/index_files/figure-html/unnamed-chunk-11-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;It seems that, in addition to the negative relationship between miles per gallon and weight, there is also:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a negative relationship between miles/gallon and horsepower (lighter points, indicating more horsepower, tend to be more present in low levels of miles per gallon)&lt;/li&gt;
&lt;li&gt;a negative relationship between miles/gallon and displacement (bigger points, indicating larger values of displacement, tend to be more present in low levels of miles per gallon).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Therefore, we would like to evaluate the relation between the fuel consumption and the weight, but this time by adding information on the horsepower and displacement. By adding this additional information, we are able to &lt;strong&gt;capture only the direct relationship between miles/gallon and weight&lt;/strong&gt; (the indirect effect due to horsepower and displacement is canceled out).&lt;/p&gt;
&lt;p&gt;This is the whole point of multiple linear regression! In fact, in multiple linear regression, the estimated relationship between the dependent variable and an explanatory variable is an &lt;strong&gt;adjusted&lt;/strong&gt; relationship, that is, free of the linear effects of the other explanatory variables.&lt;/p&gt;
&lt;p&gt;Let’s illustrate this notion of adjustment by adding both horsepower and displacement in our linear regression model:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;model2 &amp;lt;- lm(mpg ~ wt + hp + disp,
  data = dat
)

summary(model2)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## Call:
## lm(formula = mpg ~ wt + hp + disp, data = dat)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.891 -1.640 -0.172  1.061  5.861 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(&amp;gt;|t|)    
## (Intercept) 37.105505   2.110815  17.579  &amp;lt; 2e-16 ***
## wt          -3.800891   1.066191  -3.565  0.00133 ** 
## hp          -0.031157   0.011436  -2.724  0.01097 *  
## disp        -0.000937   0.010350  -0.091  0.92851    
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## Residual standard error: 2.639 on 28 degrees of freedom
## Multiple R-squared:  0.8268,	Adjusted R-squared:  0.8083 
## F-statistic: 44.57 on 3 and 28 DF,  p-value: 8.65e-11&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can see that now, the relationship between miles/gallon and weight is weaker in terms of slope (&lt;span class=&#34;math inline&#34;&gt;\(\widehat\beta_1 =\)&lt;/span&gt; -3.8 now, against &lt;span class=&#34;math inline&#34;&gt;\(\widehat\beta_1 =\)&lt;/span&gt; -5.34 when only the weight was considered).&lt;/p&gt;
&lt;p&gt;The effect of weight on fuel consumption was adjusted according to the effect of horsepower and displacement. This is the remaining effect between miles/gallon and weight after the effects of horsepower and displacement have been taken into account. More detailed interpretations in this &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/#interpretations-of-coefficients-widehatbeta-1&#34;&gt;section&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;equation-1&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Equation&lt;/h2&gt;
&lt;p&gt;Multiple linear regression models are defined by the equation&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[Y = \beta_0 + \beta_1 X_1 + \beta_2 X_2 + \dots + \beta_p X_p + \epsilon\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;It is similar than the equation of simple linear regression, except that there is more than one independent variables (&lt;span class=&#34;math inline&#34;&gt;\(X_1, X_2, \dots, X_p\)&lt;/span&gt;).&lt;/p&gt;
&lt;p&gt;Estimation of the parameters &lt;span class=&#34;math inline&#34;&gt;\(\beta_0, \dots, \beta_p\)&lt;/span&gt; by the method of least squares is based on the same principle as that of simple linear regression, but applied to &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt; dimensions. It is thus no longer a question of finding the best line (the one which passes closest to the pairs of points (&lt;span class=&#34;math inline&#34;&gt;\(y_i, x_i\)&lt;/span&gt;)), but finding the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-dimensional plane which passes closest to the coordinate points (&lt;span class=&#34;math inline&#34;&gt;\(y_i, x_{i1}, \dots, x_{ip}\)&lt;/span&gt;).&lt;/p&gt;
&lt;p&gt;This is done by &lt;strong&gt;&lt;em&gt;minimizing&lt;/em&gt; the sum of the squares of the deviations of the points on the plane&lt;/strong&gt;:&lt;/p&gt;
&lt;div class=&#34;float&#34;&gt;
&lt;img src=&#34;images/multiple-linear-regression-plane.png&#34; style=&#34;width:100.0%&#34; alt=&#34;Source: James, G., Witten, D., Hastie, T., &amp;amp; Tibshirani, R. (2013)&#34; /&gt;
&lt;div class=&#34;figcaption&#34;&gt;Source: James, G., Witten, D., Hastie, T., &amp;amp; Tibshirani, R. (2013)&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;interpretations-of-coefficients-widehatbeta-1&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Interpretations of coefficients &lt;span class=&#34;math inline&#34;&gt;\(\widehat\beta\)&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;The least squares method results in an adjusted estimate of the coefficients. The term adjusted means &lt;strong&gt;after taking into account the linear effects&lt;/strong&gt; of the other independent variables on the dependent variable, but also on the predictor variable.&lt;/p&gt;
&lt;p&gt;In other words, the coefficient &lt;span class=&#34;math inline&#34;&gt;\(\beta_1\)&lt;/span&gt; corresponds to the slope of the relationship between &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(X_1\)&lt;/span&gt; when the linear effects of the other explanatory variables (&lt;span class=&#34;math inline&#34;&gt;\(X_2, \dots, X_p\)&lt;/span&gt;) have been removed, both at the level of the dependent variable &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt; but also at the level of &lt;span class=&#34;math inline&#34;&gt;\(X_1\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Applied to our model with weight, horsepower and displacement as independent variables, we have:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;summary(model2)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## Call:
## lm(formula = mpg ~ wt + hp + disp, data = dat)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.891 -1.640 -0.172  1.061  5.861 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(&amp;gt;|t|)    
## (Intercept) 37.105505   2.110815  17.579  &amp;lt; 2e-16 ***
## wt          -3.800891   1.066191  -3.565  0.00133 ** 
## hp          -0.031157   0.011436  -2.724  0.01097 *  
## disp        -0.000937   0.010350  -0.091  0.92851    
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## Residual standard error: 2.639 on 28 degrees of freedom
## Multiple R-squared:  0.8268,	Adjusted R-squared:  0.8083 
## F-statistic: 44.57 on 3 and 28 DF,  p-value: 8.65e-11&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The table &lt;code&gt;Coefficients&lt;/code&gt; gives the estimate for each parameter (column &lt;code&gt;Estimate&lt;/code&gt;), together with the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value of the nullity of the parameter (column &lt;code&gt;Pr(&amp;gt;|t|)&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;The hypotheses are the same as for simple linear regression, that is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(H_0 : \beta_j = 0\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(H_1 : \beta_j \ne 0\)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The test of &lt;span class=&#34;math inline&#34;&gt;\(\beta_j = 0\)&lt;/span&gt; is equivalent to testing the hypothesis: is the dependent variable associated with the independent variable studied, all other things being equal, that is to say, at constant level of the other independent variables.&lt;/p&gt;
&lt;p&gt;In other words:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the test of &lt;span class=&#34;math inline&#34;&gt;\(\beta_1 = 0\)&lt;/span&gt; corresponds to testing the hypothesis: is fuel consumption associated with a car’s weight, at a constant level of horsepower and displacement&lt;/li&gt;
&lt;li&gt;the test of &lt;span class=&#34;math inline&#34;&gt;\(\beta_2 = 0\)&lt;/span&gt; corresponds to testing the hypothesis: is fuel consumption associated with horsepower, at a constant level of weight and displacement&lt;/li&gt;
&lt;li&gt;the test of &lt;span class=&#34;math inline&#34;&gt;\(\beta_3 = 0\)&lt;/span&gt; corresponds to testing the hypothesis: is fuel consumption associated with displacement, at a constant level of weight and horsepower&lt;/li&gt;
&lt;li&gt;(for the sake of completeness: the test of &lt;span class=&#34;math inline&#34;&gt;\(\beta_0 = 0\)&lt;/span&gt; corresponds to testing the hypothesis: is miles/gallon different from 0 when weight, horsepower and displacement are equal to 0)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In practice, we usually check the conditions of application &lt;em&gt;before&lt;/em&gt; interpreting the coefficients (because if they are not respected, results may be biased). In this article, however, I present the interpretations before testing the conditions because the point is to show how to interpret the results, and less about finding a valid model.&lt;/p&gt;
&lt;p&gt;Based on the output of our model, we conclude that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;There is a significant and negative relationship between miles/gallon and weight, &lt;strong&gt;all else being equal&lt;/strong&gt;. So for an increase of one unit in the weight (that is, an increase of 1000 lbs), the number of miles/gallon decreases, on average, by 3.8, for a constant level of horsepower and displacement (&lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value = 0.001).&lt;/li&gt;
&lt;li&gt;There is a significant and negative relationship between miles/gallon and horsepower, all else being equal. So for an increase of one unit of horsepower, the distance traveled with a gallon decreases, on average, by 0.03 mile, for a constant level of weight and displacement (&lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value = 0.011).&lt;/li&gt;
&lt;li&gt;We do not reject the hypothesis of no relationship between miles/gallon and displacement when weight and horsepower stay constant (because &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value = 0.929 &amp;gt; 0.05).&lt;/li&gt;
&lt;li&gt;(For completeness but it should be interpreted only when it makes sense: for a weight, horsepower and displacement = 0, we can expect that a car has, on average, a fuel consumption of 37.11 miles/gallon (&lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value &amp;lt; 0.001). See a more useful interpretation of the intercept when the independent variables are centered in this &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/#another-interpretation-of-the-intercept&#34;&gt;section&lt;/a&gt;.)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is how to interpret quantitative independent variables. &lt;strong&gt;Interpreting qualitative independent variables&lt;/strong&gt; is slightly different in the sense that it quantifies the effect of a level in comparison with the reference level, sill all else being equal.&lt;/p&gt;
&lt;p&gt;So it compares the different groups (formed by the different levels of the categorical variable) in terms of the dependent variable (this is why linear regression can be seen as an extension to the t-test and ANOVA).&lt;/p&gt;
&lt;p&gt;For the illustration, we model the fuel consumption (&lt;code&gt;mpg&lt;/code&gt;) on the weight (&lt;code&gt;wt&lt;/code&gt;) and the shape of the engine (&lt;code&gt;vs&lt;/code&gt;). The variable &lt;code&gt;vs&lt;/code&gt; has two levels: V-shaped (the &lt;a href=&#34;https://statsandr.com/blog/data-manipulation-in-r/#change-reference-level&#34;&gt;reference level&lt;/a&gt;) and straight engine.&lt;a href=&#34;#fn10&#34; class=&#34;footnote-ref&#34; id=&#34;fnref10&#34;&gt;&lt;sup&gt;10&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;## Recoding dat$vs
library(forcats)
dat$vs &amp;lt;- as.character(dat$vs)
dat$vs &amp;lt;- fct_recode(dat$vs,
  &amp;quot;V-shaped&amp;quot; = &amp;quot;0&amp;quot;,
  &amp;quot;Straight&amp;quot; = &amp;quot;1&amp;quot;
)

model3 &amp;lt;- lm(mpg ~ wt + vs,
  data = dat
)

summary(model3)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## Call:
## lm(formula = mpg ~ wt + vs, data = dat)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.7071 -2.4415 -0.3129  1.4319  6.0156 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(&amp;gt;|t|)    
## (Intercept)  33.0042     2.3554  14.012 1.92e-14 ***
## wt           -4.4428     0.6134  -7.243 5.63e-08 ***
## vsStraight    3.1544     1.1907   2.649   0.0129 *  
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## Residual standard error: 2.78 on 29 degrees of freedom
## Multiple R-squared:  0.801,	Adjusted R-squared:  0.7873 
## F-statistic: 58.36 on 2 and 29 DF,  p-value: 6.818e-11&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Based on the output of our model, we conclude that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;For a V-shaped engine and for an increase of one unit in the weight (that is, an increase of 1000 lbs), the number of miles/gallon decreases, on average, by 4.44 (&lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value &amp;lt; 0.001).&lt;/li&gt;
&lt;li&gt;The distance traveled with a gallon of fuel increases by, on average, 3.15 miles &lt;strong&gt;when the engine is straight compared to a V-shaped engine&lt;/strong&gt;, for a constant weight (&lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value = 0.013).&lt;/li&gt;
&lt;li&gt;(For completeness but it should be interpreted only when it makes sense: for a weight = 0 and a V-shaped engine, we can expect that the car has, on average, a fuel consumption of 33 miles/gallon (&lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value &amp;lt; 0.001). See a more useful interpretation of the intercept when the independent variables are centered in this &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/#another-interpretation-of-the-intercept&#34;&gt;section&lt;/a&gt;.)&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;conditions-of-application-1&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Conditions of application&lt;/h2&gt;
&lt;p&gt;As for simple linear regression, multiple linear regression requires some conditions of application for the model to be usable and the results to be interpretable. Conditions for simple linear regression also apply to multiple linear regression, that is:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;&lt;strong&gt;Linearity&lt;/strong&gt; of the relationships between the dependent and independent variables&lt;a href=&#34;#fn11&#34; class=&#34;footnote-ref&#34; id=&#34;fnref11&#34;&gt;&lt;sup&gt;11&lt;/sup&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Independence&lt;/strong&gt; of the observations&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Normality&lt;/strong&gt; of the residuals&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Homoscedasticity&lt;/strong&gt; of the residuals&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No influential points&lt;/strong&gt; (&lt;a href=&#34;https://statsandr.com/blog/outliers-detection-in-r/&#34;&gt;outliers&lt;/a&gt;)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;But there is one more condition for multiple linear regression:&lt;/p&gt;
&lt;ol start=&#34;6&#34; style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;&lt;strong&gt;No multicollinearity:&lt;/strong&gt; Multicollinearity arises when there is a strong linear &lt;strong&gt;&lt;a href=&#34;https://statsandr.com/blog/correlation-coefficient-and-correlation-test-in-r/&#34;&gt;correlation&lt;/a&gt; between the independent variables&lt;/strong&gt;, conditional on the other variables in the model. It is important to check it because it may lead to an imprecision or an instability of the estimated parameters when a variable changes. It can be assessed by studying the correlation between each pair of independent variables, or even better, by computing the variance inflation factor (VIF). The VIF measures how much the variance of an estimated regression coefficient increases, relative to a situation in which the explanatory variables are strictly independent. A high value of VIF is a sign of multicollinearity (the threshold is generally admitted at 5 or 10 depending on the domain). The easiest way to reduce the VIF is to remove some correlated independent variables, or eventually to &lt;a href=&#34;https://statsandr.com/blog/data-manipulation-in-r/#scale&#34;&gt;standardize&lt;/a&gt; the data.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You will often see that these conditions are verified by running &lt;code&gt;plot(model, which = 1:6)&lt;/code&gt; and it is totally correct. However, I recently discovered the &lt;code&gt;check_model()&lt;/code&gt; function from the &lt;code&gt;{performance}&lt;/code&gt; package which tests these conditions all at the same time (and let’s be honest, in a more elegant way).&lt;a href=&#34;#fn12&#34; class=&#34;footnote-ref&#34; id=&#34;fnref12&#34;&gt;&lt;sup&gt;12&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Applied on our &lt;code&gt;model2&lt;/code&gt; with miles/gallon as dependent variable, and weight, horsepower and displacement as independent variables, we have:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# install.packages(&amp;quot;performance&amp;quot;)
# install.packages(&amp;quot;see&amp;quot;)
library(performance)

check_model(model2)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/index_files/figure-html/unnamed-chunk-15-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;In addition to testing all conditions at the same time, it also gives insight on how to interpret the different diagnostic plots and what you should expect (see in the subtitles of each plot).&lt;/p&gt;
&lt;p&gt;Based on these diagnostic plots, we see that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Homogeneity of variance (middle left plot) is respected&lt;/li&gt;
&lt;li&gt;Multicollinearity (bottom left plot) is not an issue (I tend to use the threshold of 10 for VIF, and all of them are below 10)&lt;a href=&#34;#fn13&#34; class=&#34;footnote-ref&#34; id=&#34;fnref13&#34;&gt;&lt;sup&gt;13&lt;/sup&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;There is no influential points (middle right plot)&lt;/li&gt;
&lt;li&gt;Normality of the residuals (bottom right plot) is also not perfect due to 3 points deviating from the reference line but it still seems acceptable to me. In any case, the number of observations is large enough given the number of parameters&lt;a href=&#34;#fn14&#34; class=&#34;footnote-ref&#34; id=&#34;fnref14&#34;&gt;&lt;sup&gt;14&lt;/sup&gt;&lt;/a&gt; and given the small deviation from normality so tests on the coefficients are (approximately) valid whether the error follows a normal distribution or not&lt;/li&gt;
&lt;li&gt;Linearity (top right plot) is not perfect so let’s check each independent variable separately:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# weight
ggplot(dat, aes(x = wt, y = mpg)) +
  geom_point() +
  theme_minimal()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/index_files/figure-html/unnamed-chunk-16-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# horsepower
ggplot(dat, aes(x = hp, y = mpg)) +
  geom_point() +
  theme_minimal()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/index_files/figure-html/unnamed-chunk-16-2.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# displacement
ggplot(dat, aes(x = disp, y = mpg)) +
  geom_point() +
  theme_minimal()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/index_files/figure-html/unnamed-chunk-16-3.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;It seems that the relationship between miles/gallon and horsepower is not linear, which could be the main component of the slight linearity defect of the model.&lt;/p&gt;
&lt;p&gt;To improve linearity, the variable could be removed, a transformation could be applied (logarithmic and/or squared for instance) or a quadratic or cubic (or even a higher-order polynomial) term could be added to the model.&lt;a href=&#34;#fn15&#34; class=&#34;footnote-ref&#34; id=&#34;fnref15&#34;&gt;&lt;sup&gt;15&lt;/sup&gt;&lt;/a&gt; If this does not fix the issue of linearity, other types of models could be considered.&lt;/p&gt;
&lt;p&gt;If you want to read more about these conditions of applications and how to deal with them, here is a very complete &lt;a href=&#34;http://quantpsych.net/stats_modeling/diagnostics.html&#34;&gt;chapter&lt;/a&gt; on diagnostics for linear models written by Prof. Dustin Fife.&lt;/p&gt;
&lt;p&gt;For the sake of easiness and for illustrative purposes, we assume linearity for the rest of the article.&lt;/p&gt;
&lt;p&gt;When the conditions of application are met, we usually say that the model is valid. But not all valid models are &lt;em&gt;good&lt;/em&gt; models. The next section deals with model selection.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;how-to-choose-a-good-linear-model&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;How to choose a good linear model?&lt;/h2&gt;
&lt;p&gt;A model which satisfies the conditions of application is the minimum requirement, but you will likely find several models that meet this criteria. So one may wonder &lt;strong&gt;how to choose between different models&lt;/strong&gt; that are all valid?&lt;/p&gt;
&lt;p&gt;The three most common tools to select a good linear model are according to:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value associated to the model,&lt;/li&gt;
&lt;li&gt;the coefficient of determination &lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt; and&lt;/li&gt;
&lt;li&gt;the Akaike Information Criterion&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The approaches are detailed in the next sections. Note that the first two are applicable to simple and multiple linear regression, whereas the third is only applicable to multiple linear regression.&lt;/p&gt;
&lt;div id=&#34;p-value-associated-to-the-model&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;&lt;span class=&#34;math inline&#34;&gt;\(P\)&lt;/span&gt;-value associated to the model&lt;/h3&gt;
&lt;p&gt;Before interpreting the estimates of a model, it is a good practice to first check the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value associated to the model. This &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value indicates if the model is &lt;strong&gt;better than a model with only the intercept&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The hypotheses of the test (called F-test) are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(H_0: \beta_1 = \beta_2 = \dots = \beta_p = 0\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(H_1:\)&lt;/span&gt; at least one coefficient &lt;span class=&#34;math inline&#34;&gt;\(\beta \ne 0\)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value can be found at the bottom of the &lt;code&gt;summary()&lt;/code&gt; output:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;summary(model2)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## Call:
## lm(formula = mpg ~ wt + hp + disp, data = dat)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.891 -1.640 -0.172  1.061  5.861 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(&amp;gt;|t|)    
## (Intercept) 37.105505   2.110815  17.579  &amp;lt; 2e-16 ***
## wt          -3.800891   1.066191  -3.565  0.00133 ** 
## hp          -0.031157   0.011436  -2.724  0.01097 *  
## disp        -0.000937   0.010350  -0.091  0.92851    
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## Residual standard error: 2.639 on 28 degrees of freedom
## Multiple R-squared:  0.8268,	Adjusted R-squared:  0.8083 
## F-statistic: 44.57 on 3 and 28 DF,  p-value: 8.65e-11&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value = 8.65e-11. The null hypothesis is rejected, so we conclude that our model is better than a model with only the intercept because at least one coefficient &lt;span class=&#34;math inline&#34;&gt;\(\beta\)&lt;/span&gt; is significantly different from 0.&lt;/p&gt;
&lt;p&gt;If this &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value &amp;gt; 0.05 for one of your model, it means that none of the variables you selected help in explaining the dependent variable. In other words, you should completely forget about this model because it cannot do better than simply taking the mean of the dependent variable.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;coefficient-of-determination-r2&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Coefficient of determination &lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt;&lt;/h3&gt;
&lt;p&gt;The coefficient of determination, &lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt;, is a measure of the &lt;strong&gt;goodness of fit of the model&lt;/strong&gt;. It measures the proportion of the total variability that is explained by the model, or how well the model fits the data.&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt; varies between 0 and 1:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(R^2 = 0\)&lt;/span&gt;: the model explains nothing&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(R^2 = 1\)&lt;/span&gt;: the model explains everything&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(0 &amp;lt; R^2 &amp;lt; 1\)&lt;/span&gt;: the model explains part of the variability&lt;/li&gt;
&lt;li&gt;the higher the &lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt;, the better the model explains the dependent variable. As a rule of thumb, a &lt;span class=&#34;math inline&#34;&gt;\(R^2 &amp;gt; 0.7\)&lt;/span&gt; indicates a good fit of the model&lt;a href=&#34;#fn16&#34; class=&#34;footnote-ref&#34; id=&#34;fnref16&#34;&gt;&lt;sup&gt;16&lt;/sup&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Note that in a simple linear regression model, the coefficient of determination is equal to the square of the Pearson &lt;a href=&#34;https://statsandr.com/blog/correlation-coefficient-and-correlation-test-in-r/&#34;&gt;correlation coefficient&lt;/a&gt;:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[R^2 = corr(X, Y)^2\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Applied on our &lt;code&gt;model2&lt;/code&gt; with miles/gallon as dependent variable, and weight, horsepower and displacement as independent variables, we have:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;summary(model2)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## Call:
## lm(formula = mpg ~ wt + hp + disp, data = dat)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.891 -1.640 -0.172  1.061  5.861 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(&amp;gt;|t|)    
## (Intercept) 37.105505   2.110815  17.579  &amp;lt; 2e-16 ***
## wt          -3.800891   1.066191  -3.565  0.00133 ** 
## hp          -0.031157   0.011436  -2.724  0.01097 *  
## disp        -0.000937   0.010350  -0.091  0.92851    
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## Residual standard error: 2.639 on 28 degrees of freedom
## Multiple R-squared:  0.8268,	Adjusted R-squared:  0.8083 
## F-statistic: 44.57 on 3 and 28 DF,  p-value: 8.65e-11&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt; is displayed at the bottom of the &lt;code&gt;summary()&lt;/code&gt; output or can be extracted with &lt;code&gt;summary(model2)$r.squared&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt; for this model is 0.8268, which means that 82.68% of the variability of the distance traveled with a gallon is explained by the weight, horsepower and displacement of the car. The relatively high &lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt; means that the weight, horsepower and displacement of a car are good characteristics to explain the distance it can drive with a gallon of fuel.&lt;/p&gt;
&lt;p&gt;Note that if you want to compare models with different number of independent variables, it is best to refer to the adjusted &lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt; (= 0.8083 here).&lt;/p&gt;
&lt;p&gt;Indeed, adding variables to the model cannot make the &lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt; to decrease, even if the variables are not related to the dependent variables (so the &lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt; will artificially increase when adding variables to the model, or at least stay constant). Therefore, the adjusted &lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt; takes into account the complexity of the model (the number of variables) by penalizing for additional variables, so it is a compromise between goodness of fit and parsimony.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;parsimony&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Parsimony&lt;/h3&gt;
&lt;p&gt;A &lt;strong&gt;parsimonious model (few variables) is usually preferred&lt;/strong&gt; over a complex model (many variables). There are two ways to obtain a parsimonious model from a model with many independent variables:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;We can &lt;strong&gt;iteratively remove the independent variable least significantly related to the dependent variable&lt;/strong&gt; (i.e., the one with the highest &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value in an &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/#overall-effect-of-categorical-variables&#34;&gt;analysis of variance table&lt;/a&gt;) until all of them are significantly associated to the response variable, or&lt;/li&gt;
&lt;li&gt;We can select the model based on the &lt;strong&gt;Akaike Information Criterion (AIC)&lt;/strong&gt;. AIC expresses a desire to fit the model with the smallest number of coefficients possible and allows to compare models. According to this criterion, the best model is the one with the lowest AIC. This criterion is based on a compromise between the quality of the fit and its complexity. We usually start from a global model with many independent variables, and the procedure (referred as stepwise algorithm)&lt;a href=&#34;#fn17&#34; class=&#34;footnote-ref&#34; id=&#34;fnref17&#34;&gt;&lt;sup&gt;17&lt;/sup&gt;&lt;/a&gt; automatically compares models then selects the best one according to the AIC.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We show how to do the second option in R. For the illustration, we start with a model with all variables in the dataset as independent variables (do not forget to transform the factor variables first):&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;## vs has already been transformed into factor
## so only am is transformed here

## Recoding dat$vs
library(forcats)
dat$am &amp;lt;- as.character(dat$am)
dat$am &amp;lt;- fct_recode(dat$am,
  &amp;quot;Automatic&amp;quot; = &amp;quot;0&amp;quot;,
  &amp;quot;Manual&amp;quot; = &amp;quot;1&amp;quot;
)

model4 &amp;lt;- lm(mpg ~ .,
  data = dat
)

model4 &amp;lt;- step(model4, trace = FALSE)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(&lt;em&gt;Tip:&lt;/em&gt; The formula &lt;code&gt;mpg ~ .&lt;/code&gt; is a shortcut to consider all variables present in the dataset as independent variables, except the one that has been specified as the dependent variable (&lt;code&gt;mpg&lt;/code&gt; here)).&lt;/p&gt;
&lt;p&gt;The model that has been selected according to this criterion is the following:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;summary(model4)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## Call:
## lm(formula = mpg ~ wt + qsec + am, data = dat)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.4811 -1.5555 -0.7257  1.4110  4.6610 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(&amp;gt;|t|)    
## (Intercept)   9.6178     6.9596   1.382 0.177915    
## wt           -3.9165     0.7112  -5.507 6.95e-06 ***
## qsec          1.2259     0.2887   4.247 0.000216 ***
## amManual      2.9358     1.4109   2.081 0.046716 *  
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## Residual standard error: 2.459 on 28 degrees of freedom
## Multiple R-squared:  0.8497,	Adjusted R-squared:  0.8336 
## F-statistic: 52.75 on 3 and 28 DF,  p-value: 1.21e-11&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Be careful when using an automatic procedure because, even though it is the best model that is selected, it is based:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;on a single criterion (AIC in this case), but more importantly;&lt;/li&gt;
&lt;li&gt;it is based on some set of mathematical rules, which means that industry knowledge or human expertise is not taken into consideration.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I believe that this kind of automatic procedure for model’s selection is a good starting point, but I also believe that the final model should always be checked and tested against other models to make sure it makes sense in practice (apply common sense).&lt;/p&gt;
&lt;p&gt;Last but not least, do not forget to also verify the &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/#conditions-of-application-1&#34;&gt;conditions of application&lt;/a&gt; because the stepwise procedure does not guarantee that they are respected.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;visualizations-1&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Visualizations&lt;/h2&gt;
&lt;p&gt;There are many ways to visualize results of a linear regression. The easiest ones I am aware of are:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;&lt;code&gt;visreg()&lt;/code&gt; illustrates the relationships between the dependent and independent variables in different plots (one for each independent variable unless you specify which relationship you want to illustrate):&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(visreg)

visreg(model4)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/index_files/figure-html/unnamed-chunk-21-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;img src=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/index_files/figure-html/unnamed-chunk-21-2.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;img src=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/index_files/figure-html/unnamed-chunk-21-3.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;ol start=&#34;2&#34; style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;&lt;code&gt;ggcoefstats()&lt;/code&gt; illustrates the results in one single plot, with many statistical details:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(ggstatsplot)

ggcoefstats(model4)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/index_files/figure-html/unnamed-chunk-22-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;In this plot:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;when the solid line does not cross the vertical dashed line, the estimates is significantly different from 0 at the 5% significance level (i.e., &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value &amp;lt; 0.05)&lt;/li&gt;
&lt;li&gt;furthermore, a point to the right (left) of the vertical dashed line means that there is a positive (negative) relationship between the two variables&lt;/li&gt;
&lt;li&gt;the more extreme the point, the stronger the relationship&lt;/li&gt;
&lt;/ul&gt;
&lt;ol start=&#34;3&#34; style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;&lt;code&gt;plot_summs()&lt;/code&gt; which also illustrates the results but in a more concise way:&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(jtools)
library(ggstance)

plot_summs(model4,
  omit.coefs = NULL
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/index_files/figure-html/unnamed-chunk-23-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The advantage of this approach is that it is possible to compare coefficients of multiple models simultaneously (particularly interesting when the models are nested):&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;model4bis &amp;lt;- lm(mpg ~ wt + qsec + am + hp,
  data = dat
)

plot_summs(model4,
  model4bis,
  omit.coefs = NULL
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/index_files/figure-html/unnamed-chunk-24-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;to-go-further&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;To go further&lt;/h2&gt;
&lt;p&gt;Below some more advanced topics related to linear regression. Feel free to comment at the end of the article if you believe I missed an important one.&lt;/p&gt;
&lt;div id=&#34;print-models-parameters&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Print model’s parameters&lt;/h3&gt;
&lt;p&gt;Thanks to the &lt;code&gt;model_parameters()&lt;/code&gt; function from the &lt;code&gt;{parameters}&lt;/code&gt; package, you can print a summary of the model in a nicely formatted way to make the output more readable:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(parameters)

model_parameters(model4, summary = TRUE)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Parameter   | Coefficient |   SE |         95% CI | t(28) |      p
## ------------------------------------------------------------------
## (Intercept) |        9.62 | 6.96 | [-4.64, 23.87] |  1.38 | 0.178 
## wt          |       -3.92 | 0.71 | [-5.37, -2.46] | -5.51 | &amp;lt; .001
## qsec        |        1.23 | 0.29 | [ 0.63,  1.82] |  4.25 | &amp;lt; .001
## am [Manual] |        2.94 | 1.41 | [ 0.05,  5.83] |  2.08 | 0.047&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And if you are using &lt;a href=&#34;https://statsandr.com/blog/getting-started-in-r-markdown/&#34;&gt;R Markdown&lt;/a&gt;, you can use the &lt;code&gt;print_html()&lt;/code&gt; function to get a compact and yet comprehensive summary table in your HTML file:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(gt)

print_html(model_parameters(model4, summary = TRUE))&lt;/code&gt;&lt;/pre&gt;
&lt;div id=&#34;yywrdylzvd&#34; style=&#34;padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;&#34;&gt;
&lt;style&gt;#yywrdylzvd table {
  font-family: system-ui, &#39;Segoe UI&#39;, Roboto, Helvetica, Arial, sans-serif, &#39;Apple Color Emoji&#39;, &#39;Segoe UI Emoji&#39;, &#39;Segoe UI Symbol&#39;, &#39;Noto Color Emoji&#39;;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#yywrdylzvd thead, #yywrdylzvd tbody, #yywrdylzvd tfoot, #yywrdylzvd tr, #yywrdylzvd td, #yywrdylzvd th {
  border-style: none;
}

#yywrdylzvd p {
  margin: 0;
  padding: 0;
}

#yywrdylzvd .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 100%;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#yywrdylzvd .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#yywrdylzvd .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#yywrdylzvd .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#yywrdylzvd .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#yywrdylzvd .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#yywrdylzvd .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#yywrdylzvd .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#yywrdylzvd .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#yywrdylzvd .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#yywrdylzvd .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#yywrdylzvd .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#yywrdylzvd .gt_spanner_row {
  border-bottom-style: hidden;
}

#yywrdylzvd .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#yywrdylzvd .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#yywrdylzvd .gt_from_md &gt; :first-child {
  margin-top: 0;
}

#yywrdylzvd .gt_from_md &gt; :last-child {
  margin-bottom: 0;
}

#yywrdylzvd .gt_row {
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#yywrdylzvd .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#yywrdylzvd .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#yywrdylzvd .gt_row_group_first td {
  border-top-width: 2px;
}

#yywrdylzvd .gt_row_group_first th {
  border-top-width: 2px;
}

#yywrdylzvd .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#yywrdylzvd .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#yywrdylzvd .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#yywrdylzvd .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#yywrdylzvd .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#yywrdylzvd .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#yywrdylzvd .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#yywrdylzvd .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#yywrdylzvd .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#yywrdylzvd .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#yywrdylzvd .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#yywrdylzvd .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#yywrdylzvd .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#yywrdylzvd .gt_left {
  text-align: left;
}

#yywrdylzvd .gt_center {
  text-align: center;
}

#yywrdylzvd .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#yywrdylzvd .gt_font_normal {
  font-weight: normal;
}

#yywrdylzvd .gt_font_bold {
  font-weight: bold;
}

#yywrdylzvd .gt_font_italic {
  font-style: italic;
}

#yywrdylzvd .gt_super {
  font-size: 65%;
}

#yywrdylzvd .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#yywrdylzvd .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#yywrdylzvd .gt_indent_1 {
  text-indent: 5px;
}

#yywrdylzvd .gt_indent_2 {
  text-indent: 10px;
}

#yywrdylzvd .gt_indent_3 {
  text-indent: 15px;
}

#yywrdylzvd .gt_indent_4 {
  text-indent: 20px;
}

#yywrdylzvd .gt_indent_5 {
  text-indent: 25px;
}

#yywrdylzvd .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#yywrdylzvd div.Reactable &gt; div.rt-table &gt; div.rt-thead &gt; div.rt-tr.rt-tr-group-header &gt; div.rt-th-group:after {
  height: 0px !important;
}
&lt;/style&gt;
&lt;table class=&#34;gt_table&#34; data-quarto-disable-processing=&#34;false&#34; data-quarto-bootstrap=&#34;false&#34;&gt;
  &lt;thead&gt;
    &lt;tr class=&#34;gt_col_headings&#34;&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_left&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;Parameter&#34;&gt;Parameter&lt;/th&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_center&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;Coefficient&#34;&gt;Coefficient&lt;/th&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_center&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;SE&#34;&gt;SE&lt;/th&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_center&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;a95%-CI&#34;&gt;95% CI&lt;/th&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_center&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;t(28)&#34;&gt;t(28)&lt;/th&gt;
      &lt;th class=&#34;gt_col_heading gt_columns_bottom_border gt_center&#34; rowspan=&#34;1&#34; colspan=&#34;1&#34; scope=&#34;col&#34; id=&#34;p&#34;&gt;p&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody class=&#34;gt_table_body&#34;&gt;
    &lt;tr&gt;&lt;td headers=&#34;Parameter&#34; class=&#34;gt_row gt_left&#34; style=&#34;border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3;&#34;&gt;(Intercept)&lt;/td&gt;
&lt;td headers=&#34;Coefficient&#34; class=&#34;gt_row gt_center&#34;&gt;9.62&lt;/td&gt;
&lt;td headers=&#34;SE&#34; class=&#34;gt_row gt_center&#34;&gt;6.96&lt;/td&gt;
&lt;td headers=&#34;95% CI&#34; class=&#34;gt_row gt_center&#34;&gt;(-4.64, 23.87)&lt;/td&gt;
&lt;td headers=&#34;t(28)&#34; class=&#34;gt_row gt_center&#34;&gt;1.38&lt;/td&gt;
&lt;td headers=&#34;p&#34; class=&#34;gt_row gt_center&#34;&gt;0.178 &lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Parameter&#34; class=&#34;gt_row gt_left&#34; style=&#34;border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3;&#34;&gt;wt&lt;/td&gt;
&lt;td headers=&#34;Coefficient&#34; class=&#34;gt_row gt_center&#34;&gt;-3.92&lt;/td&gt;
&lt;td headers=&#34;SE&#34; class=&#34;gt_row gt_center&#34;&gt;0.71&lt;/td&gt;
&lt;td headers=&#34;95% CI&#34; class=&#34;gt_row gt_center&#34;&gt;(-5.37, -2.46)&lt;/td&gt;
&lt;td headers=&#34;t(28)&#34; class=&#34;gt_row gt_center&#34;&gt;-5.51&lt;/td&gt;
&lt;td headers=&#34;p&#34; class=&#34;gt_row gt_center&#34;&gt;&amp;lt; .001&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Parameter&#34; class=&#34;gt_row gt_left&#34; style=&#34;border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3;&#34;&gt;qsec&lt;/td&gt;
&lt;td headers=&#34;Coefficient&#34; class=&#34;gt_row gt_center&#34;&gt;1.23&lt;/td&gt;
&lt;td headers=&#34;SE&#34; class=&#34;gt_row gt_center&#34;&gt;0.29&lt;/td&gt;
&lt;td headers=&#34;95% CI&#34; class=&#34;gt_row gt_center&#34;&gt;(0.63, 1.82)&lt;/td&gt;
&lt;td headers=&#34;t(28)&#34; class=&#34;gt_row gt_center&#34;&gt;4.25&lt;/td&gt;
&lt;td headers=&#34;p&#34; class=&#34;gt_row gt_center&#34;&gt;&amp;lt; .001&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td headers=&#34;Parameter&#34; class=&#34;gt_row gt_left&#34; style=&#34;border-right-width: 1px; border-right-style: solid; border-right-color: #d3d3d3;&#34;&gt;am (Manual)&lt;/td&gt;
&lt;td headers=&#34;Coefficient&#34; class=&#34;gt_row gt_center&#34;&gt;2.94&lt;/td&gt;
&lt;td headers=&#34;SE&#34; class=&#34;gt_row gt_center&#34;&gt;1.41&lt;/td&gt;
&lt;td headers=&#34;95% CI&#34; class=&#34;gt_row gt_center&#34;&gt;(0.05, 5.83)&lt;/td&gt;
&lt;td headers=&#34;t(28)&#34; class=&#34;gt_row gt_center&#34;&gt;2.08&lt;/td&gt;
&lt;td headers=&#34;p&#34; class=&#34;gt_row gt_center&#34;&gt;0.047 &lt;/td&gt;&lt;/tr&gt;
  &lt;/tbody&gt;
  
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;automatic-reporting&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Automatic reporting&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;report()&lt;/code&gt; function from the package of the same name allows to automatically produces reports of models according to best practices guidelines:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(report)

report(model4)[1]&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;We fitted a linear model (estimated using OLS) to predict mpg with wt, qsec and am (formula: mpg ~ wt + qsec + am). The model explains a statistically significant and substantial proportion of variance (R2 = 0.85, F(3, 28) = 52.75, p &amp;lt; .001, adj. R2 = 0.83). The model&amp;#39;s intercept, corresponding to wt = 0, qsec = 0 and am = Automatic, is at 9.62 (95% CI [-4.64, 23.87], t(28) = 1.38, p = 0.178). Within this model:\n\n  - The effect of wt is statistically significant and negative (beta = -3.92, 95% CI [-5.37, -2.46], t(28) = -5.51, p &amp;lt; .001; Std. beta = -0.64, 95% CI [-0.87, -0.40])\n  - The effect of qsec is statistically significant and positive (beta = 1.23, 95% CI [0.63, 1.82], t(28) = 4.25, p &amp;lt; .001; Std. beta = 0.36, 95% CI [0.19, 0.54])\n  - The effect of am [Manual] is statistically significant and positive (beta = 2.94, 95% CI [0.05, 5.83], t(28) = 2.08, p = 0.047; Std. beta = 0.49, 95% CI [7.59e-03, 0.97])\n\nStandardized parameters were obtained by fitting the model on a standardized version of the dataset. 95% Confidence Intervals (CIs) and p-values were computed using a Wald t-distribution approximation.&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that the function also works for dataframes, &lt;a href=&#34;https://statsandr.com/blog/what-statistical-test-should-i-do/&#34;&gt;statistical tests&lt;/a&gt; and other models.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;predictions&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Predictions&lt;/h3&gt;
&lt;p&gt;Linear regression is also very often used for &lt;strong&gt;predictive purposes&lt;/strong&gt;. Confidence and prediction intervals for &lt;strong&gt;new data&lt;/strong&gt; can be computed with the &lt;code&gt;predict()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;Suppose we want to predict the miles/gallon for a car with a manual transmission, weighting 3000 lbs and which drives a quarter of a mile (&lt;code&gt;qsec&lt;/code&gt;) in 18 seconds:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# confidence interval for new data
predict(model4,
  new = data.frame(wt = 3, qsec = 18, am = &amp;quot;Manual&amp;quot;),
  interval = &amp;quot;confidence&amp;quot;,
  level = .95
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##        fit      lwr    upr
## 1 22.87005 21.09811 24.642&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# prediction interval for new data
predict(model4,
  new = data.frame(wt = 3, qsec = 18, am = &amp;quot;Manual&amp;quot;),
  interval = &amp;quot;prediction&amp;quot;,
  level = .95
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##        fit      lwr      upr
## 1 22.87005 17.53074 28.20937&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Based on our model, it is expected that this car will drive 22.87 miles with a gallon.&lt;/p&gt;
&lt;p&gt;The difference between the confidence and prediction interval is that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a &lt;strong&gt;confidence&lt;/strong&gt; interval gives the predicted value for the &lt;strong&gt;mean&lt;/strong&gt; of &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt; for a new observation, whereas&lt;/li&gt;
&lt;li&gt;a &lt;strong&gt;prediction&lt;/strong&gt; interval gives the predicted value for an &lt;strong&gt;individual&lt;/strong&gt; &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt; for a new observation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The prediction interval is wider than the confidence interval to account for the &lt;strong&gt;additional uncertainty due to predicting an individual response&lt;/strong&gt;, and not the mean, for a given value of &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;linear-hypothesis-tests&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Linear hypothesis tests&lt;/h3&gt;
&lt;p&gt;Linear hypothesis tests make it possible to generalize the F-test mentioned in this &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/#p-value-associated-to-the-model&#34;&gt;section&lt;/a&gt;, while offering the possibility to perform either tests of comparison of coefficients, or tests of equality of linear combinations of coefficients.&lt;/p&gt;
&lt;p&gt;For example, to test the linear constraint:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(H_0: \beta_1 = \beta_2 = 0\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(H_1:\)&lt;/span&gt; not &lt;span class=&#34;math inline&#34;&gt;\(H_0\)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;we use the &lt;code&gt;linearHypothesis()&lt;/code&gt; function of the &lt;code&gt;{car}&lt;/code&gt; package as follows:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(car)
linearHypothesis(model4, c(&amp;quot;wt = 0&amp;quot;, &amp;quot;qsec = 0&amp;quot;))&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## Linear hypothesis test:
## wt = 0
## qsec = 0
## 
## Model 1: restricted model
## Model 2: mpg ~ wt + qsec + am
## 
##   Res.Df    RSS Df Sum of Sq      F   Pr(&amp;gt;F)    
## 1     30 720.90                                 
## 2     28 169.29  2    551.61 45.618 1.55e-09 ***
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We reject the null hypothesis and we conclude that at least one of &lt;span class=&#34;math inline&#34;&gt;\(\beta_1\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(\beta_2\)&lt;/span&gt; is different from 0 (&lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value = 1.55e-09).&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;overall-effect-of-categorical-variables&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Overall effect of categorical variables&lt;/h3&gt;
&lt;p&gt;When the independent variables are categorical with &lt;span class=&#34;math inline&#34;&gt;\(k\)&lt;/span&gt; categories, the regression table provides &lt;span class=&#34;math inline&#34;&gt;\(k-1\)&lt;/span&gt; &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-values:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;model5 &amp;lt;- lm(mpg ~ vs + am + as.factor(cyl),
  data = dat
)

summary(model5)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## Call:
## lm(formula = mpg ~ vs + am + as.factor(cyl), data = dat)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.2821 -1.4402  0.0391  1.8845  6.2179 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(&amp;gt;|t|)    
## (Intercept)       22.809      2.928   7.789 2.24e-08 ***
## vsStraight         1.708      2.235   0.764  0.45135    
## amManual           3.165      1.528   2.071  0.04805 *  
## as.factor(cyl)6   -5.399      1.837  -2.938  0.00668 ** 
## as.factor(cyl)8   -8.161      2.892  -2.822  0.00884 ** 
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## Residual standard error: 3.097 on 27 degrees of freedom
## Multiple R-squared:  0.7701,	Adjusted R-squared:  0.736 
## F-statistic: 22.61 on 4 and 27 DF,  p-value: 2.741e-08&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The variables &lt;code&gt;vs&lt;/code&gt; and &lt;code&gt;am&lt;/code&gt; have 2 levels so one is displayed in the regression output. The variable &lt;code&gt;cyl&lt;/code&gt; has 3 levels (4, 6 and 8) so 2 of them are displayed. The overall effect of &lt;code&gt;vs&lt;/code&gt; and &lt;code&gt;am&lt;/code&gt; are reported in the &lt;code&gt;Pr(&amp;gt;|t|)&lt;/code&gt; column, but not the &lt;strong&gt;overall&lt;/strong&gt; effect of &lt;code&gt;cyl&lt;/code&gt; because there are more than 2 levels for this variable.&lt;/p&gt;
&lt;p&gt;To get the &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value of the overall effect of a categorical variable, we need to get an analysis of variance table via the &lt;code&gt;Anova()&lt;/code&gt; function from the &lt;code&gt;{car}&lt;/code&gt; package:&lt;a href=&#34;#fn18&#34; class=&#34;footnote-ref&#34; id=&#34;fnref18&#34;&gt;&lt;sup&gt;18&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(car)
Anova(model5)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Anova Table (Type II tests)
## 
## Response: mpg
##                 Sum Sq Df F value  Pr(&amp;gt;F)  
## vs               5.601  1  0.5841 0.45135  
## am              41.122  1  4.2886 0.04805 *
## as.factor(cyl)  94.591  2  4.9324 0.01493 *
## Residuals      258.895 27                  
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;From this analysis of variance table, we conclude that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;vs&lt;/code&gt; is not significantly associated with &lt;code&gt;mpg&lt;/code&gt; (&lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value = 0.451)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;am&lt;/code&gt; and &lt;code&gt;cyl&lt;/code&gt; are significantly associated with &lt;code&gt;mpg&lt;/code&gt; (&lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-values &amp;lt; 0.05)&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;interaction&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Interaction&lt;/h3&gt;
&lt;p&gt;So far we have covered multiple linear regression without any interaction.&lt;/p&gt;
&lt;p&gt;There is an &lt;strong&gt;interaction&lt;/strong&gt; effect between factors A and B &lt;strong&gt;if the effect of factor A on the response depends on the level taken by factor B&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;In R, interaction can be added as follows:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;model6 &amp;lt;- lm(mpg ~ wt + am + wt:am,
  data = dat
)

# Or in a shorter way:
model6 &amp;lt;- lm(mpg ~ wt * am,
  data = dat
)

summary(model6)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## Call:
## lm(formula = mpg ~ wt * am, data = dat)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.6004 -1.5446 -0.5325  0.9012  6.0909 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(&amp;gt;|t|)    
## (Intercept)  31.4161     3.0201  10.402 4.00e-11 ***
## wt           -3.7859     0.7856  -4.819 4.55e-05 ***
## amManual     14.8784     4.2640   3.489  0.00162 ** 
## wt:amManual  -5.2984     1.4447  -3.667  0.00102 ** 
## ---
## Signif. codes:  0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1
## 
## Residual standard error: 2.591 on 28 degrees of freedom
## Multiple R-squared:  0.833,	Adjusted R-squared:  0.8151 
## F-statistic: 46.57 on 3 and 28 DF,  p-value: 5.209e-11&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;From the output we conclude that there is an interaction between the weight and the transmission (&lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt;-value = 0.00102). This means that the effect of the weight on the distance traveled with a gallon &lt;strong&gt;depends on the transmission type&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The easiest way to handle interaction is to visualize the relationship for each level of the categorical variable:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;visreg(model6, &amp;quot;wt&amp;quot;, by = &amp;quot;am&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/index_files/figure-html/unnamed-chunk-33-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;We see that the relationship between weight and miles/gallon is stronger (the slope is steeper) for cars with a manual transmission compared to cars with an automatic transmission.&lt;/p&gt;
&lt;p&gt;This is a good example to illustrate the point that when studying a relationship between two variables, say &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt;, if one also has data for other variables which are potentially associated with both &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(Y\)&lt;/span&gt;, it is important to include them in the regression and to analyze the relationship &lt;strong&gt;conditionally on these variables&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Omitting some variables that should be included in the model may lead to erroneous and misleading conclusions, up to the point that the relationship is completely reversed (a phenomenon referred as &lt;a href=&#34;https://en.wikipedia.org/wiki/Simpson%27s_paradox&#34; target=&#34;_blank&#34;&gt;Simpson’s paradox&lt;/a&gt;).&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;summary&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Summary&lt;/h1&gt;
&lt;p&gt;In this article, we started with a reminder of &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/#simple-linear-regression-reminder&#34;&gt;simple linear regression&lt;/a&gt; and in particular its &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/#principle&#34;&gt;principle&lt;/a&gt; and how to &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/#interpretations-of-coefficients-widehatbeta&#34;&gt;interpret the results&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This laid the foundations for a better understanding of &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/#multiple-linear-regression&#34;&gt;multiple linear regression&lt;/a&gt;. After explaining its &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/#principle-1&#34;&gt;principle&lt;/a&gt;, we showed how to &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/#interpretations-of-coefficients-widehatbeta-1&#34;&gt;interpret the output&lt;/a&gt; and how to choose a &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/#how-to-choose-a-good-linear-model&#34;&gt;good linear model&lt;/a&gt;. We then mentioned a couple of &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/#visualizations-1&#34;&gt;visualizations&lt;/a&gt; and finished the article with some more &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/#to-go-further&#34;&gt;advanced topics&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Thanks for reading.&lt;/p&gt;
&lt;p&gt;I hope this article helped you to understand better linear regression and gave you the confidence to do your own linear regressions in R. If you need to model a binary variable instead of a quantitative continuous variable, see how to perform a &lt;a href=&#34;https://statsandr.com/blog/binary-logistic-regression-in-r/&#34;&gt;binary logistic regression in R&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;As always, if you have a question or a suggestion related to the topic covered in this article, please add it as a comment so other readers can benefit from the discussion.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;references&#34; class=&#34;section level1 unnumbered&#34;&gt;
&lt;h1&gt;References&lt;/h1&gt;
&lt;div id=&#34;refs&#34; class=&#34;references csl-bib-body hanging-indent&#34;&gt;
&lt;div id=&#34;ref-austin2015number&#34; class=&#34;csl-entry&#34;&gt;
Austin, Peter C, and Ewout W Steyerberg. 2015. &lt;span&gt;“The Number of Subjects Per Variable Required in Linear Regression Analyses.”&lt;/span&gt; &lt;em&gt;Journal of Clinical Epidemiology&lt;/em&gt; 68 (6): 627–36.
&lt;/div&gt;
&lt;div id=&#34;ref-ernst2017regression&#34; class=&#34;csl-entry&#34;&gt;
Ernst, Anja F, and Casper J Albers. 2017. &lt;span&gt;“Regression Assumptions in Clinical Psychology Research Practice?a Systematic Review of Common Misconceptions.”&lt;/span&gt; &lt;em&gt;PeerJ&lt;/em&gt; 5: e3323.
&lt;/div&gt;
&lt;div id=&#34;ref-james2013introduction&#34; class=&#34;csl-entry&#34;&gt;
James, Gareth, Daniela Witten, Trevor Hastie, and Robert Tibshirani. 2013. &lt;em&gt;An Introduction to Statistical Learning&lt;/em&gt;. Vol. 112. Springer.
&lt;/div&gt;
&lt;div id=&#34;ref-lumley2002importance&#34; class=&#34;csl-entry&#34;&gt;
Lumley, Thomas, Paula Diehr, Scott Emerson, and Lu Chen. 2002. &lt;span&gt;“The Importance of the Normality Assumption in Large Public Health Data Sets.”&lt;/span&gt; &lt;em&gt;Annual Review of Public Health&lt;/em&gt; 23 (1): 151–69.
&lt;/div&gt;
&lt;div id=&#34;ref-schmidt2018linear&#34; class=&#34;csl-entry&#34;&gt;
Schmidt, Amand F, and Chris Finan. 2018. &lt;span&gt;“Linear Regression and the Normality Assumption.”&lt;/span&gt; &lt;em&gt;Journal of Clinical Epidemiology&lt;/em&gt; 98: 146–51.
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&#34;footnotes footnotes-end-of-document&#34;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&#34;fn1&#34;&gt;&lt;p&gt;Some people see regression analysis as a part of inferential statistics. It is true, as a sample is taken to evaluate the link between two or more variables in a population of interest. I tend to distinguish regression from inferential statistics for the simple reasons that (i) regressions are often used to a broader extent (for predictive analyses, among others), and because (ii) the main goal of linear regression (see this &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/#simple-linear-regression-reminder&#34;&gt;section&lt;/a&gt;) differs from the objectives of confidence intervals and hypothesis testing well known in the field of inferential statistics.&lt;a href=&#34;#fnref1&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn2&#34;&gt;&lt;p&gt;Formally, ANOVA can also be used to compare 2 groups, but in practice we tend to use it for 3 or more groups, leaving the t-test for 2 groups.&lt;a href=&#34;#fnref2&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn3&#34;&gt;&lt;p&gt;More information about the dataset can be found by executing &lt;code&gt;?mtcars&lt;/code&gt;.&lt;a href=&#34;#fnref3&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn4&#34;&gt;&lt;p&gt;Note that it best to avoid interpreting the intercept when &lt;span class=&#34;math inline&#34;&gt;\(X\)&lt;/span&gt; cannot be equal to 0 or when it makes no sense in practice.&lt;a href=&#34;#fnref4&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn5&#34;&gt;&lt;p&gt;&lt;span class=&#34;math inline&#34;&gt;\(n\)&lt;/span&gt; is the number of observations.&lt;a href=&#34;#fnref5&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn6&#34;&gt;&lt;p&gt;Other values than 0 are accepted as well. In that case, the test statistic becomes &lt;span class=&#34;math inline&#34;&gt;\(T_{n - 2} = \frac{\widehat\beta - a}{se(\widehat\beta_1)}\)&lt;/span&gt; where &lt;span class=&#34;math inline&#34;&gt;\(a\)&lt;/span&gt; is the hypothesized slope.&lt;a href=&#34;#fnref6&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn7&#34;&gt;&lt;p&gt;Note that linearity can be checked with a scatterplot of the two variables, or via a scatterplot of the residuals and the fitted values. See more about this in this &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/#conditions-of-application-1&#34;&gt;section&lt;/a&gt;.&lt;a href=&#34;#fnref7&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn8&#34;&gt;&lt;p&gt;An observation is considered as an outlier based on the Cook’s distance if its value is &amp;gt; 1.&lt;a href=&#34;#fnref8&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn9&#34;&gt;&lt;p&gt;An observation has a high leverage value (and thus needs to be investigated) if it is greater than &lt;span class=&#34;math inline&#34;&gt;\(2p/n\)&lt;/span&gt;, where &lt;span class=&#34;math inline&#34;&gt;\(p\)&lt;/span&gt; is the number of parameters in the model (intercept included) and &lt;span class=&#34;math inline&#34;&gt;\(n\)&lt;/span&gt; is the number of observations.&lt;a href=&#34;#fnref9&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn10&#34;&gt;&lt;p&gt;You can always change the reference level with the &lt;code&gt;relevel()&lt;/code&gt; function. See more &lt;a href=&#34;https://statsandr.com/blog/data-manipulation-in-r/&#34;&gt;data manipulation techniques&lt;/a&gt;.&lt;a href=&#34;#fnref10&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn11&#34;&gt;&lt;p&gt;Note that linearity can also be tested with a scatterplot of the residuals and the fitted values.&lt;a href=&#34;#fnref11&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn12&#34;&gt;&lt;p&gt;After installing the &lt;code&gt;{performance}&lt;/code&gt; package, you will also need to install the &lt;code&gt;{see}&lt;/code&gt; package manually. See &lt;a href=&#34;https://statsandr.com/blog/an-efficient-way-to-install-and-load-r-packages/&#34;&gt;how to install a R package&lt;/a&gt; if you need more help.&lt;a href=&#34;#fnref12&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn13&#34;&gt;&lt;p&gt;I use the threshold of 10 because, as shown by &lt;span class=&#34;citation&#34;&gt;James et al. (&lt;a href=&#34;#ref-james2013introduction&#34;&gt;2013&lt;/a&gt;)&lt;/span&gt;, a value between 5 and 10 indicates a moderate correlation, while VIF values greater than 10 indicate a high and &lt;em&gt;non-tolerable&lt;/em&gt; correlation.&lt;a href=&#34;#fnref13&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn14&#34;&gt;&lt;p&gt;&lt;span class=&#34;citation&#34;&gt;Austin and Steyerberg (&lt;a href=&#34;#ref-austin2015number&#34;&gt;2015&lt;/a&gt;)&lt;/span&gt; showed that two subjects per variable tends to permit accurate estimation of regression coefficients in a linear regression model estimated using ordinary least squares. Moreover, the general rule of thumb says that there should be at least 10 observations per variable &lt;span class=&#34;citation&#34;&gt;(&lt;a href=&#34;#ref-schmidt2018linear&#34;&gt;Schmidt and Finan 2018&lt;/a&gt;)&lt;/span&gt;. Our dataset contains 32 observations, above the minimum of 10 subjects per variable.&lt;a href=&#34;#fnref14&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn15&#34;&gt;&lt;p&gt;If you apply a logarithmic transformation, see two guides on how to interpret the results: in &lt;a href=&#34;https://statsandr.com/blog/multiple-linear-regression-made-simple/images/Interpret-Regression-Coefficient-Estimates-in-linear-regression.png&#34;&gt;English&lt;/a&gt; and in &lt;a href=&#34;https://www.parisschoolofeconomics.eu/docs/yin-remi/interpretation-des-coefficients.pdf&#34; target=&#34;_blank&#34;&gt;French&lt;/a&gt;.&lt;a href=&#34;#fnref15&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn16&#34;&gt;&lt;p&gt;Note that a high &lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt; does not guarantee that you selected the best variables or that your model is good. It simply tells that the model fits the data quite well. It is advised to apply common sense when comparing models and not only refer to &lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt; (in particular when &lt;span class=&#34;math inline&#34;&gt;\(R^2\)&lt;/span&gt; are close).&lt;a href=&#34;#fnref16&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn17&#34;&gt;&lt;p&gt;There are two main methods; backward and forward. The backward method consists in starting from the model containing all the explanatory variables likely to be relevant, then recursively removing the variable which reduces the information criterion of the model, until no reduction is possible. The forward method is the reverse of the backward method in the sense that we start from a one-variable model with the lowest information criterion and at each step, an explanatory variable is added. By default, the &lt;code&gt;step()&lt;/code&gt; function in R combines the backward and forward methods.&lt;a href=&#34;#fnref17&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn18&#34;&gt;&lt;p&gt;To not be confused with the &lt;code&gt;anova()&lt;/code&gt; function because it provides results that depend on the order in which the variables appear in the model.&lt;a href=&#34;#fnref18&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</description>
    </item>
    
  </channel>
</rss>