<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-603166843861435412</id><updated>2026-11-06T05:02:48.298+04:00</updated><category term="R"/><category term="machine learning"/><category term="data analysis"/><category term="Data Science"/><category term="Python"/><category term="big data"/><category term="classification"/><category term="Exploratory analysis"/><category term="Recommendation Engine"/><category term="Statistics"/><category term="data mining"/><category term="AI"/><category term="Artificial Intelligence"/><category term="Data Scientist"/><category term="GPU"/><category term="Twitter Analysis"/><category term="cloud"/><category term="colaboratory"/><category term="collaborative filtering"/><category term="deep learning"/><category term="deeplearning"/><category term="functions R"/><category term="google"/><category term="logistic regression"/><category term="multi layer neural network"/><category term="neural networks"/><category term="supervised learning"/><category term="visualization"/><category term="visualization tools"/><category term="API"/><category term="ARIMA"/><category term="Curse of Dimensionality"/><category term="Data Preparation"/><category term="Data Science R"/><category term="Data Science path"/><category term="Hypothesis"/><category term="LDA"/><category term="PCA"/><category term="Precision"/><category term="Rapache"/><category term="Regression"/><category term="Rest API"/><category term="Rstudio"/><category term="Sentiment analysis"/><category term="Text classification"/><category term="Topic modeling"/><category term="VSM"/><category term="algorithm"/><category term="apache"/><category term="apply function r"/><category term="apply r"/><category term="break statement R"/><category term="cluster analysis"/><category term="content based recommender system"/><category term="cosine similarity"/><category term="data frame R"/><category term="data mining standards"/><category term="data modeling"/><category term="data types R"/><category term="dimensionality reduction"/><category term="document classification"/><category term="document search"/><category term="feature extraction"/><category term="for loop statement R"/><category term="forecast"/><category term="hadoop"/><category term="hive"/><category term="ifelse statement R"/><category term="image processing"/><category term="information retrieval"/><category term="integer R"/><category term="inverse document frequency"/><category term="item   based collaborative filtering"/><category term="jaccard similarity"/><category term="k-means cluster algorithm"/><category term="lapply r"/><category term="linear regression"/><category term="list R"/><category term="machine learning R"/><category term="mapply r"/><category term="matrix"/><category term="matrix R"/><category term="missing values imputation"/><category term="model assess"/><category term="movie review"/><category term="multiple regression"/><category term="natural language processing"/><category term="nlp"/><category term="normalization"/><category term="pig"/><category term="principal component analysis"/><category term="r programming"/><category term="recall"/><category term="recommender systems"/><category term="repeat statement R"/><category term="sapply r"/><category term="sklearn"/><category term="sklearn load data"/><category term="speech recognition"/><category term="step for data analysis"/><category term="tapply r"/><category term="term document matrix"/><category term="term frequency"/><category term="text mining"/><category term="text processing"/><category term="tfidf"/><category term="time series"/><category term="tools"/><category term="tseries"/><category term="unsupervised learning"/><category term="user based collaborative filtering"/><category term="vector space model"/><category term="web application using R"/><category term="web service"/><category term="while statement R"/><title type='text'>Data Perspective</title><subtitle type='html'>Data Perspective is technical blog on Data Science maintained by Suresh Gorakala. Tutorials are focused on areas of Artificial Intelligence,Machine Learning, Deep Learning, Natural language Processing, Data Mining.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.dataperspective.info/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>28</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-8335270983360498935</id><published>2019-02-23T11:25:00.001+04:00</published><updated>2019-02-24T13:22:18.209+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="AI"/><category scheme="http://www.blogger.com/atom/ns#" term="Artificial Intelligence"/><category scheme="http://www.blogger.com/atom/ns#" term="classification"/><category scheme="http://www.blogger.com/atom/ns#" term="cloud"/><category scheme="http://www.blogger.com/atom/ns#" term="colaboratory"/><category scheme="http://www.blogger.com/atom/ns#" term="deep learning"/><category scheme="http://www.blogger.com/atom/ns#" term="deeplearning"/><category scheme="http://www.blogger.com/atom/ns#" term="google"/><category scheme="http://www.blogger.com/atom/ns#" term="GPU"/><category scheme="http://www.blogger.com/atom/ns#" term="machine learning"/><category scheme="http://www.blogger.com/atom/ns#" term="multi layer neural network"/><category scheme="http://www.blogger.com/atom/ns#" term="neural networks"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="sklearn load data"/><title type='text'>How to import data into Google Colab Jupyter Noteook</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Accesing data is one of the first step that we need when performing any data analysis. In this tutorial, we will see two ways of loading data 
into the google colab environment.
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;li&gt;Uploading csv from local machine and loading into colab&lt;/li&gt;
&lt;li&gt;Loading data from google drive to colab&lt;/li&gt;
&lt;br /&gt;
&lt;b&gt;Uploading CSV from local machine using IMPORT functionality.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;li&gt;Load import files library from google colab&lt;/li&gt;
&lt;li&gt;upload file using the upload button control&lt;/li&gt;
&lt;br /&gt;
Running below commands will allow us to upload data files into the colab environment. Once the &lt;b&gt;Choose Files&lt;/b&gt; button is visible, 
after executing the below listed python commands, we can easily upload files from local directory.
&lt;br /&gt;
&lt;pre&gt;from google.colab import files
uploaded = files.upload()
&lt;/pre&gt;
&lt;em&gt;Saving DOLPHIN.csv to DOLPHIN.csv&lt;/em&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;To view the uploaded files&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Below command allows us to verify if the file is uploaded correctly.

&lt;br /&gt;
&lt;pre&gt;for fn in uploaded.keys():
  print(&#39;User uploaded file &quot;{name}&quot; with length {length} bytes&#39;.format(name=fn, length=len(uploaded[fn])))
&lt;/pre&gt;
&lt;em&gt;User uploaded file &quot;DOLPHIN.csv&quot; with length 117269 bytes&lt;/em&gt; &lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Reading the uploaded from into pandas dataframe and displaying results&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
After the data file is uploaded to the colab, we can use pandas functions to load data into python environment and continue our further analysis. &lt;br /&gt;
&lt;pre&gt;import pandas as pd
import io
df = pd.read_csv(io.StringIO(uploaded[&#39;DOLPHIN.csv&#39;].decode(&#39;utf-8&#39;)))
&lt;/pre&gt;
&lt;pre&gt;print(df.head(2))
&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvEAZmjlDCRcTpiIHhAYlqYVpw4pWuuZ6zjxkbGoSTLt6ZHAYv3UvhxiCDgHKMY-LePND_ne3BCe4YpnwCp99JPZtn9_cv7WtGQsRG9JIB4-IggqWDjRzx8mrfSqbSJSJ_7Njz2gYoxk6t/s1600/gogole+colab+upload+data.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;213&quot; data-original-width=&quot;630&quot; height=&quot;135&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvEAZmjlDCRcTpiIHhAYlqYVpw4pWuuZ6zjxkbGoSTLt6ZHAYv3UvhxiCDgHKMY-LePND_ne3BCe4YpnwCp99JPZtn9_cv7WtGQsRG9JIB4-IggqWDjRzx8mrfSqbSJSJ_7Njz2gYoxk6t/s400/gogole+colab+upload+data.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;Load data from google drive:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Sometimes we may require to load data from google drive. Below commands will be useful in reading
data from google drive. Here we assume that the data file to be loaded into python environemnt is already uploaded to Google Drive.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;li&gt;Step 1, we need to mount the google drive&lt;/li&gt;
&lt;li&gt;Step 2, After mounting we need to provide authorization&lt;/li&gt;
&lt;li&gt;Step 3, we can view the current list of files available at the mounted location&lt;/li&gt;
&lt;li&gt;Step 4, Load data using pandas read_csv function&lt;/li&gt;
&lt;br /&gt;
&lt;em&gt;
Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&amp;amp;redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&amp;amp;scope=email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdocs.test%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.photos.readonly%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fpeopleapi.readonly&amp;amp;response_type=code &lt;br /&gt;

Enter your authorization code:&lt;br /&gt;
··········&lt;br /&gt;
Mounted at /content/gdrive&lt;br /&gt;
&lt;/em&gt;
&lt;br /&gt;
&lt;b&gt;
&lt;br/&gt;
&lt;b&gt;
&lt;I&gt;Note: My data files are located at MY DRIVE\COLAB NOTEBOOKS folder of my google drive account. Please change the code accordingly
&lt;/I&gt;
&lt;/b&gt;&lt;/br&gt;
After clicking the link and entering the authorization code, you can access your drive as follows:&lt;/b&gt;&lt;br /&gt;
&lt;pre&gt;!ls -la /content/gdrive/My\ Drive/Colab\ Notebooks/
&lt;/pre&gt;
&lt;em&gt;
total 230&lt;br /&gt;
-rw------- 1 root root 117269 Feb 23 06:43  DOLPHINOFFALLN.csv&lt;br /&gt;
-rw------- 1 root root  12104 Feb 23 06:46  ImportDatatoColab.ipynb&lt;br /&gt;
-rw------- 1 root root   8935 Sep  8 17:23 &#39;Running first neural network model  on google colaboratory&#39;&lt;br /&gt;
-rw------- 1 root root  13498 Aug 25 17:19  SettingupDrive_GSPGC.ipynb&lt;br /&gt;
-rw------- 1 root root  81691 Nov 26 06:47 &#39;Upload data to colab from google drive&#39;&lt;br /&gt;
&lt;/em&gt;

&lt;br /&gt;
&lt;pre&gt;df2 = pd.read_csv(&#39;/content/gdrive/My Drive/Colab Notebooks/DOLPHINOFFALLN.csv&#39;)
&lt;/pre&gt;
&lt;pre&gt;print(df2.head(2))
&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4dBBigZB2E94CwPidhZmb7d0KZ4P8TFiKXhGrdJ8U-iRe85vO0Udm9m5wtFpDGgr9ggQ7rkwdgExUxgTti0K1G2wCj4dc9MrxRl1Qk914-qN_Xpf371_SLufiBaKmCkb7PnoXa6Z0KSyx/s1600/gogole+colab+upload+data.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;213&quot; data-original-width=&quot;630&quot; height=&quot;135&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4dBBigZB2E94CwPidhZmb7d0KZ4P8TFiKXhGrdJ8U-iRe85vO0Udm9m5wtFpDGgr9ggQ7rkwdgExUxgTti0K1G2wCj4dc9MrxRl1Qk914-qN_Xpf371_SLufiBaKmCkb7PnoXa6Z0KSyx/s400/gogole+colab+upload+data.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;b&gt;
TIP: We can Install new libraries in python environment inline using below command&lt;/b&gt;&lt;br /&gt;
&lt;i&gt;Note that appending ! before pip command&lt;/i&gt;

&lt;br /&gt;
&lt;pre&gt;!pip install matplotlib&lt;/pre&gt;
&lt;em&gt;
&lt;br /&gt;Requirement already satisfied: matplotlib in /usr/local/lib/python3.6/dist-packages (3.0.2)
&lt;br /&gt;Requirement already satisfied: numpy&amp;gt;=1.10.0 in /usr/local/lib/python3.6/dist-packages (from matplotlib) (1.14.6)
&lt;br /&gt;Requirement already satisfied: cycler&amp;gt;=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib) (0.10.0)
&lt;br /&gt;Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,&amp;gt;=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib) (2.3.1)
&lt;br /&gt;Requirement already satisfied: kiwisolver&amp;gt;=1.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib) (1.0.1)
&lt;br /&gt;Requirement already satisfied: python-dateutil&amp;gt;=2.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib) (2.5.3)
&lt;br /&gt;Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from cycler&amp;gt;=0.10-&amp;gt;matplotlib) (1.11.0)
&lt;br /&gt;Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from kiwisolver&amp;gt;=1.0.1-&amp;gt;matplotlib) (40.8.0)
&lt;/em&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;References:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;li&gt;&lt;a href=&quot;https://www.kdnuggets.com/2018/02/essential-google-colaboratory-tips-tricks.html&quot; target=&quot;_blank&quot;&gt;https://www.kdnuggets.com/2019/01/more-google-colab-environment-management-tips.html &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.kdnuggets.com/2018/02/essential-google-colaboratory-tips-tricks.html&quot;&gt;https://www.kdnuggets.com/2018/02/essential-google-colaboratory-tips-tricks.html&lt;/a&gt;&lt;/li&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Full code implementation:
&lt;/h3&gt;
&lt;script src=&quot;https://gist.github.com/sureshgorakala/1315555e66adb167c3d6d849e5c96c7c.js&quot;&gt;&lt;/script&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/8335270983360498935/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2019/02/how-to-import-data-into-google-colab.html#comment-form' title='390 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/8335270983360498935'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/8335270983360498935'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2019/02/how-to-import-data-into-google-colab.html' title='How to import data into Google Colab Jupyter Noteook'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvEAZmjlDCRcTpiIHhAYlqYVpw4pWuuZ6zjxkbGoSTLt6ZHAYv3UvhxiCDgHKMY-LePND_ne3BCe4YpnwCp99JPZtn9_cv7WtGQsRG9JIB4-IggqWDjRzx8mrfSqbSJSJ_7Njz2gYoxk6t/s72-c/gogole+colab+upload+data.png" height="72" width="72"/><thr:total>390</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-7336285942556203195</id><published>2018-09-08T22:36:00.001+04:00</published><updated>2019-02-24T09:14:08.667+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="AI"/><category scheme="http://www.blogger.com/atom/ns#" term="Artificial Intelligence"/><category scheme="http://www.blogger.com/atom/ns#" term="classification"/><category scheme="http://www.blogger.com/atom/ns#" term="cloud"/><category scheme="http://www.blogger.com/atom/ns#" term="colaboratory"/><category scheme="http://www.blogger.com/atom/ns#" term="deep learning"/><category scheme="http://www.blogger.com/atom/ns#" term="deeplearning"/><category scheme="http://www.blogger.com/atom/ns#" term="google"/><category scheme="http://www.blogger.com/atom/ns#" term="GPU"/><category scheme="http://www.blogger.com/atom/ns#" term="machine learning"/><category scheme="http://www.blogger.com/atom/ns#" term="multi layer neural network"/><category scheme="http://www.blogger.com/atom/ns#" term="neural networks"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="sklearn"/><title type='text'>Getting started with google laboratory for running deep learning applications</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
What is Google Colab: &lt;/h3&gt;
&lt;br /&gt;
We all know that deep learning algorithms improve the accuracy of AI applications to great extent. But this accuracy comes with requiring heavy computational processing units such as GPU for developing deep learning models. Many of the machine learning developers cannot afford GPU as they are very costly and find this as a roadblock for learning and developing Deep learning applications. To help the AI, machine learning developers Google has released a free cloud based service Google Colaboratory - Jupyter notebook environment with free GPU processing capabilities with no strings attached for using this service. It is a ready to use service which requires no set at all. &lt;br /&gt;
&lt;br /&gt;
Any AI developers can use this free service to develop deep learning applications using popular AI libraries like Tensorflow, Pytorch, Keras, etc.&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Setting up colab:&lt;/h3&gt;
&lt;br /&gt;
&lt;i&gt;Go to google drive → new item → More → colaboratory &lt;/i&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrrEbLO8Y-0dfD-BS7zaMI8gCWf7XjNE-Z_vbADUf9dV9De6gMStVVXtuH4r6iY806tL0AKZ0SzQ2VQ6sHnboSpdwegAhgmyOx_MfkMG2RHRAOFAx7hmHJVbc72QDRgK3KkEIDMr27R-ZS/s1600/google+colab+intro.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;516&quot; data-original-width=&quot;877&quot; height=&quot;188&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrrEbLO8Y-0dfD-BS7zaMI8gCWf7XjNE-Z_vbADUf9dV9De6gMStVVXtuH4r6iY806tL0AKZ0SzQ2VQ6sHnboSpdwegAhgmyOx_MfkMG2RHRAOFAx7hmHJVbc72QDRgK3KkEIDMr27R-ZS/s320/google+colab+intro.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
This opens up a python Jupyter notebook in browser.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVuI8fnunLOj6VHWS8UKJcO-hzrynkoDOYWb9P9800fouYljYV_9rjyx2V64fzqr65BDITZZDMxPOXxj6RQ-t4Tk-szWnWdDyObHiGXjRRntuOVsohwd0Hc6-IN3EotPeVtNRyI7ho9Grk/s1600/colab+python+notebook.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;299&quot; data-original-width=&quot;1282&quot; height=&quot;75&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVuI8fnunLOj6VHWS8UKJcO-hzrynkoDOYWb9P9800fouYljYV_9rjyx2V64fzqr65BDITZZDMxPOXxj6RQ-t4Tk-szWnWdDyObHiGXjRRntuOVsohwd0Hc6-IN3EotPeVtNRyI7ho9Grk/s320/colab+python+notebook.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
By default, the Jupyter notebook runs on Python 2.7 version and CPU processor. We may change the python version to Python 3.6 and  processing capability to GPU by changing the settings as shown below:&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;Go to Runtime → Change runtime type &lt;/i&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSOIqEM-gVmzCDfyLWnfbm1Hvl-pnWh6QNis7eV3leipFe_FpjJIpTH3C7eN-RyFHj6E4SkrtRCDrbyf4Kwj6BqGi0j3EieWCsdDpAIWdVIBAhyphenhyphenRmF2dwPz40NC0Fqwm1wPFs-XNDKhold/s1600/colab+gpu+runtime.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;493&quot; data-original-width=&quot;822&quot; height=&quot;192&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSOIqEM-gVmzCDfyLWnfbm1Hvl-pnWh6QNis7eV3leipFe_FpjJIpTH3C7eN-RyFHj6E4SkrtRCDrbyf4Kwj6BqGi0j3EieWCsdDpAIWdVIBAhyphenhyphenRmF2dwPz40NC0Fqwm1wPFs-XNDKhold/s320/colab+gpu+runtime.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
This opens up a Notebook settings pop-up where we can change Runtime Type to Python 3.6 and processing Hardware to GPU.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgaBKpylwUB06HE7RtQmA7wfQ8EoXrzvjW2ijJ5oCv5z4q79foq251sMkEn-3EGv1SvqsThV0-5Q6lzZnyBMbpASLfFaD7MQPYmntLn78eDU-NpeueRv9G76G-ZOe5g7ohUQ-nGyyYozjj2/s1600/python+colab+gpu.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;515&quot; data-original-width=&quot;1289&quot; height=&quot;128&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgaBKpylwUB06HE7RtQmA7wfQ8EoXrzvjW2ijJ5oCv5z4q79foq251sMkEn-3EGv1SvqsThV0-5Q6lzZnyBMbpASLfFaD7MQPYmntLn78eDU-NpeueRv9G76G-ZOe5g7ohUQ-nGyyYozjj2/s320/python+colab+gpu.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Bingo, your python environment with the processing power of GPU is ready use.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Important things to remember:&lt;/b&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;The supported browsers are Chrome and Firefox &lt;/li&gt;
&lt;li&gt;Currently only Python is supported &lt;/li&gt;
&lt;li&gt;We can you use upto 12 hours of processing time in one go &lt;/li&gt;
&lt;/ul&gt;
Let’s check if our newly created Jupyter notebook works perfectly. Run below commands and see if we are getting expected results. &lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3x5K_HlVvo-SJfbW_QzzKB7OsLCyO6hzHz7Gn73ORDm1wWUkHNa0XEXQUJQlzO-QsPL6tcRgv9urQUFqh7uwis88R_B9dXqg9AqOwOV89lBl4rrxqXP2T0JxU6Ocut8fRSx4hXRfkJUma/s1600/python+colab+google+ai.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;469&quot; data-original-width=&quot;1310&quot; height=&quot;115&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3x5K_HlVvo-SJfbW_QzzKB7OsLCyO6hzHz7Gn73ORDm1wWUkHNa0XEXQUJQlzO-QsPL6tcRgv9urQUFqh7uwis88R_B9dXqg9AqOwOV89lBl4rrxqXP2T0JxU6Ocut8fRSx4hXRfkJUma/s320/python+colab+google+ai.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
By default most frequently used python libraries such as Numpy, Pandas, scipy, Sklearn, Matplotlib etc are pre-installed when we create a notebook. Below we can see plotting 
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOmKLhM2OUaMSquvJ_w1OI4hUrMdHxVM9UOUFNkL1zqdOkmNp9MWiaLwCZOYUFc4a_jMd8Zb_elR-1esqZ828UHx9yp5jA5DUrZvzYe4R-ZUrBqWgqmAPTiNSqeRT7wRyftLB9NToTwTbS/s1600/google+python+colab+ai.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;542&quot; data-original-width=&quot;1266&quot; height=&quot;137&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOmKLhM2OUaMSquvJ_w1OI4hUrMdHxVM9UOUFNkL1zqdOkmNp9MWiaLwCZOYUFc4a_jMd8Zb_elR-1esqZ828UHx9yp5jA5DUrZvzYe4R-ZUrBqWgqmAPTiNSqeRT7wRyftLB9NToTwTbS/s320/google+python+colab+ai.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Running Machine Learning example:&lt;/h3&gt;
&lt;br /&gt;
Below Python Notebook code shows us an example of multi layer neural network using python and sklearn library.&lt;br /&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Full code implementation:
&lt;/h3&gt;
&lt;br /&gt;
&lt;script src=&quot;https://gist.github.com/sureshgorakala/61ad5c5975321537374691d64aba8810.js&quot;&gt;&lt;/script&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;a href=&quot;https://www.dataperspective.info/2019/02/how-to-import-data-into-google-colab.html&quot;&gt;In the next post we see how to import data into Colab environment&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/7336285942556203195/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2018/09/getting-started-with-google-laboratory-deep-learning.html#comment-form' title='181 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/7336285942556203195'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/7336285942556203195'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2018/09/getting-started-with-google-laboratory-deep-learning.html' title='Getting started with google laboratory for running deep learning applications'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrrEbLO8Y-0dfD-BS7zaMI8gCWf7XjNE-Z_vbADUf9dV9De6gMStVVXtuH4r6iY806tL0AKZ0SzQ2VQ6sHnboSpdwegAhgmyOx_MfkMG2RHRAOFAx7hmHJVbc72QDRgK3KkEIDMr27R-ZS/s72-c/google+colab+intro.png" height="72" width="72"/><thr:total>181</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-9220989719676124442</id><published>2017-12-02T18:17:00.000+04:00</published><updated>2018-08-28T09:49:49.867+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Science R"/><category scheme="http://www.blogger.com/atom/ns#" term="machine learning R"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="Rstudio"/><title type='text'>Getting Started with R</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;body&gt;
  &lt;div class=&quot;border-box-sizing&quot; id=&quot;notebook&quot; tabindex=&quot;-1&quot;&gt;
    &lt;div class=&quot;container&quot; id=&quot;notebook-container&quot;&gt;
In this post we get familiar with R Studio and basic syntax of R programming language

&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;

&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;h1 id=&quot;Rstudio-OverView&quot;&gt;
Rstudio OverView&lt;/h1&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;&lt;br /&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
we have 4 panes&lt;br /&gt;
1) script pane - to write and save the programing script&lt;br /&gt;
2) Console pane - where all the code will get executed&lt;br /&gt;
3) Environment/history pane - displays all the variables created,functions&lt;br /&gt;
used with in the current session&lt;br /&gt;
4) Helper pane - contains multiple tabs to install/display pacakges,&lt;br /&gt;
view visualization plots,&lt;br /&gt;
locate files within the workspace&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigqi7_3ioWHDFa-HFOaFsVevUVjYD_PyLXEZZcOZDygT3n65KWxWWQcxGL_yZ1EW5WScScU5AWUmxZ5tLi9amjHKj3I3U_wDxgZs8PsRhk54ucmuQ52spn_0ExXCf0uwudiEC_RvrLAjnj/s1600/rstudio_view.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;546&quot; data-original-width=&quot;1025&quot; height=&quot;170&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigqi7_3ioWHDFa-HFOaFsVevUVjYD_PyLXEZZcOZDygT3n65KWxWWQcxGL_yZ1EW5WScScU5AWUmxZ5tLi9amjHKj3I3U_wDxgZs8PsRhk54ucmuQ52spn_0ExXCf0uwudiEC_RvrLAjnj/s320/rstudio_view.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing code_cell rendered&quot;&gt;
&lt;div class=&quot;input&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
In&amp;nbsp;[1]:&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;input_area&quot;&gt;
&lt;div class=&quot; highlight hl-r&quot;&gt;
&lt;pre&gt;help&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kp&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;pre&gt;&lt;span class=&quot;p&quot;&gt;&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
&lt;h1 id=&quot;getting-and-setting-workspace&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
getting and setting workspace&lt;/span&gt;&lt;/h1&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing code_cell rendered&quot;&gt;
&lt;div class=&quot;input&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
In&amp;nbsp;[2]:&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;input_area&quot;&gt;
&lt;div class=&quot; highlight hl-r&quot;&gt;
&lt;pre&gt;&lt;span class=&quot;c1&quot;&gt;# to display current working directory use getwd() function&lt;/span&gt;
&lt;span class=&quot;kp&quot;&gt;getwd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;output_wrapper&quot;&gt;
&lt;div class=&quot;output&quot;&gt;
&lt;div class=&quot;output_area&quot;&gt;
&lt;div class=&quot;prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;output_html rendered_html output_subarea &quot;&gt;
&#39;C:/Users/Suresh/mlclassscripts&#39;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing code_cell rendered&quot;&gt;
&lt;div class=&quot;input&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
In&amp;nbsp;[&amp;nbsp;]:&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;input_area&quot;&gt;
&lt;div class=&quot; highlight hl-r&quot;&gt;
&lt;pre&gt;&lt;span class=&quot;c1&quot;&gt;# to set up workspace or working directory use setwd() function&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#syntax is shown below&lt;/span&gt;
&lt;span class=&quot;kp&quot;&gt;setwd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;path&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing code_cell rendered&quot;&gt;
&lt;div class=&quot;input&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
In&amp;nbsp;[6]:&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;input_area&quot;&gt;
&lt;div class=&quot; highlight hl-r&quot;&gt;
&lt;pre&gt;&lt;span class=&quot;kp&quot;&gt;setwd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;C:\\Suresh\\R&amp;amp;D\\Projects\\ML classroom training\\sessions&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kp&quot;&gt;setwd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;C:/Suresh/R&amp;amp;D/Projects/ML classroom training/sessions&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
&lt;h1 id=&quot;getting-help-in-R&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
getting help in R&lt;/span&gt;&lt;/h1&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
To get help within R environment, we use help() function to get the&lt;br /&gt;
documentation&lt;br /&gt;
for any of the functions/packages available within R environment.&lt;br /&gt;
To see the arguments required for a function, we use args() function.&lt;br /&gt;
to see the example of a function, example() function is used.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing code_cell rendered&quot;&gt;
&lt;div class=&quot;input&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
In&amp;nbsp;[&amp;nbsp;]:&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;input_area&quot;&gt;
&lt;div class=&quot; highlight hl-r&quot;&gt;
&lt;pre&gt;help&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;stats&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
help&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;mean&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kp&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;mean&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
example&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;mean&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;#getting help documentation for a package&lt;/span&gt;
help&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;package&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;caret&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;pre&gt;&lt;span class=&quot;p&quot;&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
&lt;h1 id=&quot;online-help-for-R-programming&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
online help for R programming&lt;/span&gt;&lt;/h1&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
We can get online help on available packages in R from official website of R-Cran&lt;br /&gt;
&lt;a href=&quot;https://cran.r-project.org/web/views/&quot;&gt;https://cran.r-project.org/web/views/&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
We can also get online support for our day to day activities from below websites:&lt;br /&gt;
&lt;a href=&quot;https://stackoverflow.com/&quot;&gt;https://stackoverflow.com/&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;https://stats.stackexchange.com/&quot;&gt;https://stats.stackexchange.com&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
&lt;h1 id=&quot;Installing-Packages&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
Installing Packages&lt;/span&gt;&lt;/h1&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing code_cell rendered&quot;&gt;
&lt;div class=&quot;input&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
In&amp;nbsp;[&amp;nbsp;]:&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;input_area&quot;&gt;
&lt;div class=&quot; highlight hl-r&quot;&gt;
&lt;pre&gt;&lt;span class=&quot;c1&quot;&gt;#install pacakges in R can be done in two ways,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#1) using install.packages() function and from the bottom right pane of Rstudio&lt;/span&gt;
install.packages&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;randomForest&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;#loading of installed or downloaded packages can be done using library() function.  &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#Note that we can only load the package if&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# we have installed the package already within our R environment&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;cluster&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing code_cell rendered&quot;&gt;
&lt;div class=&quot;input&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
In&amp;nbsp;[&amp;nbsp;]:&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;input_area&quot;&gt;
&lt;div class=&quot; highlight hl-r&quot;&gt;
&lt;pre&gt;&lt;span class=&quot;c1&quot;&gt;#below code to first verify if the library is installed in the R environment, &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#if it is not available&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# then the package will get installed.&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;cluster&lt;span class=&quot;p&quot;&gt;)){&lt;/span&gt;
install.pacakges&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;cluster&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
&lt;h1 id=&quot;basic-operations-in-R&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
basic operations in R&lt;/span&gt;&lt;/h1&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing code_cell rendered&quot;&gt;
&lt;div class=&quot;input&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
In&amp;nbsp;[&amp;nbsp;]:&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;input_area&quot;&gt;
&lt;div class=&quot; highlight hl-r&quot;&gt;
&lt;pre&gt;&lt;span class=&quot;c1&quot;&gt;# Adding two numericals&lt;/span&gt;
&lt;span class=&quot;m&quot;&gt;1+1&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;#multiplying two numericals&lt;/span&gt;
&lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;#dividing two numericals&lt;/span&gt;
&lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;#applying modulus operation on two numericals&lt;/span&gt;
&lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%%&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
&lt;h1 id=&quot;printing-results-to-R-console&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
printing results to R console&lt;/span&gt;&lt;/h1&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing code_cell rendered&quot;&gt;
&lt;div class=&quot;input&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
In&amp;nbsp;[&amp;nbsp;]:&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;input_area&quot;&gt;
&lt;div class=&quot; highlight hl-r&quot;&gt;
&lt;pre&gt;&lt;span class=&quot;c1&quot;&gt;#printing the data on the console&lt;/span&gt;
&lt;span class=&quot;kp&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;kp&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;data science&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;kp&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;pi&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
&lt;h1 id=&quot;Variable-declaration-and-assignment-in-R&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
Variable declaration and assignment in R&lt;/span&gt;&lt;/h1&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
variable assignment: In the below example, we are creating variable named z:&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing code_cell rendered&quot;&gt;
&lt;div class=&quot;input&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
In&amp;nbsp;[8]:&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;input_area&quot;&gt;
&lt;div class=&quot; highlight hl-r&quot;&gt;
&lt;pre&gt;z &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;  &lt;span class=&quot;m&quot;&gt;100&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
we use left arrow or = symbol for variable assignment. Its always good&lt;br /&gt;
practice to use left arrow for assignment.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing code_cell rendered&quot;&gt;
&lt;div class=&quot;input&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
In&amp;nbsp;[9]:&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;input_area&quot;&gt;
&lt;div class=&quot; highlight hl-r&quot;&gt;
&lt;pre&gt;z &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10.009&lt;/span&gt;
z &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10.009&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
&lt;h1 id=&quot;Loading-existing-or-default-datasets-available-in-R-environment&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
Loading existing or default datasets available in R environment&lt;/span&gt;&lt;/h1&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
we can access default datasets avaiable in R using data() function.&lt;br /&gt;
data() function will displays all the avaiable datasets within R.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing code_cell rendered&quot;&gt;
&lt;div class=&quot;input&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
In&amp;nbsp;[&amp;nbsp;]:&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;input_area&quot;&gt;
&lt;div class=&quot; highlight hl-r&quot;&gt;
&lt;pre&gt;data&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
In order to load a specific dataset into R, we need to give the dataset name as argument to the data() function&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing code_cell rendered&quot;&gt;
&lt;div class=&quot;input&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
In&amp;nbsp;[&amp;nbsp;]:&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;input_area&quot;&gt;
&lt;div class=&quot; highlight hl-r&quot;&gt;
&lt;pre&gt;data&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;AirPassengers&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
&lt;h1 id=&quot;Viewing-data-of-R-objects&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
Viewing data of R objects&lt;/span&gt;&lt;/h1&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
To view first 5 records of a R object (ex:dataframe), we use head() function.&lt;br /&gt;
head() function expects the data object as argument and prints the first 5 records on the R console.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing code_cell rendered&quot;&gt;
&lt;div class=&quot;input&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
In&amp;nbsp;[&amp;nbsp;]:&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;input_area&quot;&gt;
&lt;div class=&quot; highlight hl-r&quot;&gt;
&lt;pre&gt;&lt;span class=&quot;kp&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;AirPassengers&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
to view all the records in a nice tabular view&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing code_cell rendered&quot;&gt;
&lt;div class=&quot;input&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
In&amp;nbsp;[&amp;nbsp;]:&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;input_area&quot;&gt;
&lt;div class=&quot; highlight hl-r&quot;&gt;
&lt;pre&gt;View&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;AirPassengers&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
&lt;h1 id=&quot;Getting-the-decription-and-structure-of-R-object&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;
Getting the description and structure of R object&lt;/span&gt;&lt;/h1&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing text_cell rendered&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;text_cell_render border-box-sizing rendered_html&quot;&gt;
use str function to see the descriptions of the data object,&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;cell border-box-sizing code_cell rendered&quot;&gt;
&lt;div class=&quot;input&quot;&gt;
&lt;div class=&quot;prompt input_prompt&quot;&gt;
In&amp;nbsp;[&amp;nbsp;]:&lt;/div&gt;
&lt;div class=&quot;inner_cell&quot;&gt;
&lt;div class=&quot;input_area&quot;&gt;
&lt;div class=&quot; highlight hl-r&quot;&gt;
&lt;pre&gt;str&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;AirPassengers&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/9220989719676124442/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2017/12/getting-started-with-r.html#comment-form' title='45 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/9220989719676124442'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/9220989719676124442'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2017/12/getting-started-with-r.html' title='Getting Started with R'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigqi7_3ioWHDFa-HFOaFsVevUVjYD_PyLXEZZcOZDygT3n65KWxWWQcxGL_yZ1EW5WScScU5AWUmxZ5tLi9amjHKj3I3U_wDxgZs8PsRhk54ucmuQ52spn_0ExXCf0uwudiEC_RvrLAjnj/s72-c/rstudio_view.png" height="72" width="72"/><thr:total>45</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-1237400655120499037</id><published>2017-11-04T17:26:00.000+04:00</published><updated>2017-12-02T19:06:19.472+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="cosine similarity"/><category scheme="http://www.blogger.com/atom/ns#" term="document search"/><category scheme="http://www.blogger.com/atom/ns#" term="information retrieval"/><category scheme="http://www.blogger.com/atom/ns#" term="inverse document frequency"/><category scheme="http://www.blogger.com/atom/ns#" term="jaccard similarity"/><category scheme="http://www.blogger.com/atom/ns#" term="natural language processing"/><category scheme="http://www.blogger.com/atom/ns#" term="nlp"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="term document matrix"/><category scheme="http://www.blogger.com/atom/ns#" term="term frequency"/><category scheme="http://www.blogger.com/atom/ns#" term="tfidf"/><category scheme="http://www.blogger.com/atom/ns#" term="vector space model"/><category scheme="http://www.blogger.com/atom/ns#" term="VSM"/><title type='text'>information retrieval document search using vector space model in R</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Introduction:&lt;/h3&gt;
In this post, we learn about building a basic search engine or document retrieval system using Vector space model. This use case is widely used in information retrieval systems.  Given a set of documents and search term(s)/query we need to retrieve relevant documents that are similar to the search query. 

