<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/commercial-data-analytics-an-economic-view-on-the-data-science-methods/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascienceplus.com/commercial-data-analytics-an-economic-view-on-data-science-methods/"> R Programming – DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<ul></ul>
<p><strong>
<p>Categories</p>
<p></p></strong>
<ol>
<li><a rel="nofollow" href="https://datascienceplus.com/category/regression-models/" title="Permanent Link toRegression Models" target="_blank">Regression Models</a></li>
</ol>
<p><strong>
<p>Tags</p>
<p></p></strong>
<ol>
<li><a rel="nofollow" href="https://datascienceplus.com/tag/data-visualisation/" title="Permanent Link toData Visualisation" target="_blank">Data Visualisation</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/tag/linear-regression/" title="Permanent Link toLinear Regression" target="_blank">Linear Regression</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/tag/logistic-regression/" title="Permanent Link toLogistic Regression" target="_blank">Logistic Regression</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/tag/rstats/" title="Permanent Link toR Programming" target="_blank">R Programming</a></li>
</ol>
<p>The most demanding skill in data science is statistical regression analysis, followed by clustering methods, and the third most required skill is visualization. So if the data scientist learns these analytical skills and focuses on how to use them it will be of great value to both the data scientist, the companies/ organizations as well as the customers.</p>
<h2>Regression analysis</h2>
<p>Let’s start with the method that is most demanded in the data science: Regression analysis. Instead of choosing random regression models to apply let’s use some of the regression models that are most common for analytics in 2018 according to <a rel="nofollow" href="https://towardsdatascience.com/5-types-of-regression-and-their-properties-c5e1fa12d55e" target="_blank">this article</a> from Towards Data Science. Here we will look at three kinds of regression models: multiple regression, logistic regression, and polynomial regression. </p>
<h3>Multiple regression</h3>
<p>The multiple regression models consist of an explained variable (y) followed by a number of explanatory variables (the x’s). Multiple regression uses ordinarily least squares (ols) to estimate the coefficients of the model, by minimizing the square distance from the observation to the regression line. </p>
<h3>Logistic regression</h3>
<p>Logistic regression is used when you have a binary variable (y) to explain. The logistic regression model uses the cumulative distribution function to estimate the logistic function of the model.</p>
<h3>Polynomial regression</h3>
<p>A polynomial regression function is any multiple regression functions where some of the explanatory variables (x’s) are a potency function of the original variable (eg. age2= age^2). </p>
<p>The below coding is the loading of a dataset followed by different types of regression analysis:</p>
<pre> # Loading of dataset for regression analysis in R
library(skimr)
library(stargazer)
rdata&lt;-read.csv(&quot;http://rstatistics.net/wp-content/uploads/2015/09/adult.csv&quot;)
skim(rdata)
# Linear regression
lMod &lt;- lm(CAPITALGAIN ~ RELATIONSHIP + AGE + ABOVE50K + OCCUPATION + EDUCATIONNUM, 
                data=rdata)

# Logistic regression
logitMod &lt;- glm(ABOVE50K ~ RELATIONSHIP + AGE + CAPITALGAIN + OCCUPATION + EDUCATIONNUM, 
                data=rdata, family=binomial(link=&quot;logit&quot;))

#Polynomial Regression
rdata$AGE2=((rdata$AGE)^2)
lpMod = lm(CAPITALGAIN ~ RELATIONSHIP + AGE + AGE2 + ABOVE50K + OCCUPATION + EDUCATIONNUM, 
             data=rdata)

# Display regression models in a table
stargazer(lMod,logitMod,lpMod,omit = &quot;OCCUPATION&quot;, type=&quot;text&quot;,out=&quot;/models.doc&quot;)
<em>=============================================================================================
Dependent variable: -----------------------------------------------------------------
CAPITALGAIN ABOVE50K CAPITALGAIN OLS logistic OLS - Polynomial (1) (2) (3) ---------------------------------------------------------------------------------------------
RELATIONSHIP Not-in-family 258.510** -2.219*** 228.366** (110.921) (0.049) (111.339) RELATIONSHIP Other-relative 355.678 -2.541*** 283.023 (246.046) (0.191) (247.147) RELATIONSHIP Own-child 383.287*** -3.542*** 241.854 (144.519) (0.137) (151.649) RELATIONSHIP Unmarried 184.779 -2.529*** 207.143 (147.388) (0.083) (147.549) RELATIONSHIP Wife -226.536 0.286*** -214.927 (196.262) (0.066) (196.273) AGE 18.750*** 0.023*** -34.725* (3.370) (0.001) (17.720) AGE2 0.607*** (0.197) ABOVE50K 3,526.927*** 3,559.029*** (112.903) (113.370) CAPITALGAIN 0.0003*** (0.00001) EDUCATIONNUM 118.749*** 0.288*** 124.206*** (19.195) (0.009) (19.274) Constant -1,814.407*** -5.489*** -898.784** (306.870) (0.161) (427.640) ---------------------------------------------------------------------------------------------
Observations 32,561 32,561 32,561 R2 0.055 0.055 Adjusted R2 0.054 0.055 Log Likelihood -10,910.820 Akaike Inf. Crit. 21,867.640 Residual Std. Error 7,181.347 (df = 32538) 7,180.415 (df = 32537) F Statistic 86.255*** (df = 22; 32538) 82.937*** (df = 23; 32537)
=============================================================================================
Note: *p&lt;0.1; **p&lt;0.05; ***p&lt;0.01
</em></pre>
<p>One of the main advantages of multiple regression is that it is very straightforward to interpret the effects of the estimated coefficients in the model. An example is that the effect of age on capital gain is positive with a highly significant (p&lt;0.001 – shown by ***) effect of 18.750. The polynomial regression model is also easy to interpret. So these models have a lot of explanatory power and are easy to explain to the customer. It is a win-win. The logistic model is a bit harder to explain. The easiest way to explain the effects in the model is to look at effects and marginal effects. The below coding shows this:</p>
<pre> # Load packages
library(effects)
library(mfx)
# effects model
eff &lt;- allEffects(logitMod)
plot(eff, rescale.axis = FALSE)
</pre>
<p>The effect plot look likes this:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/10/WffwxtplotLogistic.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2018/10/WffwxtplotLogistic-490x306.png?w=450&ssl=1" alt="" class="wp-image-17336" data-recalc-dims="1"></a></p>
<p>The plot shows the effect of an explanatory variable on the explained variable at any given level of the explanatory variable. So it is a great way to visualize the effect of the explanatory variables.</p>
<p>The marginal effect code gives us this result (I have not taken all results into this table):</p>
<pre>
# Marginal effects model 
mfx &lt;-logitmfx(ABOVE50K ~ RELATIONSHIP + AGE + CAPITALGAIN + OCCUPATION + EDUCATIONNUM,
                data=rdata, atmean = TRUE, robust = FALSE)
mfx 
<em>> mfx
Call:
logitmfx(formula = ABOVE50K ~ RELATIONSHIP + AGE + CAPITALGAIN + OCCUPATION + EDUCATIONNUM, data = rdata, atmean = TRUE, robust = FALSE)
Marginal Effects:
dF/dx Std. Err. z P>|z| RELATIONSHIP Not-in-family -1.7788e-01 4.5764e-03 -38.8692 &lt; 2.2e-16 ***
RELATIONSHIP Other-relative -1.2431e-01 4.0250e-03 -30.8841 &lt; 2.2e-16 ***
RELATIONSHIP Own-child -1.9550e-01 4.0274e-03 -48.5418 &lt; 2.2e-16 ***
RELATIONSHIP Unmarried -1.4571e-01 3.9049e-03 -37.3139 &lt; 2.2e-16 ***
RELATIONSHIP Wife 3.5126e-02 8.8522e-03 3.9680 7.247e-05 ***
AGE 2.5532e-03 1.6595e-04 15.3856 &lt; 2.2e-16 ***
CAPITALGAIN 3.3967e-05 1.4328e-06 23.7066 &lt; 2.2e-16 ***
EDUCATIONNUM 3.2153e-02 1.1105e-03 28.9526 &lt; 2.2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
</em></pre>
<p>The above marginal effects tell us how an explained variable (y) changes when a specific explanatory variable (x) changes. Other variables are assumed to be held constant. </p>
<h2>Clustering analysis
<p>Let us look at some of the cluster analysis that are in most demand according to this 2018 <a rel="nofollow" href="https://www.kdnuggets.com/2018/06/5-clustering-algorithms-data-scientists-need-know.html" target="_blank">article</a> from KDnugget. I have selected some of these analyses in the below coding – first model is K-Nearest Neighbour (KNN). The KNN method is a good way to introduce yourself to machine learning and classification. The method is basically classification by finding similar data points in the training data and making the best guess based on the classification of the observations. This method has wide application in many domains, such as semantic searching, emantic searching, and recommendation systems. </p>
<p>The below coding shows application of a KNN model:</p>
<pre># KNN R, K-NEAREST NEIGHBOR Clustering
library(caret)  
dataurl &lt;- &quot;https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data&quot;
download.file(url = dataurl, destfile = &quot;wine.data&quot;)
wine_df &lt;- read.csv(&quot;wine.data&quot;, header = FALSE)  
str(wine_df)  
set.seed(3033)
intrain &lt;- createDataPartition(y = wine_df$V1, p= 0.6, list = FALSE)
training &lt;- wine_df[intrain,]
testing &lt;- wine_df[-intrain,]
dim(training); dim(testing);
anyNA(wine_df)  
summary(wine_df)
training[[&quot;V1&quot;]] = factor(training[[&quot;V1&quot;]])  
trctrl &lt;- trainControl(method = &quot;repeatedcv&quot;, number = 8, repeats = 3)
set.seed(3322)
knn_fit_m &lt;- train(V1 ~., data = training, method = &quot;knn&quot;,
                 trControl=trctrl,
                 preProcess = c(&quot;center&quot;, &quot;scale&quot;),
                 tuneLength = 10)
knn_fit_m
plot(knn_fit_m)</pre>
<p>The KNN model above gives us the following plot:<br>
<a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/10/KNNplot.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/10/KNNplot-490x306.png?w=450&ssl=1" alt="" class="wp-image-17306" data-recalc-dims="1"></a></p>
<p>The next cluster model is a Density Based Spartial Clustering of Applications model with Noise (DBSCAN). The model is given by observations and it groups together observations that are closely packed, and at the same time it find outlier points that are located in low-density regions. DBSCAN is one of the most common clustering algorithms in data science. The below coding demonstrates a DBSCAN model: </p>
<pre>
# Density-Based Spatial Clustering of Applications with Noise (DBSCAN)
library(factoextra)
library(plotly)
data(&quot;multishapes&quot;)
df &lt;- multishapes[, 1:2]
set.seed(120)
km.res &lt;- kmeans(df, 6, nstart = 30)
fviz_cluster(km.res, df,  geom = &quot;point&quot;, 
             ellipse= FALSE, show.clust.cent = FALSE,
             palette = &quot;jco&quot;, ggtheme = theme_classic())
# Load the data 
data(&quot;multishapes&quot;, package = &quot;factoextra&quot;)
df &lt;- multishapes[, 1:2]
# Compute DBSCAN using fpc package
library(&quot;fpc&quot;)
set.seed(123)
dbs &lt;- fpc::dbscan(df, eps = 0.15, MinPts = 6)
# Plot DBSCAN results
library(&quot;factoextra&quot;)
fviz_cluster(dbs, data = df, stand = FALSE,
             ellipse = FALSE, show.clust.cent = FALSE,
             geom = &quot;point&quot;,palette = &quot;jco&quot;, ggtheme = theme_classic())
print(dbs)
ggplotly(p = ggplot2::last_plot())
</pre>
<p>The above DBSCAN model gives us the following plot:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2018/10/DBSplot.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/10/DBSplot-490x306.png?w=450&ssl=1" alt="" class="wp-image-17307" data-recalc-dims="1"></a></p>
<h2>Visualization</h2>
<p>The last skillset that we will work with is visualization. There are some very elegant and efficient visualization in R like <code>ggplot2</code>, <code>dygraphs</code> and <code>Plotly</code>. In this case we will work with <code>Plotly</code> due to its ability to create efficient visualization with high quality interactive modules. The below coding creates this:</p>
<pre>
# Basic Line Plot
library(plotly)
t_0 &lt;- rnorm(90, mean = 3)
t_1 &lt;- rnorm(90, mean = 0)
t_2 &lt;- rnorm(90, mean = -3)
x &lt;- c(1:90)
datap &lt;- data.frame(x, trace_0, trace_1, trace_2)
r %
  add_trace(y = ~t_0, name = 'trace 0',mode = 'lines') %>%
  add_trace(y = ~t_1, name = 'trace 1', mode = 'lines+markers') %>%
  add_trace(y = ~t_2, name = 'trace 2', mode = 'markers')
r
</pre>
<p>The above coding gives us the following visualization:<br>
<a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2018/10/Plotlyplot.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2018/10/Plotlyplot-490x306.png?w=450&ssl=1" alt="" class="wp-image-17321" data-recalc-dims="1"></a></p>
<h3>References</h3>
<p>1. <a rel="nofollow" href="https://cran.r-project.org/web/packages/skimr/skimr.pdf" target="_blank">Using Skimr in R – CRAN.R-project.org</a><br>
2. <a rel="nofollow" href="https://cran.r-project.org/web/packages/stargazer/stargazer.pdf" target="_blank">Using Stargazer in R – CRAN.R-project.org</a><br>
3. <a rel="nofollow" href="https://cran.r-project.org/web/packages/caret/caret.pdf" target="_blank">Using Caret in R – CRAN.R-project.org</a><br>
4. <a rel="nofollow" href="https://cran.r-project.org/web/packages/factoextra/factoextra.pdf" target="_blank">Using Factoextra in R – CRAN.R-project.org</a><br>
5. <a rel="nofollow" href="https://cran.r-project.org/web/packages/plotly/plotly.pdf" target="_blank">Using Plotly in R – CRAN.R-project.org</a><br>
6. <a rel="nofollow" href="https://cran.r-project.org/web/packages/fpc/fpc.pdf" target="_blank">Using Fpc in R – CRAN.R-project.org</a></p>
<p><strong>
<p>Related Post</p>
<p></p></strong>
<ol>
<li><a rel="nofollow" href="https://datascienceplus.com/weight-loss-in-the-u-s-an-analysis-of-nhanes-data-with-tidyverse/" title="Permanent Link toWeight loss in the U.S. – An analysis of NHANES data with tidyverse" target="_blank">Weight loss in the U.S. – An analysis of NHANES data with tidyverse</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/machine-learning-results-in-r-one-plot-to-rule-them-all-part-2-regression-models/" title="Permanent Link toMachine Learning Results in R: one plot to rule them all! (Part 2 – Regression Models)" target="_blank">Machine Learning Results in R: one plot to rule them all! (Part 2 – Regression Models)</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/story-of-pairs-ggpairs-and-the-linear-regression/" title="Permanent Link toStory of pairs, ggpairs, and the linear regression" target="_blank">Story of pairs, ggpairs, and the linear regression</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/extract-fred-data-for-ols-regression-analysis-a-complete-tutorial/" title="Permanent Link toExtract FRED Data for OLS Regression Analysis: A Complete R Tutorial" target="_blank">Extract FRED Data for OLS Regression Analysis: A Complete R Tutorial</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/mnist-for-machine-learning-beginners-with-softmax-regression/" title="Permanent Link toMNIST For Machine Learning Beginners With Softmax Regression" target="_blank">MNIST For Machine Learning Beginners With Softmax Regression</a></li>
</ol>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://datascienceplus.com/commercial-data-analytics-an-economic-view-on-data-science-methods/"> R Programming – DataScience+</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/ppXUF48SdnE" height="1"></a></div></h2>