<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/generalized-linear-functions-beginners/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/09/16/generalized-linear-functions-beginners/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/05/copulas_part2-7.png" target="_blank"><img sizes="(max-width: 150px) 100vw, 150px" src="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/05/copulas_part2-7-150x150.png?resize=150%2C150" alt="" class="wp-image-5912" data-recalc-dims="1"></a>On this set of exercises, we are going to use the <code>lm</code> and <code>glm</code> functions to perform several generalized linear models on one dataset.</p>
<p>Since this is a basic set of exercises we will take a closer look at the arguments of these functions and how to take advantage of the output of each function so we can find a model that fits our data.</p>
<p>Before starting this set of exercises, I strongly suggest you look at the R Documentation of <code>lm</code> and <code>glm</code>.</p>
<p>Note: This set of exercises assume that you have a basic understanding of generalized linear models.</p>
<p>Answers to the exercises are available <a rel="nofollow" href="http://r-exercises.com/2017/09/16/generalized-linear-models-solutionbeginners/" target="_blank">here.</a></p>
<p>If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.</p>
<p>The dataset we will be using contains information from passengers of the Titanic including if they survived or not.</p>
<p>To obtain the data run these lines of code.</p>
<p><code><br>
if (!'titanic' %in% installed.packages()) install.packages('titanic')<br>
library(titanic)<br>
DATA &lt;- titanic_train[,-c(1,4,9,11)]<br>
</code></p>
<p><strong>Exercise 1</strong></p>
<p><strong>Linear regression</strong></p>
<p>1. Use <code>DATA</code> to create a linear model using the function <code>lm</code> with the variables Age and Fare as independent variables and Survived as the independent one. Save the regression in an object called <code>lm_reg</code></p>
<p>2. Use the function <code>glm</code> to perform the same task and save the regression in an object called <code>glm_reg</code></p>
<p><strong>Exercise 2</strong></p>
<p>If you print any of the previous objects you will realize that there’s not much information about the performance of the models, fortunately <code>summary</code> is a great function to find out more about any statistical model you preform to a dataset. Depending on the model <code>summary</code> will produce different outputs.</p>
<ul>
<li>Apply <code>summary</code> to <code>lm_reg</code> and to <code>glm_reg</code>. You will find a slight difference between both of the outputs, that is because <code>glm</code> is more flexible than <code>lm</code>.</li>
</ul>
<p><strong>Exercise 3</strong></p>
<p>So far we have been assuming (incorrectly) that the dependent variable (<code>Survived</code>) follows a normal distribution and that’s why we have been performing a linear regression. Obviously <code>Survived</code> follows a binomial distribution, there are only two options either the passenger survived (1) or the passenger wasn’t that lucky and he died (0). Since the data has a binomial distribution we should perform a logistic regression, to do this use the function <code>glm</code> to perform a logistic regression using <code>Age</code> and <code>Fare</code> as independent variables and save it in an object called <code>bin_model</code>. Hint: Define the value of the argument <code>family</code> properly.</p>
<p><strong>Exercise 4</strong></p>
<p>Inside the family attribute you can always specify a particular link, in case you don’t a default link will be associated depending on the family you chose.</p>
<p>1. To find out the default link associated to a certain family, you can write the family name followed by a parenthesis (Ex. <code>gaussian()</code>. Find the default link associated to the binomial family.<br>
2. Create a probit model with the same variables used in <code>bin_model</code> and save it in an object called <code>bin_probit_model</code>.</p>
<p><strong>Exercise 5</strong></p>
<p>Findind the right model requires to compare different models and choose the best, although there are many performance measures, for now we will use the <code>AIC</code> as our measure (smaller AIC are better). This means that <code>bin_model</code> is better than <code>bin_probit_model</code>, so let’s keep working with <code>bin_model</code>.</p>
<p>Until now intercept variable has been part of the models. Create a logistic regression with the same variables but with no intercept.</p>
<p><strong>Exercise 6</strong></p>
<p><strong>Impute data</strong>. If you run the <code>summary</code> function to any of the previous models you will find out that 177 observations have been deleted due to missingness. This happens because the <code>glm</code> function has as default argument <code>na.acton =&quot;na.omit&quot;</code>. This make easier to run a model with messier data, but that is not always great. You want to have full control an understanding of what does the function is doing.</p>
<p>1. There are some missing values in <code>age</code>, replace this values with the median.<br>
2. Update the <code>glm_model</code> with the updated data, specify <code>na.action='na.fail'</code> This will assure us that the dataset has no missing values, otherwise it will show an error.</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/09/16/generalized-linear-functions-beginners/"></aside>
<div><strong>Learn more</strong> about evaluating different statistical models in the online courses <a rel="nofollow" href="http://www.r-exercises.com/product/linear-regression-in-r-for-data-scientists/" target="_blank">Linear regression in R for Data Scientists</a> and <a rel="nofollow" href="http://www.r-exercises.com/product/structural-equation-modeling-sem-with-lavaan/" target="_blank">Structural equation modeling (SEM) with lavaan</a>. These courses cover different statistical models that can help you choose the right design for your solution.</div>
</div>
<p><strong>Exercise 7</strong></p>
<p><strong>Add polynomial independent variables</strong>. Some variables have a quadratic interaction between them and the dependent variable, this can be solved by specifying in the formula of the model a quadratic interaction.</p>
<p>Add a quadratic interaction for the variable <code>Fare</code> into the current model, specified in <code>glm_model</code></p>
<p><strong>Exercise 8</strong></p>
<p><strong>Add categorical variables. </strong> Add <code>Sex</code> as an independent variable  into the current model specified in <code>glm_model</code>. Note that Sex is not a numeric variable.</p>
<p><strong>Exercise 9</strong></p>
<p>Now that we have found a good model that fits our data, so it’s time to use the <code>predict</code> function to find how good the model predicts in our own data. Use the function <code>predict</code> to find the prediction of the model in <code>DATA</code> and save it in <code>Pred.default</code></p>
<p><strong>Exercise 10</strong></p>
<p><code>Pred.default</code> shows the predicted values under the link transformation, in this case logit. This is not easily interpretable, to fix this problem we can specify the <code>type</code> of prediction we want.</p>
<ol>Obtain the predictions as probability values.</ol>
<ol>Exta: What’s the percentage accuracy of this model if we assigned as died (0) if the predicted probability is less than 0.5 and survived (1) otherwise?</ol>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/04/15/forecasting-linear-models-exercises-part-2/" title="Forecasting: Linear Trend and ARIMA Models Exercises (Part-2)" target="_blank">Forecasting: Linear Trend and ARIMA Models Exercises (Part-2) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/08/03/parallel-computing-exercises-snow-and-rmpi-part-3/" title="Parallel Computing Exercises: Snow and Rmpi (Part-3)" target="_blank">Parallel Computing Exercises: Snow and Rmpi (Part-3) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/06/10/density-based-clustering-exercises/" title="Density-Based Clustering Exercises" target="_blank">Density-Based Clustering Exercises </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.r-exercises.com/2017/09/16/generalized-linear-functions-beginners/"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/in_4sYigfIM" height="1"></a></div>