&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Problem statement:&lt;/h3&gt;
The problem statement explained above is represented as in below image.
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhi7orP1erye959rws2x4I5E0jEGdGU3yN_Z4eGG-vsd7RrS_XDz7uloHowPbWlcyTzkEEfY31kMK3yTOOswkRyHpt4utKtlloFKjCGluqbEWwNeYXJLtKCNcDeJVQRKCBEmedN7gEHV9Cd/s1600/information+retrieval_1.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;293&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhi7orP1erye959rws2x4I5E0jEGdGU3yN_Z4eGG-vsd7RrS_XDz7uloHowPbWlcyTzkEEfY31kMK3yTOOswkRyHpt4utKtlloFKjCGluqbEWwNeYXJLtKCNcDeJVQRKCBEmedN7gEHV9Cd/s320/information+retrieval_1.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;
Document retrieval system&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Before we get into building the search engine, we will learn briefly about different concepts we use in this post:
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Vector Space Model:&lt;/h3&gt;
A vector space model is an algebraic model, involving two steps, in first step we represent the text documents into vector of words and in second step we transform to numerical format so that we can apply any text mining techniques such as information retrieval, information extraction,information filtering etc.&lt;br /&gt;
&lt;br /&gt;
Let us understand with an example. consider below statements and a query term. The statements are referred as documents hereafter.&lt;br /&gt;
&lt;div align=&quot;center&quot; style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;Document 1: Cat runs behind rat&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;Document 2: Dog runs behind cat&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;Query: rat&lt;/i&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Document vectors representation:&lt;/h3&gt;
&lt;br /&gt;
In this step includes breaking each document into words, applying preprocessing steps such as removing stopwords, punctuations, special characters etc. After preprocessing the documents we represent them as vectors of words. &lt;br /&gt;
Below is a sample representation of the document vectors.&lt;br /&gt;
&lt;div align=&quot;center&quot; style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;Document 1: (cat, runs, behind, rat)&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;Document 2: (Dog, runs, behind, cat)&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;Query: (rat)&lt;/i&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;center&quot; style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;the relevant document to Query = 
greater of  (similarity score between (Document1, Query), similarity score between (Document2, Query)&lt;/i&gt;&lt;/div&gt;
&lt;br /&gt;
Next step is to represent the above created vectors of terms to numerical format known as term document matrix. 
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Term Document Matrix:&lt;/h3&gt;
A term document matrix is a way of representing documents vectors in a matrix format in which each row represents term vectors across all the documents and columns
represent document vectors across all the terms. The cell values frequency counts of each term in corresponding document. 
If a term is present in a document, then the corresponding cell value contains 1 else if the term is not present in the document then the cell value contains 0.&lt;br /&gt;
&lt;br /&gt;
After creating the term document matrix, we will calculate term weights for all the terms in the matrix across all the documents. 
It is also important to calculate the term weightings because we need to find out terms which uniquely define a document. 
&lt;br /&gt;
&lt;br /&gt;
We should note that a word which occurs in most of the documents might not contribute to represent the document relevance whereas less frequently occurred terms might define document relevance. This can be achieved using a method known as term frequency - inverse document frequency (tf-idf), which gives higher weights to the terms which occurs more in a document but rarely occurs in all other documents, lower weights to the terms which commonly occurs within and across all the documents.&lt;br /&gt;
&lt;div align=&quot;center&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;Tf-idf = tf X idf
&lt;/b&gt;&lt;br /&gt;
&lt;i&gt;tf = term frequency is the number of times a term occurs in a document&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;idf = inverse of the document frequency, given as below&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;idf = log(N/df), where df is the document frequency-number of documents containing a term&lt;/i&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1GzSyIWqk95gADyIHCjfNuZhKffjhfdUGZkGFUOYDSEmX-EoyZwQLf-f-kbEEK5b1CAxpSZjsij6TEVr70hO6CioNfkWAzB3eUoOhmVYa_JoTlHK9SInR14tVhBfBjr_5AV5oO75TPiE2/s1600/information+retrieval_3.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1GzSyIWqk95gADyIHCjfNuZhKffjhfdUGZkGFUOYDSEmX-EoyZwQLf-f-kbEEK5b1CAxpSZjsij6TEVr70hO6CioNfkWAzB3eUoOhmVYa_JoTlHK9SInR14tVhBfBjr_5AV5oO75TPiE2/s400/information+retrieval_3.PNG&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;div class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;total number of documents&lt;/i&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrQwswitYv1y5OUzPIOuUPHUU2qqTBpKMwDOWtkKIsxqxBEARYdMbPDqFMo9JvkZ9wnrmFc8ddUvgcB3qz8ETCcheCk3YDe2cmdZMa31aUosZi8WGJQ_ygOfPCr1GK71EajQ9SualYl5nh/s1600/information+retrieval_term+document+matrix.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;169&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrQwswitYv1y5OUzPIOuUPHUU2qqTBpKMwDOWtkKIsxqxBEARYdMbPDqFMo9JvkZ9wnrmFc8ddUvgcB3qz8ETCcheCk3YDe2cmdZMa31aUosZi8WGJQ_ygOfPCr1GK71EajQ9SualYl5nh/s320/information+retrieval_term+document+matrix.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;div class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;term document matrix&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBMHCeBICU4CKHHu9PTLjRiUIFAyXkozLsUFl4KmXkRxZ6UhuM95_jB6aK4CObIElHT0PI43T2BBL2H_vXDCYmUoHGY7sKUdGc2N1ZojSHk76bG6pnK_7pQ56QckBArVsiKJiu_eiA0AoO/s1600/information+retrieval_inverse_document_frequency.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBMHCeBICU4CKHHu9PTLjRiUIFAyXkozLsUFl4KmXkRxZ6UhuM95_jB6aK4CObIElHT0PI43T2BBL2H_vXDCYmUoHGY7sKUdGc2N1ZojSHk76bG6pnK_7pQ56QckBArVsiKJiu_eiA0AoO/s400/information+retrieval_inverse_document_frequency.PNG&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;div class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;inverse document frequency&lt;/i&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;center&quot; style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;Note: idf is calculated using logarithm of inverse fraction between document count and document frequency
&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiViLpUlQoRdIytSVANqYd9gy5_wDAcrPGlcLSxXEoVeRuTGESHyAn7YsuWeoWUiU82CA_jU45Xb9DAhsZyalfLSH88w1qsGIV6SaGZ-EFqGBz6LavT3yACDmGQ7yZg5oCKBtmz3xFJyyEt/s1600/tf_idf_term_document_matrix.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;134&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiViLpUlQoRdIytSVANqYd9gy5_wDAcrPGlcLSxXEoVeRuTGESHyAn7YsuWeoWUiU82CA_jU45Xb9DAhsZyalfLSH88w1qsGIV6SaGZ-EFqGBz6LavT3yACDmGQ7yZg5oCKBtmz3xFJyyEt/s320/tf_idf_term_document_matrix.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;div class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;tf-idf calculation&lt;/i&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align=&quot;center&quot; style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;Note: Tf-idf weightage is calculated using tf X idf 
&lt;/i&gt;&lt;/div&gt;
&lt;br /&gt;
Note, there are many variations in the way we calculate the term-frequency(tf) and inverse document frequency (idf), in this post we have seen one variation. Below images show as the other recommended variations of tf and idf, taken from &lt;a href=&quot;https://en.wikipedia.org/wiki/Tf%E2%80%93idf&quot; target=&quot;_blank&quot;&gt;wiki&lt;/a&gt;.&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEgPbXGWDYFUFlxSDxCnD663RTfu9sRkclPYqkE-dxmKeikrxg_RQ9VGHZPdwMgQMMbRSoOn4i9n-l49B4aG1jdzgCLL5Gv6voTXxP-K0xD7mZXnYMDIiq91Zel3GJsU19yJ61zccz4NTJ/s1600/term_frequency_variants.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;240&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEgPbXGWDYFUFlxSDxCnD663RTfu9sRkclPYqkE-dxmKeikrxg_RQ9VGHZPdwMgQMMbRSoOn4i9n-l49B4aG1jdzgCLL5Gv6voTXxP-K0xD7mZXnYMDIiq91Zel3GJsU19yJ61zccz4NTJ/s320/term_frequency_variants.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;div class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;term frequency variations &lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivoQOdVusB-mauPL90_8XAuavOBtR-j5ItPMH6C9CRCC772tltqt4MHZvB2icSrC15X5H_dxfYJJr5vqVOQVymjEfVl2ISur346lwuV88mo0mdbupvuUaLzzBCCwSv5EE5j0Fyy7As6z2E/s1600/inverse_document_frequency_variants.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;173&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEivoQOdVusB-mauPL90_8XAuavOBtR-j5ItPMH6C9CRCC772tltqt4MHZvB2icSrC15X5H_dxfYJJr5vqVOQVymjEfVl2ISur346lwuV88mo0mdbupvuUaLzzBCCwSv5EE5j0Fyy7As6z2E/s320/inverse_document_frequency_variants.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;div class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;inverse document frequency variations &lt;/i&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Similarity Measures: cosine similarity&lt;/h3&gt;
&lt;br /&gt;
Mathematically, closeness between two vectors is calculated by calculating the cosine angle between two vectors. In similar lines, we can calculate cosine angle between each document vector and the query vector to find its closeness. To find relevant document to the query term , we may calculate the similarity score between each document vector and the query term vector by applying cosine similarity . Finally, whichever documents having high similarity scores will be considered as relevant documents to the query term.&lt;br /&gt;
&lt;br /&gt;
When we plot the term document matrix, each document vector represents a point in the vector space. In the below example query, Document 1 and Document 2 represent 3 points in the vector space. We can now compare the query with each of the document by calculating the cosine angle between them. &lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-7wz8-YP_bT6gLx2HKGSkvjKEDnJmEYMSMfTFnbOe3QqgroEMByLbsUxROrVgxlk94gIgAAX9HBs5VOaxMRgNG6vsbENvzSUQ-H6ZzbhKSSGsu0UYvokV4MvbOGTWKYHKYbS8gDPJ7HrH/s1600/cosine_similarity.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;300&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-7wz8-YP_bT6gLx2HKGSkvjKEDnJmEYMSMfTFnbOe3QqgroEMByLbsUxROrVgxlk94gIgAAX9HBs5VOaxMRgNG6vsbENvzSUQ-H6ZzbhKSSGsu0UYvokV4MvbOGTWKYHKYbS8gDPJ7HrH/s320/cosine_similarity.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;div class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;cosine similarity&lt;/i&gt;&lt;/div&gt;
&lt;br /&gt;
Apart from cosine similarity, we have other variants for calculating the similarity scores and are shown below: &lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Jaccard distance&lt;/li&gt;
&lt;li&gt;Kullback-Leibler divergence&lt;/li&gt;
&lt;li&gt;Euclidean distance&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
Now that we have learnt the important concepts required for implementing our problem statement, we now look at the data which will be used in this post and its implementation in R programming language.&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Data description:&lt;/h3&gt;
&lt;br /&gt;
For this post, we use 9 text files containing news articles and a query file containing search queries. Our task is to find top-3 news articles relevant to each of the query in the queries files. &lt;br /&gt;
The dataset which we will be using is uploaded to GitHub and is located at below location:
&lt;br /&gt;
https://github.com/sureshgorakala/machinelearning/tree/master/data
&lt;br /&gt;
&lt;br /&gt;
The news articles data is available in txt files as shown in below image:
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhza0CXEaYLsnGhiwV_L5u1vJzG1Lvj-a3aW-g5WR3xzt2xdwTSiSF3lXCmDvGLkXkFLDXPPdp8zn7ZUOvY1W4ruO6GMlIlgqTLzUtSTRqmYrp-v9XEwzXj3VIYUHheqV4hozI4jgps6-Bp/s1600/information+retrieval_6.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhza0CXEaYLsnGhiwV_L5u1vJzG1Lvj-a3aW-g5WR3xzt2xdwTSiSF3lXCmDvGLkXkFLDXPPdp8zn7ZUOvY1W4ruO6GMlIlgqTLzUtSTRqmYrp-v9XEwzXj3VIYUHheqV4hozI4jgps6-Bp/s400/information+retrieval_6.PNG&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;
Below is the snippet of news article in the first txt file baract_hussein_obama.txt,
&lt;br /&gt;
&lt;i&gt;“Barack Hussein Obama II (US Listeni/bəˈrɑːk huːˈseɪn oʊˈbɑːmə/;[1][2] born August 4, 1961) is the 44th and current President of the United States. He is the first African American to hold the office and the first president born outside the continental United States. Born in Honolulu, Hawaii, Obama is a graduate of Columbia University and Harvard Law School, where he was president of the Harvard Law Review. He was a community organizer in Chicago before earning his law degree. He worked as a civil rights attorney and taught constitutional law at the University of Chicago Law School between 1992 and 2004. While serving three terms representing the 13th District in the Illinois Senate from 1997 to 2004, he ran unsuccessfully in the Democratic primary for the United States House of Representatives in 2000 against incumbent Bobby Rush ….”&lt;/i&gt;
&lt;br /&gt;
Below are the sample queries to which we will extract relevant documents, available in query.txt file, is shown below: 
&lt;br /&gt;
&lt;i&gt;“largest world economy&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;barack obama&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;united state president&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;donald trump and united state&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;donald trump and barack obama&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;current President of the United States”&lt;/i&gt;
&lt;br /&gt;
our task is to create a system in which for each of the query terms retrieve top-3 relevant documents.
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
High level system design:&lt;/h3&gt;
&lt;br /&gt;
In this section we show the high-level design implementation. The implementation steps are as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Load documents and search queries into the R programming environment as list objects.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Preprocess the data by creating a corpus object with all the documents and query terms, removing stop words, punctuations using tm package.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmUJ6XpAqwyUCXXqXlYVov39YV6Hk03wJdCgHtN5oGJdYA5Aubr2Pyt-lG9L0Nue5ukhLVuBz8mwFRbiQ6gFSTRx5xUt4iGEq6XPzNsgrL3Oy-ALYbNfceI7IcLg4IgfG2ZC-qSjrNr9jm/s1600/information+retrieval_2.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmUJ6XpAqwyUCXXqXlYVov39YV6Hk03wJdCgHtN5oGJdYA5Aubr2Pyt-lG9L0Nue5ukhLVuBz8mwFRbiQ6gFSTRx5xUt4iGEq6XPzNsgrL3Oy-ALYbNfceI7IcLg4IgfG2ZC-qSjrNr9jm/s400/information+retrieval_2.PNG&quot; /&gt;&lt;/a&gt;
&lt;/div&gt;
&lt;div class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;high level information retrieval system &lt;/i&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Creating a term document matrix with tf-idf weight setting available in TermDocumentMatrix()  method.&lt;/li&gt;
&lt;li&gt;Separate the term document matrix into two parts- one containing all the documents with term weights and other containing all the queries with term weights.&lt;/li&gt;
&lt;li&gt;Now calculate cosine similarity between each document and each query.&lt;/li&gt;
&lt;li&gt;For each query sort the cosine similarity scores for all the documents and take top-3 documents having high scores.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Full code implementation:&lt;/h3&gt;
&lt;br /&gt;
&lt;script src=&quot;https://gist.github.com/sureshgorakala/c990c3cd681b7cecdf57ef8a2ce42005.js&quot;&gt;&lt;/script&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/1237400655120499037/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2017/11/information-retrieval-document-search-using-vector-space-model-in-r.html#comment-form' title='85 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/1237400655120499037'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/1237400655120499037'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2017/11/information-retrieval-document-search-using-vector-space-model-in-r.html' title='information retrieval document search using vector space model in R'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhi7orP1erye959rws2x4I5E0jEGdGU3yN_Z4eGG-vsd7RrS_XDz7uloHowPbWlcyTzkEEfY31kMK3yTOOswkRyHpt4utKtlloFKjCGluqbEWwNeYXJLtKCNcDeJVQRKCBEmedN7gEHV9Cd/s72-c/information+retrieval_1.PNG" height="72" width="72"/><thr:total>85</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-4494756101058617652</id><published>2016-03-18T21:42:00.000+04:00</published><updated>2017-12-02T14:55:08.113+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="apply function r"/><category scheme="http://www.blogger.com/atom/ns#" term="apply r"/><category scheme="http://www.blogger.com/atom/ns#" term="functions R"/><category scheme="http://www.blogger.com/atom/ns#" term="lapply r"/><category scheme="http://www.blogger.com/atom/ns#" term="mapply r"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="r programming"/><category scheme="http://www.blogger.com/atom/ns#" term="sapply r"/><category scheme="http://www.blogger.com/atom/ns#" term="tapply r"/><title type='text'>apply lapply rapply sapply functions in R</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
As part of &lt;a href=&quot;http://www.dataperspective.info/2015/12/datasciencewithR.html&quot; target=&quot;_blank&quot;&gt;Data Science with R&lt;/a&gt;, this is third tutorial after &lt;a href=&quot;http://www.dataperspective.info/2016/02/basic-data-types-in-r.html&quot; target=&quot;_blank&quot;&gt;basic data types&lt;/a&gt;,&lt;a href=&quot;http://www.dataperspective.info/2016/02/control-structures-loops-in-r.html&quot; target=&quot;_blank&quot;&gt;control structures&lt;/a&gt; in r.&lt;br /&gt;
&lt;br /&gt;
One of the issues with for loop is its memory consumption and its slowness in executing a repetitive task at hand. Often dealing with large data and iterating it, for loop is not advised. R provides many few alternatives to be applied on vectors for looping operations. In this section, we deal with&amp;nbsp;&lt;b&gt;apply&lt;/b&gt;&amp;nbsp;function and its variants:&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHcHkj15oYsyShkf6Ro7nW1b0qSA6waN23zO-tP7qDCB_rroZaucfk_cqG_nwZ56vvaP5t6437ugu3SRm3V3i3yAqwHKhuvw600ndu1nUohZxTGcWxOmPpO-GOFrQszdoGbD4ObOXurs_r/s1600/apply_family.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;312&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHcHkj15oYsyShkf6Ro7nW1b0qSA6waN23zO-tP7qDCB_rroZaucfk_cqG_nwZ56vvaP5t6437ugu3SRm3V3i3yAqwHKhuvw600ndu1nUohZxTGcWxOmPpO-GOFrQszdoGbD4ObOXurs_r/s320/apply_family.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;pre&gt;?apply
&lt;/pre&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBzBuo_LfW4cfrX_-PFoTAsJ-9ghtA_uB3yhdX-0XoNuSbPm1ijdaBGMsGVluQwcMlgT4fNJ8drBcFiihO5cZw4ZjCZtH8vVwT-c25cUG3N7xChgmYRUvIrOfcUzmMwFkkZAQ8SwtLBoi7/s1600/apply_r_help.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;204&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiBzBuo_LfW4cfrX_-PFoTAsJ-9ghtA_uB3yhdX-0XoNuSbPm1ijdaBGMsGVluQwcMlgT4fNJ8drBcFiihO5cZw4ZjCZtH8vVwT-c25cUG3N7xChgmYRUvIrOfcUzmMwFkkZAQ8SwtLBoi7/s320/apply_r_help.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt; Datasets for apply family tutorial&lt;/b&gt;&lt;br /&gt;
&lt;pre&gt;&lt;em&gt; For understanding the apply functions in R we use,the data from 1974 Motor Trend
US magazine which comprises fuel consumption and 10 aspects of automobile design and
performance for 32 automobiles (1973–74 models).
&lt;/em&gt; 
data(&quot;mtcars&quot;)
head(mtcars)
                   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;em&gt; 

Reynolds (1994) describes a small part of a study of the long-term temperature dynamics
of beaver Castor canadensis in north-central Wisconsin. Body temperature was measured by
telemetry every 10 minutes for four females, but data from a one period of less than a 
day for each of two animals is used there.&lt;/em&gt; 

data(beavers)
head(t(beaver1)[1:4,1:10])
        [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]   [,8]    [,9]   [,10]
day   346.00 346.00 346.00 346.00 346.00 346.00 346.00 346.00  346.00  346.00
time  840.00 850.00 900.00 910.00 920.00 930.00 940.00 950.00 1000.00 1010.00
temp   36.33  36.34  36.35  36.42  36.55  36.69  36.71  36.75   36.81   36.88
activ   0.00   0.00   0.00   0.00   0.00   0.00   0.00   0.00    0.00    0.00

&lt;/pre&gt;
&lt;b&gt; apply():&lt;/b&gt;&lt;br /&gt;
apply() function is the base function. We will learn how to apply family functions by trying out the code. apply() function takes 3 arguments:
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;data matrix&lt;/li&gt;
&lt;li&gt;row/column operation, - 1 for row wise operation, 2 for column wise operation&lt;/li&gt;
&lt;li&gt;function to be applied on the data.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;em&gt; 
when 1 is passed as second parameter, the function max is applied row wise and gives
us the result. In the below example, row wise maximum value is calculated.Since we 
have four types of attributes we got 4 results.
&lt;/em&gt; 
apply(t(beaver1),1,max) 
    day    time    temp   activ 
 347.00 2350.00   37.53    1.00 

&lt;em&gt; 
When 2 is passed as second  parameter the function  mean is applied column wise.
In the below example mean function is applied on each column and mean for each 
column is calculated. Hence  we can see results for each column.
&lt;/em&gt; 
apply(mtcars,2,mean) 
       mpg        cyl       disp         hp       drat         wt       qsec         vs         am       gear       carb 
 20.090625   6.187500 230.721875 146.687500   3.596563   3.217250  17.848750   0.437500   0.406250   3.687500   2.812500 
&lt;em&gt; 
We can also pass custom function instead of default functions. For example in 
the below example let us divide each column element with modulus of 10.
For this we use a custom function which takes each element from each column and
apply the modulus operation.
&lt;/em&gt; 
head(apply(mtcars,2,function(x) x%%10))
                  mpg cyl disp hp drat    wt qsec vs am gear carb
Mazda RX4         1.0   6    0  0 3.90 2.620 6.46  0  1    4    4
Mazda RX4 Wag     1.0   6    0  0 3.90 2.875 7.02  0  1    4    4
Datsun 710        2.8   4    8  3 3.85 2.320 8.61  1  1    4    1
Hornet 4 Drive    1.4   6    8  0 3.08 3.215 9.44  1  0    3    1
Hornet Sportabout 8.7   8    0  5 3.15 3.440 7.02  0  0    3    2
Valiant           8.1   6    5  5 2.76 3.460 0.22  1  0    3    1
&lt;/pre&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;
lapply():&lt;/b&gt;&lt;br /&gt;
lapply function is applied for operations on list objects and returns a list object of same length of original set.&lt;br /&gt;
lapply function in R, returns a list of the same length as input list object, each element of which is the result of applying FUN to the corresponding element of list.
&lt;br /&gt;
&lt;pre&gt;&lt;em&gt; #create a list with 2 elements&lt;/em&gt; 
l = (a=1:10,b=11:20) &lt;em&gt; # the mean of the value in each element&lt;/em&gt;
lapply(l, mean)
$a
[1] 5.5
$b
[1] 15.5
class(lapply(l, mean))
[1] &quot;list
&lt;em&gt;  # the sum of the values in each element&lt;/em&gt; 
lapply(l, sum)
$a
[1] 55

$b
[1] 155



&lt;/pre&gt;
&lt;b&gt;sapply():&lt;/b&gt;&lt;br /&gt;
sapply is wrapper class to lapply with difference being it returns vector or matrix instead
of list object.&lt;br /&gt;
&lt;pre&gt;&lt;em&gt; 
 # create a list with 2 elements&lt;/em&gt; 
 l = (a=1:10,b=11:20) &lt;em&gt; # mean of values using sapply &lt;/em&gt;
sapply(l, mean)
   a    b 
 5.5 15.5
&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;tapply():&lt;/b&gt;&lt;br /&gt;
tapply() is a very powerful function that lets you break a vector into pieces and then apply some function to each of the pieces. 
In the below code, first each of mpg in mtcars data is grouped by cylinder type and then mean() function is calculated.
&lt;br /&gt;
&lt;pre&gt;str(mtcars$cyl)
 num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
levels(as.factor(mtcars$cyl))
[1] &quot;4&quot; &quot;6&quot; &quot;8&quot;
&lt;em&gt;
In the dataset we have 3 types of cylinders and now we want to see the average mpg
for each cylinder type.
&lt;/em&gt;
tapply(mtcars$mpg,mtcars$cyl,mean)
       4        6        8 
26.66364 19.74286 15.10000 

&lt;em&gt;In the output above we see that the average mpg for 4 cylinder engine 
is 26.664, 6-cyinder engine is 19.74 and 8-cylinder engine is 15.10
&lt;/em&gt;&lt;/pre&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;by():&lt;/b&gt;&lt;br /&gt;
by works similar to group by function in SQL, applied to factors, where in we may apply operations on individual results set.
In the below example, we apply colMeans() function to all the observations on iris dataset grouped by Species.
&lt;br /&gt;
&lt;pre&gt;data(iris) 
&#39;data.frame&#39;: 150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels &quot;setosa&quot;,&quot;versicolor&quot;,..: 1 1 1 1 1 1 1 1 1 1 ...

by(iris[,1:4],iris$Species,colMeans)
iris$Species: setosa
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
       5.006        3.428        1.462        0.246 
------------------------------------------------------------------------------------ 
iris$Species: versicolor
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
       5.936        2.770        4.260        1.326 
------------------------------------------------------------------------------------ 
iris$Species: virginica
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
       6.588        2.974        5.552        2.026 
&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;rapply():&lt;/b&gt;&lt;br /&gt;
rapply() is a recursive version of lapply.&lt;br /&gt;
rapply() applies a function recursively on each element of the list with 2 modes for &quot;how&quot; parameter.
If how = &quot;replace&quot;, each element of the list which is not itself a list and has a class included in classes
is replaced by the result of applying f to the element.If the mode is how = &quot;list&quot; or how = &quot;unlist&quot;,
the list is copied, all non-list elements which have a class included in classes are replaced by the result 
of applying f to the element and all others are replaced by deflt. Finally, if how = &quot;unlist&quot;, unlist(recursive = TRUE) 
is called on the result.&lt;br /&gt;
&#39;&lt;br /&gt;
&lt;pre&gt;l2 = list(a = 1:10, b = 11:20,c=c(&#39;d&#39;,&#39;a&#39;,&#39;t&#39;,&#39;a&#39;))
l2
$a
 [1]  1  2  3  4  5  6  7  8  9 10

$b
 [1] 11 12 13 14 15 16 17 18 19 20

$c
[1] &quot;d&quot; &quot;a&quot; &quot;t&quot; &quot;a&quot;

rapply(l2, mean, how = &quot;list&quot;, classes = &quot;integer&quot;)
$a
[1] 5.5

$b
[1] 15.5

$c
NULL

rapply(l2, mean, how = &quot;unlist&quot;, classes = &quot;integer&quot;)
 a    b 
 5.5 15.5 
 
rapply(l2, mean, how = &quot;replace&quot;, classes = &quot;integer&quot;)
$a
[1] 5.5

$b
[1] 15.5

$c
[1] &quot;d&quot; &quot;a&quot; &quot;t&quot; &quot;a&quot;
&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;mapply():&lt;/b&gt;&lt;br /&gt;
mapply is a multivariate version of sapply.
By R definition, mapply is a multivariate version of sapply. mapply applies FUN to the first elements of each ... argument,
the second elements, the third elements, and so on. Arguments are recycled if necessary.
Its purpose is to be able to vectorize arguments to a function that is not usually accepting vectors as arguments.
In short, mapply applies a Function to Multiple List or multiple Vector Arguments.
In the below example word function is applied to vector argument LETTERS. 
&#39;
&lt;br /&gt;
&lt;pre&gt;word = function(C, k) paste(rep.int(C, k), collapse = &quot;&quot;)
utils::str(mapply(word, LETTERS[1:6], 6:1, SIMPLIFY = FALSE))
List of 6
 $ A: chr &quot;AAAAAA&quot;
 $ B: chr &quot;BBBBB&quot;
 $ C: chr &quot;CCCC&quot;
 $ D: chr &quot;DDD&quot;
 $ E: chr &quot;EE&quot;
 $ F: chr &quot;F&quot;
 &lt;/pre&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/4494756101058617652/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2016/03/apply-lapply-rapply-sapply-functions-r.html#comment-form' title='64 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/4494756101058617652'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/4494756101058617652'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2016/03/apply-lapply-rapply-sapply-functions-r.html' title='apply lapply rapply sapply functions in R'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHcHkj15oYsyShkf6Ro7nW1b0qSA6waN23zO-tP7qDCB_rroZaucfk_cqG_nwZ56vvaP5t6437ugu3SRm3V3i3yAqwHKhuvw600ndu1nUohZxTGcWxOmPpO-GOFrQszdoGbD4ObOXurs_r/s72-c/apply_family.PNG" height="72" width="72"/><thr:total>64</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-2208581808708745212</id><published>2016-02-27T13:51:00.000+04:00</published><updated>2019-02-24T09:10:52.505+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="break statement R"/><category scheme="http://www.blogger.com/atom/ns#" term="for loop statement R"/><category scheme="http://www.blogger.com/atom/ns#" term="functions R"/><category scheme="http://www.blogger.com/atom/ns#" term="ifelse statement R"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="repeat statement R"/><category scheme="http://www.blogger.com/atom/ns#" term="while statement R"/><title type='text'>Control Structures Loops in R</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
As part of Data Science tutorial Series in my previous post I posted on &lt;a href=&quot;http://www.dataperspective.info/2016/02/basic-data-types-in-r.html&quot;&gt;basic data types in R&lt;/a&gt;. I have kept the tutorial very simple so that beginners of R programming &amp;nbsp;may takeoff immediately. &lt;br /&gt;
Please find the online R editor at the end of the post so that you can execute the code on the page itself.&lt;br /&gt;
In this section we learn about control structures loops used in R. Control strcutures in R contains conditionals, loop statements like any other programming languages.&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAa2P3njeeawpY_5XW_QI6VZhhyphenhyphenaLZYx6_O13vScdYErB11mZ6fc3-iV5mzAGnZ3EXGKZcAjXcjgHJ4k_f2hZueHEpWUHPlxiwb0mDnRv-z9fEOHKuBbP69Wzcojit5AuuI5v0GKPJj6CN/s1600/control_structures+in+R.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;313&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAa2P3njeeawpY_5XW_QI6VZhhyphenhyphenaLZYx6_O13vScdYErB11mZ6fc3-iV5mzAGnZ3EXGKZcAjXcjgHJ4k_f2hZueHEpWUHPlxiwb0mDnRv-z9fEOHKuBbP69Wzcojit5AuuI5v0GKPJj6CN/s320/control_structures+in+R.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Loops are very important and forms backbone to any programming languages.Before we get into the control structures in R, just type as below in &lt;a href=&quot;http://www.rstudio.com/&quot;&gt;Rstudio&lt;/a&gt;:
&lt;br /&gt;
&lt;pre&gt; ?control 
&lt;/pre&gt;
&lt;pre&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAP4Uxi81PuN_dD7SKZcLDaNdqZ_3xiOIowIS5TT3HQbXpLxmzPWrCDntkqvdFyId275L0LnO-ZFEBMbp6jIpZVwHhvHUsoSIbR8ZOwnrjPBjiuR4f78PCp7wZlm5OXF0kkifDDLVGc_QI/s1600/control_structure_r.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAP4Uxi81PuN_dD7SKZcLDaNdqZ_3xiOIowIS5TT3HQbXpLxmzPWrCDntkqvdFyId275L0LnO-ZFEBMbp6jIpZVwHhvHUsoSIbR8ZOwnrjPBjiuR4f78PCp7wZlm5OXF0kkifDDLVGc_QI/s1600/control_structure_r.PNG&quot; /&gt;&lt;/a&gt;&lt;/pre&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;If else statement:&lt;/b&gt;&lt;br /&gt;
&lt;pre&gt;&lt;em&gt;#See the code syntax below for if else statement&lt;/em&gt; 
if(x&amp;gt;1){
 print(&quot;x is greater than 1&quot;)
 }else{
  print(&quot;x is less than 1&quot;)
  } 

&lt;em&gt;#See the code below for nested if else statement&lt;/em&gt;

 x=10
  x=10
 if(x&amp;gt;1 &amp;amp; x&amp;lt;7){
     print(&quot;x is between 1 and 7&quot;)}else if(x&amp;gt;8 &amp;amp; x&amp;lt; 15){
         print(&quot;x is between 8 and 15&quot;)
     }

[1] &quot;x is between 8 and 15&quot; 
&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;For loops:&lt;/b&gt;&lt;br /&gt;
As we know for loops are used for iterating items
&lt;br /&gt;
&lt;pre&gt; &lt;em&gt;#Below code shows for  loop implementation&lt;/em&gt;
x = c(1,2,3,4,5)
 for(i in 1:5){
     print(x[i])
 }
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;b&gt;While loop :&lt;/b&gt;
&lt;br /&gt;
&lt;pre&gt; &lt;em&gt;#Below code shows while loop in R&lt;/em&gt;
x = 2.987
while(x &amp;lt;= 4.987) { 
     x = x + 0.987
     print(c(x,x-2,x-1)) 
 }
[1] 3.974 1.974 2.974
[1] 4.961 2.961 3.961
[1] 5.948 3.948 4.948
&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;b&gt;Repeat Loop:&lt;/b&gt;&lt;br /&gt;
The repeat loop is an infinite loop and used in association with a break statement.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt; &lt;em&gt;#Below code shows repeat loop:&lt;/em&gt;
a = 1
 repeat { print(a) a = a+1 if(a &amp;gt; 4) break }
[1] 1
[1] 2
[1] 3
[1] 4
&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;b&gt;Break statement:&lt;/b&gt;&lt;br /&gt;
A break statement is used in a loop to stop the iterations and flow the control outside of the loop. &lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt; &lt;em&gt;#Below code shows break statement:&lt;/em&gt;
x = 1:10 
 for (i in x){ 
     if (i == 2){ 
         break 
     }
     print(i)
 }
[1] 1
&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;b&gt;Next statement:&lt;/b&gt;&lt;br /&gt;
Next statement enables to skip the current iteration of a loop without terminating it. &lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt; &lt;em&gt;#Below code shows next statement &lt;/em&gt;
x = 1: 4 
 for (i in x) { 
     if (i == 2){ 
         next}
     print(i)
 }
[1] 1
[1] 3
[1] 4
&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;b&gt;Creating a function in R:&lt;/b&gt;&lt;br /&gt;
function() is a built-in R function whose job is to create functions. In the below example function() takes one parameter x, executes a for loop logic.&lt;br /&gt;
The function object thus created using function() is assigned to a variable (&#39;words.names&#39;). Now this created function will be called using the variable &#39;word.names&#39;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt; &lt;em&gt;#Below code shows us, how a function is created in R:

Syntax: 
function_name = function(parameters,..){ code}
 &lt;/em&gt;
words = c(&quot;R&quot;, &quot;datascience&quot;, &quot;machinelearning&quot;,&quot;algorithms&quot;,&quot;AI&quot;) 
words.names = function(x) {
     for(name in x){ 
         print(name) 
     }
} 
&lt;em&gt;#Calling the function&lt;/em&gt;
 words.names(words)
[1] &quot;R&quot;
[1] &quot;datascience&quot;
[1] &quot;machinelearning&quot;
[1] &quot;algorithms&quot;
[1] &quot;AI&quot;
&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;Hands on exercise of what we have learnt so far&lt;/b&gt;&lt;/div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;br /&gt;
&lt;pre&gt;We create a data frame DF, run for loop, ifelse in a function and call the function
&lt;em&gt;#create 3 vectors name,age,salary&lt;/em&gt;
name = c(&quot;David&quot;,&quot;John&quot;,&quot;Mathew&quot;)
age = c(30,40,50)
salary = c(30000,120000,55000)
&lt;em&gt;#create a data frame DF by combining the 3 vectors using cbind() function&lt;/em&gt;
DF = data.frame(cbind(name,age,salary))
&lt;em&gt;#display DF&lt;/em&gt;
DF
    name age salary
1  David  30  30000
2   John  40 120000
3 Mathew  50  55000
&lt;em&gt;#dimensions of DF&lt;/em&gt;
 dim(DF)
[1] 3 3
 
&lt;em&gt;#write a function which displays the salaried person name&lt;/em&gt;
findHighSalary = function(df){
     Maxsal = 0
     empname = &quot;&quot;
     for(i in 1:nrow(DF)){
         tmpsal = as.numeric(DF[i,3] )
         if(tmpsal &amp;gt; Maxsal){
             Maxsal = tmpsal
             empname = DF[i,1]
         }
     }
     return(as.character(empname))
 }
&lt;em&gt;#calling the function&lt;/em&gt;
findHighSalary(DF)
[1] &quot;Mathew&quot;
&lt;/pre&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/2208581808708745212/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2016/02/control-structures-loops-in-r.html#comment-form' title='83 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/2208581808708745212'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/2208581808708745212'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2016/02/control-structures-loops-in-r.html' title='Control Structures Loops in R'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAa2P3njeeawpY_5XW_QI6VZhhyphenhyphenaLZYx6_O13vScdYErB11mZ6fc3-iV5mzAGnZ3EXGKZcAjXcjgHJ4k_f2hZueHEpWUHPlxiwb0mDnRv-z9fEOHKuBbP69Wzcojit5AuuI5v0GKPJj6CN/s72-c/control_structures+in+R.PNG" height="72" width="72"/><thr:total>83</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-5609405768670421758</id><published>2016-02-27T12:18:00.000+04:00</published><updated>2017-10-17T07:39:50.876+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Curse of Dimensionality"/><category scheme="http://www.blogger.com/atom/ns#" term="dimensionality reduction"/><category scheme="http://www.blogger.com/atom/ns#" term="feature extraction"/><category scheme="http://www.blogger.com/atom/ns#" term="image processing"/><category scheme="http://www.blogger.com/atom/ns#" term="machine learning"/><category scheme="http://www.blogger.com/atom/ns#" term="matrix"/><category scheme="http://www.blogger.com/atom/ns#" term="PCA"/><category scheme="http://www.blogger.com/atom/ns#" term="principal component analysis"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="Recommendation Engine"/><category scheme="http://www.blogger.com/atom/ns#" term="speech recognition"/><category scheme="http://www.blogger.com/atom/ns#" term="text processing"/><title type='text'>Principal Component Analysis using R</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;Curse of Dimensionality:&lt;/b&gt;&lt;br /&gt;
One of the most commonly faced problems while dealing with data analytics problem such as recommendation engines, text analytics is high-dimensional and sparse data. At many times, we face a situation where we have a large set of features and fewer data points, or we have data with very high feature vectors.  In such scenarios, fitting a model to the dataset, results in lower predictive power of the model. This scenario is often termed as the curse of dimensionality. In general, adding more data points or decreasing the feature space, also known as dimensionality reduction, often reduces the effects of the curse of dimensionality. 
&lt;br /&gt;
In this blog, we will discuss about principal component analysis, a popular dimensionality reduction technique. PCA is a useful statistical method that has found application in a variety of fields and is a common technique for finding patterns in data of high dimension.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFAnP8hiBQTVRKwekGtTPJQVamw29CfBEE2KzhZYb8XJKzl2K62gv531h6xU88ypMa7FKcDrLT-FJqwbqWxNBh_Y08QroGsq8Zhg7KBeIHkpJ-Dg8K52vS4obLyuxHHt2IZEtpcwn4RaQS/s1600/applications+of+pca.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFAnP8hiBQTVRKwekGtTPJQVamw29CfBEE2KzhZYb8XJKzl2K62gv531h6xU88ypMa7FKcDrLT-FJqwbqWxNBh_Y08QroGsq8Zhg7KBeIHkpJ-Dg8K52vS4obLyuxHHt2IZEtpcwn4RaQS/s1600/applications+of+pca.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;Principal component analysis:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&lt;/b&gt;&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/div&gt;
Consider below scenario:&lt;br /&gt;
The data, we want to work with, is in the form of a matrix A of mXn dimension, shown as below, where Ai,j represents the value of the i-th observation of the j-th variable. &lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEge-J2h7qKOcv1BhKpnwwz58GvtqKcYmxUyvDuEAhl4c-kSGOsOkl85mDsNpjz153MfCC5JA3BR5RSdj3WJfd57EbF6z-S51Ly8ZZ3xeORHY3yOAVQ2D3NvR3TcIJFOHwGlm_RyhiMlgIZt/s1600/matrix.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEge-J2h7qKOcv1BhKpnwwz58GvtqKcYmxUyvDuEAhl4c-kSGOsOkl85mDsNpjz153MfCC5JA3BR5RSdj3WJfd57EbF6z-S51Ly8ZZ3xeORHY3yOAVQ2D3NvR3TcIJFOHwGlm_RyhiMlgIZt/s1600/matrix.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
Thus the N members of the matrix can be identified with the M rows, each variable corresponding to N-dimensional vectors. If N is very large it is often desirable to reduce the number of variables to a smaller number of variables, say k variables as in the image below, while losing as little information as possible. &lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVorb6tXgleiV4lK1GOEzJvXZjPVEvnBVXLok86eJ9tJQ8Liv5L624yPWvDtnvNLEjNxhZk1RcPZfhTlbfwFkh66Wog6y80H7Sc6Bczqt9l6ekArJN52Prdnvy8sN-tZyKw0Q-HsKrvPfo/s1600/dimesionality_reduction.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVorb6tXgleiV4lK1GOEzJvXZjPVEvnBVXLok86eJ9tJQ8Liv5L624yPWvDtnvNLEjNxhZk1RcPZfhTlbfwFkh66Wog6y80H7Sc6Bczqt9l6ekArJN52Prdnvy8sN-tZyKw0Q-HsKrvPfo/s1600/dimesionality_reduction.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
Mathematically spoken, PCA is a linear orthogonal transformation that transforms the data to a new coordinate system such that the greatest variance by any projection of the data comes to lie on the first coordinate (called the first principal component), the second greatest variance on the second coordinate, and so on.&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAG7q7ECVPDebVDGZNchw6LgyRVrLtuhH3_19RKPfkRzqpXCiibO15ylUX7AyteeLc10J2nl0b7H9nPF39xO2bDCbBM-o_3w4YW1ragNc7_sKDh7vP14sL8mpHcO0LXztdK1dQs5MLOltv/s1600/orthogonal_projection.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAG7q7ECVPDebVDGZNchw6LgyRVrLtuhH3_19RKPfkRzqpXCiibO15ylUX7AyteeLc10J2nl0b7H9nPF39xO2bDCbBM-o_3w4YW1ragNc7_sKDh7vP14sL8mpHcO0LXztdK1dQs5MLOltv/s400/orthogonal_projection.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
The algorithm when applied linearly transforms m-dimensional input space to n-dimensional (n &amp;lt; m) output space, with the objective to minimize the amount of information/variance lost by discarding (m-n) dimensions. PCA allows us to discard the variables/features that have less variance.&lt;br /&gt;
Technically speaking, PCA uses orthogonal projection of highly correlated variables to a set of values of linearly uncorrelated variables called principal components. The number of principal components is less than or equal to the number of original variables. This linear transformation is defined in such a way that the first principal component has the largest possible variance. It accounts for as much of the variability in the data as possible by considering highly correlated features. Each succeeding component in turn has the highest variance using the features that are less correlated with the first principal component and that are orthogonal to the preceding component.&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKNKh8ceFXo3lTwEr3r0Vo0CXRNcsOETII8iqHADiw6ASjc4Y58IiA3yjH4WufKi3-94YkkpRA1teEZ15Dteq1i2BCsiQ9vU-ScN7UBmOEqxa9T5XRlgPC54Hdz3Ls20Dc_Zvna8yByhEC/s1600/PCA.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;244&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKNKh8ceFXo3lTwEr3r0Vo0CXRNcsOETII8iqHADiw6ASjc4Y58IiA3yjH4WufKi3-94YkkpRA1teEZ15Dteq1i2BCsiQ9vU-ScN7UBmOEqxa9T5XRlgPC54Hdz3Ls20Dc_Zvna8yByhEC/s320/PCA.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
In the above image, u1 &amp;amp; u2 are principal components wherein u1 accounts for highest variance in the dataset and u2 accounts for next highest variance and is orthogonal to u1.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;PCA implementation in R:&lt;/b&gt;&lt;/div&gt;
For today’s post we use crimtab dataset available in R. Data of 3000 male criminals over 20 years old undergoing their sentences in the chief prisons of England and Wales.
The 42 row names (&quot;9.4&quot;, 9.5&quot; ...) correspond to midpoints of intervals of finger lengths whereas the 22 column names (&quot;142.24&quot;, &quot;144.78&quot;...) correspond to (body) heights of 3000 criminals, see also below.
&lt;br /&gt;
&lt;pre&gt;head(crimtab)
    142.24 144.78 147.32 149.86 152.4 154.94 157.48 160.02 162.56 165.1 167.64 170.18 172.72 175.26 177.8 180.34
9.4      0      0      0      0     0      0      0      0      0     0      0      0      0      0     0      0
9.5      0      0      0      0     0      1      0      0      0     0      0      0      0      0     0      0
9.6      0      0      0      0     0      0      0      0      0     0      0      0      0      0     0      0
9.7      0      0      0      0     0      0      0      0      0     0      0      0      0      0     0      0
9.8      0      0      0      0     0      0      1      0      0     0      0      0      0      0     0      0
9.9      0      0      1      0     1      0      1      0      0     0      0      0      0      0     0      0
    182.88 185.42 187.96 190.5 193.04 195.58
9.4      0      0      0     0      0      0
9.5      0      0      0     0      0      0
9.6      0      0      0     0      0      0
9.7      0      0      0     0      0      0
9.8      0      0      0     0      0      0
9.9      0      0      0     0      0      0
 dim(crimtab)
[1] 42 22
str(crimtab)
 &#39;table&#39; int [1:42, 1:22] 0 0 0 0 0 0 1 0 0 0 ...
 - attr(*, &quot;dimnames&quot;)=List of 2
  ..$ : chr [1:42] &quot;9.4&quot; &quot;9.5&quot; &quot;9.6&quot; &quot;9.7&quot; ...
  ..$ : chr [1:22] &quot;142.24&quot; &quot;144.78&quot; &quot;147.32&quot; &quot;149.86&quot; ...

sum(crimtab)
[1] 3000

colnames(crimtab)
 [1] &quot;142.24&quot; &quot;144.78&quot; &quot;147.32&quot; &quot;149.86&quot; &quot;152.4&quot;  &quot;154.94&quot; &quot;157.48&quot; &quot;160.02&quot; &quot;162.56&quot; &quot;165.1&quot;  &quot;167.64&quot; &quot;170.18&quot; &quot;172.72&quot; &quot;175.26&quot; &quot;177.8&quot;  &quot;180.34&quot;
[17] &quot;182.88&quot; &quot;185.42&quot; &quot;187.96&quot; &quot;190.5&quot;  &quot;193.04&quot; &quot;195.58&quot;
&lt;/pre&gt;
&lt;br /&gt;
let us use apply() to the crimtab dataset row wise to calculate the variance to see how each variable is varying.&lt;br /&gt;
&lt;pre&gt;apply(crimtab,2,var)
&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNLoVakFufnxz4JVjqnq5NAHuHJ_BTM1Nb8OKelSqF_OjgwKbv7Mi70pMP-akzLyRsVx281wxVzYF4bJxtnAAKzZMPvGZ2yMR4M9BqXGL75CSeIhwbiWeyDwUHI7XRFJmcBwayeTUPaEwd/s1600/variance_analysis.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNLoVakFufnxz4JVjqnq5NAHuHJ_BTM1Nb8OKelSqF_OjgwKbv7Mi70pMP-akzLyRsVx281wxVzYF4bJxtnAAKzZMPvGZ2yMR4M9BqXGL75CSeIhwbiWeyDwUHI7XRFJmcBwayeTUPaEwd/s1600/variance_analysis.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
We observe that column “165.1” contains maximum variance in the data.
Applying PCA using prcomp().
&lt;br /&gt;
&lt;pre&gt;pca =prcomp(crimtab)
&lt;div style=&quot;text-align: center;&quot;&gt;
pca&lt;/div&gt;
&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgClg7-YDznm38cTVxpbWTSKlJVp3vjqjDgffMGP73SFEMLzgaPCWpsYQwDbXjhDpk_hnTsdNPXUvba_8iO5qyvFGR4Moy8BFmR4yiieak632LNHHSd-fLH9_o4-TCR7C6CnZY0BrdfZEZX/s1600/principle_components.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em; text-align: center;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgClg7-YDznm38cTVxpbWTSKlJVp3vjqjDgffMGP73SFEMLzgaPCWpsYQwDbXjhDpk_hnTsdNPXUvba_8iO5qyvFGR4Moy8BFmR4yiieak632LNHHSd-fLH9_o4-TCR7C6CnZY0BrdfZEZX/s1600/principle_components.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;em&gt;Note: the resultant components of pca object from the above code corresponds to the standard deviations and Rotation. From the above standard deviations we can observe that the 1st PCA explained most of the variation, followed by other pcas’.  Rotation contains the principal component loadings matrix values which explains /proportion of each variable along each principal component.&lt;/em&gt;&lt;/div&gt;
&lt;/pre&gt;
&lt;br /&gt;
Let’s plot all the principal components and see how the variance is accounted with each component.&lt;br /&gt;
&lt;pre&gt;par(mar = rep(2, 4))
plot(pca)
&lt;/pre&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUkglGbrqvI_nSb0J6Q5FBTTaI7yK4AL3FxebRrllTQ3_f47sYwp9EOQsMW6CgtfDtvlmtmXyY0iELNA_AktDzBcW0WJFOwzncKuGWbD9r4-UBHJ4ptgaVjx9yN7Sg_4B3gDSUdMAHIFrP/s1600/biplot_pca.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUkglGbrqvI_nSb0J6Q5FBTTaI7yK4AL3FxebRrllTQ3_f47sYwp9EOQsMW6CgtfDtvlmtmXyY0iELNA_AktDzBcW0WJFOwzncKuGWbD9r4-UBHJ4ptgaVjx9yN7Sg_4B3gDSUdMAHIFrP/s1600/biplot_pca.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
Clearly the first principal component accounts for maximum information.&lt;br /&gt;
Let us interpret the results of pca using biplot graph. Biplot is used to show the proportions of each variable along the two principal components.&lt;br /&gt;
&lt;pre&gt;&lt;em&gt;#below code changes the directions of the biplot, if we donot include the below two lines the plot will be mirror image to the below one.&lt;/em&gt;
pca$rotation=-pca$rotation
pca$x=-pca$x
biplot (pca , scale =0)
&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
The output of the preceding code is as follows:&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimwNr2wJ9bzpecKYdUecjjU9MLU9cBcY7HG_C-9rAInrpVLWh61mALmStffaNY_iPomtbT-0Me3PLQGthWuC9bvUK5KWzwoE6yvCJ8P6o5RPha2HtV1LYmNHZBRxJrpFnxS1oF4j8qLFU1/s1600/interpret_pca.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimwNr2wJ9bzpecKYdUecjjU9MLU9cBcY7HG_C-9rAInrpVLWh61mALmStffaNY_iPomtbT-0Me3PLQGthWuC9bvUK5KWzwoE6yvCJ8P6o5RPha2HtV1LYmNHZBRxJrpFnxS1oF4j8qLFU1/s1600/interpret_pca.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
In the preceding image, known as a biplot, we can see the two principal components (PC1 and PC2) of the crimtab dataset. The red arrows represent the loading vectors, which represent how the feature space varies along the principal component vectors.&lt;br /&gt;
From the plot, we can see that the first principal component vector, PC1, more or less
places equal weight on three features: 165.1, 167.64, and 170.18. This means that
these three features are more correlated with each other than the 160.02 and 162.56 features.
&lt;br /&gt;
In the second principal component, PC2 places more weight on 160.02, 162.56 than the
3 features, &quot;165.1, 167.64, and 170.18&quot; which are less correlated with them.
&lt;br /&gt;
&lt;b&gt;Complete Code for PCA implementation in R:&lt;/b&gt;
&lt;br /&gt;
&lt;script src=&quot;https://gist.github.com/sureshgorakala/6e335dec0606f3c519df.js&quot;&gt;&lt;/script&gt;
  &lt;noscript&gt;
    &lt;pre&gt;
       //Your code here
       //Be sure you escape your tags with &lt; and &gt; when displaying things like HTML.
    &lt;/pre&gt;
&lt;/noscript&gt;
&lt;br /&gt;
So by now we understood how to run the PCA, and how to interpret the principal components, where do we go from here? How do we apply the reduced variable dataset?
In our next post we shall answer the above questions.
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/5609405768670421758/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2016/02/principal-component-analysis-using-r.html#comment-form' title='23 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/5609405768670421758'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/5609405768670421758'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2016/02/principal-component-analysis-using-r.html' title='Principal Component Analysis using R'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFAnP8hiBQTVRKwekGtTPJQVamw29CfBEE2KzhZYb8XJKzl2K62gv531h6xU88ypMa7FKcDrLT-FJqwbqWxNBh_Y08QroGsq8Zhg7KBeIHkpJ-Dg8K52vS4obLyuxHHt2IZEtpcwn4RaQS/s72-c/applications+of+pca.png" height="72" width="72"/><thr:total>23</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-2879420956628662893</id><published>2016-02-16T19:00:00.001+04:00</published><updated>2017-10-17T07:40:04.811+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="data analysis"/><category scheme="http://www.blogger.com/atom/ns#" term="data frame R"/><category scheme="http://www.blogger.com/atom/ns#" term="data mining"/><category scheme="http://www.blogger.com/atom/ns#" term="data types R"/><category scheme="http://www.blogger.com/atom/ns#" term="integer R"/><category scheme="http://www.blogger.com/atom/ns#" term="list R"/><category scheme="http://www.blogger.com/atom/ns#" term="matrix R"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><title type='text'>Basic Data Types in R </title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div&gt;
As part of tutorial series on Data Science with R from Data Perspective, this first tutorial introduces the very basics of R programming language about basic data types in R.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;What we learn:&lt;/b&gt;&lt;br /&gt;
After the end of the chapter, you are provided with R console so that you can practice what you have learnt in this chapter.&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGcPVWIbuwbLXJzWi4MS2fz-96b1DTueJpqmtRaZ61VgpvecyCMnnL8R8LgfGmjKlocjPWeY_DSAEEb1W-qESiFNDi0lnYg6Esf6ep6XSUJish0PmRP-BqLJPH4nAHRPu1xUEjTZVTjaEY/s1600/basic_data_types_R.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGcPVWIbuwbLXJzWi4MS2fz-96b1DTueJpqmtRaZ61VgpvecyCMnnL8R8LgfGmjKlocjPWeY_DSAEEb1W-qESiFNDi0lnYg6Esf6ep6XSUJish0PmRP-BqLJPH4nAHRPu1xUEjTZVTjaEY/s320/basic_data_types_R.PNG&quot; width=&quot;253&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;details open=&quot;&quot;&gt;
&lt;summary&gt;&lt;b&gt;R assignment operator&lt;/b&gt;&lt;/summary&gt;
&lt;pre&gt;x = &#39;welcome to R programming&#39;&lt;em&gt; # assigning string literal to variable x &lt;/em&gt;
x
[1] &quot;welcome to R programming&quot;
typeof(x) &lt;em&gt;#to check the data type of the variable x&lt;/em&gt;
[1] &quot;character&quot;
&lt;/pre&gt;
&lt;/details&gt;
&lt;details&gt;&lt;summary&gt;
&lt;b&gt;Numeric&lt;/b&gt;&lt;/summary&gt;
Numeric data represents decimal data.&lt;br /&gt;
&lt;pre&gt;x = 1.5 &lt;em&gt;#assigning decimal value1.5 to x&lt;/em&gt;
x
[1] 1.5 
&lt;/pre&gt;
To check the data type we use class() function:&lt;br /&gt; 
&lt;pre&gt;class(x)
[1] &quot;numeric&quot;
&lt;/pre&gt;
To check if the variable “x” is of numerical or not, we use&lt;br /&gt;
&lt;pre&gt;is.numeric(x)
[1] TRUE
&lt;/pre&gt;
To convert any compatible data into numeric, we use:&lt;br /&gt;
&lt;pre&gt; x = &#39;1&#39; &lt;em&gt;#assigning value 1 to variable x&lt;/em&gt;
 class(x)
[1] &quot;character&quot;
 x = as.numeric(x)
[1] 1
 class(x)
[1] &quot;numeric&quot;

Note: if we try to convert a string literal to numeric data type we get the following result.

x= &#39;welcome to R programming&#39;
as.numeric(x)
[1] NA
Warning message:
NAs introduced by coercion
&lt;/pre&gt;
&lt;/details&gt;
&lt;details&gt;&lt;summary&gt;
&lt;b&gt;Integer&lt;/b&gt;&lt;/summary&gt;
We use as.integer() function to convert into integers. This converts numeric value to integer values.&lt;br /&gt;
&lt;pre&gt;x = 1.34
 [1] 1.34
class(x)
[1] &quot;numeric&quot;
y = as.integer(x)
class(y)
[1] &quot;integer&quot;
y
[1] 1
&lt;/pre&gt;
Note: to check if the value is integer or not we use is.integer() function. &lt;br /&gt;
In the below example ‘y’ is numerical or decimal value whereas x is integer.&lt;br /&gt;
&lt;pre&gt; is.integer(y)
[1] TRUE
 is.integer(x)
[1] FALSE
&lt;/pre&gt;
&lt;/details&gt;
&lt;details&gt;&lt;summary&gt;
&lt;b&gt;Complex: &lt;/b&gt;&lt;/summary&gt;
Complex data types are shown as below, though we use it very less in our day to day data analysis:&lt;br /&gt;
&lt;pre&gt;c = 3.5+4i
[1] 3.5+4i
is.complex(c)
[1] TRUE
class(c)
[1] &quot;complex&quot;
&lt;/pre&gt;
&lt;/details&gt;&lt;details&gt;
&lt;summary&gt;&lt;b&gt;Logical&lt;/b&gt;&lt;/summary&gt;
Logical data type is one of the frequently used data type usually used for comparing two values. Values a logical data type takes is TRUE or FALSE. &lt;br /&gt;
&lt;pre&gt;logical = T
logical
[1] TRUE
l = FALSE
l
[1] FALSE
&lt;/pre&gt;
&lt;/details&gt;
&lt;details&gt;&lt;summary&gt;
&lt;b&gt;Character&lt;/b&gt;&lt;/summary&gt;
String literals or string values are stored as Character objects in R. &lt;br /&gt;
&lt;pre&gt;str = &quot;R Programming&quot;
str
[1] &quot;R Programming&quot;
class(str)
[1] &quot;character&quot;
is.character(str)
[1] TRUE
&lt;/pre&gt;
We  can  convert other data types to character data type using as.character() function.&lt;br /&gt;
&lt;pre&gt;x = as.character(1)
x
[1] &quot;1&quot;
class(x)
[1] &quot;character&quot;
&lt;/pre&gt;
Note: There are a variety of operations that can be applied on characters such as substrings, finding lengths; etc will be dealt as when appropriate.&lt;br /&gt;
So far we learnt about the basic data types in R, let’s get into a bit complex data types.&lt;br /&gt;
&lt;/details&gt;
&lt;details&gt;&lt;summary&gt;&lt;b&gt;Vector&lt;/b&gt;&lt;/summary&gt;
How do we hold collection of same data types? We come across this requirement very frequently. We have vector data type to solve this problem.&lt;br /&gt;
Consider a numerical vector below:&lt;br /&gt;
&lt;pre&gt;num_vec = c(1,2,3,4,5)
num_vec
[1] 1 2 3 4 5
class(num_vec)
[1] &quot;numeric&quot;
&lt;/pre&gt;
We can apply many operations on the vector variables such as length, accessing values or members of the vector variable.&lt;br /&gt;
Length of the vector can be found using length() function.&lt;br /&gt;
&lt;pre&gt;length(num_vec)
[1] 5
&lt;/pre&gt;
We access each element or member of the vector num_vec using its indexes starting from &lt;br /&gt;
In the below example we can access the members at 1st,2nd,3rd positons.&lt;br /&gt;
&lt;pre&gt;num_vec[1]
[1] 1
num_vec[2]
[1] 2
num_vec[3]
[1] 3
&lt;/pre&gt;
Similarly string vectors, logical vectors, integer vectors can be created.&lt;br /&gt;
&lt;pre&gt;char_vec = c(&quot;A&quot;, &quot;Course&quot;,&quot;On&quot;,&quot;Data science&quot;,&quot;R rprogramming&quot;)
char_vec
[1] &quot;A&quot;  &quot;Course&quot; &quot;On&quot; &quot;Data science&quot; &quot;R rprogramming&quot;
length(char_vec)
[1] 5
char_vec[1]
[1] &quot;A&quot;
char_vec[2]
[1] &quot;Course&quot;
char_vec[4]
[1] &quot;Data science&quot;
&lt;/pre&gt;
&lt;/details&gt;&lt;details&gt;&lt;summary&gt;&lt;b&gt;Matrix&lt;/b&gt; &lt;/summary&gt;
Matrix data type is used when we want to represent the data as collection of numerical values in mXn, m by n, dimensions. Matrices are used mostly when dealing with mathematical equations, machine learning, text mining algorithms.&lt;br /&gt;
Now how do we create a matrix?&lt;br /&gt;
&lt;pre&gt;m = matrix(c(1,2,3,6,7,8),nrow = 2,ncol = 3)
m
     [,1] [,2] [,3]
[1,]    1    3    7
[2,]    2C    6    8
class(m)
[1] &quot;matrix&quot;
&lt;/pre&gt;
Knowing the dimension of the matrix is:&lt;br /&gt;
&lt;pre&gt;dim(m)
[1] 2 3
&lt;/pre&gt;
How do we access elements of matrix m:&lt;br /&gt;
&lt;pre&gt;&lt;em&gt;#accessing individual elements are done using the indexes shown as below. In the below example we are accessing 1st, 2nd, 6th element of matrix m.&lt;/em&gt;
m[1]
[1] 1
m[2]
[1] 2
m[6]
[1] 8
m[2,3] &lt;em&gt;#  here we accessing 2nd row 3rd column element.&lt;/em&gt;
[1] 8
&lt;em&gt;# accessing all elements of rows of the matrix m shown below. &lt;/em&gt;
m[1,]
[1] 1 3 7
m[2,]
[1] 2 6 8
&lt;em&gt;#accessing all elements of each column&lt;/em&gt;
m[,1]
[1] 1 2
m[,2]
[1] 3 6
m[,3]
[1] 7 8 
&lt;/pre&gt;
What happens when we add different data types to a vector?&lt;br /&gt;
&lt;pre&gt;v = c(&quot;a&quot;,&quot;b&quot;,1,2,3,T)
v
[1] &quot;a&quot;    &quot;b&quot;    &quot;1&quot;    &quot;2&quot;    &quot;3&quot;    &quot;TRUE&quot;
class(v)
[1] &quot;character&quot;
v[6]
[1] &quot;TRUE&quot;
class(v[6])
[1] &quot;character&quot;
&lt;/pre&gt;
What happened in the above example is, R coerced all different data types into a single data type of character type to maintain the condition of single data type.&lt;br /&gt;
&lt;/details&gt;&lt;details&gt;&lt;summary&gt;&lt;b&gt;List&lt;/b&gt;&lt;/summary&gt;
What if we want to handle different data types in a single object?&lt;br /&gt;
List data type helps us in storing elements of different data types in a single object.&lt;br /&gt;
We create list objects using list() function.&lt;br /&gt;
In the below example I have created a list object “list_exp” with 6 different elements of  character, numeric and logical data types.&lt;br /&gt;
&lt;pre&gt;list_exp = list(&quot;r programming&quot;,&quot;data perspective&quot;,12345,67890,TRUE,F)
list_exp
[[1]]
[1] &quot;r programming&quot;
[[2]]
[1] &quot;data perspective&quot;
[[3]]
[1] 12345
[[4]]
[1] 67890
[[5]]
[1] TRUE
[[6]]
[1] FALSE
&lt;/pre&gt;
Using str() function, we can know the structure of the list object, i.e. the internal structure of the list objects can be known. This is one of the very important functions which we use in our day to day analysis.&lt;br /&gt;
In the below example we can see a list of 6 elements of character, numerical and logical data types.&lt;br /&gt;
&lt;pre&gt;str(list_exp)
List of 6
 $ : chr &quot;r programming&quot;
 $ : chr &quot;data perspective&quot;
 $ : num 12345
 $ : num 67890
 $ : logi TRUE
 $ : logi FALSE
&lt;em&gt;#accessing the  data type of list_exp&lt;/em&gt;
class(list_exp)
[1] &quot;list&quot;
length(list_exp)
[1] 6
list_exp[1]
[[1]]
[1] &quot;r programming&quot;
&lt;em&gt;#accessing the list elements using indexing.&lt;/em&gt;
list_exp[[1]]
[1] &quot;r programming&quot;
list_exp[[6]]
[1] FALSE
list_exp[[7]] &lt;em&gt;# when we try accessing not existing elements we get the below error.&lt;/em&gt;
Error in list_exp[[7]] : subscript out of bounds
&lt;em&gt;# finding the class of individual list element&lt;/em&gt;
class(list_exp[[6]])
[1] &quot;logical&quot;
class(list_exp[[3]])
[1] &quot;numeric&quot;
class(list_exp[[1]])
[1] &quot;character&quot;
&lt;/pre&gt;
&lt;/details&gt;&lt;details&gt;&lt;summary&gt;
&lt;b&gt;Data Frame:&lt;/b&gt;&lt;/summary&gt;
Most of us would be from a bit of SQL background and we would be very much comfortable in handling data in the form of SQL table because of the functionalities which a SQL table offers while working the data.&lt;br /&gt;
How would it be if we have such data type object available in R which can be used to store the data and manipulate data in very easy, efficient and convenient way?&lt;br /&gt;
R offers a data frame data type object. It is another way that information is stored as data frames. We can treat a data frame similar to a SQL table.&lt;br /&gt;
How do we create a data frame? &lt;br /&gt;
&lt;pre&gt;&lt;em&gt;#creating a data frame&lt;/em&gt;
data_frame = data.frame(first=c(1,2,3,4),second=c(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;))
data_frame
  first second
     1      a
     2      b
     3      c
     4      d
&lt;em&gt;#accessing  the data type of the object&lt;/em&gt;
class(data_frame)
[1] &quot;data.frame&quot;
&lt;em&gt;#finding out the row count of data_frame using nrow()&lt;/em&gt;
nrow(data_frame)
[1] 4
&lt;em&gt;#finding out the column count of data_frame using ncol()&lt;/em&gt;
ncol(data_frame)
[1] 2
&lt;em&gt;#finding out the dimensions of data_frame using dim()&lt;/em&gt;
dim(data_frame)
[1] 4 2
&lt;em&gt;#finding the structure of the data frame using str()&lt;/em&gt;
str(data_frame)
&#39;data.frame&#39;: 4 obs. of  2 variables:
 $ first : num  1 2 3 4
 $ second: Factor w/ 4 levels &quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;: 1 2 3 4
&lt;em&gt;#accessing the entire row of data frame using row index number. Observe below that if we use data_frame[1,] without specifying the column number it means that we want to access all the columns of row 1.&lt;/em&gt;
data_frame[1,]
  first second
1     1      a
&lt;em&gt;#similarly to access only 1st column values without row information use data_frame[,1] &lt;/em&gt;
data_frame[,1]
[1] 1 2 3 4
&lt;em&gt;#accessing the row names of the data frame.&lt;/em&gt;
rownames(data_frame)
[1] &quot;1&quot; &quot;2&quot; &quot;3&quot; &quot;4&quot;
&lt;em&gt;#accessing the column names of the data frame&lt;/em&gt;
colnames(data_frame)
[1] &quot;first&quot;  &quot;second&quot;
&lt;em&gt;#column data can accessed using the column names explicitly instead of column indexes&lt;/em&gt;
data_frame$first
[1] 1 2 3 4
data_frame$second
[1] a b c d
Levels: a b c d
&lt;em&gt;#accessing individual values using row and column indexes&lt;/em&gt;
data_frame[1,1] &lt;em&gt;# accessing first row first column&lt;/em&gt;
[1] 1
data_frame[2,2] &lt;em&gt;# accessing second row second column&lt;/em&gt;
[1] b
Levels: a b c d
data_frame[3,2]  &lt;em&gt;# accessing third row second column&lt;/em&gt;
[1] c
Levels: a b c d
data_frame[3,1] &lt;em&gt;# accessing third row first column&lt;/em&gt;
[1] 3
&lt;/pre&gt;
Note: Observe the below data frame:&lt;br /&gt;
&lt;pre&gt;dt_frame = data.frame(first=c(1,2,3,4,5,6,7),second=c(&quot;Big data&quot;,&quot;Python&quot;,&quot;R&quot;,&quot;NLP&quot;,&quot;machine learning&quot;,&quot;data science&quot;,&quot;data perspective&quot;))
dt_frame
  first           second
     1         Big data
     2           Python
     3                R
     4              NLP
     5 machine learning
     6     data science
     7 data perspective
&lt;/pre&gt;
Assume we have a dataset with 1000 rows instead of 6 rows shown in above data frame. If we want to see a sample of data of the data frame, how do we do?&lt;br /&gt;
Using head() function.&lt;br /&gt;
&lt;pre&gt;head(dt_frame)
  first           second
     1         Big data
     2           Python
     3                R
     4              NLP
     5 machine learning
     6     data science
&lt;/pre&gt;
head() function returns us the first six rows of any data frame so that we can have a look of what the data frame is like.&lt;br /&gt;
Also we can use tail() function to see the last six rows of the data frame.&lt;br /&gt; 
&lt;pre&gt;tail(dt_frame)
  first           second
     2           Python
     3                R
     4              NLP
     5 machine learning
     6     data science
     7 data perspective
&lt;/pre&gt;
We have View() function to see the values of a data frame in a tabular form.&lt;br /&gt;
View(dt_frame)&lt;br /&gt;
&lt;/details&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;!-- Trigger the modal with a button --&gt;

&lt;br /&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;a class=&quot;btn btn-info btn-xs&quot; data-target=&quot;#dataperspective&quot; data-toggle=&quot;modal&quot; href=&quot;https://www.blogger.com/blogger.g?blogID=603166843861435412&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Try Code&lt;/span&gt;&lt;/a&gt;

    &lt;!-- Modal --&gt;
    &lt;br /&gt;
&lt;div class=&quot;modal fade&quot; id=&quot;dataperspective&quot; role=&quot;dialog&quot;&gt;
&lt;div class=&quot;modal-dialog&quot;&gt;
&lt;!-- Modal content--&gt;
       &lt;br /&gt;
&lt;div class=&quot;modal-content&quot;&gt;
&lt;div class=&quot;modal-body&quot;&gt;
&lt;button class=&quot;close&quot; data-dismiss=&quot;modal&quot; type=&quot;button&quot;&gt;×&lt;/button&gt;
                &lt;iframe height=&quot;100%&quot; src=&quot;http://www.r-fiddle.org&quot; width=&quot;100%&quot;&gt;&lt;/iframe&gt;
        &lt;/div&gt;
&lt;!-- Modal content--&gt;
     &lt;/div&gt;
&lt;/div&gt;
&lt;!-- Modal --&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;!-- Trigger the modal with a button --&gt;&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/2879420956628662893/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2016/02/basic-data-types-in-r.html#comment-form' title='33 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/2879420956628662893'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/2879420956628662893'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2016/02/basic-data-types-in-r.html' title='Basic Data Types in R '/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGcPVWIbuwbLXJzWi4MS2fz-96b1DTueJpqmtRaZ61VgpvecyCMnnL8R8LgfGmjKlocjPWeY_DSAEEb1W-qESiFNDi0lnYg6Esf6ep6XSUJish0PmRP-BqLJPH4nAHRPu1xUEjTZVTjaEY/s72-c/basic_data_types_R.PNG" height="72" width="72"/><thr:total>33</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-2490483455879365265</id><published>2015-12-25T05:42:00.002+04:00</published><updated>2017-10-17T07:40:31.917+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Science"/><category scheme="http://www.blogger.com/atom/ns#" term="machine learning"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="supervised learning"/><category scheme="http://www.blogger.com/atom/ns#" term="unsupervised learning"/><category scheme="http://www.blogger.com/atom/ns#" term="visualization"/><title type='text'>Data Science with R</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
As R programming language becoming popular more and more among data science group, industries, researchers, companies embracing R, going forward I will be writing posts on learning Data science using R. The tutorial course will include topics on data types of R, handling data using R, probability theory, Machine Learning, Supervised – unSupervised learning, Data Visualization using R, etc. Before going further, let’s just see some stats and tidbits on data science and R.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&quot;A &lt;a href=&quot;http://www.dataperspective.info/2013/12/data-scientist-path-i-chose.html&quot; target=&quot;_blank&quot;&gt;data scientist&lt;/a&gt; is simply someone who is highly adept at studying large amounts of often unorganized/undigested data&quot;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfZvMGJZsC3-LycqOplLzXSoCyotXSm9ieuu2vXLX6_VkNpQagaK997KZwT9Fk9JYwUQ-FVxZtdy07dZj9W3MGZoTH7HlTDG1pkMutzUxlEX4TvlOyXK5JUhm3ronHD8iZlNJeSILQ6vG7/s1600/datascientist.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfZvMGJZsC3-LycqOplLzXSoCyotXSm9ieuu2vXLX6_VkNpQagaK997KZwT9Fk9JYwUQ-FVxZtdy07dZj9W3MGZoTH7HlTDG1pkMutzUxlEX4TvlOyXK5JUhm3ronHD8iZlNJeSILQ6vG7/s400/datascientist.png&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNVzuiWoklVR2v7rEP2olsPnfCLaSJTunurd7XMjzfPJ45lvKbiYN1Ie5qkYSbDm0EmyxBq-R47P_ZdmyULevUF4IGSyuQYBSuYoIMmItohUIeUOo2Q9xw1-OnG91cb6nSAYaq4P7_7Xog/s1600/who_is_data_scientist.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNVzuiWoklVR2v7rEP2olsPnfCLaSJTunurd7XMjzfPJ45lvKbiYN1Ie5qkYSbDm0EmyxBq-R47P_ZdmyULevUF4IGSyuQYBSuYoIMmItohUIeUOo2Q9xw1-OnG91cb6nSAYaq4P7_7Xog/s1600/who_is_data_scientist.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
“R programming language is becoming the Magic Wand for Data Scientists”&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;Why R for Data Science?
&lt;/b&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
Daryl Pregibon, a research scientist at Google said-
“R is really important to the point that it’s hard to overvalue it. It allows statisticians to do very intricate and complicated &lt;a href=&quot;http://www.dataperspective.info/2014/02/data-analysis-steps.html&quot; target=&quot;_blank&quot;&gt;data analysis&lt;/a&gt; without knowing the blood and guts of computing systems.”
&lt;br /&gt;
&lt;br /&gt;
&lt;b style=&quot;text-align: left;&quot;&gt;A brief stats for R popularity&lt;/b&gt;&lt;br /&gt;
&lt;b style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6KK6-RF2j-sq2ePlxkhFKSTygJgoWSf2TZ8qR8HMH7PWwdD2kwkxzEZNPB-9HEEImnIspq4r8Nfk05XyB184FPWSXQ44vTPoLcslpYPkLKKOgksitWV6Avpc7Ugf7hp8V_mb5nhkz8PKn/s1600/top_programming_languages_2015.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6KK6-RF2j-sq2ePlxkhFKSTygJgoWSf2TZ8qR8HMH7PWwdD2kwkxzEZNPB-9HEEImnIspq4r8Nfk05XyB184FPWSXQ44vTPoLcslpYPkLKKOgksitWV6Avpc7Ugf7hp8V_mb5nhkz8PKn/s1600/top_programming_languages_2015.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
Source:  &lt;a href=&quot;http://www.kdnuggets.com/2015/05/poll-r-rapidminer-python-big-data-spark.html&quot; target=&quot;_blank&quot;&gt;2015 Analytics/Data Mining Software Poll&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
“The shortage of &lt;a href=&quot;http://www.dataperspective.info/2013/12/data-scientist-path-i-chose.html&quot; target=&quot;_blank&quot;&gt;data scientists&lt;/a&gt; is becoming a serious constraint in some sectors”
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNyV1wbMvw0eBm04TAgDbacGcUc7nWPcc0CvGC1rOt8TQC7x7Deko8QdMrp2YIMUDjFupp4Ofu5nn24f4VORBf-ged0Fa69nB8BypG4q0Fu8dSSDCPvy925hyOVhZhiM5KrGirLn5EHdcb/s1600/companies_using_R.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNyV1wbMvw0eBm04TAgDbacGcUc7nWPcc0CvGC1rOt8TQC7x7Deko8QdMrp2YIMUDjFupp4Ofu5nn24f4VORBf-ged0Fa69nB8BypG4q0Fu8dSSDCPvy925hyOVhZhiM5KrGirLn5EHdcb/s1600/companies_using_R.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
David Smith, Chief Community Officer at Revolution Analytics said –&lt;br /&gt;
“Investing in R, whether from the point of view of an individual Data Scientist or a company as a whole is always going to pay off because R is always available. If you’ve got a Data Scientist new to an organization, you can always use R. If you’re a company and you’re putting your practice on R, R is always going to be available. And, there’s also an ecosystem of companies built up around R including Revolution Enterprise to help organizations implement R into their machine critical production processes.”
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGzgAVDM8e2fF1WidwyVEePux0zgDSqMSkwmvLKIuYPpV9YQoaex8BiD0BiQNNSS2ABsfPdptPbimmC3NIudJP2LY-UdxEwAtgeUEd_iK-lIOb1EpwcrdyV2ykv9I5cw2xINoyEmqtFV0m/s1600/why_Learn_R.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGzgAVDM8e2fF1WidwyVEePux0zgDSqMSkwmvLKIuYPpV9YQoaex8BiD0BiQNNSS2ABsfPdptPbimmC3NIudJP2LY-UdxEwAtgeUEd_iK-lIOb1EpwcrdyV2ykv9I5cw2xINoyEmqtFV0m/s1600/why_Learn_R.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
Enough of candies about R, The topics which I cover in the course are:&lt;br /&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;a href=&quot;http://www.dataperspective.info/2016/02/basic-data-types-in-r.html&quot;&gt;R Basics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Probability theory in R&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.dataperspective.info/2014/10/regression-analysis-using-r.html&quot; target=&quot;_blank&quot;&gt;Machine Learning in R&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.dataperspective.info/2015/10/introduction-to-logistic-regression.html&quot; target=&quot;_blank&quot;&gt;Supervised machine learning&lt;/a&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&lt;a href=&quot;http://www.dataperspective.info/2013/12/cluster-analysis-using-r.html&quot; target=&quot;_blank&quot;&gt;Unsupervised machine learning&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Advanced Machine Learning in R&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Data Visualization in R&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;
See you in the first chapter, meanwhile read about the various &lt;a href=&quot;http://www.dataperspective.info/2014/02/data-analysis-steps.html&quot; target=&quot;_blank&quot;&gt;data analysis steps&lt;/a&gt; involved.&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/2490483455879365265/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2015/12/datasciencewithR.html#comment-form' title='346 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/2490483455879365265'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/2490483455879365265'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2015/12/datasciencewithR.html' title='Data Science with R'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjfZvMGJZsC3-LycqOplLzXSoCyotXSm9ieuu2vXLX6_VkNpQagaK997KZwT9Fk9JYwUQ-FVxZtdy07dZj9W3MGZoTH7HlTDG1pkMutzUxlEX4TvlOyXK5JUhm3ronHD8iZlNJeSILQ6vG7/s72-c/datascientist.png" height="72" width="72"/><thr:total>346</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-2605844877495981657</id><published>2015-11-18T22:39:00.001+04:00</published><updated>2017-10-17T07:41:07.211+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="collaborative filtering"/><category scheme="http://www.blogger.com/atom/ns#" term="content based recommender system"/><category scheme="http://www.blogger.com/atom/ns#" term="item   based collaborative filtering"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="Recommendation Engine"/><category scheme="http://www.blogger.com/atom/ns#" term="recommender systems"/><category scheme="http://www.blogger.com/atom/ns#" term="user based collaborative filtering"/><title type='text'>Item Based Collaborative Filtering Recommender Systems in R</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvV7o_hkl-SqKoDBKmtInsuvJ9QXisSwsxt89iJ4dObX7rKfEe3DopIY3bb9NYE52qEmbtlx1jR6HcszYi3cUea0bcqbN8c7TDdvIq-JnlvZkWfaGENQ300_G6OSBhx-IOMOSTLP6FUfUL/s1600/item_based_collaborative_filtering3.jpeg&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;95&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvV7o_hkl-SqKoDBKmtInsuvJ9QXisSwsxt89iJ4dObX7rKfEe3DopIY3bb9NYE52qEmbtlx1jR6HcszYi3cUea0bcqbN8c7TDdvIq-JnlvZkWfaGENQ300_G6OSBhx-IOMOSTLP6FUfUL/s320/item_based_collaborative_filtering3.jpeg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
In the series of implementing Recommendation engines, in my previous blog about &lt;a href=&quot;http://www.dataperspective.info/2014/05/basic-recommendation-engine-using-r.html&quot;&gt;recommendation system in R&lt;/a&gt;, I have explained about implementing user based collaborative filtering approach using R. In this post, I will be explaining about basic implementation of Item based collaborative filtering recommender systems in r.
&lt;br /&gt;
&lt;b&gt;Intuition:&lt;/b&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEje_1at9UNzyZebaGr34NJdCwhe286HS3DSnxOLtxrAPGsULLlqVr72-8YzR6QMEb-9U6OlrYLciIII_IAOmBHPzDdd36Um1pWl7FFInIon2NFVi1ejDd0unf_yAwzp-9sM32hUGPHQo6Ay/s1600/item_based_collaborative_filtering1.jpeg&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;163&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEje_1at9UNzyZebaGr34NJdCwhe286HS3DSnxOLtxrAPGsULLlqVr72-8YzR6QMEb-9U6OlrYLciIII_IAOmBHPzDdd36Um1pWl7FFInIon2NFVi1ejDd0unf_yAwzp-9sM32hUGPHQo6Ay/s320/item_based_collaborative_filtering1.jpeg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
&lt;b&gt;Item based Collaborative Filtering:&lt;/b&gt;&lt;br /&gt;
Unlike in user based collaborative filtering discussed previously, in item-based collaborative filtering, we consider set of items rated by the user and computes item similarities with the targeted item. Once similar items are found, and then rating for the new item is predicted by taking weighted average of the user’s rating on these similar items.&lt;br /&gt;
&lt;b&gt;let&#39;s understand with an example:&lt;/b&gt;&lt;br /&gt;
As an example: consider below dataset, containing users rating to movies. Let us build an algorithm to recommend movies to CHAN.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQ32O9mgGK4W74A88vvoJumt88D0LSn548TIcSfSsNFJKIypxqzMEEAY4U0mTNC0zJh7GTMHfmOCnyyvaRtYZ3Tzk5HGrBCnJ-8GLv3STy1bXZc2MIyVnJUa8l9BNRLugBT6HJmUmzP7LU/s1600/item_item_collaborative_filtering1.jpg&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;135&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQ32O9mgGK4W74A88vvoJumt88D0LSn548TIcSfSsNFJKIypxqzMEEAY4U0mTNC0zJh7GTMHfmOCnyyvaRtYZ3Tzk5HGrBCnJ-8GLv3STy1bXZc2MIyVnJUa8l9BNRLugBT6HJmUmzP7LU/s320/item_item_collaborative_filtering1.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
Implementing Item based recommender systems, like user based collaborative filtering, requires two steps:&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;text-indent: -0.25in;&quot;&gt;Calculating Item similarities&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: 7pt; font-stretch: normal; text-indent: -0.25in;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;text-indent: -0.25in;&quot;&gt;Predicting the targeted item rating for the
targeted User.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhY7SFN15wZ_-um1nwe99NJWchJ6OKn72iADeLAH9BbV6E4QBoMwkC35yNrIf8f4ioeiasZjwYDxWsC_x5l5SaQbAAtpSExm0Y6vsXsfYdab3P74MzzOsWOa5GTuWUtVIQmNTyQbrBht_je/s1600/item_based_collaborative_filtering2.jpeg&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;176&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhY7SFN15wZ_-um1nwe99NJWchJ6OKn72iADeLAH9BbV6E4QBoMwkC35yNrIf8f4ioeiasZjwYDxWsC_x5l5SaQbAAtpSExm0Y6vsXsfYdab3P74MzzOsWOa5GTuWUtVIQmNTyQbrBht_je/s320/item_based_collaborative_filtering2.jpeg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;Step1: Calculating Item Similarity:&lt;/b&gt;&lt;br /&gt;
This is a critical step; we calculate the similarity between co-rated items. We use cosine similarity or pearson-similarity to compute the similarity between items.  The output for step is similarity matrix between Items.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Code snippet:&lt;/b&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span style=&quot;color: #00b050;&quot;&gt;#step 1: item-similarity
calculation co-rated items are considered and similarity between two items&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span style=&quot;color: #00b050;&quot;&gt;#are calculated
using cosine similarity&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
library(lsa)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
ratings =
read.csv(&quot;Rating Matrix.csv&quot;)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
x =
ratings[,2:7]&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
x[is.na(x)]
= 0&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
item_sim =
cosine(as.matrix(x))&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0EC_zcdC8NWDYduc9nOlOYARz0kYr-nD0Ndn8I_jCKL3Tj7Ec-o60aL4CF29OBClszz3vrvKtOK6YAF4rGsYImN7d9DWFbJ5Wz_MxHsflAlaSHhaO6e3bIlc6aolyDbdIlci0snuFkqNz/s1600/item_based_collaborative_filtering9.jpeg&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;71&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0EC_zcdC8NWDYduc9nOlOYARz0kYr-nD0Ndn8I_jCKL3Tj7Ec-o60aL4CF29OBClszz3vrvKtOK6YAF4rGsYImN7d9DWFbJ5Wz_MxHsflAlaSHhaO6e3bIlc6aolyDbdIlci0snuFkqNz/s320/item_based_collaborative_filtering9.jpeg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;b&gt;Step2: Predicting the targeted item rating for the targeted User CHAN.&lt;/b&gt;&lt;br /&gt;
In this most important step, we first predict the items which the user is not rated by making use of the ratings he has made to previously interacted items and the similarity values calculated in the previous step. First we select item to be predicted, in our case “INCEPTION”, we predict the rating for INCEPTION movie by calculating the weighted sum of ratings made to movies similar to INCEPTION. i.e We take the similarity score for each rated movie by CHAN w.r.t INCEPTION and multiply with the corresponding rating and sum up all the for all the rated movies. This final sum is divided by total sum of similarity scores of rated items w.r.t INCEPTION.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOe1R6Mjw-w3DvfRvX2aAxlFrk0M2GZSOG62bGFjneRkQtAZvGUY9Q67gSGieLmEP6SJIFWJZxIGGJM_XAxzDdFoWEBFIsRaAGZBj6UPvRhJVRSPSejX3vRyiohM2dOC45iKFPxviTxLTG/s1600/collaborative_filtering_Recommender_systems_R.jpg&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;160&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOe1R6Mjw-w3DvfRvX2aAxlFrk0M2GZSOG62bGFjneRkQtAZvGUY9Q67gSGieLmEP6SJIFWJZxIGGJM_XAxzDdFoWEBFIsRaAGZBj6UPvRhJVRSPSejX3vRyiohM2dOC45iKFPxviTxLTG/s320/collaborative_filtering_Recommender_systems_R.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;Recommending Top N items:&lt;/b&gt;&lt;br /&gt;
Once all the non rated movies are predicted we recommend top N movies to CHAN.
Code for Item based collaborative filtering in R:&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;#data input&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;ratings =
read.csv(&quot;~Rating Matrix.csv&quot;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 10pt;&quot;&gt;&quot;step 1: item-similarity
calculation\nco-rated items are considered and similarity between two
items\nare calculated using cosine similarity&quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;library(lsa)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;x = ratings[,2:7]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;x[is.na(x)] = 0&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;item_sim =
cosine(as.matrix(x))&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 10pt;&quot;&gt;&quot;Recommending items for chan: since three
movies are not rated\nas a first step we have to predict rating value for each
movie\nin CHANs case we have to first predict values for Titanic,
Inception,Matrix&quot;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;br /&gt;
&amp;nbsp;rec_itm_for_user =
function(userno)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp; #extract all the movies
not rated by CHAN&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp; userRatings = ratings[userno,]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp; non_rated_movies = list()&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp; rated_movies = list()&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp; for(i in
2:ncol(userRatings)){&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
if(is.na(userRatings[,i]))&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; non_rated_movies =
c(non_rated_movies,colnames(userRatings)[i])&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rated_movies = c(rated_movies,colnames(userRatings)[i])&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp; non_rated_movies =
unlist(non_rated_movies)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp; rated_movies =
unlist(rated_movies)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp; #create weighted
similarity for all the rated movies by CHAN&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp; non_rated_pred_score =
list()&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp; for(j in 1:length(non_rated_movies)){&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; temp_sum = 0&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; df =
item_sim[which(rownames(item_sim)==non_rated_movies[j]),]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(i in
1:length(rated_movies)){&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; temp_sum = temp_sum+
df[which(names(df)==rated_movies[i])]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; weight_mat =
df*ratings[userno,2:7]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; non_rated_pred_score =
c(non_rated_pred_score,rowSums(weight_mat,na.rm=T)/temp_sum)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&amp;nbsp;}&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp; pred_rat_mat =
as.data.frame(non_rated_pred_score)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp; names(pred_rat_mat) =
non_rated_movies&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp; for(k in
1:ncol(pred_rat_mat)){&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ratings[userno,][which(names(ratings[userno,])
== names(pred_rat_mat)[k])] = pred_rat_mat[1,k]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp; return(ratings[userno,])&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;lucida console&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;nbsp;}&lt;/span&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 10pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span class=&quot;gewyw5ybjeb&quot;&gt;&lt;span style=&quot;background: #e1e2e5; color: blue; font-family: &amp;quot;lucida console&amp;quot;;&quot;&gt;&amp;gt; &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;gewyw5ybmdb&quot;&gt;&lt;span style=&quot;background: #e1e2e5; color: blue; font-family: &amp;quot;lucida console&amp;quot;;&quot;&gt;rec_itm_for_user(7)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;;&quot;&gt;&amp;nbsp; Users&amp;nbsp; Titanic Batman Inception SuperMan Spiderman&amp;nbsp;&amp;nbsp; matrix&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;;&quot;&gt;7&amp;nbsp; CHAN 3.085298&amp;nbsp;&amp;nbsp;&amp;nbsp; 4.5&amp;nbsp; 2.940811&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1 3.170034&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;;&quot;&gt;
&lt;/span&gt;&lt;/pre&gt;
Calling above function gives the predicted values not previously seen values for movies Titanic, Inception, Matrix. Now we can sort and recommend the top items.&lt;br /&gt;
This is all about Collaborative filtering in R, in my upcoming posts I will talk about content based recommender systems in r.

&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/2605844877495981657/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2015/11/item-based-collaborative-filtering-in-r.html#comment-form' title='36 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/2605844877495981657'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/2605844877495981657'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2015/11/item-based-collaborative-filtering-in-r.html' title='Item Based Collaborative Filtering Recommender Systems in R'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvV7o_hkl-SqKoDBKmtInsuvJ9QXisSwsxt89iJ4dObX7rKfEe3DopIY3bb9NYE52qEmbtlx1jR6HcszYi3cUea0bcqbN8c7TDdvIq-JnlvZkWfaGENQ300_G6OSBhx-IOMOSTLP6FUfUL/s72-c/item_based_collaborative_filtering3.jpeg" height="72" width="72"/><thr:total>36</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-527780739742251406</id><published>2015-10-19T05:47:00.000+04:00</published><updated>2017-10-17T07:41:20.594+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="data mining standards"/><category scheme="http://www.blogger.com/atom/ns#" term="data modeling"/><category scheme="http://www.blogger.com/atom/ns#" term="Data Preparation"/><category scheme="http://www.blogger.com/atom/ns#" term="Data Scientist"/><category scheme="http://www.blogger.com/atom/ns#" term="Hypothesis"/><category scheme="http://www.blogger.com/atom/ns#" term="logistic regression"/><category scheme="http://www.blogger.com/atom/ns#" term="missing values imputation"/><category scheme="http://www.blogger.com/atom/ns#" term="normalization"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="visualization"/><title type='text'>Data Mining Standard Process across Organizations</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Recently I have come across a term, CRISP-DM - a data mining standard. Though this process is not a new one but I felt every analyst should know about commonly used Industry wide process. In this post I will explain about different phases involved in creating a data mining solution.
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;CRISP-DM&lt;/b&gt;, an acronym for &lt;b&gt;Cross Industry Standard Process for Data Mining&lt;/b&gt;, is a data mining process model that includes commonly used approaches that data analytics Organizations use to tackle business problems related to Data mining. Polls conducted at one and the same website (KDNuggests) in 2002, 2004, 2007 and 2014 show that it was the leading methodology used by industry data miners who decided to respond to the survey.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
CRISP-DM model is a phased approach to tackle a business problem. Different phases involved in the model are defined below:&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Use case Identification&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background: white; color: #252525; font-family: &amp;quot;arial&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 10.5pt; line-height: 115%;&quot;&gt;Business Understanding&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background: white; color: #252525; font-family: &amp;quot;arial&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;Data Acquisition and Data Understanding&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background: white; color: #252525; font-family: &amp;quot;arial&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;Data Preparation&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background: white; color: #252525; font-family: &amp;quot;arial&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;Exploratory Analysis&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background: white; color: #252525; font-family: &amp;quot;arial&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;Data Modeling&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background: white; color: #252525; font-family: &amp;quot;arial&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;Data Evaluation&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background: white; color: #252525; font-family: &amp;quot;arial&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;Deployment&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li style=&quot;text-align: center;&quot;&gt;&lt;span style=&quot;background: white; color: #252525; font-family: &amp;quot;arial&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhybcn9HVLRCqxQax0CN1zShc4j8xT5SkKTDO1X_Jg38PfMb7WohxLPOat2t-aOFqSZ2la9NzXNNqG-vpwZDNbZZRdX6YZYl-i19O0kjo-pXRRlQD6oyE-uWTd7rvoJX9vITJR6ihAZgPe7/s1600/Data_mining_standards.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhybcn9HVLRCqxQax0CN1zShc4j8xT5SkKTDO1X_Jg38PfMb7WohxLPOat2t-aOFqSZ2la9NzXNNqG-vpwZDNbZZRdX6YZYl-i19O0kjo-pXRRlQD6oyE-uWTd7rvoJX9vITJR6ihAZgPe7/s320/Data_mining_standards.png&quot; width=&quot;95&quot; /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;background: white; color: #252525; font-family: &amp;quot;arial&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 10.5pt; line-height: 115%;&quot;&gt;Let us see each of the phases in detailed way:&lt;/span&gt;&lt;/div&gt;
&lt;b&gt;Use case Identification:&lt;/b&gt;This is the initial phase of CRISP-DM in which a potential business problem is formulated into a Data mining use case. Various levels of brainstorming sessions are conducted between different stakeholders to define the problem statement, its impact on the business and a clear objective of the solution and its timelines.&lt;br /&gt;
&lt;span style=&quot;background: white; color: #252525; font-family: &amp;quot;arial&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 10.5pt; line-height: 115%;&quot;&gt;Audience:&lt;/span&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;color: #252525; font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 14px; line-height: 16.1px;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif; font-size: 10.5pt; line-height: 115%;&quot;&gt;higher management&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: #252525; font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 14px; line-height: 16.1px;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif; font-size: 10.5pt; line-height: 115%;&quot;&gt;IT teams – Application team, DBA team&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: #252525; font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 14px; line-height: 16.1px;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif; font-size: 10.5pt; line-height: 115%;&quot;&gt;Analytics team – Data Scientist&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;b&gt;Business Understanding:&lt;/b&gt;This phase focuses on understanding the business flow – how the current system addresses the business problem in consideration, identifying the data sources which would be required for data mining. Understanding the key features required for modeling from domain point of view.&lt;br /&gt;
&lt;span style=&quot;color: #252525; font-family: &amp;quot;arial&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 10.5pt; line-height: 115%;&quot;&gt;Audience:&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;color: #252525; font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 14px; line-height: 16.1px;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif; font-size: 10.5pt; line-height: 115%;&quot;&gt;domain experts - for domain knowledge, business rules understanding&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: #252525; font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 14px; line-height: 16.1px;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif; font-size: 10.5pt; line-height: 115%;&quot;&gt;IT teams -&amp;nbsp; for data sources
identifications, key features of the system&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: #252525; font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 14px; line-height: 16.1px;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif; font-size: 10.5pt; line-height: 115%;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif; font-size: 10.5pt; line-height: 115%;&quot;&gt;Analytics teams – Data Scientist&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;b&gt;Data Acquisition and Data Understanding: &lt;/b&gt;This phase involves data collection activities such as pulling data sources from the client databases to a central repository where Data Analytics team develop solutions. Based on the type of problem in hand the data sources might vary from SQL databases to text files to log files to web pages etc.This phase also involves getting familiarity with data - such as data quality, missing values, extracting basic insights and identifying interesting patterns to form Hypothesis.
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Data Preparation:&lt;/b&gt;This phase of CRISP-DM involves preparing data required to be fed into data mining algorithms. This Phase involves processing or cleaning of raw data. This is one of the crucial steps in data mining. The accuracy of the data mining solution depends on the quality of the data. All the data preparation activities which are required for creating final dataset for feeding into algorithms are done here - Handling missing data using methods such as imputations, converting data into proper formats such as unstructured to structured format, identifying outliers, normalizing the data etc.
&lt;br /&gt;
Audience:&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;background-color: white; color: #252525; font-family: &amp;quot;arial&amp;quot; , sans-serif; font-size: 10.5pt; line-height: 115%;&quot;&gt;Data Analytics team&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;b&gt;Exploratory Analysis:&lt;/b&gt;This is visual analytics phase wherein the data scientist tries to understand the various patterns hiding in the data. The objective of this step is to understand the main characteristics of the data. This analysis is generally done using visualizing tools such as Tableau, R etc. &lt;br /&gt;
Performing an Exploratory analysis helps us:&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&amp;nbsp;To understand causes of an observed event.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;To understand the nature of the data we are dealing with.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Assess assumptions on which our analysis will be based.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;To identify the key features in the data needed for the analysis.&lt;/li&gt;
&lt;/ol&gt;
Exploratory Analysis involves both Quantitative analysis such as basic statistics such as mean/median/mode, standard deviations etc. and Graphical Analysis such as normal plots, histograms, box plots, scatter plots etc.
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Data Modeling:&lt;/b&gt;In this phase, various modeling techniques are selected and applied to the data for feature extractions, to model the data, tune the model and to calibrate its parameters to optimal values. Typically this phase involves applying suitable data mining/machine learning algorithms to the dataset. Some problems can be solved using single methods where as some problems involves combination of multiple techniques.&lt;br /&gt;
&lt;i&gt;For ex:  A recommendation systems of Netflix uses a combination of Boltzman machines, Gradient Boosted Decision trees, logistic regression etc.&lt;/i&gt;&lt;br /&gt;
Also sometimes different methods are applied separately to select the optimal method to solve the issue at hand. &lt;br /&gt;
&lt;i&gt;For ex: Logistic regression, decision tree, Random forests are applied to the dataset to see which model will result in optimal data model.&lt;/i&gt;&lt;br /&gt;
In this phase of modeling the data, the dataset is divided into two sets, Training Set &amp;amp; Test Set. The modeling the data is done using Training Set and the Test Set is used to evaluate the model.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Data Evaluation: &lt;/b&gt;This is the follow-up step to the data Modeling phase. Data Model built in the previous step needs to be thoroughly validated before moving into deployment. The model should address all the business objectives mentioned in the problem statement. The Test Data set created in the previous set is used to test the model build. The objective of this step is to check if the prediction error made on the test set. If the prediction error is less, then our model is good to go. Sometimes the error would be larger indicating the situation of under fitting and Overfitting. Based on the results we might have to go back to previous phases and tune the model.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Deployment:&lt;/b&gt;Once the model building and evaluation is completed and we are satisfied with results, the next step is to present the business users with the results. These publishing results should be in user readable or understandable form. Most of the time the results will be published in the form of reports or UI. For example: If the results are needed by the top management for taking key business decisions, visualization reports will be the accurate. If the end user needs to be recommended any new item on e-commerce website, then the results should be displayed on to the web UI.&lt;br /&gt;
&lt;br /&gt;
Most of the time, back and forth between phases is required. For example, during evaluating the data model, if we find that model is suffering from over-fitting we can go back to the model phase and fine tune the Model. As an another example, if in modeling phase if we observe that the a feature column in the dataset with sparse data is very critical in achieving the solution then we will go back to the Business Understanding step and consult the domain experts to know if we can derive more information about the sparse data column and impute the column with relevant values.&lt;br /&gt;
To know more information about CRISP-DM, see the wiki page &lt;a href=&quot;https://en.wikipedia.org/wiki/Cross_Industry_Standard_Process_for_Data_Mining&quot;&gt;here&lt;/a&gt;.
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/527780739742251406/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2015/10/data-mining-standard-process-Organizations.html#comment-form' title='19 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/527780739742251406'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/527780739742251406'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2015/10/data-mining-standard-process-Organizations.html' title='Data Mining Standard Process across Organizations'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhybcn9HVLRCqxQax0CN1zShc4j8xT5SkKTDO1X_Jg38PfMb7WohxLPOat2t-aOFqSZ2la9NzXNNqG-vpwZDNbZZRdX6YZYl-i19O0kjo-pXRRlQD6oyE-uWTd7rvoJX9vITJR6ihAZgPe7/s72-c/Data_mining_standards.png" height="72" width="72"/><thr:total>19</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-4205032274200044310</id><published>2015-10-07T05:25:00.000+04:00</published><updated>2017-10-17T07:41:33.539+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="big data"/><category scheme="http://www.blogger.com/atom/ns#" term="classification"/><category scheme="http://www.blogger.com/atom/ns#" term="data mining"/><category scheme="http://www.blogger.com/atom/ns#" term="Data Science"/><category scheme="http://www.blogger.com/atom/ns#" term="Exploratory analysis"/><category scheme="http://www.blogger.com/atom/ns#" term="logistic regression"/><category scheme="http://www.blogger.com/atom/ns#" term="machine learning"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="Statistics"/><category scheme="http://www.blogger.com/atom/ns#" term="supervised learning"/><title type='text'>Introduction to Logistic Regression with R</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
In my &lt;a href=&quot;http://www.dataperspective.info/2014/10/regression-analysis-using-r.html&quot; target=&quot;_blank&quot;&gt;previous blog&lt;/a&gt; I have explained about linear regression. In today’s post I will explain about logistic regression.
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Consider a scenario where we need to predict a medical condition of a patient (HBP) ,HAVE HIGH BP or NO HIGH BP, based on some observed symptoms – Age, weight, Issmoking, Systolic value, Diastolic value, RACE, etc..  In this scenario we have to build a model which takes the above mentioned symptoms as input values and HBP as response variable. Note that the response variable (HBP) is a value among a fixed set of classes, HAVE HIGH BP or NO HIGH BP.&lt;br /&gt;
&lt;h3 style=&quot;text-align: center;&quot;&gt;
Logistic regression – a classification problem, not a prediction problem:&lt;/h3&gt;
In my &lt;a href=&quot;http://www.dataperspective.info/2014/10/regression-analysis-using-r.html&quot; target=&quot;_blank&quot;&gt;previous blog &lt;/a&gt;I told that we use linear regression for scenarios which involves prediction. But there is a check; the regression analysis cannot be applied in scenarios where the response variable is not continuous. In our case the response variable is not a continuous variable but a value among a fixed set of classes. We call such scenarios as Classification problem rather than prediction problem. In such scenarios where the response variables are more of qualitative nature rather than continuous nature, we have to apply more suitable models namely logistic regression for classification.&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;b&gt;Definition:&lt;/b&gt;&lt;/div&gt;
Assume we have a binary category output variable Y and a vector of p input variables X.
Rather than modeling this response Y directly, logistic regression models the conditional probability, &lt;b&gt;Pr(Y = 1|X = x)&lt;/b&gt; as a function of x, that Y belongs to a particular category.

&lt;br /&gt;
&lt;br /&gt;
Mathematically, logistic regression is expressed as:
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgomhSLLSzQ-omZzKkxGrDpL8Ikw7wK0_mSx_cJkoFCOTYchNbJDJ9lwMBosnrbszgi1-rJmAnL4PhFtrZLchM92YlOjpzywexQtl7hDQWa36tQo2vKbQFvtKHTNWkwmp19YpA0OnrNuxrj/s1600/log_equ.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;116&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgomhSLLSzQ-omZzKkxGrDpL8Ikw7wK0_mSx_cJkoFCOTYchNbJDJ9lwMBosnrbszgi1-rJmAnL4PhFtrZLchM92YlOjpzywexQtl7hDQWa36tQo2vKbQFvtKHTNWkwmp19YpA0OnrNuxrj/s320/log_equ.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;b&gt;Estimating Coefficients – Maximum Likelihood function:&lt;/b&gt;&lt;br /&gt;
The unknown parameters, β0/ β1, in the function are estimated by maximum likelihood method using available input training data. The Maximum likelihood function expresses the probability of the observed data as a function of the unknown parameters. The maximum likelihood estimators of these parameters are chosen to be those values that maximize this function. Thus, the estimators are those which agree most closely with the observed data.
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_c1zhRiqRNSVvG_dJx6cm5Z8d9NxV_HOKfjPBJH5yVkvP1rc1hzP9eeuS2WTRru_vYvxZ9plvvSCOciWs2xyjtFnP5M6JGYETmG6zhk7nAvgJc_sGBSDWuetSOiHFiHQrA8Oxqa0jmiWL/s1600/log_reg_img1.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;69&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_c1zhRiqRNSVvG_dJx6cm5Z8d9NxV_HOKfjPBJH5yVkvP1rc1hzP9eeuS2WTRru_vYvxZ9plvvSCOciWs2xyjtFnP5M6JGYETmG6zhk7nAvgJc_sGBSDWuetSOiHFiHQrA8Oxqa0jmiWL/s400/log_reg_img1.JPG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
For now we assume that solving the above equation can be used to estimate the unknown parameters.&lt;br /&gt;
&lt;br /&gt;
In R, we glm() which takes training data as input and gives us the fitted model with estimated parameters as output, which we will see in the later section.&lt;br /&gt;
&lt;h4&gt;
Making Predictions:&lt;/h4&gt;
Once the coefficients have been estimated, it is a simple matter to compute the probability of response variable for any given input values by putting values of β0/ β1/X in the below equation.
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgov52Pr2kEopZCXqjZoE6xk-5EHs_-jgv2ekm5iTmtVAtOXNqgRszrzW_HnCIrQpe8JdZP1LPckWBwbBCNaREFPItVlMkTIjgEYfR0_tyv39cSAk0ZCA5yQbjj3CtU1rvlMr6UUeXjZaCC/s1600/log_equ1.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;97&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgov52Pr2kEopZCXqjZoE6xk-5EHs_-jgv2ekm5iTmtVAtOXNqgRszrzW_HnCIrQpe8JdZP1LPckWBwbBCNaREFPItVlMkTIjgEYfR0_tyv39cSAk0ZCA5yQbjj3CtU1rvlMr6UUeXjZaCC/s400/log_equ1.JPG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;
Note: we have predict() in R which takes fitted model, input parameters as input values to predict the response variables.&lt;/i&gt;
&lt;br /&gt;
&lt;h4&gt;
Use case – Classify if a person may have HBP/not HBP :&lt;/h4&gt;
Let us take a use case and implement logistic regression in R. Let us classify/predict if a person suffers with High Blood Pressure (HPB) given input predictors AGE, SEX, IsSmoking, Avg Systolic BP, Average diastolic BP, RACE, Body Weight, Height, etc. Access the data set from &lt;a href=&quot;https://www.umass.edu/statdata/statdata/data/nhanes3.txt&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;. The dataset is NHANES III dataset. In order to implement logistic regression, we need to follow the below steps:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;data = read.csv(&#39;~\nhanes.csv&#39;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;str(data)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 10pt;&quot;&gt;data.frame&#39;:&amp;nbsp;&amp;nbsp; 15643 obs. of&amp;nbsp;
10 variables:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 10pt;&quot;&gt;&amp;nbsp;$ HSAGEIR : int&amp;nbsp; 21 32 48 35 48 44 42 56 82 44 ...&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 10pt;&quot;&gt;&amp;nbsp;$ HSSEX&amp;nbsp;&amp;nbsp;
: int&amp;nbsp; 1 0 0 1 1 1 0 0 0 1 ...&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 10pt;&quot;&gt;&amp;nbsp;$ BMPWTLBS: num&amp;nbsp; 180 136 150 204 155 ...&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 10pt;&quot;&gt;&amp;nbsp;$ BMPHTIN : num&amp;nbsp; 70.4 63.9 61.8 69.8 66.2 70.2 62.6 67.6 59.5
71.1 ...&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 10pt;&quot;&gt;&amp;nbsp;$ TCP&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
: int&amp;nbsp; 268 160 236 225 260 187 216
156 179 162 ...&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 10pt;&quot;&gt;&amp;nbsp;$ HBP&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
: int&amp;nbsp; 0 0 0 0 0 0 0 0 1 0 ...&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 10pt;&quot;&gt;&amp;nbsp;$ RACE_2&amp;nbsp;
: num&amp;nbsp; 0 0 0 0 0 1 1 0 0 0 ...&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 10pt;&quot;&gt;&amp;nbsp;$ RACE_3&amp;nbsp;
: num&amp;nbsp; 0 0 0 0 0 0 0 0 0 0 ...&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 10pt;&quot;&gt;&amp;nbsp;$ SMOKE_2 : num&amp;nbsp; 0 0 1 0 1 0 0 1 0 0 ...&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 10pt;&quot;&gt;&amp;nbsp;$ SMOKE_3 : num&amp;nbsp; 0 0 0 0 0 1 1 0 0 1 ...&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;color: #00b050;&quot;&gt;#removing
SEQN,SDPPSU6,SDPSTRA6 from the dataset, from description of the datset,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;color: #00b050;&quot;&gt;#we
concluded that it is not related to data.&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;data = data[,-c(1:3)]&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;#excluding HAR3 for now.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;data = data[,-c(1,10)]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;color: #00b050;&quot;&gt;# removing
entire row if there is any single missing column in the data.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;color: #00b050;&quot;&gt;#check
for NA&#39;s or missing values in the data&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;color: #00b050;&quot;&gt;#removing
all the na rows from data&lt;/span&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;data = na.omit(data)&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;color: #00b050;&quot;&gt;#
from the description we consider HBP as response variable.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;names(data)&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 10pt;&quot;&gt;&#39;[1]
&quot;HSAGEIR&quot;&amp;nbsp;
&quot;HSSEX&quot;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&quot;DMARACER&quot; &quot;BMPWTLBS&quot; &quot;BMPHTIN&quot;&amp;nbsp; &quot;PEPMNK1R&quot; &quot;PEPMNK5R&quot; &quot;HAR1&quot;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &quot;SMOKE&quot;&amp;nbsp;&amp;nbsp;&amp;nbsp; &quot;TCP&quot;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &quot;HBP&quot; &#39;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;summary(data)&lt;/span&gt;&lt;span lang=&quot;EN-GB&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWRwzO_YCGhrxggNvM4uOT-FEs5bAkNuvyw8T7Y3FDJauHeG7t1Goq0rItqHGQg7dQicQ8T223HIXUVLQS4ZQgSJK441Jb6UTmlJHSVT6UzRlDgHgdIuZvONn1a_WUYaIBk00EaX0ShBkg/s1600/correlation.JPG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWRwzO_YCGhrxggNvM4uOT-FEs5bAkNuvyw8T7Y3FDJauHeG7t1Goq0rItqHGQg7dQicQ8T223HIXUVLQS4ZQgSJK441Jb6UTmlJHSVT6UzRlDgHgdIuZvONn1a_WUYaIBk00EaX0ShBkg/s400/correlation.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span lang=&quot;EN-GB&quot;&gt;Data Transformations:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;color: #00b050;&quot;&gt;&#39;Creating
dummy variables/design variables for DMARACER/HSSEX &amp;nbsp;create number of dummy variables based on the
levels -1, i.e if &amp;nbsp;if there 3 levels
create 3-1 levels of dummy variables &amp;nbsp;x=
data&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;color: #00b050;&quot;&gt;&amp;nbsp;for RACE create 2 dummy variables DMRACE1, DMRACE2
where White(1) - (0,0) &amp;nbsp;black(2)
(1,0),other (3) - (1,1)&#39;&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;color: #00b050;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;for(level in unique(data$DMARACER)){&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;&amp;nbsp;
data[paste(&quot;RACE&quot;, level, sep = &quot;_&quot;)] &amp;lt;-
ifelse(data$DMARACER == level, 1, 0)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;color: #00b050;&quot;&gt;#removing
race_1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;data = data[,-12]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;for(level in unique(data$SMOKE)){&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;&amp;nbsp; data[paste(&quot;SMOKE&quot;,
level, sep = &quot;_&quot;)] &amp;lt;- ifelse(data$SMOKE == level, 1, 0)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;color: #00b050;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;color: #00b050;&quot;&gt;#removing
SMOKE_1&lt;/span&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;data = data[,-14]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinnLTId7Pk0C3TrYxPKmXEXuAkI-blIfwqVPU9Z9eC_zYZu31c9ZMO1Ay4uztdYagTYREcGV5_jA8ebEz4ZLJ-PxiwnSqzyCI6dIaE01N9Vz7sMuWeDUZULeZjOxj23c4EooskOX3tUDdD/s1600/histogram_1.jpg&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinnLTId7Pk0C3TrYxPKmXEXuAkI-blIfwqVPU9Z9eC_zYZu31c9ZMO1Ay4uztdYagTYREcGV5_jA8ebEz4ZLJ-PxiwnSqzyCI6dIaE01N9Vz7sMuWeDUZULeZjOxj23c4EooskOX3tUDdD/s320/histogram_1.jpg&quot; width=&quot;188&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;color: #00b050;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;color: #00b050;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiz3AwiPgG7ewHMBqVObn7_UmZ51GAdo_MXT_ApzNo2vhyphenhyphenV2oe2G3nIGfZijNfqlGpAaf7L-sbiuaeQEIWg1jvs1_7QJxrC8rej2feiRzOFmZb7xMQrNGz-6YmNyw0f8SOmswaPL-qIBbUW/s1600/histogram_2.jpg&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiz3AwiPgG7ewHMBqVObn7_UmZ51GAdo_MXT_ApzNo2vhyphenhyphenV2oe2G3nIGfZijNfqlGpAaf7L-sbiuaeQEIWg1jvs1_7QJxrC8rej2feiRzOFmZb7xMQrNGz-6YmNyw0f8SOmswaPL-qIBbUW/s320/histogram_2.jpg&quot; width=&quot;307&quot; /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;table(data$HSSEX,data$HBP)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;background-color: #e1e2e5; font-family: &amp;quot;lucida console&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: #e1e2e5; font-family: &amp;quot;lucida console&amp;quot;;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;background-color: #e1e2e5; font-family: &amp;quot;lucida console&amp;quot;;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;background-color: #e1e2e5; font-family: &amp;quot;lucida console&amp;quot;;&quot;&gt;1&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;;&quot;&gt;&amp;nbsp; 0 6576 1702&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;;&quot;&gt;&amp;nbsp; 1 5817 1548&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;Fem_HBP = (1702/6576+1702)) = 0.2056052&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;Mal_HBP = (1548/(1548+5817)) = 0.2101833&lt;/span&gt;&lt;/i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmc0DupxA9QY26VtWZ2bxxz0PGXXer1z_RF6lXTGKrauAE-Zfrq-14nBMzgEn2ohyphenhyphenCWG2ifAO_wa-_0z7pMv8FaqZnLkW3eXkOf5zcRXWhMYRYCtPTBAUgNL6rWsWS8lxKHmuDn6hCYfkW/s1600/correlation.JPG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgmc0DupxA9QY26VtWZ2bxxz0PGXXer1z_RF6lXTGKrauAE-Zfrq-14nBMzgEn2ohyphenhyphenCWG2ifAO_wa-_0z7pMv8FaqZnLkW3eXkOf5zcRXWhMYRYCtPTBAUgNL6rWsWS8lxKHmuDn6hCYfkW/s400/correlation.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;Modelling:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;#remove HAR1,PEPMNK1R as SMOKE &amp;amp; HBP are created
from these variables.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;data = data[,-c(3,6,7,8,9,16)]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;#create training &amp;amp; testing data&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;train_x = data&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;smp_size &amp;lt;- floor(0.75 * nrow(train_x))&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;train_ind &amp;lt;- sample(seq_len(nrow(train_x)), size =
smp_size)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;train &amp;lt;- train_x[train_ind, ]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; mso-highlight: lightgrey;&quot;&gt;test &amp;lt;- train_x[-train_ind, ]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0in;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;glm2 = glm(train$HBP~train$HSAGEIR+train$HSSEX+train$BMPWTLBS+train$BMPHTIN+train$TCP+train$RACE_2+train$RACE_3,data=train,family=binomial)&lt;/span&gt;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1Lg8VFnbvUCri-7N0XGs77azzVyx6zyp4dAschFV-nphQaWlar_TTAqllRwdFbo9Fpic20zQ6TfMlYRPK2pXF1MDSQkHPypky5j3cwxQdSsp4So8JxsLmG0atBb_Mleb03dpq5ktQ1dYg/s1600/logistic_regression_output.JPG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;261&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1Lg8VFnbvUCri-7N0XGs77azzVyx6zyp4dAschFV-nphQaWlar_TTAqllRwdFbo9Fpic20zQ6TfMlYRPK2pXF1MDSQkHPypky5j3cwxQdSsp4So8JxsLmG0atBb_Mleb03dpq5ktQ1dYg/s320/logistic_regression_output.JPG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/i&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;
&lt;i&gt;&lt;span lang=&quot;EN-GB&quot; style=&quot;background: lightgrey; font-family: &amp;quot;calibri&amp;quot; , &amp;quot;sans-serif&amp;quot;; font-size: 11.0pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;br /&gt;
The image shows us the summary of the model. In my next post let us evaluate the logistic regression model and let us consider few other models and choose better model among them.
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/4205032274200044310/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2015/10/introduction-to-logistic-regression.html#comment-form' title='16 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/4205032274200044310'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/4205032274200044310'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2015/10/introduction-to-logistic-regression.html' title='Introduction to Logistic Regression with R'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgomhSLLSzQ-omZzKkxGrDpL8Ikw7wK0_mSx_cJkoFCOTYchNbJDJ9lwMBosnrbszgi1-rJmAnL4PhFtrZLchM92YlOjpzywexQtl7hDQWa36tQo2vKbQFvtKHTNWkwmp19YpA0OnrNuxrj/s72-c/log_equ.PNG" height="72" width="72"/><thr:total>16</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-3208259409633098593</id><published>2015-04-09T06:20:00.000+04:00</published><updated>2017-10-17T07:42:17.308+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="apache"/><category scheme="http://www.blogger.com/atom/ns#" term="API"/><category scheme="http://www.blogger.com/atom/ns#" term="data analysis"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="Rapache"/><category scheme="http://www.blogger.com/atom/ns#" term="web service"/><title type='text'>Exposing R-script as API</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
R is getting popular programming language in the area of Data Science.  Integrating Rscript with web UI pages is a challenge which many application developers are facing. In this blog post I will explain how we can expose R script as an API, using rApache and Apache webserver. &lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
rApache is a project supporting web application development using the R statistical language and environmentand the Apache web server.&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;
Exposing Rscipt as API typically involves 3 steps:&lt;br /&gt;
&lt;div&gt;
&lt;ol type=&quot;1&quot;&gt;
&lt;li&gt;Pre-requisites&lt;/li&gt;
&lt;li&gt;Installing rApache&lt;/li&gt;
&lt;li&gt;Configuring rApache&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Step #1 - Pre_requisites :
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Linux environment - ubuntu &lt;/li&gt;
&lt;li&gt;latest version of R  - R- 3.1.0&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
Step#2 - Install  apache webserver as below:&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;i&gt;apt-get install r-base-dev apache2-mpm-prefork apache2-prefork-dev&lt;br /&gt;wget http://www.rapache.net/rapache-1.2.3.tar.gz&lt;br /&gt;&amp;nbsp;tarxzvf rapache-1.2.3.tar.gz&lt;br /&gt;cd rapache-1.2.3&lt;br /&gt;&amp;nbsp;./configure&lt;br /&gt;&amp;nbsp;make&lt;br /&gt;make install&lt;/i&gt;&lt;/blockquote&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
Step#3: Configuring rApache.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Once rApache is installed, start the rApache server as below:&lt;/li&gt;
&lt;blockquote class=&quot;tr_bq&quot; style=&quot;text-align: left;&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;service apache2 start&lt;/i&gt;&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidWNZOmAcSrRY2nb0wBY1mUBVAQt1H6SwAkQBtTy4YPKZ_dMkhojRlGaDyi6yqFpmqyYzkA3vHVt_GOnh6DxFYdZa5dria1ve1GNzelNz0tzNpb9bjuAhRBFGJowXo_rSkB5dIhX9PW_Ws/s1600/rapche1.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidWNZOmAcSrRY2nb0wBY1mUBVAQt1H6SwAkQBtTy4YPKZ_dMkhojRlGaDyi6yqFpmqyYzkA3vHVt_GOnh6DxFYdZa5dria1ve1GNzelNz0tzNpb9bjuAhRBFGJowXo_rSkB5dIhX9PW_Ws/s320/rapche1.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;li&gt;Since we are installing for the first time let us test if the setup is installed properly. Do the below steps.&lt;/li&gt;
&lt;blockquote&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;sudo vim /etc/apache2/apache2.conf &lt;/i&gt;
&lt;i&gt;
#Added the following:&lt;/i&gt;

&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiSMWTBG6M3DsJEYMXc_W4ulCB_FoHzEdrH82piE33H6LmbYqCqhLSLSyzxgVFy0KopsK_zEMgKhNqgO7chY5fLmNO3Y2yDcoMzKVxZmAt4Bl3ORIV2UCDu-Iccp0pw5N5s3-W3qToQZitD/s1600/apache2.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;51&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiSMWTBG6M3DsJEYMXc_W4ulCB_FoHzEdrH82piE33H6LmbYqCqhLSLSyzxgVFy0KopsK_zEMgKhNqgO7chY5fLmNO3Y2yDcoMzKVxZmAt4Bl3ORIV2UCDu-Iccp0pw5N5s3-W3qToQZitD/s1600/apache2.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;li&gt;Once you have done the above step,  Now access the below pages:&lt;/li&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;
IPAddress/html/index.html&lt;/i&gt;&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOlneSowpLtGabSdrYaUrcj-YClP1deF7PIzwGJ_o_0G59Cd0mI-N4fYOO_k6mXKSqNwZHFWB4klkiYXx7hyIE3ZAON4RMZvQ4WJXrq5_rtg22Wqr09olpU7NX4-g4vSyZlI7mK7Hsa3Hn/s1600/apache3.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;182&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOlneSowpLtGabSdrYaUrcj-YClP1deF7PIzwGJ_o_0G59Cd0mI-N4fYOO_k6mXKSqNwZHFWB4klkiYXx7hyIE3ZAON4RMZvQ4WJXrq5_rtg22Wqr09olpU7NX4-g4vSyZlI7mK7Hsa3Hn/s1600/apache3.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;

IPAddress/RApacheInfo.html&lt;/i&gt;&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkAfhhSwsfe8ZF2B9mXjn_4PLsrhrMCtIaIH1pF3JfBoQ8odJku8KiPtODPep-QcIMe_PuN6RbBUzP9keqhx-_2MDyrcUnem0KOpSomOdhhnJic6R-rbaS5ENHUF-klv7ONOfHVXnbQENv/s1600/apache4.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;150&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkAfhhSwsfe8ZF2B9mXjn_4PLsrhrMCtIaIH1pF3JfBoQ8odJku8KiPtODPep-QcIMe_PuN6RbBUzP9keqhx-_2MDyrcUnem0KOpSomOdhhnJic6R-rbaS5ENHUF-klv7ONOfHVXnbQENv/s1600/apache4.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;li&gt;Before getting into expose R-script as API let us understand the rApache configuration.&lt;br /&gt;
Once the rApache is installed the folder structure of rApache would be as below:&lt;/li&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_PzzoY91_R5_iax11wu7JbWVyGvDDOFlsjPyqKzkhc9vwlC-3SIQNSX8VJPd5SueuBxSSAmK9LONHEo4_qfRJRZlWOLeYi0w-Yi4ykTQzv4WAzIP9LAud8AvpTZ3mo8rHOrMTg7Ay8w9A/s1600/rapche5.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;147&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_PzzoY91_R5_iax11wu7JbWVyGvDDOFlsjPyqKzkhc9vwlC-3SIQNSX8VJPd5SueuBxSSAmK9LONHEo4_qfRJRZlWOLeYi0w-Yi4ykTQzv4WAzIP9LAud8AvpTZ3mo8rHOrMTg7Ay8w9A/s1600/rapche5.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;li&gt;In apache2.conf file, we need to configure all the sites/r-scripts that we need to expose as api.. Below we have set a directory at path “/var/www/RProject“ in directory tags in apache2.conf file.&lt;/li&gt;
&lt;br /&gt;


&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIEj8XFdOoZyk29T9I-IKKemrJGfJHRQViWstcDQOFtl5_dXLntbP57MlEiyhM7FCeGQqnmomA6tg5hY_3-0tW_mous_LOxb_kk1eHDyClftNog1pj_UGI-qbE9CgYVR16F3dXLXF1rv7F/s1600/rapche6.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIEj8XFdOoZyk29T9I-IKKemrJGfJHRQViWstcDQOFtl5_dXLntbP57MlEiyhM7FCeGQqnmomA6tg5hY_3-0tW_mous_LOxb_kk1eHDyClftNog1pj_UGI-qbE9CgYVR16F3dXLXF1rv7F/s320/rapche6.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;li&gt;Now place the below R-script (test.r) generating a random normal distribution in the RProject folder specified above,in the code belowGET$p is input parameter we pass from URL.&lt;/li&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXaF_nn6H7KJkFshLwzY9kPhMbUP8XbZ3zxuxFz3UzD3sReFthjFoVR93Axsf9FiaFCkC_vfPXkn9nJUpU6-aIDMq-IhpSL-VRvXPUCG45FpVzvuMWsjlMUzXi5ZjZoHUp7laKQiwuGF98/s1600/rapache7.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXaF_nn6H7KJkFshLwzY9kPhMbUP8XbZ3zxuxFz3UzD3sReFthjFoVR93Axsf9FiaFCkC_vfPXkn9nJUpU6-aIDMq-IhpSL-VRvXPUCG45FpVzvuMWsjlMUzXi5ZjZoHUp7laKQiwuGF98/s320/rapache7.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;li&gt;The api is ready for testing.We can access the api using IP.XXX.XXX.XXX/RProject/test.r?q=10
And the results would be as below:&lt;/li&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgN3PydLpunGwZgF7obpsMTFRsopUe0l4KA8KcSQxjx07FwtPLL2guMivtKZc21SKCJYG64ZcTGPmMEn7L1ghuUapnUbJc1V6WCly7w-_4wsIrsmBT-ph_RqShoFRRserpU1Wky4UgAqZDj/s1600/rapache8.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgN3PydLpunGwZgF7obpsMTFRsopUe0l4KA8KcSQxjx07FwtPLL2guMivtKZc21SKCJYG64ZcTGPmMEn7L1ghuUapnUbJc1V6WCly7w-_4wsIrsmBT-ph_RqShoFRRserpU1Wky4UgAqZDj/s320/rapache8.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;li&gt;Detailed documentation of Installation is found &lt;a href=&quot;http://rapache.net/manual.html&quot;&gt;here&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/3208259409633098593/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2015/04/r-as-service.html#comment-form' title='33 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/3208259409633098593'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/3208259409633098593'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2015/04/r-as-service.html' title='Exposing R-script as API'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidWNZOmAcSrRY2nb0wBY1mUBVAQt1H6SwAkQBtTy4YPKZ_dMkhojRlGaDyi6yqFpmqyYzkA3vHVt_GOnh6DxFYdZa5dria1ve1GNzelNz0tzNpb9bjuAhRBFGJowXo_rSkB5dIhX9PW_Ws/s72-c/rapche1.png" height="72" width="72"/><thr:total>33</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-6023779921425891212</id><published>2014-10-05T00:35:00.000+04:00</published><updated>2017-10-17T07:42:39.692+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="linear regression"/><category scheme="http://www.blogger.com/atom/ns#" term="machine learning"/><category scheme="http://www.blogger.com/atom/ns#" term="multiple regression"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="Regression"/><title type='text'>Regression Analysis using R</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div&gt;
&lt;b&gt;What is a Prediction Problem?&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
A business problem which involves predicting future events by extracting patterns in the historical data. Prediction problems are solved using Statistical techniques, mathematical models or machine learning techniques.&lt;/div&gt;
&lt;div&gt;
For example: Forecasting stock price for the next week, predicting which football team wins the world cup, etc.&lt;br /&gt;
&lt;b&gt;&lt;/b&gt;&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;What is Regression analysis, where is it applicable?&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
While dealing with any prediction problem, the easiest, most widely used yet powerful technique is the Linear Regression. Regression analysis is used for modeling the relationship between a response variable and one or more input variables.&lt;/div&gt;
&lt;div&gt;
In simpler terms,Regression Analysis helps us to find answers to:&lt;/div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Prediction of Future observations&lt;/li&gt;
&lt;li&gt;find association, relationship between variables.&lt;/li&gt;
&lt;li&gt;Identify which variables contribute more towards predicting the future outcomes.Types of regression problems:&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
Simple Linear Regression:&lt;/h4&gt;
&amp;nbsp;If model deals with one input, called as independent or predictor variable and one output variable, called as dependent or response variable then it is called Simple Linear Regression. In this type of Linear regression, &lt;i&gt;it assumes that there exists a linear relation between predictor and response variable of the form&lt;/i&gt;.&lt;br /&gt;
&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt;&quot;&gt;Y ≈&amp;nbsp;β0&amp;nbsp;+ β1X +&amp;nbsp;e.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-hXQRn0Q87JsiCsXuF_z3tN_F-WkSRS_PqumUBYeOCTz32jRa56s-wEm4LM9LZcEb7bVV5JmrPr-m0I-fbMsyLZ9XyOgpEnVWxNzYCrpqhuGqa0BmZHX7SflpKNUNAXJBmuondc1fo_ql/s1600/regression.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;236&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-hXQRn0Q87JsiCsXuF_z3tN_F-WkSRS_PqumUBYeOCTz32jRa56s-wEm4LM9LZcEb7bVV5JmrPr-m0I-fbMsyLZ9XyOgpEnVWxNzYCrpqhuGqa0BmZHX7SflpKNUNAXJBmuondc1fo_ql/s320/regression.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: left;&quot;&gt;
In the above equation, β0,β1 are the unknown constants that represent intercept and slop of a straight line which we learned in our high schools. These known constants are known as the model coefficients or parameters. From the above equation, X is the known input variable and if we can estimate β0,β1 by some method then Y can be predicted.
In order to predict future outcomes, by using the training data we need to estimate the unknown model parameters (ˆ β0,ˆ β1) using the equation.&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt;&quot;&gt;ˆy = ˆ β0 + ˆ β1x + ˆe,&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt; text-align: left;&quot;&gt;where
ˆ y,ˆ β0,ˆ β1 are the estimates.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Multiple Linear Regression:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
If the problem contains more than one input variables  and one response variable, then it is called Multiple Linear regression.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;How do we apply Regression analysis using R?&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
Let us apply regression analysis on power plant dataset available from here. The dataset contains 9568 data points collected from a Combined Cycle Power Plant over 6 years (2006-2011), when the power plant was set to work with full load. Features consist of hourly average ambient variables Temperature (T), Ambient Pressure (AP), Relative Humidity (RH) and Exhaust Vacuum (V) to predict the net hourly electrical energy output (EP) of the plant.&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt; text-indent: -0.25in;&quot;&gt;Read
the data into R environment:&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;pre style=&quot;margin-left: .5in; word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;require(xlsx)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;margin-left: .5in; word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;sample1 = read.xlsx(&quot;C:\\Suresh\\blogs\\datasets\\CCPP\\Folds5x2_pp.xlsx&quot;,sheetIndex=1)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;div class=&quot;MsoListParagraphCxSpFirst&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;/div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt; text-indent: -0.25in;&quot;&gt;Understand
and observing the data:&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;View(sample1)&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgw1WmxT0daTM3AOfOKXOezjvcLtvR-s8DCDeL68bPbRmXhhqcUTrllo0cBXTr6YILEaDy3ALD8AfdvTFH0hkHPBIDmQoihji2CaBc3WNk8fONnwwtHH9vTbjQ5bPzD0FSxUCf2fcJe8MQa/s1600/reg_1.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgw1WmxT0daTM3AOfOKXOezjvcLtvR-s8DCDeL68bPbRmXhhqcUTrllo0cBXTr6YILEaDy3ALD8AfdvTFH0hkHPBIDmQoihji2CaBc3WNk8fONnwwtHH9vTbjQ5bPzD0FSxUCf2fcJe8MQa/s400/reg_1.PNG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraph&quot; style=&quot;margin-bottom: 0.0001pt; text-indent: 0.5in;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt;&quot;&gt;Check for missing values, range of
variables, density plots for each of the varaible:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;pre style=&quot;margin-left: .5in; word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;margin-left: .5in; word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;sum(is.na(sample1$AT))&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;margin-left: .5in; word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;[1] 0&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;margin-left: .5in; word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;range(sample1$AT) #1.81,37.11&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;margin-left: .5in; word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;mean(sample1$AT) #m: 19.65&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;margin-left: .5in; word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;plot(density(sample1$AT))&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;margin-left: .5in; word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;Plot(sample1)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;margin-left: .5in; word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVqe4WKMUCTWmG2xJgTVL-dUPSM0SD_DGzWXdWBuyiL-2tm_uT4mXvJUELycgW2fRVJk84Q-VNsfhu0uKVxNY4bQ2G2KDpVH8j_XolVvsKRKKvfMHYwfa9hAP7PZN1qi7i8oCUFWyzhfyk/s1600/reg_density_plot.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;186&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVqe4WKMUCTWmG2xJgTVL-dUPSM0SD_DGzWXdWBuyiL-2tm_uT4mXvJUELycgW2fRVJk84Q-VNsfhu0uKVxNY4bQ2G2KDpVH8j_XolVvsKRKKvfMHYwfa9hAP7PZN1qi7i8oCUFWyzhfyk/s320/reg_density_plot.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;pre style=&quot;margin-left: .5in; word-break: break-all;&quot;&gt;Density plot for Temperature&lt;/pre&gt;
&lt;pre style=&quot;margin-left: .5in; word-break: break-all;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhoy21bscAtNGk3fCLdFnvIMebIe5ZRnPTnGCXiM3xzMsnAATj6rNvr_gkfCQdr86SYTQMV12gG4FwQA0tQq-qt78vqSfJzlwPyhp68e-oi9Z8uLmuFsyMOCUAwPGc5WZw_fjXqiQtV66q_/s1600/reg_scatter_plot.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhoy21bscAtNGk3fCLdFnvIMebIe5ZRnPTnGCXiM3xzMsnAATj6rNvr_gkfCQdr86SYTQMV12gG4FwQA0tQq-qt78vqSfJzlwPyhp68e-oi9Z8uLmuFsyMOCUAwPGc5WZw_fjXqiQtV66q_/s400/reg_scatter_plot.PNG&quot; /&gt;&lt;/a&gt;&lt;/pre&gt;
&lt;pre style=&quot;margin-left: .5in; text-align: center; word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;pre style=&quot;margin-left: .5in; text-align: center; word-break: break-all;&quot;&gt;&lt;/pre&gt;
&lt;pre style=&quot;margin-left: .5in; text-align: center; word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;Scatter plots shows us that temperature (AT) and vaccum(V) are inversely related to power while pressure(AP) and RH are not related.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt; text-align: left; text-indent: -0.25in;&quot;&gt;Check
for correlation among the variables. This step is very important to understand
the relation of dependant variable with the independent variables and
correlations among the variables. In general, there shouldn’t be any
correlation among the independent variables.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
cor(sample1)&lt;br /&gt;
&lt;pre style=&quot;margin-left: .5in; word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;&#39;AT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; V&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AP&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RH&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PE&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;margin-left: .5in; word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;AT&amp;nbsp; 1.0000000&amp;nbsp; 0.8441067 -0.50754934 -0.54253465 -0.9481285&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;margin-left: .5in; word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;V&amp;nbsp;&amp;nbsp; 0.8441067&amp;nbsp; 1.0000000 -0.41350216 -0.31218728 -0.8697803&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;margin-left: .5in; word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;AP -0.5075493 -0.4135022&amp;nbsp; 1.00000000&amp;nbsp; 0.09957432&amp;nbsp; 0.5184290&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;margin-left: .5in; word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;RH -0.5425347 -0.3121873&amp;nbsp; 0.09957432&amp;nbsp; 1.00000000&amp;nbsp; 0.3897941&#39;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;margin-left: .5in; word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;&#39;inferences--&amp;gt; AT has -ve relation with PE&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;margin-left: .5in; word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;V is highly related to PE&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;margin-left: .5in; word-break: break-all;&quot;&gt;&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;other two are relatively related&#39;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt; text-indent: -0.25in;&quot;&gt;Divide &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt; text-indent: -0.25in;&quot;&gt;the data into training and test set and train the model with linear regression &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt; text-indent: -0.25in;&quot;&gt;using lm()  method available in R and thendo predictions on new test data using &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt; text-indent: -0.25in;&quot;&gt;predict() method.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/pre&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;text-align: justify;&quot;&gt;rand=sample1[sample(nrow(sample1)),]&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;tr = rand[1:6697,]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;ts = rand[6698:9568,]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;;&quot;&gt;&lt;span style=&quot;color: red;&quot;&gt;model2 = lm(PE~AT+V+AP+RH,data=tr)&lt;/span&gt;&lt;span style=&quot;font-size: 9pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;summary(model2)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: #e1e2e5; color: #00b050; font-family: &amp;quot;lucida console&amp;quot;; font-size: 9.0pt;&quot;&gt;Call:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: #e1e2e5; color: #00b050; font-family: &amp;quot;lucida console&amp;quot;; font-size: 9.0pt;&quot;&gt;lm(formula = PE ~ AT + V + AP + RH, data = tr)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: #e1e2e5; color: #00b050; font-family: &amp;quot;lucida console&amp;quot;; font-size: 9.0pt;&quot;&gt;Residuals:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: #e1e2e5; color: #00b050; font-family: &amp;quot;lucida console&amp;quot;; font-size: 9.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Min&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1Q&amp;nbsp;
Median&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3Q&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Max &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: #e1e2e5; color: #00b050; font-family: &amp;quot;lucida console&amp;quot;; font-size: 9.0pt;&quot;&gt;-43.533&amp;nbsp; -3.170&amp;nbsp; -0.068&amp;nbsp;&amp;nbsp;
3.229&amp;nbsp; 17.451 &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: #e1e2e5; color: #00b050; font-family: &amp;quot;lucida console&amp;quot;; font-size: 9.0pt;&quot;&gt;Coefficients:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: #e1e2e5; color: #00b050; font-family: &amp;quot;lucida console&amp;quot;; font-size: 9.0pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Estimate Std.
Error&amp;nbsp; t value Pr(&amp;gt;|t|)&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: #e1e2e5; color: #00b050; font-family: &amp;quot;lucida console&amp;quot;; font-size: 9.0pt;&quot;&gt;(Intercept) 457.729155&amp;nbsp;
11.794172&amp;nbsp;&amp;nbsp; 38.810&amp;nbsp; &amp;lt; 2e-16 ***&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: #e1e2e5; color: #00b050; font-family: &amp;quot;lucida console&amp;quot;; font-size: 9.0pt;&quot;&gt;AT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -1.987307&amp;nbsp;&amp;nbsp; 0.018208 -109.147&amp;nbsp; &amp;lt; 2e-16 ***&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: #e1e2e5; color: #00b050; font-family: &amp;quot;lucida console&amp;quot;; font-size: 9.0pt;&quot;&gt;V&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -0.231996&amp;nbsp;&amp;nbsp; 0.008692&amp;nbsp;
-26.689&amp;nbsp; &amp;lt; 2e-16 ***&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: #e1e2e5; color: #00b050; font-family: &amp;quot;lucida console&amp;quot;; font-size: 9.0pt;&quot;&gt;AP&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.059235&amp;nbsp;&amp;nbsp; 0.011442&amp;nbsp;&amp;nbsp;&amp;nbsp;
5.177 2.32e-07 ***&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: #e1e2e5; color: #00b050; font-family: &amp;quot;lucida console&amp;quot;; font-size: 9.0pt;&quot;&gt;RH&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; -0.159916&amp;nbsp;&amp;nbsp; 0.005015&amp;nbsp;
-31.886&amp;nbsp; &amp;lt; 2e-16 ***&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: #e1e2e5; color: #00b050; font-family: &amp;quot;lucida console&amp;quot;; font-size: 9.0pt;&quot;&gt;---&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: #e1e2e5; color: #00b050; font-family: &amp;quot;lucida console&amp;quot;; font-size: 9.0pt;&quot;&gt;Signif. codes:&amp;nbsp; 0 ‘***’ 0.001 ‘**’
0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
Residual standard error: 4.585 on 6692 degrees of freedom&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: #e1e2e5; color: #00b050; font-family: &amp;quot;lucida console&amp;quot;; font-size: 9.0pt;&quot;&gt;Multiple R-squared:&amp;nbsp; 0.9281,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Adjusted R-squared:&amp;nbsp; 0.9281 &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: #e1e2e5; color: #00b050; font-family: &amp;quot;lucida console&amp;quot;; font-size: 9.0pt;&quot;&gt;F-statistic: 2.161e+04 on 4 and 6692 DF,&amp;nbsp;
p-value: &amp;lt; 2.2e-16&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
New predictions are made using predict method.&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;pred = predict(model,ts[,1:4])&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijSnr72P3rwRioU1_eKKE5Cib-9O_DAgQdAKQ_z65obaJq9Kbth2g_tRSExTd3MkoZBxwo2fdFnhJ5RWc7dHUQHSMGzIeQTWzz-S5b9LfL4kam6xAvtixnJtpyl1dkSh83p4Vx5NIV5heA/s1600/reg_results.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;75&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijSnr72P3rwRioU1_eKKE5Cib-9O_DAgQdAKQ_z65obaJq9Kbth2g_tRSExTd3MkoZBxwo2fdFnhJ5RWc7dHUQHSMGzIeQTWzz-S5b9LfL4kam6xAvtixnJtpyl1dkSh83p4Vx5NIV5heA/s320/reg_results.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;; font-size: 9pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt; text-align: left;&quot;&gt;The
above image shows the results of actual vs predicted which are quite accurate.
In the summary results of&amp;nbsp; the model,
below are the key takeways:&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraphCxSpMiddle&quot; style=&quot;margin-bottom: 0.0001pt; text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt; text-align: left; text-indent: -0.25in;&quot;&gt;Model
is accurate as R&lt;/span&gt;&lt;sup style=&quot;font-family: &#39;Times New Roman&#39;, serif; text-align: left; text-indent: -0.25in;&quot;&gt;2 &lt;/sup&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt; text-align: left; text-indent: -0.25in;&quot;&gt;is near to 1 (0.912).&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt; text-align: left; text-indent: -0.25in;&quot;&gt;Model
states all the variables are significant, the ***&amp;nbsp; indicate the significance.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;symbol&amp;quot;; font-size: 12pt; text-align: left; text-indent: -0.25in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot;; font-size: 7pt;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt; text-align: left; text-indent: -0.25in;&quot;&gt;P-statistics
is less than 0.05, F-statistics is significantly high.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt; text-align: left; text-indent: -0.25in;&quot;&gt;Residuals
vs fitted plots and Q-Q normal plots are also good with mean variance of the
errors around 0.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOtGpwGx1ZmqY3X6S8mA96kviXehiBU2tk-7TMCLII5Yab5OiIKlz2B7w1akIID0oJVgJmfryCbj8De-xtCnjRwQ6nLHoj_Nl3AowbvR0qf5DrunvO3BTVuY5itvwx6otzCs-bwRnTDwpo/s1600/reg_model_valid.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;96&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOtGpwGx1ZmqY3X6S8mA96kviXehiBU2tk-7TMCLII5Yab5OiIKlz2B7w1akIID0oJVgJmfryCbj8De-xtCnjRwQ6nLHoj_Nl3AowbvR0qf5DrunvO3BTVuY5itvwx6otzCs-bwRnTDwpo/s320/reg_model_valid.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div align=&quot;center&quot; class=&quot;MsoListParagraphCxSpLast&quot; style=&quot;margin-bottom: 0.0001pt; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt;&quot;&gt;In
the next blog we learn about model validations and extensions in linear
regression.&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/6023779921425891212/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2014/10/regression-analysis-using-r.html#comment-form' title='20 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/6023779921425891212'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/6023779921425891212'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2014/10/regression-analysis-using-r.html' title='Regression Analysis using R'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-hXQRn0Q87JsiCsXuF_z3tN_F-WkSRS_PqumUBYeOCTz32jRa56s-wEm4LM9LZcEb7bVV5JmrPr-m0I-fbMsyLZ9XyOgpEnVWxNzYCrpqhuGqa0BmZHX7SflpKNUNAXJBmuondc1fo_ql/s72-c/regression.png" height="72" width="72"/><thr:total>20</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-2713806405574667273</id><published>2014-07-31T12:35:00.000+04:00</published><updated>2017-10-17T07:43:21.531+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="classification"/><category scheme="http://www.blogger.com/atom/ns#" term="machine learning"/><category scheme="http://www.blogger.com/atom/ns#" term="Precision"/><category scheme="http://www.blogger.com/atom/ns#" term="recall"/><category scheme="http://www.blogger.com/atom/ns#" term="Statistics"/><title type='text'>Assessing Model Accuracy - Part 2</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
In my &lt;a href=&quot;http://www.dataperspective.info/2014/06/assessing-model-accuracy-part1.html&quot; target=&quot;_blank&quot;&gt;last post&lt;/a&gt;, I have explained about MSE, today I will explain the variance &amp;amp; bias trade-off, Precision recall trade-off  while assessing the model accuracy.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;What is Variance and bias of a statistical learning Method?&lt;/b&gt;&lt;br /&gt;
Variance refers to the amount by which the estimated output (f) would change if we estimated it (f) using a different training dataset. Since the training data is used to fit the statistical learning method, different training sets will result in different outputs (f).
&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
&lt;i&gt;Ideally, the estimate should not vary much between training sets.&lt;/i&gt;
&lt;br /&gt;
Bias refers to the error that is introduced by approximating a complicated problem by a simpler model.
&lt;br /&gt;
&lt;i&gt;For example:&lt;/i&gt; Consider the distribution of dataset (black fit curve). If a simple linear regression (orange fit curve) is fitted for a dataset which actually needs much flexible model (blue fit curve), the simple linear regression model induces Bias in the model.&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjs9th8w6TEf6mq8exd1hRQnLQqsQpCGR3x-uuPyRlKdHEtvUIDF_5PeBPe4Hy_UJO_J9P4n1sQqlA14n9rvB8ykq-mCngcO9vRxJZCZKb1qto7A_N1vG1zPEcTXdBMvfIacBhqwq2OVpGA/s1600/variance_bias.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjs9th8w6TEf6mq8exd1hRQnLQqsQpCGR3x-uuPyRlKdHEtvUIDF_5PeBPe4Hy_UJO_J9P4n1sQqlA14n9rvB8ykq-mCngcO9vRxJZCZKb1qto7A_N1vG1zPEcTXdBMvfIacBhqwq2OVpGA/s400/variance_bias.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;i&gt;Fig: linear regression provides a very poor fit to the data&lt;/i&gt;&lt;/div&gt;
&lt;b&gt;Explanation:&lt;/b&gt;&lt;br /&gt;
The test MSE calculated here, can be decomposed into three properties, Variance, Bias, error.&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
E(y0 − ˆ f(x0))^2 = Var( ˆ f(x0)) + [Bias( ˆ f(x0))]^2 +Var(ɛ). &lt;/div&gt;
Where E(y0 − ˆ f(x0))^2 defines the expected test MSE, first part of the equation is the variance, second part is the bias and the third part is the variance of error.&lt;br /&gt;
In order to develop best model for any analysis, we need to select a statistical method which achieves low Variance and low bias. This is called Variance-bias trade-off.
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6nBmMpCXOHXLcSIk8bT0uIXhiBd_b9_pImeDY-3SXtJ45corTeucWejQVBhqmW_dSJydxvgn_w4TlNg3dTmIA-HR3uUy2E4TVTLiu4wYWjOfGHzNeUcqgzsD65oJywrwgipOb-6huonS3/s1600/variance+bais+tradeoff.jpg&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6nBmMpCXOHXLcSIk8bT0uIXhiBd_b9_pImeDY-3SXtJ45corTeucWejQVBhqmW_dSJydxvgn_w4TlNg3dTmIA-HR3uUy2E4TVTLiu4wYWjOfGHzNeUcqgzsD65oJywrwgipOb-6huonS3/s400/variance+bais+tradeoff.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;Fig: Variance-bias trade-off - Below explanation explains the details.&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/div&gt;
As a general rule, as the complexity of the model increases the variance will increase &amp;amp; Bias will decrease. The relative rate of change of these two quantities determines whether the test MSE increases or decreases. Initially, as the flexibility increases the bias decreases faster than the variance increases. As a result, test MSE decreases. However, at some point increasing flexibility has little effect on the bias and after this point the variance tends to increase significantly. This point can be treated as optimal point for model selection.
As an end note, while assessing the model accuracy we need to take variance-bias trade-off into consideration.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;What is Precision-Recall of a statistical learning Method?&lt;/b&gt;&lt;br /&gt;
While dealing with a classification problem, we validate the relevance of the model using Precision Recall methods.&lt;br /&gt;
Consider the below confusion matrix, one of the best ways to represent the results of a classifier:&lt;br /&gt;
&lt;br /&gt;
&lt;div align=&quot;center&quot;&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoNormalTable&quot; style=&quot;border-collapse: collapse; margin-left: 4.55pt; mso-padding-alt: 0cm 0cm 0cm 0cm; mso-yfti-tbllook: 1184; width: 371px;&quot;&gt;
 &lt;tbody&gt;
&lt;tr style=&quot;height: 15.0pt; mso-yfti-firstrow: yes; mso-yfti-irow: 0;&quot;&gt;
  &lt;td nowrap=&quot;&quot; style=&quot;border: solid windowtext 1.0pt; height: 15.0pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 56.0pt;&quot; valign=&quot;bottom&quot; width=&quot;75&quot;&gt;&lt;/td&gt;
  &lt;td nowrap=&quot;&quot; style=&quot;border-left: none; border: solid windowtext 1.0pt; height: 15.0pt; mso-border-left-alt: solid windowtext 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 48.25pt;&quot; valign=&quot;bottom&quot; width=&quot;64&quot;&gt;&lt;/td&gt;
  &lt;td colspan=&quot;2&quot; nowrap=&quot;&quot; style=&quot;border-left: none; border: solid windowtext 1.0pt; height: 15.0pt; mso-border-left-alt: solid windowtext 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 174.0pt;&quot; width=&quot;232&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12.0pt;&quot;&gt;ACTUAL&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 15.0pt; mso-yfti-irow: 1;&quot;&gt;
  &lt;td nowrap=&quot;&quot; style=&quot;border-top: none; border: solid windowtext 1.0pt; height: 15.0pt; mso-border-top-alt: solid windowtext 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 56.0pt;&quot; valign=&quot;bottom&quot; width=&quot;75&quot;&gt;&lt;/td&gt;
  &lt;td nowrap=&quot;&quot; style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 15.0pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 48.25pt;&quot; valign=&quot;bottom&quot; width=&quot;64&quot;&gt;&lt;/td&gt;
  &lt;td nowrap=&quot;&quot; style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 15.0pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 88.5pt;&quot; valign=&quot;bottom&quot; width=&quot;118&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12.0pt;&quot;&gt;POSITIVE&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td nowrap=&quot;&quot; style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 15.0pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 85.5pt;&quot; valign=&quot;bottom&quot; width=&quot;114&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12.0pt;&quot;&gt;NEGATIVE&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 15.0pt; mso-yfti-irow: 2;&quot;&gt;
  &lt;td nowrap=&quot;&quot; rowspan=&quot;2&quot; style=&quot;border-top: none; border: solid windowtext 1.0pt; height: 15.0pt; mso-border-top-alt: solid windowtext 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 56.0pt;&quot; width=&quot;75&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12.0pt;&quot;&gt;PREDICTED&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td nowrap=&quot;&quot; style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 15.0pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 48.25pt;&quot; valign=&quot;bottom&quot; width=&quot;64&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12.0pt;&quot;&gt;POSITIVE&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td nowrap=&quot;&quot; style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 15.0pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 88.5pt;&quot; valign=&quot;bottom&quot; width=&quot;118&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;i&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12.0pt;&quot;&gt;TRUE
  POSITIVE&lt;/span&gt;&lt;/i&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td nowrap=&quot;&quot; style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 15.0pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 85.5pt;&quot; valign=&quot;bottom&quot; width=&quot;114&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;i&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12.0pt;&quot;&gt;FALSE
  POSITIVE&lt;/span&gt;&lt;/i&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 15.0pt; mso-yfti-irow: 3; mso-yfti-lastrow: yes;&quot;&gt;
  &lt;td nowrap=&quot;&quot; style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 15.0pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 48.25pt;&quot; valign=&quot;bottom&quot; width=&quot;64&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12.0pt;&quot;&gt;NEGATIVE&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td nowrap=&quot;&quot; style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 15.0pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 88.5pt;&quot; valign=&quot;bottom&quot; width=&quot;118&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;i&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12.0pt;&quot;&gt;FALSE
  NEGATIVE&lt;/span&gt;&lt;/i&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td nowrap=&quot;&quot; style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 15.0pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 85.5pt;&quot; valign=&quot;bottom&quot; width=&quot;114&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;i&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12.0pt;&quot;&gt;TRUE
  NEGATIVE&lt;/span&gt;&lt;/i&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;Let’s understand the confusion matrix:&lt;/b&gt;&lt;br /&gt;
TRUE POSITVE: The actual is +ve and model predicted +ve.&lt;br /&gt;
FALSE POSITIVE: The actual is –ve but the model predicted positive, FALSE ALARM&lt;br /&gt;
FALSE NEGATIVE: The actual is +ve but the model predicted negative - A MISS&lt;br /&gt;
TRUE NEGATIVE: The actual is –ve &amp;amp; model predicted –ve.&lt;br /&gt;
&lt;b&gt;Precision: % of the predicted values that are correct.&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;Precision = TP/(TP+FP)&lt;/i&gt;&lt;/div&gt;
&lt;b&gt;Recall: % of the correct items that are relevant.&lt;/b&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;Recall = TP/(TP+FN)&lt;/i&gt;&lt;/div&gt;
Both Precision &amp;amp; Recall are inversely related, if precision increases, the recall falls, vice versa. Trade-off between Precision and Recall varies from problem to problem. For example, in Legal document classification problem, model needs to have high Recall, as the model needs to extract/classify more relevant.&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/2713806405574667273/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2014/07/assessing-model-accuracy.html#comment-form' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/2713806405574667273'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/2713806405574667273'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2014/07/assessing-model-accuracy.html' title='Assessing Model Accuracy - Part 2'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjs9th8w6TEf6mq8exd1hRQnLQqsQpCGR3x-uuPyRlKdHEtvUIDF_5PeBPe4Hy_UJO_J9P4n1sQqlA14n9rvB8ykq-mCngcO9vRxJZCZKb1qto7A_N1vG1zPEcTXdBMvfIacBhqwq2OVpGA/s72-c/variance_bias.png" height="72" width="72"/><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-7901549515838858904</id><published>2014-06-21T22:42:00.000+04:00</published><updated>2015-12-28T09:56:24.462+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="machine learning"/><category scheme="http://www.blogger.com/atom/ns#" term="model assess"/><category scheme="http://www.blogger.com/atom/ns#" term="Statistics"/><title type='text'>Assessing Model Accuracy - Part1</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Recently, I have started reading a book &quot;Introduction to statistical Learning&quot;, which had good introduction for model accuracy assessing. This post contains excerpts of the chapter:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Often we take different statistical approaches to build a solution for a data analytical problem. Why is it necessary to introduce so many different approaches, rather than a single best method? The answer is: in Statistics no single method dominates all other methods over all possible datasets. One statistical method may work well with a specific dataset and some other method may work better on a similar but different dataset. So it is important to decide for a particular dataset which method produces best results.&lt;/span&gt;&lt;/div&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&amp;nbsp;In order to evaluate the performance of a statistical learning method, we need to measure how well its predictions actually match the observed data. Today we will look into differnt measures which help us in assessing the model accuracy:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&amp;nbsp;Mean Squared Error&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&amp;nbsp;Variance/bias&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;
Mean Squared Error:&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&amp;nbsp;In regression Analysis, the most commonly used measure is Mean Squared Error, given by the equation:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxVOOurUH3Mrmwe8QPvK_mct0isRKCEy7fxTK3B_TX2CQUqI8dEocZRsNxGTZlwROYCmbgSXA03E4voh-vVnIYLSSolhEa1lVbxZgeDbSGnOuynfN9LZgaHK8faEe3QqI5I5JYf7qrLeoA/s1600/mse_training.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;55&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxVOOurUH3Mrmwe8QPvK_mct0isRKCEy7fxTK3B_TX2CQUqI8dEocZRsNxGTZlwROYCmbgSXA03E4voh-vVnIYLSSolhEa1lVbxZgeDbSGnOuynfN9LZgaHK8faEe3QqI5I5JYf7qrLeoA/s1600/mse_training.PNG&quot; width=&quot;200&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&amp;nbsp;where f(xi) is the prediction that f gives for the ith  observation.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;i&gt;The MSE will be small if the predicted responses are very close to the true responses, and will be large if for some of the observations, the predicted and true responses differ substantially. The MSE calculated above is called training MSE&lt;/i&gt;. But here we need to make a note that we would be more interested in the accuracy of test MSE, i.e. the MSE of previously unseen test data on the model rather than training MSE. The equation for calculating the test MSE is&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Test MSE =Avg(f(x0)-y0)^2&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&amp;nbsp;Where test MSE is calculated by squared average of the difference between the actual (y0) &amp;amp; estimated f(x0) on.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&amp;nbsp;Consider the below situation where 3 different regression model fits are applied on a randomly generated data set and is shown in below fig:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGYSy3IyYIMmDC9g9NfGB3Zisauy9vynaWUGY5AnTPdBOMIJU5LbCcHJvZcWBET_ZmzQgXG1cTzg01JPpArtdvuKPht8KLJiaV0Dfbumvd4lqqeFrWBLtQXCO2wUovemYDKFxFH89BVQu4/s1600/Mean+Squared+Error.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;186&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGYSy3IyYIMmDC9g9NfGB3Zisauy9vynaWUGY5AnTPdBOMIJU5LbCcHJvZcWBET_ZmzQgXG1cTzg01JPpArtdvuKPht8KLJiaV0Dfbumvd4lqqeFrWBLtQXCO2wUovemYDKFxFH89BVQu4/s320/Mean+Squared+Error.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;Note: The above images are taken from An Introduction to Statistical Learning - Authors: Gareth James, Daniela Witten, Trevor Hastie and Robert Tibshirani&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;In the above figure, black curve is actual distribution of data, orange, blue, green are three different fits to the original data distribution. For the right portion of the image we can say that the model fit representing the blue curve is more near to the original &amp;amp; it is the ideal model for prediction.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&amp;nbsp;Now consider left part of image, the red curve is the test MSE for three model fits &amp;amp; the grey curve is the training MSE for the same three model fits. We can observe that the grey curve, the training MSE will keep on declining telling us that with more complex models than required, the MSE will be high. On the other hand, the test MSE is a U shaped curve which decreases initially and then raises. The ideal model is the one after which the test MSE increases. In our example it is at the Blue point where the curve starts raising.
The model we built should not result in Overfitting, a condition in which the method yields small training MSE but a large test MSE. It means that the modelled fit/method is trying to so hard to find patterns in the data and fitting up even though the occurrences were by chance in reality.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Georgia, Times New Roman, serif;&quot;&gt;&amp;nbsp;In my &lt;a href=&quot;http://www.dataperspective.info/2014/07/assessing-model-accuracy.html&quot; target=&quot;_blank&quot;&gt;next post&lt;/a&gt; will explain about Variance-Bias trade-off while choosing a best fit model.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/7901549515838858904/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2014/06/assessing-model-accuracy-part1.html#comment-form' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/7901549515838858904'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/7901549515838858904'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2014/06/assessing-model-accuracy-part1.html' title='Assessing Model Accuracy - Part1'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxVOOurUH3Mrmwe8QPvK_mct0isRKCEy7fxTK3B_TX2CQUqI8dEocZRsNxGTZlwROYCmbgSXA03E4voh-vVnIYLSSolhEa1lVbxZgeDbSGnOuynfN9LZgaHK8faEe3QqI5I5JYf7qrLeoA/s72-c/mse_training.PNG" height="72" width="72"/><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-8915994340923869152</id><published>2014-05-25T11:03:00.000+04:00</published><updated>2017-10-17T07:43:58.945+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="collaborative filtering"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="Recommendation Engine"/><title type='text'>Basic recommendation engine using R</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div&gt;
In our day to day life, we come across a large number of Recommendation engines like Facebook Recommendation Engine for Friends’ suggestions, and suggestions of similar Like Pages, Youtube recommendation engine suggesting videos similar to our previous searches/preferences. In today’s blog post I will explain how to build a basic recommender System.&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSu1b0C6KS_tNjR-dOM0zci-ShahpAvTmipIpyfMRJKC_0Zb7TDCa1k76-JHC2Pj_rrQkAOALEzxTpDA0aSn4Q1kNaOwLP3DlYApjw9sCc2Ysm7UYk34ksdCD9bafhV05h6lrsYXDbwpNI/s1600/Recommender+Engine.jpg&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSu1b0C6KS_tNjR-dOM0zci-ShahpAvTmipIpyfMRJKC_0Zb7TDCa1k76-JHC2Pj_rrQkAOALEzxTpDA0aSn4Q1kNaOwLP3DlYApjw9sCc2Ysm7UYk34ksdCD9bafhV05h6lrsYXDbwpNI/s320/Recommender+Engine.jpg&quot; width=&quot;185&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;h4 style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;/h4&gt;
&lt;h4 style=&quot;clear: both; text-align: left;&quot;&gt;
Types of Collaborative Filtering:&lt;/h4&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;User based Collaborative Filtering&lt;/li&gt;
&lt;li&gt;Item based Collaborative filtering&lt;/li&gt;
&lt;/ol&gt;
&amp;nbsp;In this post will explain about User based Collaborative Filtering. 
This algorithm usually works by searching a large group of people and finding a smaller set with tastes similar to yours. It looks at other things they like and combines them to create a ranked list of suggestions.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Implementing User Based Collaborative Filtering:&lt;/b&gt;&lt;br /&gt;
This involves two steps:
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Calculating Similarity Function&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Recommend items to users based on user Similarity Score&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
Consider the below data sample of Movie critics and their movie rankings, the objective is to recommend the unrated movies based on similar users:&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjcZRMS9D92Maclg96B46WhIHlZ6PM__Y7fa4-sN3kY59CEhoIKTNh3sxHkwyUT5tndtqu_PECpB4ie706N_zcOZSrby9Wr9WzuhR5w7hIIuFd0ro5RoS1cOEaDVgcVxvRmrNDud6-fOjg7/s1600/1.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;132&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjcZRMS9D92Maclg96B46WhIHlZ6PM__Y7fa4-sN3kY59CEhoIKTNh3sxHkwyUT5tndtqu_PECpB4ie706N_zcOZSrby9Wr9WzuhR5w7hIIuFd0ro5RoS1cOEaDVgcVxvRmrNDud6-fOjg7/s320/1.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXU294UWBgjL_0XmlH9RVw-_uI9f2taPTiqLZQO32tJCpIhW2t8f2qZ7_AJn6RKMTByVnNcBCemeRi38edPB319w7uj9RxgiM2O_OdvNzBbsAhy8gRNjhLLkQUU9AQjHVcdUB9pAqncj8t/s1600/1.PNG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;br /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;h4 style=&quot;clear: both; text-align: left;&quot;&gt;
Step1- Calculate Similarity Score for CHAN:&lt;/h4&gt;
&lt;/div&gt;
Creating Similarity score for people helps us to identify similar people. We use Cosine based Similarity function to calculate the similarity between the users. Know more about cosine similarity &lt;a href=&quot;http://en.wikipedia.org/wiki/Cosine_similarity&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;. In R we have a cosine function readily available:
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;background-color: #cfe2f3;&quot;&gt;user_sim = cosine(as.matrix(t(x)))
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
Step2- recommending Movies for CHAN:&lt;/h4&gt;
&lt;div&gt;
For recommending movies for Chan using the above similarity matrix, we need to first fill the N/A where he has not rated. As first step, separate the non-rated movies by Chan and a weighted matrix is created by multiplying user similarity score (user_sim[,7]) with ratings given by other users.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoZrB85LdR3rqt6OscVLFMD7Ev1yp4luKYYDxbD4k8VeKJzalP4gRPvssin7iBuMIJL-Fm-u3U3r-TkWduXBgGVsf1CSHXfJTsKxtb37FkinmdfTFOyskvKxHR-zWzgV0pbYe-Ss8IJHOW/s1600/2.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;137&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoZrB85LdR3rqt6OscVLFMD7Ev1yp4luKYYDxbD4k8VeKJzalP4gRPvssin7iBuMIJL-Fm-u3U3r-TkWduXBgGVsf1CSHXfJTsKxtb37FkinmdfTFOyskvKxHR-zWzgV0pbYe-Ss8IJHOW/s320/2.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
Next step is to sum up all the columns of the weight matrix, then divide by the sum of all the similarities for critics that reviewed that movie. The result calculation gives what the user might rate this movie, the results as below:&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1GryRZKTnHjRWUIQEY1wh_pLiSnVHmRqUefCETZJQxBy1hqxB_sHWfLytq7Bcr0M-zKNihyphenhyphenZd9BwMaiKQkEF3XPwg4UCKaEqkfk3ffTmqZet9C5vgq8vF8hQWcZyDVensG39YAzy-2Y-V/s1600/3.PNG&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;126&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1GryRZKTnHjRWUIQEY1wh_pLiSnVHmRqUefCETZJQxBy1hqxB_sHWfLytq7Bcr0M-zKNihyphenhyphenZd9BwMaiKQkEF3XPwg4UCKaEqkfk3ffTmqZet9C5vgq8vF8hQWcZyDVensG39YAzy-2Y-V/s320/3.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
The above explanation is written in the below R function:&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;rec_itm_for_user = function(userNo)&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;{
#calcualte column wise sum&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;col_sums= list()&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;rat_user = critics[userNo,2:7]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;x=1&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;tot = list()&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;z=1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;for(i in 1:ncol(rat_user)){&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;if(is.na(rat_user[1,i]))&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;{&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;col_sums[x] = sum(weight_mat[,i],na.rm=TRUE)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;x=x+1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;temp = as.data.frame(weight_mat[,i])&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;sum_temp=0&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;for(j in 1:nrow(temp))&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;{
      if(!is.na(temp[j,1]))&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;sum_temp = sum_temp+user_sim[j,7]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;}&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;tot[z] = sum_temp
    z=z+1&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;z=NULL&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;z=1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;for(i in 1:ncol(rat_user)){&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;if(is.na(rat_user[1,i]))&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;rat_user[1,i] = col_sums[[z]]/tot[[z]]
    z=z+1&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;}&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;return(rat_user)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d0e0e3;&quot;&gt;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;
Calling the above function gives the below results:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span lang=&quot;EN-GB&quot; style=&quot;background: #e1e2e5; color: blue; font-family: &amp;quot;lucida console&amp;quot;;&quot;&gt;rec_itm_for_user(7)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span lang=&quot;EN-GB&quot; style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;;&quot;&gt;Titanic Batman Inception Superman.Returns spiderMan Matrix&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span lang=&quot;EN-GB&quot; style=&quot;background: rgb(225 , 226 , 229); font-family: &amp;quot;lucida console&amp;quot;;&quot;&gt;2.811 &amp;nbsp;&amp;nbsp;4.5 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2.355783&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;1&amp;nbsp;&amp;nbsp;&amp;nbsp;
3.481427&lt;/span&gt;&lt;br /&gt;
Recommending movies for Chan will be in the order: Matrix (3.48), Titanic(2.81), Inception(2.35).&lt;/div&gt;
complete sourceCode is available on &lt;a href=&quot;https://github.com/sureshgorakala/RecommenderSystems_R/blob/master/User_based_Collaborative_filtering&quot; target=&quot;_blank&quot;&gt;github&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/8915994340923869152/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2014/05/basic-recommendation-engine-using-r.html#comment-form' title='22 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/8915994340923869152'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/8915994340923869152'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2014/05/basic-recommendation-engine-using-r.html' title='Basic recommendation engine using R'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSu1b0C6KS_tNjR-dOM0zci-ShahpAvTmipIpyfMRJKC_0Zb7TDCa1k76-JHC2Pj_rrQkAOALEzxTpDA0aSn4Q1kNaOwLP3DlYApjw9sCc2Ysm7UYk34ksdCD9bafhV05h6lrsYXDbwpNI/s72-c/Recommender+Engine.jpg" height="72" width="72"/><thr:total>22</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-7558049583632630275</id><published>2014-04-17T11:26:00.003+04:00</published><updated>2015-12-26T09:53:26.207+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="ARIMA"/><category scheme="http://www.blogger.com/atom/ns#" term="forecast"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="time series"/><category scheme="http://www.blogger.com/atom/ns#" term="tseries"/><title type='text'>Time Series Analysis using R - forecast package</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
In today’s blog post, we shall look into time series analysis using R package – &lt;a href=&quot;http://cran.r-project.org/web/packages/forecast/index.html&quot; target=&quot;_blank&quot;&gt;forecast&lt;/a&gt;. Objective of the post will be explaining the different methods available in forecast package which can be applied while dealing with time series analysis/forecasting. 
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;
What is Time Series?&lt;/h3&gt;
A&lt;a href=&quot;http://en.wikipedia.org/wiki/Time_series&quot; target=&quot;_blank&quot;&gt; time series&lt;/a&gt; is a collection of observations of well-defined data items obtained through repeated measurements over time. For example, measuring the value of retail sales each month of the year would comprise a time series. 
&lt;br /&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
Objective:&lt;/h4&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Identify patterns in the data – stationarity/non-stationarity.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Prediction from previous patterns.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Time series Analysis in R:
&lt;/h3&gt;
My data set contains data of Sales of CARS from Jan-2008 to Dec 2013.&lt;br /&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;&lt;i&gt;Problem Statement:&lt;/i&gt;&lt;/b&gt;&amp;nbsp;&lt;span style=&quot;font-weight: normal;&quot;&gt;Forecast sales for 2013&lt;/span&gt;&lt;/h4&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;MyData[1,1:14]&lt;/b&gt;&lt;/div&gt;
&lt;table style=&quot;text-align: center;&quot;&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;PART&lt;/td&gt;
&lt;td&gt;Jan08&lt;/td&gt;
&lt;td&gt;FEB08&lt;/td&gt;
&lt;td&gt;MAR08&lt;/td&gt;
&lt;td&gt;....&lt;/td&gt;
&lt;td&gt;....&lt;/td&gt;
&lt;td&gt;NOV12&lt;/td&gt;
&lt;td&gt;DEC12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MERC&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;127&lt;/td&gt;
&lt;td&gt;56&lt;/td&gt;
&lt;td&gt;....&lt;/td&gt;
&lt;td&gt;....&lt;/td&gt;
&lt;td&gt;776&lt;/td&gt;
&lt;td&gt;557&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
Table: shows the first row data from Jan 2008 to Dec 2012&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;
Results:&lt;/b&gt;&lt;/div&gt;
The forecasts of the timeseries data will be:
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkloC6-bU7zKoAmFpgipQDcWSjkarwQH-dRSwbmtfLFOzDIV0UvtguDtB_3j85jcxodcwgYH5tW-bHLTEjlz8mBO8O6OVK_-T_JtJx1tLTt9rI6nGzJZFHD-th368RoLtWgMNbI3ZkoUoR/s1600/1.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkloC6-bU7zKoAmFpgipQDcWSjkarwQH-dRSwbmtfLFOzDIV0UvtguDtB_3j85jcxodcwgYH5tW-bHLTEjlz8mBO8O6OVK_-T_JtJx1tLTt9rI6nGzJZFHD-th368RoLtWgMNbI3ZkoUoR/s400/1.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Assuming that the data sources for the analysis are finalized and cleansing of the data is done, for further &lt;a href=&quot;http://www.dataperspective.info/2014/02/data-analysis-steps.html&quot; target=&quot;_blank&quot;&gt;details&lt;/a&gt;,
&lt;br /&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
Step1: Understand the data:&amp;nbsp;&lt;/h4&gt;
As a first step, Understand the data visually, for this purpose, the data is converted to time series object using ts(), and plotted visually using plot() functions available in R.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;ts = ts(t(data[,7:66]))&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;plot(ts[1,],type=’o’,col=’blue’)&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghfU_x8GE9w1kvPybLQug6nhobVJ1zhwCC0qDrJpFZ6yfPt4tbsq6bhS8F9rw0Mh4E1HHPna89gC4xthVD9FkSfdO0K9BKhgL9N_9_VJsh_XKKMuMEoenoW9uacTiOk6Mo6rLcYnfltVUA/s1600/2.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghfU_x8GE9w1kvPybLQug6nhobVJ1zhwCC0qDrJpFZ6yfPt4tbsq6bhS8F9rw0Mh4E1HHPna89gC4xthVD9FkSfdO0K9BKhgL9N_9_VJsh_XKKMuMEoenoW9uacTiOk6Mo6rLcYnfltVUA/s400/2.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;span class=&quot;MsoSubtleEmphasis&quot;&gt;Image above shows the monthly sales of an automobile&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
Forecast package &amp;amp; methods:&lt;/h4&gt;
Forecast package is written by Rob J Hyndman and is available from CRAN &lt;a href=&quot;http://cran.r-project.org/web/packages/forecast/index.html&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.  The package contains Methods and tools for displaying and analyzing univariate time series forecasts including exponential smoothing via state space models and automatic ARIMA modelling.&lt;br /&gt;
&lt;br /&gt;
Before going into more accurate Forecasting functions for Time series, let us do some basic forecasts using Meanf(), naïve(), random walk with drift – rwf() methods. Though these may not give us proper results but we can use the results as bench marks.&lt;br /&gt;
&lt;br /&gt;
All these forecasting models returns objects which contain original series, point forecasts, forecasting methods used residuals.
Below functions shows three methods &amp;amp; their plots.&lt;br /&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;Library(forecast)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;mf = meanf(ts[,1],h=12,level=c(90,95),fan=FALSE,lambda=NULL)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;plot(mf)
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhwTZtqEW4zRtpp6OXNMkQHa9A0cDqVQMYVIHeG28ga3HoN6bxiK0GssL7A6wW22TIJodzYcpyC3pFQIvPWdPA3gTYkS1JTzj9gPSxnMAr9HZYz93IYq6fyaihLi8oqXaRxo7M8jOk89VN4/s1600/3.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhwTZtqEW4zRtpp6OXNMkQHa9A0cDqVQMYVIHeG28ga3HoN6bxiK0GssL7A6wW22TIJodzYcpyC3pFQIvPWdPA3gTYkS1JTzj9gPSxnMAr9HZYz93IYq6fyaihLi8oqXaRxo7M8jOk89VN4/s400/3.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;mn = naive(ts[,1],h=12,level=c(90,95),fan=FALSE,lambda=NULL)&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;plot(mn)

&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiizVc16VVr4j0l29P6VMnM0lMmGfx_GDJgroKo2gop_02SB8HUpQuFLsC-3id6qWL3FsZK6KF8WcD0becYuumc5kIWbdNO-S2GttqQeNY74iGtyNbQG9t4ANaht5Q079I8EqGjRDHYPhCi/s1600/4.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiizVc16VVr4j0l29P6VMnM0lMmGfx_GDJgroKo2gop_02SB8HUpQuFLsC-3id6qWL3FsZK6KF8WcD0becYuumc5kIWbdNO-S2GttqQeNY74iGtyNbQG9t4ANaht5Q079I8EqGjRDHYPhCi/s400/4.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;background-color: #cccccc;&quot;&gt;

md = rwf(ts[,1],h=12,drift=T,level=c(90,95),fan=FALSE,lambda=NULL)&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;plot(md)&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-hnN-qOlku998I0NVjPN5wLzklH7b9xk60hgMPbRekHzY2L0jl9LIDB8xhHQuyJSSc-S2HpTVquy0JFtK62NTbnmgTIT0kd2ugvjDwXS7EuvYHYkl7fUFV4-S4ELSH-T7TkjWreNFbgZT/s1600/5.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-hnN-qOlku998I0NVjPN5wLzklH7b9xk60hgMPbRekHzY2L0jl9LIDB8xhHQuyJSSc-S2HpTVquy0JFtK62NTbnmgTIT0kd2ugvjDwXS7EuvYHYkl7fUFV4-S4ELSH-T7TkjWreNFbgZT/s400/5.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
Measuring accuracy:&lt;/h4&gt;
&amp;nbsp;Once the model has been generated the accuracy of the model can tested using accuracy(). The Accuracy function returns MASE value which can be used to measure the accuracy of the model. The best model is chosen from the below results which gives have relatively lesser values of ME,RMSE,MAE,MPE,MAPE,MASE.
&lt;br /&gt;
&amp;gt; accuracy(md)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ME &amp;nbsp; &amp;nbsp; RMSE &amp;nbsp; &amp;nbsp; &amp;nbsp; MAE &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MPE &amp;nbsp; &amp;nbsp;MAPE &amp;nbsp; &amp;nbsp; MASE&lt;br /&gt;
Training set &amp;nbsp; &amp;nbsp; &amp;nbsp;1.806244e-16 2.445734 1.889687 -41.68388 79.67588 1.197689&lt;br /&gt;
accuracy(mf)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ME &amp;nbsp; &amp;nbsp; &amp;nbsp;RMSE &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MAE &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; MPE &amp;nbsp; &amp;nbsp; MAPE MASE&lt;br /&gt;
Training set &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1.55489e-16 &amp;nbsp;1.903214 1.577778 -45.03219 72.00485 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;br /&gt;
&amp;gt; accuracy(mn)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ME &amp;nbsp; RMSE &amp;nbsp; &amp;nbsp; &amp;nbsp; MAE &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; MPE &amp;nbsp; &amp;nbsp; &amp;nbsp;MAPE &amp;nbsp; &amp;nbsp; MASE&lt;br /&gt;
Training set 0.1355932 2.44949 1.864407 -36.45951 76.98682 1.181666&lt;br /&gt;
&lt;br /&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
&amp;nbsp;Step2: Time Series Analysis Approach:&lt;/h4&gt;
A typical time-series analysis involves below steps:&lt;br /&gt;
&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Check for identifying under lying patterns - Stationary &amp;amp; non-stationary, seasonality, trend.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;After the patterns have been identified, if needed apply Transformations to the data – based on Seasonality/trends appeared in the data.&lt;/li&gt;
&lt;li&gt;Apply forecast() the future values using Proper ARIMA model obtained by auto.arima() methods.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
Identify Stationarity/Non-Stationarity:&lt;/h4&gt;
&lt;b&gt;A stationary time series is one whose properties do not depend on the time at which the series is observed. Time series with trends, or with seasonality, are not stationary&lt;/b&gt;.&lt;br /&gt;
The stationarity /non-stationarity of the data can be known by applying Unit Root Tests - augmented Dickey–Fuller test (ADF), Kwiatkowski-Phillips-Schmidt-Shin (KPSS) test.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;ADF:&lt;/b&gt; The null-hypothesis for an ADF test is that the data are non-stationary. So large p-values are indicative of non-stationarity, and small p-values suggest stationarity. Using the usual 5% threshold, differencing is required if the p-value is greater than 0.05.
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: #E1E2E5; color: blue; font-family: &amp;quot;Lucida Console&amp;quot;; font-size: 10.0pt; mso-bidi-font-family: &amp;quot;Courier New&amp;quot;; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;; mso-fareast-language: EN-GB;&quot;&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/span&gt;&lt;span style=&quot;background-color: #e1e2e5; color: blue; font-family: &#39;Lucida Console&#39;; font-size: 10pt;&quot;&gt;library(tseries)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: #E1E2E5; color: blue; font-family: &amp;quot;Lucida Console&amp;quot;; font-size: 10.0pt; mso-bidi-font-family: &amp;quot;Courier New&amp;quot;; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;; mso-fareast-language: EN-GB;&quot;&gt;adf =
adf.test(ts[,1])&lt;/span&gt;&lt;span lang=&quot;EN-US&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: #E1E2E5; color: blue; font-family: &amp;quot;Lucida Console&amp;quot;; font-size: 10.0pt; mso-bidi-font-family: &amp;quot;Courier New&amp;quot;; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;; mso-fareast-language: EN-GB;&quot;&gt;adf&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Augmented Dickey-Fuller Test&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt;&quot;&gt;data:&amp;nbsp; ts[, 1]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt;&quot;&gt;Dickey-Fuller =
-4.8228, Lag order = 3, p-value = 0.01&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt;&quot;&gt;alternative
hypothesis: stationary&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjn4nWi_JG_ejeBTAQqZkIVWUFQ3xC3-SYWkV4MrlUclV2RI9e7dtn69Xq8PVYQ6yX8fRI3JZa6qv7EDFvq2iPCDt0Yl0JytsWz6fwnSSBmLUVaJ1nKwht4fq32TXIE6RdGnYWZbDmOrWlQ/s1600/6.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjn4nWi_JG_ejeBTAQqZkIVWUFQ3xC3-SYWkV4MrlUclV2RI9e7dtn69Xq8PVYQ6yX8fRI3JZa6qv7EDFvq2iPCDt0Yl0JytsWz6fwnSSBmLUVaJ1nKwht4fq32TXIE6RdGnYWZbDmOrWlQ/s400/6.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;span class=&quot;MsoSubtleEmphasis&quot;&gt;The above figure suggests us that the data is of
stationary and we can go ahead with ARIMA models.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt;&quot;&gt;&lt;o:p&gt;&lt;br /&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;b&gt;KPSS: &lt;/b&gt;Another popular unit root test is the Kwiatkowski-Phillips-Schmidt-Shin (KPSS) test. This reverses the hypotheses, so the null-hypothesis is that the data are stationary. In this case, small p-values (e.g., less than 0.05) suggest that differencing is required.
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: #E1E2E5; color: blue; font-family: &amp;quot;Lucida Console&amp;quot;; font-size: 10.0pt; mso-bidi-font-family: &amp;quot;Courier New&amp;quot;; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;; mso-fareast-language: EN-GB;&quot;&gt;kpss =
kpss.test(ts[,1])&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: #E1E2E5; color: #c5060b; font-family: &amp;quot;Lucida Console&amp;quot;; font-size: 10.0pt; mso-bidi-font-family: &amp;quot;Courier New&amp;quot;; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;; mso-fareast-language: EN-GB;&quot;&gt;Warning message:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: #E1E2E5; color: #c5060b; font-family: &amp;quot;Lucida Console&amp;quot;; font-size: 10.0pt; mso-bidi-font-family: &amp;quot;Courier New&amp;quot;; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;; mso-fareast-language: EN-GB;&quot;&gt;In kpss.test(ts[,
1]) : p-value greater than printed p-value&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: #E1E2E5; color: blue; font-family: &amp;quot;Lucida Console&amp;quot;; font-size: 10.0pt; mso-bidi-font-family: &amp;quot;Courier New&amp;quot;; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;; mso-fareast-language: EN-GB;&quot;&gt;kpss&lt;/span&gt;&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; KPSS Test for Level Stationarity&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt;&quot;&gt;data:&amp;nbsp; ts[, 1]&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt;&quot;&gt;KPSS Level = 0.1399,
Truncation lag parameter = 1, p-value = 0.1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;Differencing:&lt;/b&gt;&lt;br /&gt;
Based on the unit test results we identify whether the data is stationary or not. If the data is stationary then we choose optimal ARIMA models and forecasts the future intervals. If the data is non- stationary, then we use Differencing - computing the differences between consecutive observations.
Use ndiffs(),diff() functions to find the number of times differencing needed for the data &amp;amp; &amp;nbsp;to difference the data respectively.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;span style=&quot;background: #E1E2E5; color: blue; font-family: &amp;quot;Lucida Console&amp;quot;; font-size: 10.0pt; line-height: 115%; mso-bidi-font-family: &amp;quot;Courier New&amp;quot;; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;; mso-fareast-language: EN-GB;&quot;&gt;ndiffs(ts[,1])&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt;&quot;&gt;[1] 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span style=&quot;background: #E1E2E5; color: blue; font-family: &amp;quot;Lucida Console&amp;quot;;&quot;&gt;diff_data = diff(ts[,1])&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt; line-height: 115%;&quot;&gt;Time Series:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt; line-height: 115%;&quot;&gt;Start = 2 &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt; line-height: 115%;&quot;&gt;End = 60 &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt; line-height: 115%;&quot;&gt;Frequency = 1 &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt; line-height: 115%;&quot;&gt;&amp;nbsp;[1]&amp;nbsp; 1&amp;nbsp; 5 -3 -1 -1&amp;nbsp; 0&amp;nbsp;
3&amp;nbsp; 1&amp;nbsp; 0 -4&amp;nbsp; 4 -5&amp;nbsp; 0&amp;nbsp; 0&amp;nbsp; 1&amp;nbsp; 1&amp;nbsp; 0&amp;nbsp;
1&amp;nbsp; 0&amp;nbsp; 0&amp;nbsp; 2 -5&amp;nbsp; 3 -2&amp;nbsp; 2&amp;nbsp; 1 -3&amp;nbsp; 0&amp;nbsp;
3&amp;nbsp; 0&amp;nbsp; 2 -1 -5&amp;nbsp; 3 -1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt; line-height: 115%;&quot;&gt;[36] -1&amp;nbsp; 2 -1 -1&amp;nbsp; 5 -2&amp;nbsp; 0&amp;nbsp; 2 -2
-4&amp;nbsp; 0&amp;nbsp; 3&amp;nbsp; 1 -1&amp;nbsp; 0&amp;nbsp; 0&amp;nbsp; 0 -2&amp;nbsp; 2 -3&amp;nbsp; 4
-3&amp;nbsp; 2&amp;nbsp; 5&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
Now retest for stationarity by applying acf()/kpss() functions if the plots shows us the Stationarity then Go ahead by applying ARIMA Models.&lt;br /&gt;
&lt;b&gt;Identify Seasonality/Trend:&lt;/b&gt;&lt;br /&gt;
The seasonality in the data can be obtained by the stl()when plotted&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: #E1E2E5; color: blue; font-family: &amp;quot;Lucida Console&amp;quot;; font-size: 10.0pt; mso-bidi-font-family: &amp;quot;Courier New&amp;quot;; mso-bidi-font-style: italic; mso-bidi-font-weight: bold; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;; mso-fareast-language: EN-GB;&quot;&gt;Stl = Stl(ts[,1],s.window=”periodic”)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: #E1E2E5; color: #c5060b; font-family: &amp;quot;Lucida Console&amp;quot;; font-size: 10.0pt; mso-bidi-font-family: &amp;quot;Courier New&amp;quot;; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;; mso-fareast-language: EN-GB;&quot;&gt;Series is not
period or has less than two periods&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background: #E1E2E5; color: #c5060b; font-family: &amp;quot;Lucida Console&amp;quot;; font-size: 10.0pt; mso-bidi-font-family: &amp;quot;Courier New&amp;quot;; mso-fareast-font-family: &amp;quot;Times New Roman&amp;quot;; mso-fareast-language: EN-GB;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
Since my data doesn’t contain any seasonal behavior I will not touch the Seasonality part.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;ARIMA Models:&lt;/b&gt;&lt;br /&gt;
For forecasting stationary time series data we need to choose an optimal ARIMA model (p,d,q). 
For this we can use auto.arima() function which can choose optimal (p,d,q) value and return us.
Know more about ARIMA from &lt;a href=&quot;https://www.otexts.org/fpp/8/7&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background: #E1E2E5; color: blue; font-family: &amp;quot;Lucida Console&amp;quot;; font-size: 10.0pt; line-height: 115%; mso-fareast-language: EN-GB;&quot;&gt;auto.arima(ts[,2])&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt; line-height: 115%;&quot;&gt;Series: ts[, 2] &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt; line-height: 115%;&quot;&gt;ARIMA(3,1,1) with
drift&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; word-break: break-all;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt; line-height: 115%;&quot;&gt;Coefficients:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt; line-height: 115%;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
ar1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ar2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
ar3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ma1&amp;nbsp;&amp;nbsp; drift&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt; line-height: 115%;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
-0.2621&amp;nbsp; -0.1223&amp;nbsp; -0.2324&amp;nbsp; -0.7825&amp;nbsp; 0.2806&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt; line-height: 115%;&quot;&gt;s.e.&amp;nbsp;&amp;nbsp;
0.2264&amp;nbsp;&amp;nbsp; 0.2234&amp;nbsp;&amp;nbsp; 0.1798&amp;nbsp;&amp;nbsp; 0.2333&amp;nbsp; 0.1316&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; word-break: break-all;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: .0001pt; margin-bottom: 0cm; word-break: break-all;&quot;&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt; line-height: 115%;&quot;&gt;sigma^2 estimated as
41.64:&amp;nbsp; log likelihood=-190.85&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt; line-height: 115%;&quot;&gt;AIC=393.7&amp;nbsp;&amp;nbsp; AICc=395.31&amp;nbsp;&amp;nbsp;
BIC=406.16&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;; font-size: 10pt; line-height: 115%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span lang=&quot;EN-US&quot;&gt;&lt;b&gt;Forecast time series:&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span lang=&quot;EN-US&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span lang=&quot;EN-US&quot;&gt;Now we use
forecast() method to forecast the future events.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span class=&quot;gcg2ujhdeab&quot;&gt;&lt;span style=&quot;background: #E1E2E5; color: blue; font-family: &amp;quot;Lucida Console&amp;quot;;&quot;&gt;forecast(auto.arima(dif_data))&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;;&quot;&gt;&amp;nbsp;&amp;nbsp; Point Forecast&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Lo 80&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Hi 80&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Lo 95&amp;nbsp;&amp;nbsp;&amp;nbsp; Hi 95&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/pre&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;;&quot;&gt;61&amp;nbsp;&amp;nbsp; -3.076531531 -5.889584 -0.2634795 -7.378723 1.225660&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;;&quot;&gt;62&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.231773625 -2.924279&amp;nbsp; 3.3878266 -4.594993 5.058540&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;;&quot;&gt;63&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.702386360 -2.453745&amp;nbsp; 3.8585175 -4.124500 5.529272&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;;&quot;&gt;64&amp;nbsp;&amp;nbsp; -0.419069906 -3.599551&amp;nbsp; 2.7614107 -5.283195 4.445055&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;;&quot;&gt;65&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.025888991 -3.160496&amp;nbsp; 3.2122736 -4.847266 4.899044&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;;&quot;&gt;66&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.098565814 -3.087825&amp;nbsp; 3.2849562 -4.774598 4.971729&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;;&quot;&gt;67&amp;nbsp;&amp;nbsp; -0.057038778 -3.243900&amp;nbsp; 3.1298229 -4.930923 4.816846&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;;&quot;&gt;68&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.002733053 -3.184237&amp;nbsp; 3.1897028 -4.871317 4.876783&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;;&quot;&gt;69&amp;nbsp;&amp;nbsp;&amp;nbsp; 0.013817766 -3.173152&amp;nbsp; 3.2007878 -4.860232 4.887868&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span style=&quot;background-color: #e1e2e5; background-position: initial initial; background-repeat: initial initial; font-family: &#39;Lucida Console&#39;;&quot;&gt;70&amp;nbsp;&amp;nbsp; -0.007757195 -3.194736&amp;nbsp; 3.1792219 -4.881821 4.866307&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;word-break: break-all;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span class=&quot;gcg2ujhdeab&quot;&gt;&lt;span style=&quot;background: #E1E2E5; color: blue; font-family: &amp;quot;Lucida Console&amp;quot;;&quot;&gt;plot(forecast(auto.arima(dif_data)))&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span class=&quot;gcg2ujhdeab&quot;&gt;&lt;span style=&quot;background: #E1E2E5; color: blue; font-family: &amp;quot;Lucida Console&amp;quot;;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgT5XfCVVMIEbZw0dfG-yRoO4CM-bMFb9XRpvP-r3SFiUPYJ2gWEc16XguuK1IBIGP-ERsEYgNa1nqqkmaKO8-uDv0hqQxVUxA3KCnJtjNxN7EcyjybSg8GB4Gxw167tANo05oYjdKyYr_j/s1600/7.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgT5XfCVVMIEbZw0dfG-yRoO4CM-bMFb9XRpvP-r3SFiUPYJ2gWEc16XguuK1IBIGP-ERsEYgNa1nqqkmaKO8-uDv0hqQxVUxA3KCnJtjNxN7EcyjybSg8GB4Gxw167tANo05oYjdKyYr_j/s400/7.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
The below flow chart will give us a summary of the time series ARIMA models approach:&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvHAHvIPpNxitKpGOG5n4chH2rD4UGa96DPHN6-HMHWXAoZGPQvhkT_DV0bnVPBKTudSxPPa98w2wT3TZYBuQerDje88xucE4b9vPaEpeM8NJp9wG1VZAf5RwgZlZBONm1r6UNmStd8b8v/s1600/arima+models+appraoch.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvHAHvIPpNxitKpGOG5n4chH2rD4UGa96DPHN6-HMHWXAoZGPQvhkT_DV0bnVPBKTudSxPPa98w2wT3TZYBuQerDje88xucE4b9vPaEpeM8NJp9wG1VZAf5RwgZlZBONm1r6UNmStd8b8v/s400/arima+models+appraoch.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;span class=&quot;MsoSubtleEmphasis&quot;&gt;The above flow &lt;a href=&quot;https://www.otexts.org/fpp/8/7&quot; target=&quot;_blank&quot;&gt;diagram &lt;/a&gt;explains the steps to be
followed for a time series forecasting&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre style=&quot;word-break: break-all;&quot;&gt;&lt;span class=&quot;gcg2ujhdeab&quot;&gt;&lt;span style=&quot;background: #E1E2E5; color: blue; font-family: &amp;quot;Lucida Console&amp;quot;;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/7558049583632630275/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2014/04/time-series-analysis-using-r.html#comment-form' title='31 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/7558049583632630275'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/7558049583632630275'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2014/04/time-series-analysis-using-r.html' title='Time Series Analysis using R - forecast package'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkloC6-bU7zKoAmFpgipQDcWSjkarwQH-dRSwbmtfLFOzDIV0UvtguDtB_3j85jcxodcwgYH5tW-bHLTEjlz8mBO8O6OVK_-T_JtJx1tLTt9rI6nGzJZFHD-th368RoLtWgMNbI3ZkoUoR/s72-c/1.png" height="72" width="72"/><thr:total>31</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-1532673261941378249</id><published>2014-03-20T00:05:00.001+04:00</published><updated>2015-12-28T09:10:45.660+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Exploratory analysis"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="web application using R"/><title type='text'>Build Web applications using Shiny R</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot;&gt;
Ever since I’ve started working on R , I always wondered how I can present&lt;br /&gt;
the results of my statistical models as web applications. After doing some&lt;br /&gt;
research over the internet I’ve come across – ShinyR – a new package&lt;br /&gt;
from RStudio which can be used to develop interactive web applications with R.&lt;br /&gt;
Before going into how to build web apps using R, let me give you some overview&lt;br /&gt;
about ShinyR.&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;b&gt;Features:&lt;/b&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;No JavaScript/HTML knowledge required.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Can build interactive web applications which can load the data dynamically.&lt;/li&gt;
&lt;li&gt;Pre-Built output widgets for displaying plots, tables interactively are available.&lt;/li&gt;
&lt;li&gt;Works in any R environment.&lt;/li&gt;
&lt;li&gt;Shiny Apps can be hosted locally/on self-hosted Shiny Server/Rstudio-hosted &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Shiny Server – in today’s post we will see hosting locally.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
&lt;b style=&quot;text-align: center;&quot;&gt;&lt;b&gt;&amp;nbsp;Building a shiny App&lt;/b&gt;&lt;/b&gt;&lt;/h4&gt;
&lt;b style=&quot;text-align: center;&quot;&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b style=&quot;text-align: center;&quot;&gt;
&lt;/b&gt;Shiny application have two components: a user-interface definition and server script,&lt;br /&gt;
&amp;nbsp;and any additional data, scripts, or other resources required to support the&lt;br /&gt;
application.&lt;br /&gt;
UI script (ui.R): In this script we define the UI layout of the webpage. We can make&lt;br /&gt;
&amp;nbsp;use of inbuilt UI controls/widgets for displaying output and for creating UI layout..&lt;br /&gt;
Server Script (server.R): In this script, the actual business logic/ code for statistical&lt;br /&gt;
models, plotting the output is defined. It accepts inputs &amp;nbsp;from UI layer and compute&lt;br /&gt;
outputs send back the response to the UI layer. The basic task of a Shiny server&lt;br /&gt;
script is to define the relationship between inputs and outputs.&lt;br /&gt;
&lt;br /&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Understanding UI.R script:&lt;/b&gt;&lt;/h4&gt;
&lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;library(shiny)  #load shiny package&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;ds = read.csv(&quot;inputdata.csv&quot;) #load data&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;x = names(ds)
# Define UI for application that plots distributions&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;shinyUI(pageWithSidebar(&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;# Application title&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;headerPanel(&quot;Demo Shiny Application&quot;),&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;#sidebar panel to the left side of the page for input controls, slider &amp;amp; dropdown&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;sidebarPanel(&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;sliderInput(&#39;Year&#39;, &#39;Year&#39;, min=2010, max=2012,&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;value=min(2010,2012), step=1, round=0),&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;selectInput(&#39;select&#39;, &#39;Select Criteria&#39;, c(x[3],x[9],x[10],x[11],x[12],x[26]))&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;),&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;#main panel for displaying outputs, Histograms, summary&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;mainPanel(&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;plotOutput(&#39;plot&#39;),&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;verbatimTextOutput(&quot;summary&quot;)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;&amp;nbsp;)
))&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #f6b26b;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgK_tv-6Juqn-99S7Y2ArlQV6ZekkGksFW-7Uzvh0Pocfof1GOYUxwqEv4pGpP-fRCwiM2etd7zMGSCtZz-DqMhLN9yLEkGVQEjECqXsSOptswm9dGv3sXxIQUIrRkdONy44SyQ2j_KLaBY/s1600/shinyui.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;148&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgK_tv-6Juqn-99S7Y2ArlQV6ZekkGksFW-7Uzvh0Pocfof1GOYUxwqEv4pGpP-fRCwiM2etd7zMGSCtZz-DqMhLN9yLEkGVQEjECqXsSOptswm9dGv3sXxIQUIrRkdONy44SyQ2j_KLaBY/s320/shinyui.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
Notice in particular, in the above code sidebarPanel and mainPanel functions&lt;br /&gt;
&amp;nbsp;are called with two arguments - corresponding to the two inputs and&lt;br /&gt;
&amp;nbsp;two outputs displayed.&lt;br /&gt;
&lt;br /&gt;
In the above image we can see the layout which contains a slider with year range,&lt;br /&gt;
where based on the year selection the dataset will be taken into consideration.&lt;br /&gt;
Dropdownlist containing varaibles, placeholder to display the graphs, table to&lt;br /&gt;
display the summaries of the models.&lt;br /&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Understanding Server.R&lt;/b&gt;&lt;/h4&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwBLUnGcqUceCDNV0pmkoPSOab5vZoQCaN27jyFHmtDvEeQBNkifvJ_8HuLXv2hjrCQqOqDEpmJhJ9Zp1ApAnWjvBICboPcjeZZmeDKphUeNSzryeWgPS06GRyHnB37ssmGh6Uhf3qHIDv/s1600/rshiny.jpg&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;271&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwBLUnGcqUceCDNV0pmkoPSOab5vZoQCaN27jyFHmtDvEeQBNkifvJ_8HuLXv2hjrCQqOqDEpmJhJ9Zp1ApAnWjvBICboPcjeZZmeDKphUeNSzryeWgPS06GRyHnB37ssmGh6Uhf3qHIDv/s320/rshiny.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
In the above script, we can observe that all the logic for&amp;nbsp;the &lt;a href=&quot;http://www.dataperspective.info/2014/03/exploratory-data-analysis-techniques.html&quot; target=&quot;_blank&quot;&gt;exploratory analysis&lt;/a&gt; is&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
written in server.R script. It contains&amp;nbsp;a main function called ShinyServer()&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
wherein we can write reactive&amp;nbsp;functions,reactive plots, outputs.&amp;nbsp;The outputs&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&amp;nbsp;are send back&amp;nbsp;to the UI layer.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
In the above code, I have written logic which contains&amp;nbsp;data fetching,&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
modification, setting the dataset based on&amp;nbsp;the year selected in the&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
slider (reactive function), displaying&amp;nbsp;histogram based on the selection&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
on slider and dropdownlist,writing summary to a table.&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h4 style=&quot;text-align: left;&quot;&gt;
Deploying the application:&lt;/h4&gt;
&lt;div&gt;
Once we are done with the application, next step is to deploy&amp;nbsp;the application.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
Using Shiny, we have two different options to move&amp;nbsp;the application to Production.&lt;/div&gt;
&lt;div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;** Create a local- self-hosted Shiny Server on Linux platform, &lt;a href=&quot;https://github.com/rstudio/shiny-server&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;** Host on the R-Studio&#39;s Shiny server, explained &lt;a href=&quot;http://rstudio.github.io/shiny/tutorial/#deployment-web&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;b&gt;Deploying locally:&lt;/b&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
As mentioned in the beginning of the post, in order to deploy locally,&amp;nbsp;we make&lt;/div&gt;
&lt;div&gt;
use of &lt;a href=&quot;https://gist.github.com/sureshgorakala/8942533&quot; target=&quot;_blank&quot;&gt;GIST&lt;/a&gt;.&amp;nbsp;The steps are quite simple:&lt;/div&gt;
&lt;div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;** Upload the Server.R &amp;amp; UI.R files to the Gist.Github.com website.&lt;/li&gt;
&lt;li&gt;** Uploading the files gives you an ID - in my case - I got &lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;&quot;8942533&quot;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;** After finishing upload, open R and run the below code:&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;library(&#39;shiny&#39;)&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class=&quot;identifier&quot; style=&quot;background-color: #f9cb9c; font-family: &#39;DejaVu Sans Mono&#39;, &#39;Droid Sans Mono&#39;, &#39;Lucida Console&#39;, Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; text-align: -webkit-auto; white-space: pre-wrap;&quot;&gt;shiny&lt;/span&gt;&lt;span class=&quot;operator&quot; style=&quot;background-color: #f9cb9c; color: #687687; font-family: &#39;DejaVu Sans Mono&#39;, &#39;Droid Sans Mono&#39;, &#39;Lucida Console&#39;, Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; text-align: -webkit-auto; white-space: pre-wrap;&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;operator&quot; style=&quot;background-color: #f9cb9c; color: #687687; font-family: &#39;DejaVu Sans Mono&#39;, &#39;Droid Sans Mono&#39;, &#39;Lucida Console&#39;, Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; text-align: -webkit-auto; white-space: pre-wrap;&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;identifier&quot; style=&quot;background-color: #f9cb9c; font-family: &#39;DejaVu Sans Mono&#39;, &#39;Droid Sans Mono&#39;, &#39;Lucida Console&#39;, Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; text-align: -webkit-auto; white-space: pre-wrap;&quot;&gt;runGist&lt;/span&gt;&lt;span class=&quot;paren&quot; style=&quot;background-color: #f9cb9c; color: #687687; font-family: &#39;DejaVu Sans Mono&#39;, &#39;Droid Sans Mono&#39;, &#39;Lucida Console&#39;, Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; text-align: -webkit-auto; white-space: pre-wrap;&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;string&quot; style=&quot;color: #036a07; font-family: &#39;DejaVu Sans Mono&#39;, &#39;Droid Sans Mono&#39;, &#39;Lucida Console&#39;, Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; text-align: -webkit-auto; white-space: pre-wrap;&quot;&gt;&lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;&#39;&lt;/span&gt;&lt;span style=&quot;background-color: #b6d7a8; color: black; font-family: &#39;Times New Roman&#39;; font-size: small; line-height: normal; text-align: left; white-space: normal;&quot;&gt;8942533&lt;/span&gt;&lt;span style=&quot;background-color: #f9cb9c;&quot;&gt;&#39;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;paren&quot; style=&quot;background-color: #f9cb9c; color: #687687; font-family: &#39;DejaVu Sans Mono&#39;, &#39;Droid Sans Mono&#39;, &#39;Lucida Console&#39;, Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; text-align: -webkit-auto; white-space: pre-wrap;&quot;&gt;)&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: -webkit-auto;&quot;&gt;
&lt;/div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &#39;DejaVu Sans Mono&#39;, &#39;Droid Sans Mono&#39;, &#39;Lucida Console&#39;, Consolas, Monaco, monospace; font-size: 12.222222328186035px; line-height: 17.77777862548828px; white-space: pre-wrap;&quot;&gt;The model will be shown as web application will be displayed&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;span style=&quot;font-family: &#39;DejaVu Sans Mono&#39;, &#39;Droid Sans Mono&#39;, &#39;Lucida Console&#39;, Consolas, Monaco, monospace; font-size: 12.222222328186035px; line-height: 17.77777862548828px; white-space: pre-wrap;&quot;&gt;&amp;nbsp;the browser with inputs for selection &amp;amp; outputs to display.&lt;/span&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;/ul&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/1532673261941378249/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2014/03/build-web-applications-using-shiny-r.html#comment-form' title='17 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/1532673261941378249'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/1532673261941378249'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2014/03/build-web-applications-using-shiny-r.html' title='Build Web applications using Shiny R'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgK_tv-6Juqn-99S7Y2ArlQV6ZekkGksFW-7Uzvh0Pocfof1GOYUxwqEv4pGpP-fRCwiM2etd7zMGSCtZz-DqMhLN9yLEkGVQEjECqXsSOptswm9dGv3sXxIQUIrRkdONy44SyQ2j_KLaBY/s72-c/shinyui.png" height="72" width="72"/><thr:total>17</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-5347826774781957820</id><published>2014-03-03T18:08:00.000+04:00</published><updated>2015-12-28T09:49:55.864+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="data analysis"/><category scheme="http://www.blogger.com/atom/ns#" term="Exploratory analysis"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><title type='text'>Exploratory data analysis techniques</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
In my previous blog &lt;a href=&quot;http://www.dataperspective.info/2014/02/data-analysis-steps.html&quot; target=&quot;_blank&quot;&gt;post &lt;/a&gt;I have explained the steps needed to solve a data analysis problem. Going further, I will be discussing in-detail each and every step of Data Analysis. In this post, we shall discuss about exploratory Analysis.&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;What is Exploratory Analysis?&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;“Understanding data visually”&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Exploratory Analysis means analyzing the datasets to summarize their main characteristics, often visually. This is the first step of any data analysis.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Objectives:&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Know the data types of the dataset – whether continuous/discreet/categorical&lt;/li&gt;
&lt;li&gt;Understand how the data is distributed&lt;/li&gt;
&lt;li&gt;Extract important input variables for the analysis&lt;/li&gt;
&lt;li&gt;Identify&amp;nbsp;outliers&lt;/li&gt;
&lt;li&gt;To identity patterns, if exists&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Exploratory Analysis Techniques:&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Box-Plot&lt;/li&gt;
&lt;li&gt;Histogram&lt;/li&gt;
&lt;li&gt;Trend analysis&lt;/li&gt;
&lt;li&gt;Scatter Plots&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Let us understand the exploratory analysis by considering a data analysis problem.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;&lt;i&gt;Problem statement:&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
to analyze the incidents/events occurred over past 3 years and try to predict the event&amp;nbsp;occurring&amp;nbsp;in the future.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b style=&quot;font-style: italic;&quot;&gt;Solution:&lt;/b&gt;&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
After understanding the problem statement and gaining the sufficient domain knowledge&lt;b style=&quot;font-style: italic;&quot;&gt;,&amp;nbsp;&lt;/b&gt;Identify the data sources &amp;amp; download the data into the programming environment.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
The next step is to perform an Exploratory analysis as explained &lt;a href=&quot;http://www.dataperspective.info/2014/02/data-analysis-steps.html&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;. in today&#39;s post we shall look how &amp;nbsp;exploratory analysis can be done.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Types of Exploratory analysis:&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;&lt;i&gt;
Type1:&lt;/i&gt;&lt;/b&gt; Understanding the data – variable names, dimensions of the dataset, data types of each and every variable.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;data = read.csv(&quot;datasource.csv&quot;) #load data&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;dim(data) #know the dimensions of the data&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;[1] 839  50&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;Colnames(data) #know the column names&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;[1] &quot;Incidents&quot; &quot;Year of Occurance&quot;  &quot;Location.of.Occurrence&quot; &quot;Date.of.Occurence&quot; [5]&quot;Time.of.Occurrence&quot;   “Operational Phase”&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;Str(data) # know the data types of each of the variable – continuous/descrete/categorical&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;$ Incidents: int  41505 41537 41539 41565 41589 41596 41598&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;$ Vehicle.Type           : Factor w/ 7 levels &quot;&quot;,&quot;Volvo(all series)&quot;,..: 6 2 2 2 6 6&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;$ Location.of.Occurrence: Factor w/ 101 levels &quot;&quot;,&quot;Abidjan&quot;,&quot;Accra&quot;,..: 53 35 35 35 96&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;$ Date.of.Occurence: Factor w/ 520 levels &quot;1/1/2010&quot;,&quot;1/1/2012&quot; ..: 1 32 37&amp;nbsp;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;Sum(!is.na(data$Date.of.Occurance) # counting the number of missing values in the column&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;i&gt;&lt;b&gt;Type2:&lt;/b&gt;&lt;/i&gt;Creating new varaibles/data type conversion suitable for the analysis – like factor variables into numerical,dates into year/month/day,time into hour of the day, etc. according to our convineince.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;#Extracting year/month from Date of occurrence and creating new variables&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;xn = as.POSIXct(data$&quot;Date.of.Occurence&quot;,format=&quot;%m/%d/%Y&quot;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;data[&quot;year&quot;] = as.numeric(format(xn,&quot;%Y&quot;))&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;data[&quot;month&quot;] = as.numeric(format(xn,&quot;%m&quot;))&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;str(data$&quot;year&quot;)&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;num [1:839] 2010 2010 2010 2010 2010 2010&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;#extracting hour of the day and creating new variable TimeOfOccurance&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;(TOC)
data[&quot;TOC&quot;] = sub(&quot;:.*&quot;, &quot;&quot;, data$&quot;Time.of.Occurrence&quot;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;str(data$&quot;TOC&quot;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;&lt;span style=&quot;color: #990000;&quot;&gt;chr [1:839] &quot;18&quot; &quot;6&quot; &quot;21&quot; &quot;13&quot; &quot;16&quot; &quot;13&quot; &quot;11&quot; &quot;11&quot; &quot;15&quot; &quot;1&quot; &quot;13&quot; &quot;6&quot;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;&lt;i&gt;Type3:&lt;/i&gt;&lt;/b&gt; Observe the summary of each and every variable to understand the variables.
summary(data$&quot;Vehicle.Type&quot;)&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Volvo (all series)          ASHOK&amp;nbsp;(all series)&amp;nbsp;FIAT (all series)  Maruthi (all series)&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;210 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;49 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 71 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;39&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;&lt;i&gt;Type4:&lt;/i&gt;&lt;/b&gt; Decide which variables are good for analysis by using trends, boxplots, histograms etc.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc; color: #990000;&quot;&gt;boxplot(formula=as.numeric(data$&quot;operational.Phase&quot;)~data$&quot;year&quot;,col=&quot;blue&quot;)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCkvDaKPnIlBDFHD8pAiAjrumiDOHluQJC6NKGMV33gMOJYKewPzElK1nW_M1QtC1-UnpUsDvolao6O-C-Lp42YDTRvNlcbw6zO0sPwK0MjDjfsEqI6ejEj70USwDZBAbw3APOZMlByY_R/s1600/boxplot+r.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;124&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCkvDaKPnIlBDFHD8pAiAjrumiDOHluQJC6NKGMV33gMOJYKewPzElK1nW_M1QtC1-UnpUsDvolao6O-C-Lp42YDTRvNlcbw6zO0sPwK0MjDjfsEqI6ejEj70USwDZBAbw3APOZMlByY_R/s320/boxplot+r.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
Box plot distribution of incidents occurring over the years.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;background-color: #cccccc; color: #990000;&quot;&gt;hist(data[which(data$&quot;year&quot; == 2011),]$&quot;month&quot;,breaks =  &quot;Sturges&quot;,col=c(&#39;blue&#39;,&#39;red&#39;,&#39;green&#39;),labels=T)&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhh4GkxjzFNvtWoq6zvoUIRRa6DVeMDJ3xRnp3uS-if4s85XyQj2ri6t7r0NpXEKJWkOHvFNyBy5yYhh13eFunq71ohK3Nmj95vQ8uufhFKsvzxE2fUdwlRGNxbdS9py9HjLgOMnulIDzSB/s1600/histogram+r.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;152&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhh4GkxjzFNvtWoq6zvoUIRRa6DVeMDJ3xRnp3uS-if4s85XyQj2ri6t7r0NpXEKJWkOHvFNyBy5yYhh13eFunq71ohK3Nmj95vQ8uufhFKsvzxE2fUdwlRGNxbdS9py9HjLgOMnulIDzSB/s320/histogram+r.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
The above histogram depicts the month wise distribution of incidents occurred in 2011
Trend analysis&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHaGXeScc1LLJDPqm0OXbqT7qe55CWifl0ORjKNx0bG0DpmdidKmU6RW3k53YNyFruNrK2NTUWQ-hN4Drl8Bc7EGg7GKzfDf-UyZVaESBjXQ9BpWCwqUvaOoE805nCVocO7Y6PIVd1ajmm/s1600/trend+excel.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;111&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHaGXeScc1LLJDPqm0OXbqT7qe55CWifl0ORjKNx0bG0DpmdidKmU6RW3k53YNyFruNrK2NTUWQ-hN4Drl8Bc7EGg7GKzfDf-UyZVaESBjXQ9BpWCwqUvaOoE805nCVocO7Y6PIVd1ajmm/s320/trend+excel.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
In the above graph, we can bring out the below inferences:&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Sharp fall in the data in 2012 might be not capturing of the incidents&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
An average of 30 incidents occurring monthly&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
In the month of Feb there is sharp fall in the incidents&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgykYJzuxcP92YYkvuD2P4Dk-Z7OoyylpfPIXaYbPOgyod3d2e2T1Wr5MD_R-pB8x2E6PL77yA_k-xsh6xEChBkGJ8C3kk353Nl2sj-Q8HIX7nnDW-j4OX8nRPxAV8maMsdLm1DY250CxS3/s1600/trend+excel2.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;92&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgykYJzuxcP92YYkvuD2P4Dk-Z7OoyylpfPIXaYbPOgyod3d2e2T1Wr5MD_R-pB8x2E6PL77yA_k-xsh6xEChBkGJ8C3kk353Nl2sj-Q8HIX7nnDW-j4OX8nRPxAV8maMsdLm1DY250CxS3/s320/trend+excel2.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
In the above trend image with graph in red color is plotted against Number of people in the deck and number of Incidents.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
This clearly says that there is no relation between the Incident occurring and number of people in the deck.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Hope the above post gives you a very good understanding of how exploratory analysis can be done. In my next post we shall learn how to do forecasting using Linear regression.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/5347826774781957820/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2014/03/exploratory-data-analysis-techniques.html#comment-form' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/5347826774781957820'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/5347826774781957820'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2014/03/exploratory-data-analysis-techniques.html' title='Exploratory data analysis techniques'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCkvDaKPnIlBDFHD8pAiAjrumiDOHluQJC6NKGMV33gMOJYKewPzElK1nW_M1QtC1-UnpUsDvolao6O-C-Lp42YDTRvNlcbw6zO0sPwK0MjDjfsEqI6ejEj70USwDZBAbw3APOZMlByY_R/s72-c/boxplot+r.png" height="72" width="72"/><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-5073180854274234976</id><published>2014-02-03T14:06:00.001+04:00</published><updated>2016-02-01T22:34:57.776+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="big data"/><category scheme="http://www.blogger.com/atom/ns#" term="data analysis"/><category scheme="http://www.blogger.com/atom/ns#" term="Data Science"/><category scheme="http://www.blogger.com/atom/ns#" term="machine learning"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="step for data analysis"/><category scheme="http://www.blogger.com/atom/ns#" term="visualization tools"/><title type='text'>Data Analysis Steps</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
After going through the overview of tools &amp;amp; technologies needed to become a Data scientist in my previous &lt;a href=&quot;http://www.dataperspective.info/2014/01/data-analysis-tools.html&quot; target=&quot;_blank&quot;&gt;blog post&lt;/a&gt;, in this post, we shall understand how to tackle a data analysis problem.&lt;br /&gt;
Any data analysis project starts with identifying a business problem where historical data exists. A business problem can be anything which can include prediction problems, analyzing customer behavior, identifying new patterns from past events, building recommendation engines etc.&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;The steps for solving a data analysis problem can be shown as below:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEisMxr2F1yMWNNe96IGXuTAQAgWpLHmZN8WsbPGN8Hu_BaiiyCzi_L4oBmxCDfzLuLJZsji8Jjo1rJKWWDP1OT_k898dNe5je9EzYX-mEoNKOnqztAJGGloNH3-BW38XGQ6YThyphenhyphenxmIh-dtX/s1600/data+analysis+steps.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;226&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEisMxr2F1yMWNNe96IGXuTAQAgWpLHmZN8WsbPGN8Hu_BaiiyCzi_L4oBmxCDfzLuLJZsji8Jjo1rJKWWDP1OT_k898dNe5je9EzYX-mEoNKOnqztAJGGloNH3-BW38XGQ6YThyphenhyphenxmIh-dtX/s320/data+analysis+steps.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Identify Business Problem:&lt;/b&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;&amp;nbsp;“Define Problem statement”&lt;/i&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;text-align: left;&quot;&gt;This is the first step of analysis. Business identifies a problem and a problem statement with desired outcome is defined. In this stage, a Data Scientist should understand the problem statement, the domain knowledge of the problem. After thorough understanding of the problem statement, a Hypothesis will be proposed.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;b&gt;&amp;nbsp;Data Acquisition:&lt;/b&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;“Identify data sources”&lt;/i&gt;&lt;/div&gt;
As a second step, all the data sources related to the problem statement will be identified and pulled into a central repository. The data sources can vary from SQL databases to text files to csv files to online data. If the data size is large we may use &lt;a href=&quot;http://en.wikipedia.org/wiki/Apache_Hadoop&quot; target=&quot;_blank&quot;&gt;Hadoop &lt;/a&gt;to pull, store &amp;amp; pre-process the data.&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;Process/Clean Data:&lt;/b&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;&amp;nbsp;“The accuracy of the results of analysis depends on the quality of data”&amp;nbsp;&lt;/i&gt;&lt;/div&gt;
Data Clean step is considered to be one of the very important phases in Data analysis. The accuracy of the analysis depends on the quality of data.&lt;br /&gt;
Few approaches:&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Formatting the data as per the data analytical tools we use.&lt;/li&gt;
&lt;li&gt;Missing data handling&lt;/li&gt;
&lt;li&gt;Data Transformations like normalizing the data
Identifying outliers &amp;amp; handling etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Exploratory Analysis:&lt;/b&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;&amp;nbsp;“Embrace the data visually before diving further”&lt;/i&gt;&lt;/div&gt;
The objective of this step is to understand the main characteristics of the data. This analysis is generally done using visualizing tools. Performing an &lt;a href=&quot;http://en.wikipedia.org/wiki/Exploratory_data_analysis&quot; target=&quot;_blank&quot;&gt;Exploratory analysis&lt;/a&gt; helps us:&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;to understand causes of an observed event&lt;/li&gt;
&lt;li&gt;to understand the nature of the data we are dealing with&lt;/li&gt;
&lt;li&gt;assess assumptions on which our analysis will be based&lt;/li&gt;
&lt;li&gt;to identify the key features in the data needed for the analysis&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Graphical Techniques:&lt;/b&gt;Scatter plots, box plots, histograms&lt;br /&gt;
&lt;b&gt;Quantitative techniques: &lt;/b&gt;Mean, median, Mode, Standard deviation&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUUP3ol0hC1CGNmj0Dwwxmq37XpeHKUCPv-VbsvDCNUDtXkD9W8AAnbt4ViXR5aKSuknQHdzo2UCwq9CtwHAbqfvqDsE9Yhb8kFF9vu8FxL5Ammr_yF4KRqNTFsrZEjvJ06YSLKoV9hlDA/s1600/scatter+plots.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUUP3ol0hC1CGNmj0Dwwxmq37XpeHKUCPv-VbsvDCNUDtXkD9W8AAnbt4ViXR5aKSuknQHdzo2UCwq9CtwHAbqfvqDsE9Yhb8kFF9vu8FxL5Ammr_yF4KRqNTFsrZEjvJ06YSLKoV9hlDA/s400/scatter+plots.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;b&gt;Model Generation &amp;amp; Validation:&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;“Select-Train-Evaluate”&amp;nbsp;&lt;/i&gt;&lt;/div&gt;
This step involves extracting features from the data and feeding them into the machine learning algorithms to build a model. Model is the solution proposed for the problem statement. This step involves: Model selection, model training and model evaluation.&lt;br /&gt;
&lt;b&gt;Model selection: &lt;/b&gt;Based on the type of business problem we are dealing, a model will be built. For example,if the objective of the analysis is to predict a future event, we need to build a Regression model for prediction.&lt;br /&gt;
&lt;b&gt;Model Training:&lt;/b&gt; After selecting the Model for the analysis, the entire dataset is divided into 2 parts – Training data &amp;amp; Test Data. 3/4th of the entire data will be fed as input to the Model Algorithms.&lt;br /&gt;
&lt;b&gt;Model Evaluation: &lt;/b&gt;Once the model is built. The next step is to test the model &amp;amp; validate it. The data used for testing the model is the remaining 1/3rd of the dataset in the previous step.&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7CHPSCXn5dy_YLNC7dA6yEbEve3FXb_YTsnO5IXHKxjXBlbh2H2yVr5Vmy34H9bbAziRNiVbVrZIRMJYsXMildim-Ayl-xxSrfyugAFOMdzgu3xJovUWBWlUrqU7jG2X6MYCZlMWOrs03/s1600/data+model.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7CHPSCXn5dy_YLNC7dA6yEbEve3FXb_YTsnO5IXHKxjXBlbh2H2yVr5Vmy34H9bbAziRNiVbVrZIRMJYsXMildim-Ayl-xxSrfyugAFOMdzgu3xJovUWBWlUrqU7jG2X6MYCZlMWOrs03/s400/data+model.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;b&gt;Visualize Results:&lt;/b&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;&amp;nbsp;&quot;Show the results visually&quot;&amp;nbsp;&lt;/i&gt;&lt;/div&gt;
This is the final step of Data analysis where the results of the model &amp;amp; problem solved will be presented generally in visual plots/graphs.&lt;br /&gt;
Few visualizing tools: d3.js, ggplot2, tableau.&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvCMI4IcZazXO8tbe5KBv3Nr9O1ZX-8_Y63sNrBXKJfCdaljuioAbzmljzCxpSHu3pUZv2OCIO70J-Nm_hdoKUz-h8gwmv6j37IDeKjaVIBb0caE8J9P7xX2Eph31NSPEqLpwkW_o3bWEE/s1600/k-means+clustering.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvCMI4IcZazXO8tbe5KBv3Nr9O1ZX-8_Y63sNrBXKJfCdaljuioAbzmljzCxpSHu3pUZv2OCIO70J-Nm_hdoKUz-h8gwmv6j37IDeKjaVIBb0caE8J9P7xX2Eph31NSPEqLpwkW_o3bWEE/s400/k-means+clustering.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
Please go through the tools/technologies , skill set required to learn Data Analysis &lt;a href=&quot;http://www.dataperspective.info/2014/01/data-analysis-tools.html&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/5073180854274234976/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2014/02/data-analysis-steps.html#comment-form' title='42 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/5073180854274234976'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/5073180854274234976'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2014/02/data-analysis-steps.html' title='Data Analysis Steps'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEisMxr2F1yMWNNe96IGXuTAQAgWpLHmZN8WsbPGN8Hu_BaiiyCzi_L4oBmxCDfzLuLJZsji8Jjo1rJKWWDP1OT_k898dNe5je9EzYX-mEoNKOnqztAJGGloNH3-BW38XGQ6YThyphenhyphenxmIh-dtX/s72-c/data+analysis+steps.png" height="72" width="72"/><thr:total>42</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-7392832422846559921</id><published>2014-01-07T16:25:00.000+04:00</published><updated>2017-10-17T07:44:54.144+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="big data"/><category scheme="http://www.blogger.com/atom/ns#" term="data analysis"/><category scheme="http://www.blogger.com/atom/ns#" term="Data Science"/><category scheme="http://www.blogger.com/atom/ns#" term="hadoop"/><category scheme="http://www.blogger.com/atom/ns#" term="hive"/><category scheme="http://www.blogger.com/atom/ns#" term="machine learning"/><category scheme="http://www.blogger.com/atom/ns#" term="pig"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="tools"/><title type='text'>Data Analysis Tools</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
As mentioned in my previous post , in this &lt;a href=&quot;http://www.dataperspective.info/2013/12/data-scientist-path-i-chose.html&quot; target=&quot;_blank&quot;&gt;post &lt;/a&gt;I will be listing out the tools, blogs and forums, online courses that I have gathered over the past one year, which I felt necessary in my journey, which will be helpful to my fellow data science aspirants.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
&amp;nbsp;&lt;b&gt;Skillset Required:&lt;/b&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&amp;nbsp;Knowledge in Statistics – Exploratory analysis, doing initial analysis of the data &amp;amp; understanding the data to decide what techniques needs to be applied, which I feel is a must know subject.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Mathematics – basics of calculus, algebra etc. for mathematical formulation of the problem statement.&lt;/li&gt;
&lt;li&gt;&amp;nbsp;Understanding Machine learning algorithms for predictive modeling, recommendation engines, classification models, cluster analysis, social network analysis.&lt;/li&gt;
&lt;li&gt;Data mining skills like data cleaning/Data Munging skills, apply Machine learning techniques on the data.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;Visualization skills to display the results, to understand the results during building data modeling.&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Tools required:&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
Programming Languages: Proficiency in any two of the below mentioned languages would be advisable:&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&amp;nbsp;R&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Python&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Java – comes in handy when we work on Hadoop&amp;nbsp;&lt;/li&gt;
&lt;li&gt;C,C++&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Tools required:&lt;/b&gt; Since I’m using Open source tools, I will be confined to them:&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;R-Studio&lt;/li&gt;
&lt;li&gt;NLTK toolkit&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Rapid Miner&lt;/li&gt;
&lt;li&gt;Weka&lt;/li&gt;
&lt;/ul&gt;
&lt;i&gt;Important Point:&amp;nbsp;&lt;/i&gt;&lt;br /&gt;
Most of the machine learning algorithms has been already implemented as packages in the above languages/tools . We need to just download and make use of them.&lt;br /&gt;
&lt;b&gt;Big data Tools:&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Hadoop setup from Cloudera/Hortonworks&lt;/li&gt;
&lt;li&gt;Mongodb- NoSQL DB&lt;/li&gt;
&lt;li&gt;HBASE, PIG, HIVE.&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Visualization tools:&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
Though I have not explored much in this area, but till day I’m happy with R packages for visualizations.&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Data exploration in R/Python&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Few Books I have referred:&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;The Elements of Statistical Learning - 2nd Edition&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Simon Sheather, A Modern Approach to Regression&lt;/li&gt;
&lt;li&gt;&amp;nbsp;Data Mining 3rd Edition by Ian H. Witten, Eibe Frank, Mark A. Hall&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Online Courses:&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
Though a lot of courses are available online, I have stick to very few sites as below,&lt;br /&gt;
For Data Analysis, Stats, Maths:&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;a href=&quot;http://www.coursera.org/&quot; target=&quot;_blank&quot;&gt;Coursera&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.udacity.com/&quot; target=&quot;_blank&quot;&gt;Udacity&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.teachingtree.co/&quot; target=&quot;_blank&quot;&gt;Teachingtree&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.khanacademy.org/&quot; target=&quot;_blank&quot;&gt;Khan Acadamy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.udemy.com/&quot; target=&quot;_blank&quot;&gt;Udemy&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.onlinecourses.com/statistics/&quot; target=&quot;_blank&quot;&gt;Statistics&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;For Big Data:&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;a href=&quot;https://www.bigdatauniversity.com/&quot; target=&quot;_blank&quot;&gt;Bigdata Univeristy&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.cloudera.com/&quot; target=&quot;_blank&quot;&gt;Cloudera&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.edureka.in/&quot; target=&quot;_blank&quot;&gt;Edureka&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Blogs and forums:&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
Online forums is one place where I used to get a lot of information, in Linked Groups I could get answer to all my trivial questions. You post any query and you will get elaborate answer from research scholars to industry experts, I really love this place. I will list down few Linkedin groups I follow,&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;a href=&quot;http://www.linkedin.com/groups?home=&amp;amp;gid=2229532&amp;amp;trk=anet_ug_hm&quot; target=&quot;_blank&quot;&gt;RProgramming&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.linkedin.com/groups/R-Machine-Learning-4783230?trk=my_groups-b-grp-v&quot; target=&quot;_blank&quot;&gt;R Machine Learning&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.linkedin.com/groups?gid=70219&amp;amp;trk=my_groups-b-grp-v&quot; target=&quot;_blank&quot;&gt;Machine Learning Connection&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Blogs I follow:&lt;/b&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;a href=&quot;http://www.kdnuggets.com/&quot; target=&quot;_blank&quot;&gt;kdnuggets&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://datasciencecentral.com/&quot; target=&quot;_blank&quot;&gt;Data Science Central&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.rbloggers.org/&quot; target=&quot;_blank&quot;&gt;R-Bloggers&amp;nbsp;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
Will add more when I come across the new tools.
Guys hope this will serve you as a starting point for the Journey. All the best, Happy New Year. Please do add any new tools and technologies to the above list.&lt;br /&gt;
In my next post, shall post about how to tackle a &lt;a href=&quot;http://www.dataperspective.info/2014/02/data-analysis-steps.html&quot; target=&quot;_blank&quot;&gt;data analysis problem&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/7392832422846559921/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2014/01/data-analysis-tools.html#comment-form' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/7392832422846559921'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/7392832422846559921'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2014/01/data-analysis-tools.html' title='Data Analysis Tools'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-9023854485963241890</id><published>2013-12-31T09:12:00.000+04:00</published><updated>2017-10-17T07:45:17.693+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="big data"/><category scheme="http://www.blogger.com/atom/ns#" term="Data Science path"/><category scheme="http://www.blogger.com/atom/ns#" term="Data Scientist"/><category scheme="http://www.blogger.com/atom/ns#" term="Python"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="visualization tools"/><title type='text'>Data Scientist. The Path I Chose</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
As we all are marching into the New Year, I would like to post about my plans to become a Data Scientist, my 2014 resolution at Professional front. The term &lt;a href=&quot;http://en.wikipedia.org/wiki/Data_science&quot; target=&quot;_blank&quot;&gt;Data Science&lt;/a&gt; was first introduced to me a year ago same time. Since then I have started researching and gathering necessary information and decided to become Data Scientist. After one year I just wanted to look back myself to understand where I stand now &amp;amp; still what needs to be done.&lt;br /&gt;
&lt;b&gt;Power of Data – possible Use Cases:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;/b&gt;&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
Automobile companies are installing special devices which continuously feed data about the nature of driving, the roads being taken, places being visited etc. to the Insurance companies who by analyzing this information can customize the insurance plans for its customers.&lt;br /&gt;
Another possible use case, making use of the data we store on social media, we can develop products which can understand us and sometimes guide us. Like, based on people’s posts on social media, with location, time, road you are travelling &amp;amp; LIVE Traffic feeds, we can develop an app which could suggest us which road to take to our destination taking.&lt;br /&gt;
&lt;b&gt;Who is Data Scientist?&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
Data Scientist termed as the Sexiest Job of the 21st Century and is the most sought after &amp;amp; well paid job all over the Industry. The role of the Data Scientist is to Make Sense out of the tons of Data being generated every day, build products which can redefine more statistically/ scientifically the way the current businesses is going.  The best part of this Job Is, a Data Scientist can fit into any industry where ever data exists.&lt;br /&gt;
&lt;b&gt;What it needs to become a Data Scientist?&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;&quot;Web is my University, Time is the only Investment&quot;&amp;nbsp;&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/div&gt;
Going back to where I have started my journey, after my initial research with my folks and over internet, I started taking up &lt;a href=&quot;https://coursera.org/&quot; target=&quot;_blank&quot;&gt;Machine Learning course from Coursera.org&lt;/a&gt;, an online offering from Stanford University, definitely a recommended Starting Point for anyone who wants to be a Data Scientist.&lt;br /&gt;
After completing the course, I have started playing with Data. Predictive models, being my first Hands-on. At this point I got an advice from my Colleague,&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&amp;nbsp;&lt;i&gt;“It is easy (because free and online) in order to grab some interesting key messages and get a rather complete overview within a 8-week period timeframe.   
Be sure you have a sufficient mathematical background to follow (maybe Calculas/mathematics before?) and more importantly, be certain you can practice afterwards!”&amp;nbsp;&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/div&gt;
My First analytics assignment on Predictive modelling gave me a lot of learnings, the need to brush up my Mathematics basics, Stats fundamentals, data mining techniques, Visualization Techniques.&lt;br /&gt;
I was lucky enough to find many of the courses listed in my TODO list&lt;br /&gt;
@ &lt;a href=&quot;http://coursera.org/&quot;&gt;Coursera.org&lt;/a&gt;, &lt;a href=&quot;http://www.udacity.com/&quot; target=&quot;_blank&quot;&gt;UDACITY&lt;/a&gt;:&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Machine Learning – for Machine Learning Algorithms&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Neural Networks for Machine Learning&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Data Analysis – Introduction to Programming Language R&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Language for Data Analysis&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Making Sense of Data - basics of Statistics&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Introduction to Data Science – intro to Data Science&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Social Network Analysis&lt;/li&gt;
&lt;/ul&gt;
The image, below which I have got over internet, is a simplified road map for a Data Scientist, which has become my Target over the next few months combined with Hands-on.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTs1uiMULY1sicCIhDEQ7EdICntMhq74bIMSEDF5p20Ogy3Fgstr1j8W00Vw1xs7-erv3wX-DYrb1l1FVQJslQSGwb0mn5LH-6Q_IHBcCxYI_qNpNeDEFdS-rR4wR2QwKqkCxoSH8vMWqv/s1600/Road+to+DataScientist+Graphic.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;259&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTs1uiMULY1sicCIhDEQ7EdICntMhq74bIMSEDF5p20Ogy3Fgstr1j8W00Vw1xs7-erv3wX-DYrb1l1FVQJslQSGwb0mn5LH-6Q_IHBcCxYI_qNpNeDEFdS-rR4wR2QwKqkCxoSH8vMWqv/s320/Road+to+DataScientist+Graphic.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
Road map to data science&amp;nbsp;&lt;/div&gt;
After completing the required courses mentioned above, I got an opportunity to play around with Data using Text Mining, Classification techniques, Predictive modelling techniques both from within my Organization &amp;amp; outside (KAGGLE.COM).&lt;br /&gt;
It would be incomplete, if I don’t mention &lt;a href=&quot;http://www.kaggle.com/&quot; target=&quot;_blank&quot;&gt;KAGGLE&lt;/a&gt;, the website where I found the much needed hands-on experience. KAGGLE is an online Open competition forum, where you can find Data Science Use cases posted by big Companies. Advantage of working on this site is not only you can get hands-on experience but also you increase your Network with similar competent techies.&lt;br /&gt;
Apart from KAGGLE, I have made use of LINKEDIN data science/data Analysis groups where I could get suggestions and answers to almost every question of mine by the Experts of this field.&lt;br /&gt;
&lt;b&gt;Data Analytics Should Meet Big Data:&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;i&gt;“Learn a bit of Java, Learn a bit of Linux – shell scripting, Learn Hadoop”&lt;/i&gt; – an advice from my friend.&lt;/div&gt;
&lt;br /&gt;
After completing few POCs and began working with real time projects I have soon realized the need to handle huge amounts of data and need to learn BIG DATA. In my personal opinion, a Data Scientist should have a very strong knowledge in Big Data-Hadoop.&lt;br /&gt;
For this, I planned to choose the road path of learning Big Data Hadoop concepts, Linux Shell scripting, MongoDB for data storage in the next 3 months.&lt;br /&gt;
That is me folks, after one year; long way to go through before becoming a Data Scientist. But still would love to hear someone calling me a Data Scientist. Over the last one year, I have spent most of my time in learning new concepts &amp;amp; technologies. I am eager to see my theory shaping into work.&lt;br /&gt;
In my &lt;a href=&quot;http://www.dataperspective.info/2014/01/data-analysis-tools.html&quot; target=&quot;_blank&quot;&gt;next &lt;/a&gt;blog post I will explain the tools, concepts, technologies, online forums available on internet. 
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/9023854485963241890/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2013/12/data-scientist-path-i-chose.html#comment-form' title='277 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/9023854485963241890'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/9023854485963241890'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2013/12/data-scientist-path-i-chose.html' title='Data Scientist. The Path I Chose'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTs1uiMULY1sicCIhDEQ7EdICntMhq74bIMSEDF5p20Ogy3Fgstr1j8W00Vw1xs7-erv3wX-DYrb1l1FVQJslQSGwb0mn5LH-6Q_IHBcCxYI_qNpNeDEFdS-rR4wR2QwKqkCxoSH8vMWqv/s72-c/Road+to+DataScientist+Graphic.png" height="72" width="72"/><thr:total>277</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-3050832299737395524</id><published>2013-12-17T15:19:00.002+04:00</published><updated>2017-10-17T07:45:40.529+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="cluster analysis"/><category scheme="http://www.blogger.com/atom/ns#" term="k-means cluster algorithm"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><title type='text'>Cluster Analysis using R</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
In this post, I will explain you about Cluster Analysis, The process of grouping objects/individuals together in such a way that objects/individuals in one group are more similar than objects/individuals in other groups.&lt;br /&gt;
&amp;nbsp;For example, from a ticket booking engine database identifying clients with similar booking activities and group them together (called Clusters). Later these identified clusters can be targeted for business improvement by issuing special offers, etc.&lt;br /&gt;
Cluster Analysis falls into Unsupervised Learning algorithms, where in Data to be analyzed will be provided to a Cluster analysis algorithm to identify hidden patterns within as shown in the figure below.&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVWONOonToEG44GzrPS1nD610o6Dma9sWwk0moJbB6ixiyEIKYJdTjrrFAMTKcXVkbfODLD-sM6-RhTPIGV_ExFGYLJtdjCABhyaao8aJIFlkdCmr_g8p25VE4TCMeuhzQq2E7YZy1Lgp6/s1600/unsupervised+learning.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVWONOonToEG44GzrPS1nD610o6Dma9sWwk0moJbB6ixiyEIKYJdTjrrFAMTKcXVkbfODLD-sM6-RhTPIGV_ExFGYLJtdjCABhyaao8aJIFlkdCmr_g8p25VE4TCMeuhzQq2E7YZy1Lgp6/s400/unsupervised+learning.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&amp;nbsp;In the image above, the cluster algorithm has grouped the input data into two groups.
There are 3 Popular Clustering algorithms, Hierarchical Cluster Analysis, K-Means Cluster Analysis, Two-step Cluster Analysis, of which today I will be dealing with K-Means Clustering.&lt;br /&gt;
&lt;b&gt;Explaining k-Means Cluster Algorithm:&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
In K-means algorithm, k stands for the number of clusters (groups) to be formed, hence this algorithm can be used to group known number of groups within the Analyzed data.&lt;br /&gt;
K Means is an iterative algorithm and it has two steps. First is a Cluster Assignment Step, and second is a Move Centroid Step.&lt;br /&gt;
CLUSTER ASSIGNMENT STEP: In this step, we randomly chose two cluster points (red dot &amp;amp; green dot) and we assign each data point to one of the two cluster points whichever is closer to it. (Top part of the below image)&lt;br /&gt;
MOVE CENTROID STEP: In this step, we take the average of the points of all the examples in each group and move the Centroid to the new position i.e. mean position calculated. (Bottom part of the below image)&lt;br /&gt;
The above steps are repeated until all the data points are grouped into 2 groups and the mean of the data points at the end of Move Centroid Step doesn’t change.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEietAuRtzoGgzdwbV3cukW5TPQAkmk512PPgQIJ8HuQsxt6fol8VxyLTf_uBc1xWFtFnc6SxCozn1V5DiQAj9DRcoQw8n_FsuTP6l613OeyNOwQz_xdhzQbTMjVzvkP3mF6j36udbDAAe49/s1600/k-means+clustering.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;158&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEietAuRtzoGgzdwbV3cukW5TPQAkmk512PPgQIJ8HuQsxt6fol8VxyLTf_uBc1xWFtFnc6SxCozn1V5DiQAj9DRcoQw8n_FsuTP6l613OeyNOwQz_xdhzQbTMjVzvkP3mF6j36udbDAAe49/s320/k-means+clustering.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&amp;nbsp;By repeating the above steps the final output grouping of the input data will be obtained.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;Cluster Analysis on Accidental Deaths by Natural Causes in India using R&lt;/b&gt;&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Implementation of k-Means Cluster algorithm can readily downloaded as R Package, CLUSTER . Using the package we shall do cluster analysis of Accidents deaths in India by Natural Causes.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Steps implemented will be discussed as below:&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
The data for our analysis was downloaded from www.data.gov.in.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Between 2001 &amp;amp; 2012. Input data is displayed as below:&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2c04YnWQse8M4BRaT-xsGFDqrqNpdj9kLF1AZztNNL0eksVvjwIIoVrXHxGod_bNnI5hOA3U_5T5wFliEQlAoTq3GYjHUabvB6IFZCxEA7rzbZIReWyGWlypDHpalKwwJHfobgn_2bwoi/s1600/cluster+analysis+input+data.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;259&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2c04YnWQse8M4BRaT-xsGFDqrqNpdj9kLF1AZztNNL0eksVvjwIIoVrXHxGod_bNnI5hOA3U_5T5wFliEQlAoTq3GYjHUabvB6IFZCxEA7rzbZIReWyGWlypDHpalKwwJHfobgn_2bwoi/s320/cluster+analysis+input+data.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
For any cluster analysis, all the features have to be converted into numerical &amp;amp; the larger values in the Year Columns are converted to z-score for better results.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Run Elbow method (code available below) is run to find the optimal number of clusters present within the data points.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Run the K-means cluster method of the R package &amp;amp; visualize the results as below:&lt;!-----&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6BNJ8-R-WDo4GdGczBBDLyc9DynEniXHbnI5klGtaS0HNoctwYqwg9-DyrESbaJjiZVp7cRNa5lLRSs2i6bEcZgDhzzUUmYam-Xo_iwkDIB_cmX_65f8SAM2tYA616ZBWa4U30yx19lrE/s1600/clusters+output.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6BNJ8-R-WDo4GdGczBBDLyc9DynEniXHbnI5klGtaS0HNoctwYqwg9-DyrESbaJjiZVp7cRNa5lLRSs2i6bEcZgDhzzUUmYam-Xo_iwkDIB_cmX_65f8SAM2tYA616ZBWa4U30yx19lrE/s320/clusters+output.png&quot; width=&quot;287&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgc9nDmjwFBoUfyRuBDY6EtoK8lfweo2UD5DMaWr7uOD07iC7GE1_nt2PQol2tdDieekqgRPNndW9I8i_gH4KQPfAKTXLcJzPpR_GnJYrTP-uqSwiXDo_iBG7RG87-Q7_6M_Z9suLeQWUCb/s1600/k-means+clustering+output+visualisation.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgc9nDmjwFBoUfyRuBDY6EtoK8lfweo2UD5DMaWr7uOD07iC7GE1_nt2PQol2tdDieekqgRPNndW9I8i_gH4KQPfAKTXLcJzPpR_GnJYrTP-uqSwiXDo_iBG7RG87-Q7_6M_Z9suLeQWUCb/s400/k-means+clustering+output+visualisation.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;Code:&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;#Fetch
data&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;data=
read.csv(&quot;Cluster Analysis.csv&quot;)&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;APStats =
data[which(data$STATE == &#39;ANDHRA PRADESH&#39;),]&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;APMale =
rowSums(APStats[,4:8])&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;APFemale
= rowSums(APStats[,9:13])&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;APStats[,&#39;APMale&#39;]
= APMale&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;APStats[,&#39;APFemale&#39;]
= APFemale&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;data =
APStats[c(2,3,14,15)]&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-size: 13.5pt;&quot;&gt;library(cluster)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;library(graphics)&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;library(ggplot2)&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-size: 13.5pt;&quot;&gt;#factor
the categorical fields&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;cause =
as.numeric(factor(data$CAUSE))&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;data$CAUSE
= cause&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-size: 13.5pt;&quot;&gt;#Z-score
for Year column&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;z = {}&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;m =
mean(data$Year)&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;sd =
sd(data$Year)&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;year =
data$Year&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;for(i in
1:length(data$Year)){&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;z[i] =
(year[i] - m)/sd&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;data$Year
= as.numeric(z)&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-size: 13.5pt;&quot;&gt;#Calculating
K-means - Cluster assignment &amp;amp; cluster group steps&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;cost_df
&amp;lt;- data.frame()&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;for(i in
1:100){&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;kmeans&amp;lt;-
kmeans(x=data, centers=i, iter.max=100)&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;cost_df&amp;lt;-
rbind(cost_df, cbind(i, kmeans$tot.withinss))&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;names(cost_df)
&amp;lt;- c(&quot;cluster&quot;, &quot;cost&quot;)&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;#Elbow
method to identify the idle number of Cluster&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;#Cost
plot&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;ggplot(data=cost_df,
aes(x=cluster, y=cost, group=1)) +&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;theme_bw(base_family=&quot;Garamond&quot;)
+&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;geom_line(colour
= &quot;darkgreen&quot;) +&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;theme(text
= element_text(size=20)) +&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;ggtitle(&quot;Reduction
In Cost For Values of &#39;k&#39;\n&quot;) +&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;xlab(&quot;\nClusters&quot;)
+&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;ylab(&quot;Within-Cluster
Sum of Squares\n&quot;)&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-size: 13.5pt;&quot;&gt;clust =
kmeans(data,5)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;clusplot(data,
clust$cluster, color=TRUE, shade=TRUE,labels=13, lines=0)&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;data[,&#39;cluster&#39;]
= clust$cluster&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: inherit , serif; font-size: 13.5pt;&quot;&gt;head(data[which(data$cluster
== 5),])&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/3050832299737395524/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2013/12/cluster-analysis-using-r.html#comment-form' title='14 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/3050832299737395524'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/3050832299737395524'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2013/12/cluster-analysis-using-r.html' title='Cluster Analysis using R'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVWONOonToEG44GzrPS1nD610o6Dma9sWwk0moJbB6ixiyEIKYJdTjrrFAMTKcXVkbfODLD-sM6-RhTPIGV_ExFGYLJtdjCABhyaao8aJIFlkdCmr_g8p25VE4TCMeuhzQq2E7YZy1Lgp6/s72-c/unsupervised+learning.png" height="72" width="72"/><thr:total>14</thr:total></entry><entry><id>tag:blogger.com,1999:blog-603166843861435412.post-5183109032571048236</id><published>2013-10-24T11:58:00.001+04:00</published><updated>2017-10-17T07:46:06.624+04:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="Rest API"/><category scheme="http://www.blogger.com/atom/ns#" term="Twitter Analysis"/><title type='text'>Fetch Twitter data using R</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;georgia&amp;quot; , &amp;quot;times new roman&amp;quot; , serif;&quot;&gt;&lt;b&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;h1&gt;
&lt;/h1&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
This short post will explain how you can fetch twitter data using &amp;nbsp;&lt;a href=&quot;http://cran.r-project.org/web/packages/twitteR/index.html&quot; target=&quot;_blank&quot;&gt;twitteR &lt;/a&gt;&amp;amp; StreamR&amp;nbsp;packages available in R. In order to connect to twitter API, we need to undergo an authentication process known as &lt;a href=&quot;http://sureshgorakala.blogspot.ae/2013/07/oauth-part-1.html&quot; target=&quot;_blank&quot;&gt;OAuth&lt;/a&gt;&amp;nbsp;explained in my previous post.&lt;/div&gt;
Twitter data can be fetched from twitter in two ways: a) Rest API b) Streaming Api.&lt;br /&gt;
In today&#39;s blog post we shall go through both using Rest API &amp;amp; Stream API.&lt;br /&gt;
&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3zFI42lHtuMWv8XCcNPYX1SfetS4L9wHGM_4H2L1-0ZZM_fcYFnXnwdLtCYQH4cw7D6k76OtcSn8fj7SFy6XEVAmM0Ca1t8AmL1d-zFM_VKWMbGpy_TLcSPocrJee7vKJ6AvPX4LGPwzP/s1600/stream_RestAPI.png&quot; imageanchor=&quot;1&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;304&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3zFI42lHtuMWv8XCcNPYX1SfetS4L9wHGM_4H2L1-0ZZM_fcYFnXnwdLtCYQH4cw7D6k76OtcSn8fj7SFy6XEVAmM0Ca1t8AmL1d-zFM_VKWMbGpy_TLcSPocrJee7vKJ6AvPX4LGPwzP/s320/stream_RestAPI.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;b&gt;twitteR Package:&lt;/b&gt;&lt;br /&gt;
One of the available package in R for fetching Twitter Data. The package can be obtained from &lt;a href=&quot;http://cran.r-project.org/web/packages/twitteR/index.html&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;
This package allows us to make REST API calls to twitter using the ConsumerKey &amp;amp; ConsumerSecret code. Code below illustrates how as how to extract the Twitter Data.&lt;br /&gt;
This package offers below functionality:&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Authenticate with Twitter API&lt;/li&gt;
&lt;li&gt;Fetch User timeline&lt;/li&gt;
&lt;li&gt;User Followers&lt;/li&gt;
&lt;li&gt;User Mentions&lt;/li&gt;
&lt;li&gt;Search twitter&lt;/li&gt;
&lt;li&gt;User Information&lt;/li&gt;
&lt;li&gt;User friends information&lt;/li&gt;
&lt;li&gt;Location based Trends&lt;/li&gt;
&lt;li&gt;Convert JSON object to dataframes&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;REST API CALLS using R - twitteR package:&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Register your application with twitter.&lt;/li&gt;
&lt;li&gt;After registration, you will be getting ConsumerKey &amp;amp; ConsumerSecret code which needs to be used for calling twitter API.&lt;/li&gt;
&lt;li&gt;Load TwitteR library in R environment.&lt;/li&gt;
&lt;li&gt;Call twitter API using OAuthFactory$new() method with ConsumerKey &amp;amp; ConsumerSecret code as input params.&lt;/li&gt;
&lt;li&gt;The above step will return an authorization link, which needs to be copied &amp;amp; pasted in the internet browser.&lt;/li&gt;
&lt;li&gt;You will be redirected to Twitter application authentication page where you need to authenticate yourself by providing you twitter credentials.&lt;/li&gt;
&lt;li&gt;After authenticating , we will be provided with a Authorization code, which needs to be pasted in the R console.&lt;/li&gt;
&lt;li&gt;Call registerTwitterOAuth().&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;b&gt;Source Code:&lt;/b&gt;&lt;br /&gt;
library(twitteR)&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
requestURL &amp;lt;- &amp;nbsp;&quot;https://api.twitter.com/oauth/request_token&quot;&lt;/div&gt;
&lt;div&gt;
accessURL = &amp;nbsp; &amp;nbsp;&quot;https://api.twitter.com/oauth/access_token&quot;&lt;/div&gt;
&lt;div&gt;
authURL = &amp;nbsp; &amp;nbsp; &amp;nbsp;&quot;https://api.twitter.com/oauth/authorize&quot;&lt;/div&gt;
&lt;div&gt;
consumerKey = &amp;nbsp; &quot;XXXXXXXXXXXX&quot;&lt;/div&gt;
&lt;div&gt;
consumerSecret = &quot;XXXXXXXXXXXXXXXX&quot;&lt;/div&gt;
&lt;div&gt;
twitCred &amp;lt;- OAuthFactory$new(consumerKey=consumerKey,&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;consumerSecret=consumerSecret,&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;requestURL=requestURL,&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;accessURL=accessURL,&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;authURL=authURL)&lt;/div&gt;
&lt;div&gt;
download.file(url=&quot;http://curl.haxx.se/ca/cacert.pem&quot;,&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; destfile=&quot;cacert.pem&quot;)&lt;/div&gt;
&lt;div&gt;
twitCred$handshake(cainfo=&quot;cacert.pem&quot;)&lt;/div&gt;
&lt;div&gt;
save(list=&quot;twitCred&quot;, file=&quot;twitteR_credentials&quot;)&lt;/div&gt;
&lt;div&gt;
load(&quot;twitteR_credentials&quot;)&lt;/div&gt;
&lt;div&gt;
registerTwitterOAuth(twitCred)#Register your app with Twitter.&lt;br /&gt;
&lt;b&gt;StreamR Package:&lt;/b&gt;&lt;br /&gt;
This package allows users to fetch twitter Data in real time by connecting to Twitter Stream API. We can obtain the package from &lt;a href=&quot;http://cran.r-project.org/web/packages/streamR/index.html&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;. &amp;nbsp;Few important functions this package offers are: it allows R users to access Twitter&#39;s search streams,user streams, parse the output into data frames.&lt;br /&gt;
&lt;br /&gt;
filterStream() - filterStream method opens a connection to Twitter’s Streaming API that will return public statuses that match one or more ﬁlter predicates like search keywords. Tweets can be ﬁltered by keywords, users, language, and location. The output can be saved as an object in memory or written to a text ﬁle.&lt;br /&gt;
&lt;br /&gt;
parseTweets() - This function parses tweets downloaded using filterStream, sampleStream or userStream and returns a data frame.&lt;br /&gt;
&lt;b&gt;Below code example shows how to fetch data in real time using RStream:&lt;/b&gt;&lt;br /&gt;
&lt;i style=&quot;background-color: #cccccc;&quot;&gt;library(streamR)&lt;/i&gt;&lt;br /&gt;
&lt;i style=&quot;background-color: #cccccc;&quot;&gt;library(twitteR)&lt;/i&gt;&lt;br /&gt;
&lt;i style=&quot;background-color: #cccccc;&quot;&gt;load(&quot;twitteR_credentials&quot;) &lt;/i&gt;&lt;i style=&quot;background-color: white;&quot;&gt;&amp;nbsp;# make using the save credentials in the previous code.&lt;/i&gt;&lt;br /&gt;
&lt;i style=&quot;background-color: #cccccc;&quot;&gt;registerTwitterOAuth(twitCred)&lt;/i&gt;&lt;br /&gt;
&lt;i style=&quot;background-color: #cccccc;&quot;&gt;filterStream(file.name = &quot;tweets.json&quot;, track = &quot;#bigdata&quot;,timeout = 0, locations=c(-74,40,-73,41), oauth = twitCred)&lt;/i&gt;&lt;br /&gt;
Executing the above will capturing Tweets on &quot;#bigdata&quot; from &quot;NEW YORK&quot; location. Here when we mention timeout=0, we are setting it to fetch continuously, to fetch records for certain time then use timeout=300 (fetches data for 300 secs)&lt;br /&gt;
To Parse the fetched tweets use the below code:&lt;br /&gt;
&lt;span style=&quot;background-color: #cccccc;&quot;&gt;tweets.df &amp;lt;- parseTweets(&quot;tweets.json&quot;)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;http://feeds.feedburner.com/DataPerspective&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.dataperspective.info/feeds/5183109032571048236/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.dataperspective.info/2013/10/Fetch-twitter-data-using-R.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/5183109032571048236'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/603166843861435412/posts/default/5183109032571048236'/><link rel='alternate' type='text/html' href='http://www.dataperspective.info/2013/10/Fetch-twitter-data-using-R.html' title='Fetch Twitter data using R'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/13993235872648224733</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3zFI42lHtuMWv8XCcNPYX1SfetS4L9wHGM_4H2L1-0ZZM_fcYFnXnwdLtCYQH4cw7D6k76OtcSn8fj7SFy6XEVAmM0Ca1t8AmL1d-zFM_VKWMbGpy_TLcSPocrJee7vKJ6AvPX4LGPwzP/s72-c/stream_RestAPI.png" height="72" width="72"/><thr:total>4</thr:total></entry></feed>