<?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: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-5049613389055921243</id><updated>2012-03-01T13:51:04.882+05:30</updated><category term='Modern Portfolio Theory'/><category term='plot'/><category term='solution'/><category term='Financial markets'/><category term='Simulation'/><category term='theme'/><category term='Keynesian economics'/><category term='example'/><category term='missing data in plot'/><category term='Memoization'/><category term='factor analysis'/><category term='GARCH'/><category term='Harry Markowitz'/><category term='Runs test'/><category term='Overnight MIBOR'/><category term='logit'/><category term='Programming'/><category term='presentation'/><category term='Time series'/><category term='beamer'/><category term='macroeconomics'/><category term='decision tree'/><category term='econometrics'/><category term='credit score'/><category term='IS-LM framework'/><category term='german credit'/><category term='auto correlation function'/><category term='Financial economics'/><category term='modelling'/><category term='Project Euler'/><category term='Unit root'/><category term='Principal component analysis'/><category term='highly correlated variables in data set'/><category term='data'/><category term='Classical economics'/><category term='LaTeX'/><category term='stock returns'/><category term='maximum likelihood estimation'/><category term='R'/><category term='problem'/><category term='ADF Test'/><title type='text'>We think therefore we R</title><subtitle type='html'>Narrating mathematics with R</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Shreyes</name><uri>http://www.blogger.com/profile/02952702110986035135</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='http://4.bp.blogspot.com/-qxonS7T4lDQ/Tnu9rDO6QhI/AAAAAAAAAOM/H6gH-qheti4/s220/DSC09046.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>25</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-8489894834339196508</id><published>2012-02-09T13:57:00.000+05:30</published><updated>2012-02-09T23:57:15.143+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Time series'/><category scheme='http://www.blogger.com/atom/ns#' term='GARCH'/><category scheme='http://www.blogger.com/atom/ns#' term='maximum likelihood estimation'/><title type='text'>GARCH estimation using maximum likelihood</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;In my &lt;a href="http://programming-r-pro-bro.blogspot.in/2011/12/modelling-returns-using-pca-evidence.html" target="_blank"&gt;previous post&lt;/a&gt; I presented my findings from my finance project under the guidance of &lt;a href="http://www.igidr.ac.in/faculty/susant/" target="_blank"&gt;Dr Susan Thomas&lt;/a&gt;. The results in my paper suggested that there are macroeconomic variables, particularly the INR/USD exchange rates, that help us understand the dynamics of stock returns. Although the results that I obtained were significant at 5% level, the weight of my assertion needs some more robust check before we consider the matter closed. One possible source of discrepancy we identified was that the error terms could be heteroskedastic. Meaning that one of our &lt;a href="http://en.wikibooks.org/wiki/Econometric_Theory/Assumptions_of_Classical_Linear_Regression_Model" target="_blank"&gt;assumption of classical linear regression model (CLRM)&lt;/a&gt; estimation viz. homoskedasticity is&amp;nbsp;violated. The resultant coefficient estimated, in case of heteroskedasticity of the error terms, can have underestimated standard errors, which in turn might lead to false acceptance/rejection of our null hypothesis. There are several&amp;nbsp;&lt;a href="http://en.wikipedia.org/wiki/Heteroscedasticity-consistent_standard_errors" target="_blank"&gt;parametric as well as non-parametric ways&lt;/a&gt; to remove the effect of heteroskedasticity in the error terms on the coefficient estimates. However, the method that I adopted to correct for this effect in my model was the path breaking conditional heteroskedasticity modelling propagated by &lt;a href="http://en.wikipedia.org/wiki/Robert_Engle" target="_blank"&gt;Robert Engl&lt;/a&gt;e for which he was awarded the Nobel prize in 2003.&lt;br /&gt;&lt;br /&gt;The idea behind auto regressive conditional heteroskedasticity (ARCH) model is quite simple and straightforward. One needs to model the mean equation (this is your regression model) and along with that there has to be a specification for the modelling the squares of the errors. Suppose we have a simple AR regression: &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;z&lt;sub&gt;t &lt;/sub&gt;= φ*z&lt;sub&gt;t-1&lt;/sub&gt; + e&lt;sub&gt;t &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/sub&gt;(Mean equation)&lt;br /&gt;&lt;br /&gt;Now to model the conditional variance of the error terms all we need to do is a simultaneous estimation of the squares of the error terms in the following manner:&lt;br /&gt;&lt;br /&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;e&lt;sub&gt;t&lt;/sub&gt; = ѵ&lt;sub&gt;t&lt;/sub&gt;h&lt;sub&gt;t&lt;/sub&gt;&lt;sup&gt;1/2&lt;/sup&gt;&amp;nbsp;, &amp;nbsp;where ѵ&lt;sub&gt;t&lt;/sub&gt;&amp;nbsp; ~&lt;sub&gt;&amp;nbsp;&lt;/sub&gt;NID(0,1)&lt;/div&gt;&lt;div class="MsoNormal"&gt;h&lt;sub&gt;t &lt;/sub&gt;= α&lt;sub&gt;0 &lt;/sub&gt;+ α&lt;sub&gt;1&lt;/sub&gt;e&lt;sub&gt;t-1&lt;/sub&gt;&lt;sup&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/sup&gt;(Variance equation)&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;span style="text-align: -webkit-auto;"&gt;The above specification of the mean and the variance equations is termed as a AR(1) ARCH(1) specification. This simultaneous estimation takes into account the particular form of heteroskedasticity (ARCH(1) in this case) and estimates the '&lt;/span&gt;φ'&amp;nbsp;&lt;span style="text-align: -webkit-auto;"&gt;coefficient accordingly. Now the above simple specification tends to pose another problem of lag selection, what lag should be considered for the variance equation. To deal with this and several other shortcomings of the simple ARCH model, Bollerslev(1986) &amp;nbsp;proposed a generalized ARCH model (GARCH). The only difference being that the variance equation now becomes:&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="text-align: -webkit-auto;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="text-align: -webkit-auto;"&gt;h&lt;sub&gt;t &lt;/sub&gt;= α&lt;sub&gt;0 &lt;/sub&gt;+ α&lt;sub&gt;1&lt;/sub&gt;e&lt;sub&gt;t-1&lt;/sub&gt;&lt;sup&gt;2&lt;/sup&gt;+ βh&lt;sub&gt;t-1&lt;/sub&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="text-align: -webkit-auto;"&gt;&lt;sub&gt;&lt;br /&gt;&lt;/sub&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="text-align: -webkit-auto;"&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;Which is nothing but a GARCH(1,1) model. The beauty of this specification is that a GARCH(1,1) model can be expressed as an ARCH(∞) model. For those who are interested in learning more about ARCH and GARCH processes and the mathematics behind them &lt;a href="http://dl.dropbox.com/u/20480592/archfull%5B1%5D.pdf" target="_blank"&gt;here&lt;/a&gt; are &lt;a href="http://www.igidr.ac.in/index.php?option=com_content&amp;amp;view=article&amp;amp;id=81%3Akrishnan-r&amp;amp;catid=13&amp;amp;Itemid=21" target="_blank"&gt;Dr Krishnan&lt;/a&gt;'s notes that provide an in-depth understanding on the matter. The reason why the ARCH and GARCH models rose to such&amp;nbsp;prominence&amp;nbsp;was because they offered us a method of not just correcting the standard errors of the estimates (as like other robust correction methods) but also provided a functional form for modelling the variance. This idea of modelling variance was heavily adopted by financial researchers. Considering the standard deviation (or&amp;nbsp;variance)&amp;nbsp;of asset prices as a crude estimate of volatility, financial researchers used ARCH and GARCH models to model asset price and/or other high frequency index volatility. &amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;Now that the idea behind the GARCH modelling is clear let us plunge right into writing the conditional maximum likelihood function for a GARCH process.&lt;br /&gt;&lt;br /&gt;&lt;script src="https://gist.github.com/1775334.js?file=GARCH.R"&gt;&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;a href="http://utkarshu.in/" target="_blank"&gt;Utkarsh&lt;/a&gt; was the generous one who provided me with the basic structure of the codes that I then customized to solve this problem at hand. Well now let me confess that a similar result or in fact a more accurate estimates of the coefficients can be obtained using unconditional maximum likelihood estimates that are offered by any of the high priced computer packages like EVIEWS, RATS, SAS etc but my motivation (apart from learning ofcourse) was to figure out a way that could give me the flexibility of modelling any functional form of heteroskedasticity. The above codes have been presented for the estimation of a GARCH(1,1) model but I could do a simple&amp;nbsp;manipulation&amp;nbsp;in the definition of&amp;nbsp;h&lt;sub&gt;t&amp;nbsp;&lt;/sub&gt;to fit any&amp;nbsp;arbitrary&amp;nbsp;functional form.&amp;nbsp;&lt;/div&gt;&lt;br /&gt;P.S Sincere thanks to&amp;nbsp;&lt;a href="http://utkarshu.in/" target="_blank"&gt;Utkarsh&lt;/a&gt;&amp;nbsp;for letting me share the codes. Any feedback/suggestions are welcome.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-8489894834339196508?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/8489894834339196508/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2012/02/garch-estimation-using-maximum.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/8489894834339196508'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/8489894834339196508'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2012/02/garch-estimation-using-maximum.html' title='GARCH estimation using maximum likelihood'/><author><name>Shreyes</name><uri>http://www.blogger.com/profile/02952702110986035135</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='http://4.bp.blogspot.com/-qxonS7T4lDQ/Tnu9rDO6QhI/AAAAAAAAAOM/H6gH-qheti4/s220/DSC09046.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-4461625306488764948</id><published>2012-01-06T00:45:00.000+05:30</published><updated>2012-01-09T19:57:36.082+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='macroeconomics'/><category scheme='http://www.blogger.com/atom/ns#' term='Keynesian economics'/><category scheme='http://www.blogger.com/atom/ns#' term='Classical economics'/><category scheme='http://www.blogger.com/atom/ns#' term='IS-LM framework'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><title type='text'>Revisiting basic macroeconomics : Illustrations with R</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;b&gt;Prologue&lt;/b&gt;&lt;br /&gt;After 3 semesters of studying economics at &lt;a href="http://www.igidr.ac.in/" target="_blank"&gt;IGIDR&lt;/a&gt;, the basics of macroeconomics still elude me. What policy shifts what curve? What determines the slope of IS-LM and AD-AS curves? What exactly was Keynes contribution to Economics? How do all these curves move in tandem? Well, I would not be surprised if you are a graduate student in economics and face similar doubts with these basic fundamentals. So in an attempt to demystify or rather simplify these macro fundamentals for econ (as well an non econ) students &lt;a href="http://www.igidr.ac.in/images/stories/stu_cv/cv_isha_agarwal.pdf" target="_blank"&gt;Isha&lt;/a&gt; and I have attempted this brave move of presenting the fundamentals of macroeconomics in a concise manner. We will try to be as explicit as we can but we might end up assuming some prior knowledge of economics. In such cases, we would encourage the readers active participation in improving the quality of the post wherever he/she thinks and idea is expressed with inadequate&amp;nbsp;explanation.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;The story&lt;/b&gt;&lt;br /&gt;Any theory/idea, be it in pure science or economics, emanates with a hypothesis that is supported by some assumptions. As the study on the subject progresses researchers try and play with the assumptions and see if they can make it better in terms of explaining the real world as we see it. Macroeconomics is no exception, classical macroeconomics dates back to the 19th century with major proponents being &lt;a href="http://en.wikipedia.org/wiki/Adam_Smith" target="_blank"&gt;Adam Smith&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/David_Ricardo" target="_blank"&gt;David Ricardo&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Thomas_Robert_Malthus" target="_blank"&gt;Malthus&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/John_Stuart_Mill" target="_blank"&gt;John Stuart Mill&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Jean-Baptiste_Say" target="_blank"&gt;J.B Say&lt;/a&gt;, etc. They believed in the functioning of free markets, that is a decentralized mechanism to ensure demand meets supply would result in efficient allocation of resources. Any counter-cyclical measure by a centralized government in order to smoothen the fluctuations in the business cycles would be self defeating. The centralized government, according to these classicals, was primarily to keep the budget deficit (difference between the govt. spending and tax revenue) in check. Meaning that in times of recession, when there is less revenue being generated by taxes, the government should &lt;b&gt;cut spending&lt;/b&gt; in order to keep the deficit in check. The reason for them to&amp;nbsp;propagate&amp;nbsp;this idea was the if govt. has to increase spending, in times of recession, it would lead to a rise in the interest rates (since it has to borrow from the citizens) and this increase in interest rates would &lt;b&gt;crowd out &lt;/b&gt;private investment (which is what we don't want at times of recession). This ideology was called into question during the great depression of 1929 in the US. This is when the "&lt;a href="http://en.wikipedia.org/wiki/Say's_law" target="_blank"&gt;Say's law&lt;/a&gt;", which was at the heart of classical economics, failed to hold.&amp;nbsp;There was mass unemployment, which was invoulantary (as opposed to the classicals view of only&amp;nbsp;voluntary unemployment), insufficient demand for the produced goods, in short "Great" was an adequate adjective to describe the depressed economic scenario during the 1930's.&lt;br /&gt;&lt;br /&gt;The Great depression of 1929 provided researchers with the much needed opportunity to revise/update the assumption that they were playing with.&amp;nbsp;&lt;a href="http://en.wikipedia.org/wiki/John_Maynard_Keynes" target="_blank"&gt;J.M Keynes&lt;/a&gt;&amp;nbsp;made a break through&amp;nbsp;contribution in revising the assumptions and ideologies of the classicals. He argued that market left completely to themselves might not lead to efficient allocation of resources. This was a paradigm shift in the way policy makers and researchers thought about macroeconomics. He argued that in times of recession the govt. should&amp;nbsp;&lt;b&gt;boost spending&lt;/b&gt;&amp;nbsp;in order to revive the economy, contrary to the idea that the classicals propagated. Government spending and private investment were seen as&amp;nbsp;&lt;b&gt;complements&lt;/b&gt;&amp;nbsp;and not substitutes.&amp;nbsp;Private spending was determined not only by the interest rates in the economy but also by the expectation of future profitability (famously called the "&lt;a href="http://en.wikipedia.org/wiki/Animal_spirits_(Keynes)" target="_blank"&gt;animal spirit&lt;/a&gt;"). Another set of assumptions that he relaxed from the classical framework was the full flexibility of wages and prices. Classical economists believed that the market mechanism ensured that the prices of commodities adjusted instantaneously and fully to make the supply = demand. For eg. if there are 10 apples being produced and the demand turns out for only 8 apples, classical economists argues that the price of the apples would adjust in a manner (fall in this case) so that the demand rises instantaneously and exactly by 2 apples and the&amp;nbsp;equilibrium&amp;nbsp;is maintained. Keynes on the other hand argued that due to rigidities in the market (sticky prices, labor unions, menu costs, etc.) the price adjustment would not be instantaneous and hence the additional demand, in the short run will have to be created by the govt. by spending more. Either it can pitch in and buy the additional 2 apples or it can expedite investment in an avenue that absorbs the labor retrenched due to insufficient demand in the apple market.&lt;br /&gt;&lt;br /&gt;Enough of literature, now let us get our hands dirty with some basic maths and visualization of some economic concept that would help us appreciate the above ideologies. We would start by illustrating the difference between the 2 idelogies using the &lt;a href="http://en.wikipedia.org/wiki/IS/LM_model" target="_blank"&gt;IS-LM&amp;nbsp;&lt;/a&gt;&amp;nbsp;framework (also called the &lt;b&gt;I&lt;/b&gt;nvestment &lt;b&gt;S&lt;/b&gt;avings/&lt;b&gt;L&lt;/b&gt;iquidity preference &lt;b&gt;M&lt;/b&gt;oney supply). The basic national income identity finds mention in the first chapters of most of the introductory Macroeconomics textbooks hence we shall start by the same identity:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Derivation&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;script src="https://gist.github.com/1567124.js?file=IS_LM.txt"&gt;&lt;/script&gt;&lt;br /&gt;For complete and comprehensive proofs of the above equations you can refer to a text book by&amp;nbsp;&lt;a href="http://www.amazon.com/Macroeconomic-Theory-Policy-William-Branson/dp/0060409320" target="_blank"&gt;William Branson&lt;/a&gt;&amp;nbsp;or another textbook by&amp;nbsp;&lt;a href="http://www.amazon.com/Macroeconomics-Rudiger-Dornbusch/dp/0072823402" target="_blank"&gt;Dornbusch and Fischer&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;IS curve:&amp;nbsp;&lt;/b&gt;The points on the IS curve represent the combinations of rate of interest (i) and output (Y) for which the goods market are in&amp;nbsp;equilibrium. Meaning, at these combinations of "I" and "Y", the aggregate supply of goods equals aggregate demand for goods in the economy.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;LM curve:&amp;nbsp;&lt;/b&gt;The points on the LM curve represent the combinations of rate of interest (i) and output for which the money market is in&amp;nbsp;equilibrium. Meaning, at these combinations of "I" and "Y", the aggregate demand for money equals aggregate supply of money in the economy. (Note that we have the prices as&amp;nbsp;&lt;b&gt;exogenously&lt;/b&gt;&amp;nbsp;given and&amp;nbsp;&lt;b&gt;fixed&lt;/b&gt;)&lt;br /&gt;&lt;br /&gt;&lt;b&gt;R codes&lt;/b&gt;&lt;br /&gt;&lt;script src="https://gist.github.com/1582223.js?file=IS_LM_Functions.R"&gt;&lt;/script&gt;&lt;br /&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;a href="http://2.bp.blogspot.com/-8_v8HpZ1-sk/Twn2Ckj7P6I/AAAAAAAAAQc/sHQxkFq61PY/s1600/IS-LM.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="315" src="http://2.bp.blogspot.com/-8_v8HpZ1-sk/Twn2Ckj7P6I/AAAAAAAAAQc/sHQxkFq61PY/s400/IS-LM.png" width="400" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="font-size: 13px;"&gt;Simple IS-LM framework with simulated data&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;b&gt;Equilibrium:&amp;nbsp;&lt;/b&gt;The point of intersection of the IS and LM curve is the combination of "I" and "Y" for which both the goods and the money market are in equilibrium.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Effects of fiscal policy (or increase in government expenditure)&lt;/b&gt;&lt;br /&gt;An increase in the government spending (fiscal expansion) results in the rightward shift of the IS curve. This happens because the autonomous component of the aggregate demand ("A" in the above derivation) increases. Fiscal policy (with exogenous price level) leads to increase in both output and interest rates.&lt;br /&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;a href="http://4.bp.blogspot.com/-P2LUpAvAFKU/Twn4Jom6ZDI/AAAAAAAAAQk/EDkmYJYOg-M/s1600/Fiscal+policy.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="316" src="http://4.bp.blogspot.com/-P2LUpAvAFKU/Twn4Jom6ZDI/AAAAAAAAAQk/EDkmYJYOg-M/s400/Fiscal+policy.png" width="400" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="font-size: 13px;"&gt;Effect of fiscal policy&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;b&gt;Effect of monetary policy (change in money supply)&lt;/b&gt;&lt;br /&gt;An increase in the money supply by the central bank (monetary policy) results in the rightward shift in the LM curve. This happens because of the increase in nominal money supply (MS), and since the prices are exogenous the entire curve shifts to the right. Monetary policy (with exogenous prices) leads to fall in interest rates and rise in the output.&lt;br /&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;a href="http://1.bp.blogspot.com/-TG_CDMIy_0s/Twn6zOGoWpI/AAAAAAAAAQs/UDjPzrLH8NA/s1600/Monetary+policy.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="316" src="http://1.bp.blogspot.com/-TG_CDMIy_0s/Twn6zOGoWpI/AAAAAAAAAQs/UDjPzrLH8NA/s400/Monetary+policy.png" width="400" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="font-size: 13px;"&gt;Effect of monetary policy&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;b&gt;Mixture of monetary and fiscal policy&lt;/b&gt;&lt;br /&gt;Any desired level of output and interest rates can be achieved by using both these policies in tandem, as illustrated below.&lt;br /&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;a href="http://4.bp.blogspot.com/-LQgsuJrkX1U/Twn79bPYm7I/AAAAAAAAAQ0/uBdhqwotE4c/s1600/Mon.Fis+policy.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="316" src="http://4.bp.blogspot.com/-LQgsuJrkX1U/Twn79bPYm7I/AAAAAAAAAQ0/uBdhqwotE4c/s400/Mon.Fis+policy.png" width="400" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="font-size: 13px;"&gt;Effect of both fiscal and monetary policy&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;b&gt;Aggregate demand curve (AD curve)&lt;/b&gt;&lt;br /&gt;Now if we introduce prices in the picture we can trace out different combinations of "&lt;b&gt;P&lt;/b&gt;"(prices) and "&lt;b&gt;Y&lt;/b&gt;" for which both the goods and the money market are in equilibrium. In order to achieve this we have endogenised the computation of prices (P) (by taking ms = (nominal money)/(prices)) &amp;nbsp;This represents an important relationship between aggregate price level and output for the economy. The downward sloping AD curve should not be confused with the demand curve for a good (as in&amp;nbsp;&lt;a href="http://en.wikipedia.org/wiki/Microeconomics" target="_blank"&gt;microeconomics&lt;/a&gt;). Although both are downward sloping, the reasons for the negative relation between the prices and output demanded are different in the 2 cases. In microeconomics if the price of a good increases less of it is demanded, ceteris paribus. However in the case of AD curve, this negative relation is established by the interplay between the goods and the money market that ensures that markets clear.&lt;br /&gt;&lt;br /&gt;&lt;script src="https://gist.github.com/1582235.js?file=AD_curve.R"&gt;&lt;/script&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-MHezxLoN_fk/TwqzOOQilNI/AAAAAAAAAQ8/MXgtet9LXEQ/s1600/AD_curve.png" imageanchor="1"&gt;&lt;img border="0" height="316" src="http://2.bp.blogspot.com/-MHezxLoN_fk/TwqzOOQilNI/AAAAAAAAAQ8/MXgtet9LXEQ/s400/AD_curve.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;This completes the story from the demand side, as to how we arrived at the aggregate demand curve for the economy. Note that we have taken a simplified version of the equations to make our point clear and to make it useful for non-econ students too. We could introduce government taxes to make the equations more realistic (and complicated) but the math looks cleaner this way and anyways the intuition remains the same even after incorporating taxes.&lt;br /&gt;&lt;br /&gt;The next thing that we need to do is to arrive at the aggregate supply curve (labor side) for the economy, which we shall take up in the next post. Once we have presented both the ideas of aggregate demand and aggregate supply, we would be in a position to better understand the above discussion about the classical and&amp;nbsp;Keynesian school of thought.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-4461625306488764948?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/4461625306488764948/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2012/01/revisiting-basic-macroeconomics.html#comment-form' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/4461625306488764948'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/4461625306488764948'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2012/01/revisiting-basic-macroeconomics.html' title='Revisiting basic macroeconomics : Illustrations with R'/><author><name>Shreyes</name><uri>http://www.blogger.com/profile/02952702110986035135</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='http://4.bp.blogspot.com/-qxonS7T4lDQ/Tnu9rDO6QhI/AAAAAAAAAOM/H6gH-qheti4/s220/DSC09046.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-8_v8HpZ1-sk/Twn2Ckj7P6I/AAAAAAAAAQc/sHQxkFq61PY/s72-c/IS-LM.png' height='72' width='72'/><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-8997561392850967241</id><published>2012-01-04T18:32:00.000+05:30</published><updated>2012-01-04T18:32:02.252+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Memoization'/><category scheme='http://www.blogger.com/atom/ns#' term='Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><category scheme='http://www.blogger.com/atom/ns#' term='Project Euler'/><title type='text'>Memoization in R : Illustrative example</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;I came across a nice problem at &lt;a href="http://projecteuler.net/" target="_blank"&gt;project euler&lt;/a&gt;&amp;nbsp;that gave me sense of satisfaction that was unusual, I think that because I don't usually get the solutions right the first time as I did in this case. Anyhow, I shall try and decode the R codes that I used in simple English language and Mathematics.&lt;br /&gt;&lt;br /&gt;&lt;script src="https://gist.github.com/1558974.js?file=Problem_14.txt"&gt;&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;Let me first illustrate the brute force method, that is usually the method used by novice coders like myself. The idea here is to find the largest number below 1 million that gives the maximum number of the above mentioned iterations.&lt;br /&gt;&lt;br /&gt;&lt;script src="https://gist.github.com/1559727.js?file=Problem_14.R"&gt;&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;So what I have done above is simply performed the iteration for each and every integer from 1 to 1 million and using a counter variable kept a track of which number gave me the largest number of iterations and recorded the&amp;nbsp;corresponding&amp;nbsp;number, which is what we needed in the end. The idea was straight forward the only challenge was to come up with that cute function (which we now see is not that challenging after all).&lt;br /&gt;&lt;br /&gt;Well, now that the novice part is done lets get to what &lt;a href="http://utkarshu.in/" target="_blank"&gt;Utkarsh&lt;/a&gt; (my pro bro) had to say about this. My codes took ~ &lt;b&gt;701.75&lt;/b&gt; seconds to run (on my &lt;a href="http://www.sony.co.in/product/vpccw12en/sku/vpccw12en_buin5" target="_blank"&gt;Sony vaio VPCCW12EN&lt;/a&gt;), this was completely fine by me. Utkarsh shrugged in his usual nonchalant manner at my codes and came up with an awesome algorithm to optimize the above calculation and saving some precious time (which I think he referred to as &lt;a href="http://en.wikipedia.org/wiki/Memoization" target="_blank"&gt;Memoization&lt;/a&gt;). The idea that he worked on was that since in many cases we would already have computed the number of iterations there was no need to keep computing then again. Suppose in the example in the question we see that 13 -&amp;gt; 40 -&amp;gt; 20 -&amp;gt; 10 -&amp;gt; 5 -&amp;gt; 16 -&amp;gt; 8 -&amp;gt; 4 -&amp;gt; 2 -&amp;gt; 1. Now in the computation of 13 if say we already have that 10 will further iterate say 6 times we would not have to go all the way to 1. Similarly even for 10 if we know that 5 further iterates 5 times we don't need to go all the way back till 1. This would be more clear when we take a look at the codes.&lt;br /&gt;&lt;br /&gt;&lt;script src="https://gist.github.com/1559838.js?file=project_14_UT.R"&gt;&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;The above codes,&amp;nbsp;courtesy Utkarsh, took ~ &lt;b&gt;50&lt;/b&gt; seconds. As it turns out I was &lt;b&gt;1,390% &lt;/b&gt;inefficient as compared to this optimal algorithm. I would glad to know if there is any other optimization technique (apart from using super computers) that might reduce the computational time, please share if you can find a better way of coding this in R.&amp;nbsp;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-8997561392850967241?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/8997561392850967241/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2012/01/memoization-in-r-illustrative-example.html#comment-form' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/8997561392850967241'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/8997561392850967241'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2012/01/memoization-in-r-illustrative-example.html' title='Memoization in R : Illustrative example'/><author><name>Shreyes</name><uri>http://www.blogger.com/profile/02952702110986035135</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='http://4.bp.blogspot.com/-qxonS7T4lDQ/Tnu9rDO6QhI/AAAAAAAAAOM/H6gH-qheti4/s220/DSC09046.JPG'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-7808380135944264567</id><published>2011-12-26T21:15:00.004+05:30</published><updated>2011-12-27T13:45:21.279+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Financial economics'/><category scheme='http://www.blogger.com/atom/ns#' term='Principal component analysis'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><title type='text'>Modelling returns using PCA : Evidence from Indian equity market</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;As my finance term paper, I investigated an interesting question where I tried to identify macroeconomic variables that explain the returns on equities. Much of the debate has already taken place on this topic which has given rise to two competing theories of asset pricing viz. CAPM (capital asset pricing theory or single factor model) and APT (arbitrage pricing theory or multi-factor model). &lt;a href="http://programming-r-pro-bro.blogspot.com/2011/10/principal-component-analysis-use_23.html" target="_blank"&gt;Here&lt;/a&gt; is a brief discussion on the two in my previous post. In this post I would like to discuss my approach to answering this question in the context of Indian stock market.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;u&gt;Methodology:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;Companies that have been actively traded on NSE stock exchange for the past 10 years (&lt;b&gt;218&lt;/b&gt; companies) were selected and their daily stock returns data for these 10 years was taken from PROWESS.&amp;nbsp;&lt;/li&gt;&lt;li&gt;Using PCA, first 10 components from the returns data of the 218 companies was extracted. More on PCA in my previous post, &lt;a href="http://programming-r-pro-bro.blogspot.com/2011/10/principal-component-analysis-use.html" target="_blank"&gt;here&lt;/a&gt;.&amp;nbsp;&lt;/li&gt;&lt;li&gt;These components were then separately regressed first on NIFTY returns (first regression)&amp;nbsp;&lt;/li&gt;&lt;li&gt;Then these components were regressed on NIFTY returns, MIBOR rate changes, and INR/USD exchange rate changes (second regression).&lt;/li&gt;&lt;li&gt;The explanatory power of the 2 regressions were compared using a F-statistic. (refer to pg. 10 in the paper attached in the end of the post)&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;b&gt;&lt;u&gt;Findings and R codes:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;We start with calculating the PCA of the returns on the 218 companies daily return data, then employing the 2 regressions, then comparing the 2 regressions using a F-statistic. F-stat tells us if there is any additional explanation offered when we include macroeconomic variables (viz. MIBOR, INR/USD) in our equation.&lt;br /&gt;&lt;br /&gt;&lt;script src="https://gist.github.com/1521777.js?file=Finance_1"&gt;&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;The results that I obtained pose an interesting observation. We find that the F-stat is significant at 5% for 7 out of the 10 regressions, meaning that out of the 10 regressions (each regression with a separate component) we find &lt;i&gt;statistically significant addition in the explanatory power of the model after adding the macroeconomic variables&lt;/i&gt;. Therefore, on &lt;b&gt;statistical ground &lt;/b&gt;I can argue that a multi factor model (APT) is preferable over a single factor model (CAPM)&amp;nbsp;for modelling stock returns&amp;nbsp;in the case of Indian equity market. This assertion, if holds true, can have reaching implications for asset pricing for Indian securities.&amp;nbsp;Let me explain why. The principal components (that are the dependent variables in the model) are essentially the common factor across all the companies stock returns with the idiosyncratic effects discounted, so any variables that explains this common component would be the systematic risk (think why!). Now we can relate it to the debate between the CAPM and APT guys. If the CAPM guys were correct, I would obtain no additional explanation in my model after adding the macroeconomic variables i.e their assertion that the market risk (market beta) capture the entire systematic risk holds true.&lt;br /&gt;&lt;br /&gt;The results, however, suggest that in &lt;b&gt;7 out of 10&lt;/b&gt; regressions there is statistically additional explanation offered by the macroeconomic variables. Well, so we can&amp;nbsp;out-rightly&amp;nbsp;reject the applicability (of the much&amp;nbsp;prevalent) CAPM in the case of Indian equities. Or is there something amiss? Now if I closely look at the absolute increase in the explanatory power by looking at the &lt;a href="http://en.wikipedia.org/wiki/Coefficient_of_determination#Adjusted_R2" target="_blank"&gt;Adjusted-R-squared &lt;/a&gt;values before and after the addition of the macroeconomic variables, the absolute increase in all the cases is &amp;lt; 1% (refer to pg. 11 in the paper at the end of this post). Therefore, &lt;i&gt;although we obtain statistical efficiency after the addition of the variables, the economic efficiency (intuition) is called to question&lt;/i&gt;. Is it worth while to complicate our model with additional macroeconomic variables, when we can simply have the market rate used as a reasonable proxy for all the variables? And all this just to prove a point that we have macro-variables that can provide 0.5% additional explanation in our model?&amp;nbsp;This takes us back to the eternal debate of statistical vs economic efficiency, what is more important? Is the above result robust enough (on economic intuition) to question the much used, simple and powerful CAPM? Is there a threshold even in statistical efficiency to ensure economic efficiency? These are some questions that still linger on in my mind.&lt;br /&gt;&lt;br /&gt;If we view the above result with this caveat of economic efficiency then there is reason for us to believe that a single factor model would be a preferable way to model stock returns. There are, however, evidences in the literature to suggest that multi factor (APT) is a superior way of modelling returns, but the identification of these "multi factors" remains a contentious issue among the researchers. In some desperate attempts to refute CAPM, researcher extracted principal components from a number of macroeconomic variables as the input to the PCA. This resulted in factors that had no economic intuition at all, that were then used as independent variables in explaining the returns. The APT (Arbitrage pricing theory) is a 'theory', whereas CAPM is a 'model' that approximates reality. So even if in reality there are multiple factors that give rise to the returns signals as we see them, the identification of these factors is not a trivial exercise as we have seen above. Statistically we managed to overturn the CAPM in the context of Indian equity markets but in term of economic intuition the results do not seem to be that promising. Therefore, the above exercise tells us exactly why people still stick to the evergreen CAPM as an asset pricing model.&lt;br /&gt;&lt;br /&gt;In case you wish to replicate the exercise the data can be obtained from here: &lt;a href="http://dl.dropbox.com/u/20480592/Returns_CNX_500.csv" target="_blank"&gt;Returns_CNX_500&lt;/a&gt;,&amp;nbsp;&lt;a href="http://dl.dropbox.com/u/20480592/Nifty_returns.csv" target="_blank"&gt;Nifty_returns&lt;/a&gt;,&amp;nbsp;&lt;a href="http://dl.dropbox.com/u/20480592/MIBOR.csv" target="_blank"&gt;MIBOR&lt;/a&gt;,&amp;nbsp;&lt;a href="http://dl.dropbox.com/u/20480592/Exchange_rates.csv" target="_blank"&gt;Exchange_rates&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://dl.dropbox.com/u/20480592/Fin_term%20paper.pdf" target="_blank"&gt;Here&lt;/a&gt; is the full text of my paper. Feedback are welcome.&amp;nbsp;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-7808380135944264567?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/7808380135944264567/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/12/modelling-returns-using-pca-evidence.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/7808380135944264567'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/7808380135944264567'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/12/modelling-returns-using-pca-evidence.html' title='Modelling returns using PCA : Evidence from Indian equity market'/><author><name>Shreyes</name><uri>http://www.blogger.com/profile/02952702110986035135</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='http://4.bp.blogspot.com/-qxonS7T4lDQ/Tnu9rDO6QhI/AAAAAAAAAOM/H6gH-qheti4/s220/DSC09046.JPG'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-1117764188988673432</id><published>2011-12-08T02:30:00.001+05:30</published><updated>2011-12-14T08:38:02.448+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Time series'/><category scheme='http://www.blogger.com/atom/ns#' term='ADF Test'/><category scheme='http://www.blogger.com/atom/ns#' term='Unit root'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><title type='text'>Movement around the mean "Stationary" OR "Unit root"</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;The idea of modelling the&amp;nbsp;time seriesof&amp;nbsp;GNP, and other macroeconomic variables, data for US as a trendstationary (TS) process was brought into question by Nelson and Plosser intheir groundbreaking&amp;nbsp;&lt;a href="http://www.sciencedirect.com/science/article/pii/0304393282900125"&gt;&lt;span style="color: blue;"&gt;research paper&lt;/span&gt;&lt;/a&gt;&amp;nbsp;in1982. Their research paper marked a paradigm shift in the way time-serieseconometrics was done post the 80's. The profound idea that prompted them tolook for an alternative to the prevalent TS process, was that theseries of GNP does not have any tendency to return back to a time trend following a shock. This means that following a shock (for example technologicalinnovations), the series keeps moving away from the time trend rather than returnback to it. If the series keeps moving away from the time trend, movements ofthe series would not be captured by a trend-stationary model.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;This marked a radical change which transformed theidea of stationarity to include another class of processes,&amp;nbsp;&lt;b&gt;differencestationary (DS)&lt;/b&gt;&amp;nbsp;processes. More on this in my previous&amp;nbsp;&lt;a href="http://programming-r-pro-bro.blogspot.com/search/label/Unit%20root"&gt;&lt;span style="color: blue;"&gt;post&lt;/span&gt;&lt;/a&gt;. But as a studentof basic time series the phenomenon of non-stationarity was not very easy forme to digest. Does it mean that if a series fluctuates around a mean, is it necessarily stationary? The answer happens to be No (now that I have completedthe course I can proudly and confidently answer that question).&amp;nbsp;Accordingto the definition of stationarity, a series is stationary if&amp;nbsp;any group ofconsecutive data points in the series,&amp;nbsp;have the same mean. Soundsconfusing? Let me illustrate this using the example of 2 Indian macro series and some R codes.The daily 3-month MIBOR rates and the daily INR/USD exchange rates for the past10 years.&lt;/span&gt;&lt;span style="font-family: 'Times New Roman', serif; font-size: 13.5pt;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: #a64d79;"&gt;###############################&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: #a64d79;"&gt;# Access the relevant files ###&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: #a64d79;"&gt;###############################&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: blue;"&gt;mibor &amp;lt;- read.csv("MIBOR.csv", na.strings="#N/A")&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: blue;"&gt;exchange &amp;lt;- read.csv("Exchange_rates.csv",na.strings="#N/A")&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: blue;"&gt;nifty &amp;lt;- read.csv("Nifty_returns.csv")&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: #a64d79;"&gt;#################################&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: #a64d79;"&gt;## Dealing with missing values ##&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: #a64d79;"&gt;#################################&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: #a64d79;"&gt;## Dealing with blanks in the MIBOR rates ##&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: blue;"&gt;mibor[, 2] &amp;lt;- approx(as.Date(mibor$Dates, '%d-%b-%y'), mibor[ ,2],as.Date(mibor$Dates, '%d-%b-%y'))$y&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: blue;"&gt;for(k in 2:nrow(mibor)) &amp;nbsp;&lt;/span&gt;&lt;span style="color: #a64d79;"&gt;# Calculating the%age change&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: blue;"&gt;{&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;mibor$Change1 &amp;lt;- diff(mibor$MIBO&lt;/span&gt;&lt;wbr style="background-color: white; text-align: -webkit-auto;"&gt;&lt;/wbr&gt;&lt;span class="Apple-style-span" style="background-color: white; text-align: -webkit-auto;"&gt;R) / mibor$MIBOR[-le&lt;/span&gt;&lt;wbr style="background-color: white; text-align: -webkit-auto;"&gt;&lt;/wbr&gt;&lt;span class="Apple-style-span" style="background-color: white; text-align: -webkit-auto;"&gt;ngth(mibor$MIBO&lt;/span&gt;&lt;wbr style="background-color: white; text-align: -webkit-auto;"&gt;&lt;/wbr&gt;&lt;span class="Apple-style-span" style="background-color: white; text-align: -webkit-auto;"&gt;R)]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: blue;"&gt;}&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: #a64d79;"&gt;## Dealing with blanks in the exchange rates ##&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: blue;"&gt;exchange[, 2] &amp;lt;- approx(as.Date(exchange$Year,'%d-%b-%y'), exchange[,2], as.Date(exchange$Year, '%d-%b-%y'))$y&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: blue;"&gt;exchange$Change &amp;lt;- as.numeric(exchange$Change)&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: blue;"&gt;for(j in 2:nrow(exchange))&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: #a64d79;"&gt;#Calculating the %age change&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: blue;"&gt;{&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: blue;"&gt;exchange$Change &amp;lt;- diff(exchange$Exchange.rates)/exchange$Exchange.rates[-length(&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;exchange$Exchange.rates)&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: blue;"&gt;}&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: #a64d79;"&gt;## Plotting the variables ##&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;png("indep_var_ns.png",width = 480, height = 480)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;par(mfrow = c(2, 1))&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;plot(as.Date(mibor$Dates,'%d-%b-%y'),mibor$MIBOR, xlab= "Date",&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;ylab= "3-month MIBOR rates (%age)", type='l', col='red',&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;main="3-month MIBOR rates")&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;abline(h = 0, lty = 8,col = "gray")&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;plot(as.Date(exchange$Year,'%d-%b-%y'), exchange$Exchange.rates, xlab= "Date",&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;ylab= "IND/USD Exchange rates", type='l', col='red',&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;main="IND/USD Exchange rate")&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;abline(h = 0, lty = 8,col = "gray")&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;dev.off()&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New'; font-size: 13.5pt;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-ymVV3zIL0Qk/Tt_d9yf-UsI/AAAAAAAAAQI/qDXAs5w1GcA/s1600/indep_var_ns.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="400" src="http://2.bp.blogspot.com/-ymVV3zIL0Qk/Tt_d9yf-UsI/AAAAAAAAAQI/qDXAs5w1GcA/s400/indep_var_ns.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;Eyeballing the above plots one can see that theseries do not have any trend in them, as in the series are moving more of lessabout a mean. But if we look at the MIBOR for example, the mean of the seriesis different in the period 2000-02 and different for 2003-04. This is the catchhere, which I think is quite probable to be overlooked by many.&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&amp;nbsp;A unit root would also cause long forays away from the mean, so&lt;/span&gt;&lt;/span&gt;&amp;nbsp;to conduct atest for non-stationarity we shall check if the above series has a unit root inthe auto-regressive (AR) polynomial using the&amp;nbsp;&lt;b&gt;ADFtest&lt;/b&gt;. And now that we can see that the meanis changing substantially over the time horizon, we would expect there to be aunit root in the series. Let us see what the results have to show.&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;gt; adf.test(exchange$Exchange.rates)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;Dickey-Fuller= -1.9266, Lag order = 13, p-value = 0.6094&lt;span style="color: #a64d79;"&gt; ## Cannot reject thenull of non-stationarity&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;alternativehypothesis: stationary&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: blue;"&gt;&amp;gt; adf.test(mibor$MIBOR)&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;Dickey-Fuller= -2.1925, Lag order = 13, p-value = 0.4968 &lt;span style="color: #a64d79;"&gt;## Cannot reject thenull of non-stationarity&lt;/span&gt;&lt;br /&gt;alternative hypothesis: stationary&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: blue;"&gt;&amp;gt; adf.test(nifty$S...P.Cnx.Nifty)&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;Dickey-Fuller= -11.8633, Lag order = 13, p-value = 0.00 &lt;span style="color: #a64d79;"&gt;## Can reject the nullof non-stationarity&lt;/span&gt;&lt;br /&gt;alternative hypothesis: stationary&lt;/span&gt;&lt;span style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;So we see that the null of unit root cannot berejected for MIBOR and INR/USD, but the null is rejected for NIFTY returns. Whyits rejected for NIFTY is because the fluctuations around the mean are of avery high frequency, so even if we took 2 different time periods thestatistical difference between their means would be negligible. Thus the NIFTYreturns gives us a stationary series. MIBOR and INR/USD series are also madestationary by taking first difference of the series. The stationary plot looklike:&lt;/span&gt;&lt;span style="font-family: 'Times New Roman', serif; font-size: 12pt;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: #a64d79;"&gt;## Plot for the %age changes of thevariables:&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: blue;"&gt;png("indep_var.png", width =480, height = 480)&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="line-height: 12.75pt; margin-bottom: .0001pt; margin-bottom: 0cm; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;par(mfrow = c(3, 1))&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="line-height: 12.75pt; margin-bottom: .0001pt; margin-bottom: 0cm; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;plot(as.Date(mibor$Dates,'%d-%b-%y'),mibor$Change1, xlab= "Date", &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="line-height: 12.75pt; margin-bottom: .0001pt; margin-bottom: 0cm; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ylab= "Change in 3-month MIBOR(%age)", type='l',col='royalblue', &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="line-height: 12.75pt; margin-bottom: .0001pt; margin-bottom: 0cm; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;main="%age change in MIBOR rates")&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="line-height: 12.75pt; margin-bottom: .0001pt; margin-bottom: 0cm; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;abline(h = 0, lty = 8, col ="gray")&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="line-height: 12.75pt; margin-bottom: .0001pt; margin-bottom: 0cm; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;plot(as.Date(nifty$Date,'%d-%b-%y'),nifty$S...P.Cnx.Nifty, xlab= "Date", &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="line-height: 12.75pt; margin-bottom: .0001pt; margin-bottom: 0cm; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ylab= "NIFTY returns(%age)", type='l', col='royalblue', &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="line-height: 12.75pt; margin-bottom: .0001pt; margin-bottom: 0cm; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;main="NIFTY returns")&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="line-height: 12.75pt; margin-bottom: .0001pt; margin-bottom: 0cm; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;abline(h = 0, lty = 8, col ="gray")&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="line-height: 12.75pt; margin-bottom: .0001pt; margin-bottom: 0cm; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;plot(as.Date(exchange$Year, '%d-%b-%y'),exchange$Change, xlab= "Date", &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="line-height: 12.75pt; margin-bottom: .0001pt; margin-bottom: 0cm; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ylab= "IND/USD Exchange rates change(%age)", type='l',col='royalblue', &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="line-height: 12.75pt; margin-bottom: .0001pt; margin-bottom: 0cm; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;main="IND/USD Exchange rate changes(%age)")&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="line-height: 12.75pt; margin-bottom: .0001pt; margin-bottom: 0cm; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;abline(h = 0, lty = 8, col ="gray")&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;dev.off()&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New'; font-size: 10pt;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-te0jddr5g1s/Tt_j7yy8rEI/AAAAAAAAAQQ/Vm_ScE50jSM/s1600/indep_var.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="400" src="http://1.bp.blogspot.com/-te0jddr5g1s/Tt_j7yy8rEI/AAAAAAAAAQQ/Vm_ScE50jSM/s400/indep_var.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="line-height: 12.75pt; margin-bottom: .0001pt; margin-bottom: 0cm; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div align="center" style="text-align: -webkit-auto;"&gt;So there are 2 takes from the exercise above (1) Series fluctuatingabout a mean need not necessarily be stationary (empirically shown) (2) 3-monthMIBOR and INR/USD exhibit unit roots in the given (10 year daily) sample for India. The first point might be a trivial statement for advancedeconometricians, but for the novice and the&amp;nbsp;amateurs I think this wouldserve as a good basic exercise.&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;In case you wish to replicate the exercise, datacan be obtained from here:&amp;nbsp;&lt;/span&gt;&lt;a href="http://dl.dropbox.com/u/20480592/MIBOR.csv"&gt;&lt;span style="color: blue;"&gt;MIBOR&lt;/span&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;a href="http://dl.dropbox.com/u/20480592/Exchange_rates.csv"&gt;&lt;span style="color: blue;"&gt;INR/USD&lt;/span&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-size: small;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;a href="http://dl.dropbox.com/u/20480592/Nifty_returns.csv"&gt;&lt;span style="color: blue;"&gt;NIFTY&lt;/span&gt;&lt;/a&gt;.&lt;/span&gt;&amp;nbsp;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-1117764188988673432?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/1117764188988673432/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/12/movement-around-mean-stationary-or-unit.html#comment-form' title='10 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/1117764188988673432'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/1117764188988673432'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/12/movement-around-mean-stationary-or-unit.html' title='Movement around the mean &quot;Stationary&quot; OR &quot;Unit root&quot;'/><author><name>Shreyes</name><uri>http://www.blogger.com/profile/02952702110986035135</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='http://4.bp.blogspot.com/-qxonS7T4lDQ/Tnu9rDO6QhI/AAAAAAAAAOM/H6gH-qheti4/s220/DSC09046.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-ymVV3zIL0Qk/Tt_d9yf-UsI/AAAAAAAAAQI/qDXAs5w1GcA/s72-c/indep_var_ns.png' height='72' width='72'/><thr:total>10</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-8882486632774357849</id><published>2011-11-14T21:30:00.001+05:30</published><updated>2011-11-17T00:20:49.565+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='theme'/><category scheme='http://www.blogger.com/atom/ns#' term='beamer'/><category scheme='http://www.blogger.com/atom/ns#' term='LaTeX'/><category scheme='http://www.blogger.com/atom/ns#' term='presentation'/><title type='text'>Create your own Beamer template</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;div style="text-align: justify;"&gt;For the past couple of days, I had been searching for a tutorial that would show how to create a custom &lt;a href="http://www.google.co.in/url?sa=t&amp;amp;rct=j&amp;amp;q=beamer&amp;amp;source=web&amp;amp;cd=1&amp;amp;ved=0CC4QFjAA&amp;amp;url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FBeamer_(LaTeX)&amp;amp;ei=FTvBTuS7FYvprQf4xs28AQ&amp;amp;usg=AFQjCNEmCmMhbuyDRhtWr0I5L3JNG33KBw&amp;amp;sig2=pNBNjmI4_65tx3bVfr8ERQ"&gt;Beamer&lt;/a&gt; template. I found some great resources and some really great customized templates (I have listed the ones that I referred to below) but none indicated how should I go about it. There was just lines and lines of code and for someone like me, who is&amp;nbsp;programmatically challenged, the task of creating my own template seemed very daunting. So I slowly started reading some of code that I found and tried to made sense of. I did eventually succeed but not entirely.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;I did manage to create my own template but is relatively much simpler as compared to the ones that I saw over the internet. But this did get me started, so I am hoping that anybody who's looking for a decently documented procedure on the topic would find this post helpful.&amp;nbsp;The code is relatively simple, both to understand and execute (I hope).&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;One of the best documents to understand&amp;nbsp;&lt;a href="http://www.google.co.in/url?sa=t&amp;amp;rct=j&amp;amp;q=beamer&amp;amp;source=web&amp;amp;cd=1&amp;amp;ved=0CC4QFjAA&amp;amp;url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FBeamer_(LaTeX)&amp;amp;ei=FTvBTuS7FYvprQf4xs28AQ&amp;amp;usg=AFQjCNEmCmMhbuyDRhtWr0I5L3JNG33KBw&amp;amp;sig2=pNBNjmI4_65tx3bVfr8ERQ"&gt;Beamer&lt;/a&gt;&amp;nbsp;is the &lt;a href="http://www.google.co.in/url?sa=t&amp;amp;rct=j&amp;amp;q=beamer%20user%20guide&amp;amp;source=web&amp;amp;cd=1&amp;amp;ved=0CB4QFjAA&amp;amp;url=http%3A%2F%2Fctan.org%2Ftex-archive%2Fmacros%2Flatex%2Fcontrib%2Fbeamer%2Fdoc%2Fbeameruserguide.pdf&amp;amp;ei=6TjCTu71PMvjrAfW-ajdCw&amp;amp;usg=AFQjCNEhi4rh3-3xIqNLhqql5sts0anT5w&amp;amp;sig2=uyFev7c0fBZxw25gjhC92Q"&gt;Beamer user guide&lt;/a&gt;. It explains the&amp;nbsp;&lt;a href="http://www.google.co.in/url?sa=t&amp;amp;rct=j&amp;amp;q=beamer&amp;amp;source=web&amp;amp;cd=1&amp;amp;ved=0CC4QFjAA&amp;amp;url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FBeamer_(LaTeX)&amp;amp;ei=FTvBTuS7FYvprQf4xs28AQ&amp;amp;usg=AFQjCNEmCmMhbuyDRhtWr0I5L3JNG33KBw&amp;amp;sig2=pNBNjmI4_65tx3bVfr8ERQ"&gt;Beamer&lt;/a&gt;&amp;nbsp;mechanisms very well and I used it as one of the major references while preparing this template. According to the guide,&amp;nbsp;&lt;a href="http://www.google.co.in/url?sa=t&amp;amp;rct=j&amp;amp;q=beamer&amp;amp;source=web&amp;amp;cd=1&amp;amp;ved=0CC4QFjAA&amp;amp;url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FBeamer_(LaTeX)&amp;amp;ei=FTvBTuS7FYvprQf4xs28AQ&amp;amp;usg=AFQjCNEmCmMhbuyDRhtWr0I5L3JNG33KBw&amp;amp;sig2=pNBNjmI4_65tx3bVfr8ERQ"&gt;Beamer&lt;/a&gt;&amp;nbsp;presentations have five flavours of themes&lt;/div&gt;&lt;br /&gt;&lt;ol style="text-align: left;"&gt;&lt;li style="text-align: justify;"&gt;Presentation themes - Every aspect of the presentation is detailed here. The colour, the font, the way the bullets look. The way the enumeration goes. The size and position of the logo etc.&lt;/li&gt;&lt;li style="text-align: justify;"&gt;Colour themes - Just of the colour details. This can be created as a separate file from the presentation theme and then later called with other presentation themes. I will explain this below. In fact, let's just enumerate the other three and jump to the explanation of how all this connects.&lt;/li&gt;&lt;li style="text-align: justify;"&gt;Font themes&lt;/li&gt;&lt;li style="text-align: justify;"&gt;Inner themes - Design the elements that are "inside" the frame like the environments, theorems, blocks etc.&lt;/li&gt;&lt;li style="text-align: justify;"&gt;Outer themes - Design the outer space of the frame like the headline, footline, sidebar etc.&amp;nbsp;&lt;/li&gt;&lt;/ol&gt;&lt;div style="text-align: justify;"&gt;To create a Beamer theme we need to specifiy four types of details-- colour, font, inner, outer. These details can be mentioned in the presentation theme itself or can be created as different files and then called in the TeX document. It is actually advisable to create them as separate files since this will allow us to use these specific themes with other presentation themes as well by just calling these in the TeX document. Additionally, it is also a more efficient way of working. For example, let's say that we like the overall feel of the Pittsburgh theme but just want to change the colours from blue to black. Then we need to just create the colour theme file and call it in the TeX document. This wouldn't be possible if we had just created one file with all the details. Let's just try to create a simple theme and see how it goes.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%%%%%-------------------------------------------%%%%%%%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&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; File created: 9 Nov, 2011&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% This beamer style file was created just for experminetation and&amp;nbsp;learning.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% The file is pretty much self-explanatory and as of 10 Nov, 2011, pretty much bug free (This is because I really don't know what "bugs" are and/or how to find them.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Howvere, in case you find and bugs, issues, or have any suggestions/comments, please feel free to contact me at programming-r-pro-bro.blogspot.com&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%%%%%-------------------------------------------%%%%%%%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% This style file is a combination of the four files required to create the Moo beamer theme.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%  1. Inner&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%       2. Outer&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%     3. Color&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%  4. Font&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% You can directly use this file instead of the using the other theme file "beamerthemeMoo-whole.sty" and calling the 4 styles in the TeX document.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%% Defining the preamble&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\mode&amp;lt;presentation&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\usepackage{pgfcomp-version-0-65}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\usepackage{color}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%%%%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%  Color theme&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%%%%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% We need to define a set of colours that will be assigned to various parts of the presentation. I personally believe that Beamer already does have some great themes, so if we really want to build our own, we will have go to the tiniest of details and tweak them.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% LaTeX, like R, has a huge set of colours to choose from and there are many ways we can access them.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Colours in LaTeX are provided by the xcolor pacakge that loads by default. However, the xcolor package by itself does not provide enough breadth in colours.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% More colours can be called by using the "dvipsnames" options while calling beamer in the preamble of the document, e.g., \documentclass[xolor = dvipsnames]{beamer}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Other than that we can also blend differnt colours to achive the desired colour mix or simply use the RGB codes to call that particular color.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% For blending two colours, we need to specify the two colours (Duh-uh!) and the percentage share of the two colours in the folowing format: &amp;lt;color1&amp;gt;!&amp;lt;percentage of color1&amp;gt;!&amp;lt;color2&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% If percentage of color1 is specified as x%, then percentage of color2 will automatically be taken as (100 - n)%.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Why don't we try out a few options and see for ourselves.   &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Here we are defining only two colours. We are primarily only going to use the first one.&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\definecolor{Ftitle}{rgb}{0, 0, 0} % (rgb - 0, 0, 0) is nothing but black&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Here, we are using "rgb" in small case and this notation for decimal values of "rgb" ranging from 0 to 1&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% To specify the integer values of "RGB", ranging from 0 t0 255, we need to use the "RGB" in CAPS or UPPERCASE &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Also, the first curly braces include the name that we assign to the colour combination&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\definecolor{Descitem}{RGB}{0, 0, 139}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\definecolor{StdTitle}{RGB}{26, 33, 141}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\definecolor{StdBody}{RGB}{213,24,0}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\definecolor{AlTitle}{RGB}{255, 190, 190}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\definecolor{AlBody}{RGB}{213,24,0}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\definecolor{ExTitle}{RGB}{201, 217, 217}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\definecolor{ExBody}{RGB}{213,24,0}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Another color for the background canvas using the blending option&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%\definecolor{BgShade}{red!30!white}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%% Important note: While trying this, I found out that a colour cannot be defined this way, it can only be set or called for a particular feature using the \setbeamercolor{}{} command. We will use this below to show this.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Assign colours to different constitutens of the presentation as per the requriements.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% This sets the colour of the title of the presentation and titles of all the slides in the presentation to black.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamercolor{frametitle}{fg = Ftitle}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamercolor{title}{fg = Ftitle}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% In case you choose to display the Table of Contents, or the Outline slide.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamercolor{section in toc}{fg = Ftitle}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamercolor{section in toc shaded}{fg = Ftitle} &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% The colour of all the items, subitems and and subsubitems are set to black.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamercolor{item}{fg = Ftitle}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamercolor{subitem}{fg = Ftitle}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamercolor{subsubitem}{fg = Ftitle}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% This sets the color for each item heading of the description environment.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamercolor{description item}{fg = Descitem}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% NOTE: Setting the color black for all the items also sets in black for other environemnts like enumerate.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% We also need to fix the colours for captions for figures and tables.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamercolor{caption}{fg = Ftitle}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamercolor{caption name}{fg = Ftitle}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% In addition, we can also change the background colour of the slides depending on pur requirement.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% \setbeamercolor{background canvas}{bg = blue!5}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% We have commented out this command because this is just for illustrative purposes and has not been used to define the background colour of the slides.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Now, there are three types of boxes in beamer: &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%   1. Simple, or standard block, which can be invoked using definition or theorem&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%  2. Alert block&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%  3. Example block &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% We will customize all these blocks based on our requirements&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Standard block&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamercolor{block title}{fg = Descitem, bg = StdTitle!15!white}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamercolor{block body}{bg = StdBody!5!white}&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Alert block&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamercolor{block title alerted}{bg = AlTitle}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamercolor{block body alerted}{bg = AlBody!5!white}&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Example block&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamercolor{block title example}{bg = ExTitle}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamercolor{block body example}{bg = ExBody!5!white}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% And one final thing, the colour of the text&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamercolor{normal text}{fg = Ftitle}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%%%%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%  Font theme&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%%%%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Here we are using default fonts&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\usefonttheme{professionalfonts}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: inherit;"&gt;% Font for the presentation title&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamerfont{title}{size = \huge}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Font of the frame titles&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;\setbeamerfont{frametitle}{size = \Large}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;%%%%%%%%%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;%%  Inner theme&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;%%%%%%%%%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Here we am using the rounded theme for the overall "feel" of the presentation. You can change the specific details by editing that particular option as we have done below.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\useinnertheme{rounded}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Instead of rounded circles, we will use triangles as the indicator for items.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamertemplate{itemize items}[triangle]&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% The default option for enumerate environment removes the circles around the numbers provided by the "rounded" inner theme. Just simple numbers remain.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;%\setbeamertemplate{enumerate items}[default]&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%%%%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%  Outer theme&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%%%%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% The outer theme takes the most amount of effort and time to customize. &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% In the outer theme, we will try to do the following:&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% 1. Change the headline by putting a logo and a horizontal line&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% 2. Change the footline and include custom information depending on our requirements&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% 3. Organize the presentation title and the frame titles&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Let's start with the headline. The approach that we plan to take for the headline and footline is similar.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% We will first define a new command and then include the command in the \setbeamertemplate{} option. &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% I tried a couple of approaches that would make the method more simple but could not come up with one. % In case you do find a more aesthetic approach, please do send it accross. The contact information is on the top of the page.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% First the horizontal line on the top portion of the slides&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Add a horizontal line that runs from left of the slide to the right, just below the logo.  &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\newcommand{\LogoLine}{%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;  \raisebox{-12mm}[0pt][0pt]{%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;    \begin{pgfpicture}{0mm}{0mm}{0mm}{0mm}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;      \pgfsetlinewidth{0.28mm}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;       \color{gray}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;       \pgfline{\pgfpoint{-3mm}{1mm}}{\pgfpoint{10.8cm}{1mm}}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;    \end{pgfpicture}}}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Include the line that we just created in the headline&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamertemplate{headline}[text line]{\LogoLine}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Now the logo. As it turns out. I could not include the logo in the headline. If I tried to, the headline kept shifting downwards.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Acting a little smart and lazy, I just included the logo in the right sidebar and shifted it up. &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamertemplate{sidebar canvas right}{&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt; \vspace*{3pt}\hspace*{-25pt}%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;  {\includegraphics[height=28pt]{moo.png}}}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Now that we have changed the headline, we will need to orient the frame titles in a way that the come at the right stop just above the horizontal line.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamertemplate{frametitle}{&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\vspace*{4mm}\hspace*{-2mm}\insertframetitle}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Like mentioned above, we will take a similar approach to customize the footline as well and inclued FAA in it.&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\newcommand{\Ffootline}{%&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;   \insertsection % The left end of the footline&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;  \hfill&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt; \textit{Moo} % The center&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;  \hfill&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;   \insertframenumber/\inserttotalframenumber} % And the right end&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamertemplate{footline}{%&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;  \usebeamerfont{structure}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;   \begin{beamercolorbox}[wd=\paperwidth,ht=2.25ex,dp=1ex]{title in head/foot}%&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;     \Tiny\hspace*{4mm} \Ffootline \hspace{4mm}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;   \end{beamercolorbox}}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% We will also remove the navigation symbols, which I personally don't find very useful&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamertemplate{navigation symbols}{}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;%%%%%%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Now the toughest part--at least for me--customizing the title page.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Putting a logo on the title page and text beside it was quite a difficult task and to be fairly honest, my code is not effecient at all. At the very least, it is plain clumsy.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Though it does solve the purpose (I hate to use this phrase) it is neither "neat" nor "cool".&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Anyway, let's see how we went about it.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% For the title page, we needed a logo on the left, a vertical separater line, and finally a place for the title, author, date etc. &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% First, let's create the line&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\newcommand{\TitleLine}{%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;  \raisebox{-12mm}[0pt][0pt]{%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;    \begin{pgfpicture}{0mm}{0mm}{0mm}{0mm}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;      \pgfsetlinewidth{0.10mm}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;       \color{gray}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;       \pgfline{\pgfpoint{55mm}{0mm}}{\pgfpoint{55mm}{50mm}}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;    \end{pgfpicture}}}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Now let's create commands for the title etc., that we can call later&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Title&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\newcommand{\MyTitle}{%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;  \hspace*{60mm}\vspace{-25mm}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;     \centering \inserttitle}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Subtitle     &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\newcommand{\MySubTitle}{%&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt; \hspace*{60mm}\vspace{-25mm}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;    \centering \footnotesize \textit{\insertsubtitle}}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Author     &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt; \newcommand{\MyAuthor}{   &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;  \hspace*{60mm}\vspace{-25mm}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;  \centering \insertauthor}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Institute  &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt; \newcommand{\MyInstitute}{   &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;  \hspace*{60mm}\vspace{-25mm}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;  \centering \footnotesize \textit{\insertinstitute}}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% Date   &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt; \newcommand{\MyDate}{  &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;   \hspace*{60mm}\vspace{-25mm}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;   \centering \insertdate}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% We declare the image that will be used as the logo&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\pgfdeclareimage[width = 0.20\paperwidth]{big}{moo.png}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;% This is quite a complicated command. We basically create a "beamercolorbox" for each field and invoke the commands that we had created earlier.&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamertemplate{title page}{\TitleLine&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt; \hspace*{11mm}\vspace*{-60mm}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;   \begin{beamercolorbox}[wd=0.5\paperwidth,ht=0.13\paperwidth]{Title}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;   \pgfuseimage{big}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;  \end{beamercolorbox}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;%  &lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;  \begin{beamercolorbox}[wd=\paperwidth,ht=0.06\paperwidth]{Title}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;      \usebeamerfont{Title}%&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;       \MyTitle &lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;  \end{beamercolorbox}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;%&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;     \begin{beamercolorbox}[wd=\paperwidth,ht=0.03\paperwidth]{Title}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;      \usebeamerfont{Title}%&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;       \MySubTitle &lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;   \end{beamercolorbox}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;%&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;   \begin{beamercolorbox}[wd=\paperwidth,ht=0.06\paperwidth]{Title}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;      \usebeamerfont{Title}%&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;       \MyAuthor &lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;    \end{beamercolorbox}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;%&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;     \begin{beamercolorbox}[wd=\paperwidth,ht=0.03\paperwidth]{Title}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;      \usebeamerfont{head/foot}%&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;       \MyInstitute&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;  \end{beamercolorbox}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;% &lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;     \begin{beamercolorbox}[wd=\paperwidth,ht=0.07\paperwidth]{Title}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;      \usebeamerfont{Title}%&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;       \MyDate&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;  \end{beamercolorbox}}&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\mode&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;lt;all&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Well, this pretty much completes the creation of Moo. I will upload the Sty file, TeX file and sample pdf for direct use and references.&amp;nbsp;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Also, in case you are trying to create a more complicated theme you should definitely check out some of the resources mentioned below.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;ol&gt;&lt;li&gt;Most important - The&amp;nbsp;&lt;a href="http://www.google.co.in/url?sa=t&amp;amp;rct=j&amp;amp;q=beamer%20user%20guide&amp;amp;source=web&amp;amp;cd=1&amp;amp;ved=0CB4QFjAA&amp;amp;url=http%3A%2F%2Fctan.org%2Ftex-archive%2Fmacros%2Flatex%2Fcontrib%2Fbeamer%2Fdoc%2Fbeameruserguide.pdf&amp;amp;ei=6TjCTu71PMvjrAfW-ajdCw&amp;amp;usg=AFQjCNEhi4rh3-3xIqNLhqql5sts0anT5w&amp;amp;sig2=uyFev7c0fBZxw25gjhC92Q"&gt;Beamer user guide&lt;/a&gt;&lt;/li&gt;&lt;li&gt;A great repository of themes &lt;a href="http://latex.simon04.net/"&gt;here&lt;/a&gt;&lt;/li&gt;&lt;li&gt;A great introduction to Beamer &lt;a href="http://www.math.umbc.edu/~rouben/beamer/quickstart.html"&gt;here&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Some &lt;a href="http://tex.stackexchange.com/questions/26299/how-does-beamer-build-the-frame"&gt;information&lt;/a&gt; that helped&lt;/li&gt;&lt;li&gt;Another custom theme &lt;a href="http://bigwww.epfl.ch/guerquin/documents/beamerthemeBIG.sty"&gt;here&lt;/a&gt;&lt;/li&gt;&lt;li&gt;A very good Beamer &lt;a href="http://www.matthiaspospiech.de/latex/vorlagen/beamer/content/beamer-examples/"&gt;example&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Hope this information helps.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;The uploaded file&lt;br /&gt;&lt;br /&gt;&lt;a href="http://dl.dropbox.com/u/20480592/beamertemplateMoo.pdf"&gt;Style file&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://dl.dropbox.com/u/20480592/beamertemplateMoo.tex"&gt;TeX File&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://dl.dropbox.com/u/20480592/beamerthemeMoo.sty"&gt;PDF output&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-8882486632774357849?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/8882486632774357849/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/11/create-your-own-beamer-template.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/8882486632774357849'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/8882486632774357849'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/11/create-your-own-beamer-template.html' title='Create your own Beamer template'/><author><name>MK</name><uri>http://www.blogger.com/profile/03144859563025317904</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-2765547462295538329</id><published>2011-11-05T02:38:00.001+05:30</published><updated>2011-11-05T02:38:23.317+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Time series'/><category scheme='http://www.blogger.com/atom/ns#' term='Unit root'/><category scheme='http://www.blogger.com/atom/ns#' term='Simulation'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><title type='text'>Unit root versus breaking trend: Perron's criticism</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;I came across an ingenious simulation by Perronduring my Time-series lecture which I thought was worth sharing. The idea wasto put your model to a further test of breaking trend before accepting the nullof unit root. Let me try and illustrate this in simple language.&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;A non-stationary time series is one that has itsmean changing with time. In other words, if you randomly choose a bunch of values fromthe series from the middle, you would end up with different values of mean fordifferent bunches. In short there is a trend in the data which needs to beremoved to make it stationary and proceed with our analysis (its far easier towork with stationary timeseries).&amp;nbsp;In order to deal with non-stationarytime-series one has to be careful about the kind of non-stationarity&amp;nbsp;thatis exhibited by the variable. Two corrections for non-stationarity include fitting&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;b&gt;(1) Trend stationary (TS)&lt;/b&gt;&amp;nbsp;models, whichare suitable for models that have a deterministic trend and fluctuations aboutthat deterministic trend.&amp;nbsp;This can be fit by a simple &lt;b&gt;z&lt;sub&gt;t&lt;/sub&gt; = a + bt + e&lt;sub&gt;t&lt;/sub&gt;&lt;/b&gt;&lt;sub&gt; &lt;/sub&gt;where e&lt;sub&gt;t &lt;/sub&gt;~ARMA(p,q)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;b&gt;(2) Difference stationary (DS)&lt;/b&gt;&amp;nbsp;models, whichare suitable for models having a stochastic trend. The DS models areappropriate for models that have a&amp;nbsp;&lt;b&gt;unit root&lt;/b&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;in the AR polynomial. Unit root in theAR polynomial means that the trend part in the series cannot be represented bya simple linear trend with time (a + bt). And the correct representation is &lt;b&gt;(1 – B)z&lt;sub&gt;t &lt;/sub&gt;= a + e&lt;sub&gt;t,&lt;/sub&gt;&lt;/b&gt;&lt;sub&gt;&lt;/sub&gt;where e&lt;sub&gt;t &lt;/sub&gt;is i.i.d.&amp;nbsp;&lt;span style="font-size: 12pt;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;The&amp;nbsp;asymptotic&amp;nbsp;properties of the estimates, forecasts andforecast errors vary substantially between the TS and DS models. (For the onesinterested in the algebra behind this, lecture notes of&amp;nbsp;&lt;a href="http://www.igidr.ac.in/index.php?option=com_content&amp;amp;view=article&amp;amp;id=81%3Akrishnan-r&amp;amp;catid=13&amp;amp;Itemid=21#kittu/"&gt;&lt;span style="color: blue; mso-bidi-font-size: 11.0pt;"&gt;Dr. Krishnan&lt;/span&gt;&lt;/a&gt;&amp;nbsp;are&amp;nbsp;&lt;a href="http://dl.dropbox.com/u/20480592/Handout7.pdf"&gt;&lt;span style="color: blue; mso-bidi-font-size: 11.0pt;"&gt;here&lt;/span&gt;&lt;/a&gt;) Therefore it is important for us be sure thatthe model belongs to the appropriate class before we fit a TS or DS model. Thisis the reason why the clash between the two school of thoughts has bred enormousliterature and discussions on the methodology to check for unit roots. Onecould&amp;nbsp;try and&amp;nbsp;endlessly argue about these discussions but I want toillustrate the genius of Perron who criticized the idea of fitting a DS modelto series that could have a structural breaks. He said that you ought to takeinto account the structural break before you check for the unit roots, if youdon't do so, you might end up accepting the null of unit root, even when thetrue data generating process (DGP) is a trend stationary process. Heillustrated this using a simple, but very elegant, simulation exercise. &lt;a href="http://www.blogger.com/profile/03144859563025317904"&gt;Madhav&lt;/a&gt;and I, along with fine-tuning on the codes provided by &lt;a href="http://www.blogger.com/profile/00017318258700020690"&gt;Utkarsh&lt;/a&gt;, replicated thisexercise with R.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;The steps involved are as follows:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraph" style="margin-bottom: 0.0001pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-align: justify; text-indent: 17.85pt;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;(1)&lt;span style="font: 7.0pt &amp;quot;Times New Roman&amp;quot;;"&gt;&amp;nbsp; &lt;/span&gt;&lt;!--[endif]--&gt;&amp;nbsp;Simulate 1000 series with the DGP as:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraph" style="margin-bottom: 0.0001pt; margin-left: 72pt; margin-right: 0cm; margin-top: 0cm; text-align: justify; text-indent: 36pt;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&amp;nbsp;&lt;b&gt;z­­&lt;sub&gt;t &lt;/sub&gt;= u&lt;sub&gt;1 &lt;/sub&gt;&amp;nbsp;+ (u&lt;sub&gt;2 &lt;/sub&gt;– u&lt;sub&gt;1&lt;/sub&gt;)DU&lt;sub&gt;t &lt;/sub&gt;+bt + e&lt;sub&gt;t&lt;/sub&gt;&lt;/b&gt;&lt;sub&gt; &lt;/sub&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 0.0001pt; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;where e&lt;sub&gt;t ­&lt;/sub&gt;are i.i.d innovations and t = 1,2,3,...100. Forsimplicity I have assumed b = 1 and u&lt;sub&gt;1 &lt;/sub&gt;= 0. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraph" style="margin-bottom: 0.0001pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-align: justify; text-indent: 17.85pt;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;(2)&lt;span style="font: 7.0pt &amp;quot;Times New Roman&amp;quot;;"&gt;&amp;nbsp; &lt;/span&gt;&lt;!--[endif]--&gt;&amp;nbsp;Assume that there is a crash at time T&lt;sub&gt;b&lt;/sub&gt;= 50 and the entire series comes down by amount u&lt;sub&gt;2&lt;/sub&gt;.&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraph" style="margin-bottom: 0.0001pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-align: justify; text-indent: 17.85pt;"&gt;&lt;span class="Apple-style-span" style="font-size: 18px;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraph" style="margin-bottom: 0.0001pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 17.85pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;## Simulating a trend stationary model with a crash in the intercept ##&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraph" style="margin-bottom: 0.0001pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 17.85pt;"&gt;&lt;/div&gt;&lt;div class="MsoListParagraph" style="margin-bottom: 0.0001pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 17.85pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;t &amp;lt;- c(1:100) &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# specifying the time&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraph" style="margin-bottom: 0.0001pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 17.85pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;dummy &amp;lt;- as.numeric(ifelse(t &amp;lt;= 50, 0, 1)) &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# specifying the dummy for trend break at T = 50&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraph" style="margin-bottom: 0.0001pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 17.85pt;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraph" style="margin-bottom: 0.0001pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 17.85pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;z &amp;lt;- ts(t - 15*dummy + rnorm(100, mean = 0, sd = 3))&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# This is the trend stationary model with break in trend&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraph" style="margin-bottom: 0.0001pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 17.85pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;x &amp;lt;- ts(t - 15*dummy) &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# This is just the trend line that we see in "red" in the plot below&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraph" style="margin-bottom: 0.0001pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 17.85pt;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraph" style="margin-bottom: 0.0001pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 17.85pt;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;plot(z, main = "Simulated series with break at T = 50")&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraph" style="margin-bottom: 0.0001pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 17.85pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;lines(x, col = "red") &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;## Plotting a sample of the model that we have simulated&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-6UA9VT_qmSo/TrRDRQjwCGI/AAAAAAAAAPw/oJUYLp-Z8RA/s1600/Simulated+series.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;img border="0" height="315" src="http://2.bp.blogspot.com/-6UA9VT_qmSo/TrRDRQjwCGI/AAAAAAAAAPw/oJUYLp-Z8RA/s400/Simulated+series.png" width="400" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="MsoListParagraph" style="margin-bottom: 0.0001pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 17.85pt;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit; font-size: medium;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraph" style="margin-bottom: 0.0001pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 17.85pt;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;(3)&lt;span style="font: normal normal normal 7pt/normal 'Times New Roman';"&gt;&amp;nbsp; &lt;/span&gt;&lt;!--[endif]--&gt;&amp;nbsp;For these simulations compute theautoregressive coefficient, “rho” in the regression:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraph" style="margin-bottom: 0.0001pt; margin-left: 72pt; margin-right: 0cm; margin-top: 0cm; text-indent: 36pt;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;z&lt;sub&gt;t&lt;/sub&gt; = u + bt + ‘rho’z&lt;sub&gt;t-1&lt;/sub&gt;+ e&lt;sub&gt;t&lt;/sub&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;(4)&lt;span style="font: 7.0pt &amp;quot;Times New Roman&amp;quot;;"&gt;&amp;nbsp; &lt;/span&gt;&lt;!--[endif]--&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;Plot the cumulativedistribution function (c.d.f) of “rho” for different values of u&lt;sub&gt;2&lt;/sub&gt;(crash).&lt;span class="Apple-style-span" style="font-size: medium;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span style="font-size: 13.5pt;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;## Now we will simulate the sample data above 1000 times and check for unit roots for each of these samples ##&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;# For simplicity we define a function to generate the "rho's" for each of the simulated series&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;sim &amp;lt;- function(crash) &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;## Function name "sim"&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;{&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;d &amp;lt;- ts(t - crash*dummy + rnorm(100, mean = 0, sd = 3))&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;## saving the simulated series in "d"&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;trend &amp;lt;- lm(d ~ t)&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;## remove the trend from the&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;simulated series&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;# crash in the above function refers to the value of&amp;nbsp;&lt;/span&gt;&lt;b style="text-align: justify; text-indent: 48px;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;u&lt;/span&gt;&lt;sub&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;2&lt;/span&gt;&amp;nbsp;&lt;/sub&gt;&lt;/b&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;in equation 1&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;res &amp;lt;- ar(ts(trend$residuals), order=1, aic= FALSE) &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;##&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;Fit an AR(1) model to the residue obtained after&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;detrending the series&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;if(length(res$ar) &amp;lt; 1) 0 else res$ar&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;## Return the ar&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;coefficient of the fitted AR(1) model above.&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;## Generate "rho's" for different magnitude of crash by&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;simply using the sim() function defined above&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;temp1 &amp;lt;- replicate(n, sim(10))&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;temp2 &amp;lt;- replicate(n, sim(15))&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;temp3 &amp;lt;- replicate(n, sim(20))&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;temp4 &amp;lt;- replicate(n, sim(35))&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;## Sort the values of "rho", we do this to plot the CDF&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;as we will see shortly&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;temp1.1 &amp;lt;- sort(temp1)&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;temp2.1 &amp;lt;- sort(temp2)&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;temp3.1 &amp;lt;- sort(temp3)&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;temp4.1 &amp;lt;- sort(temp4)&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;y &amp;lt;- seq(from=0, to=1, length.out=n)&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;## This is how I&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;define the y-axis of my CDF which are basically the&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;probabilities.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;## Plotting all the CDF of rho for different magnitude in one plot.&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;plot(c(min(temp1.1), max(temp4.1)), c(0, 1), type='n', &amp;nbsp; &amp;nbsp; &amp;nbsp; xlab = "Rho", ylab= "Probability", main = "CDF of 'Rho' for &amp;nbsp; differnt magniturde of crashes")&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;lines(temp1.1, y, type = 'l', col = 'red')&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;lines(temp2.1, y, type = 'l', col = 'green')&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;lines(temp3.1, y, type = 'l', col = 'blue')&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;lines(temp4.1, y, type = 'l', col = 'black')&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp;b &amp;lt;- c("10 unit crash", "15 unit crash", "20 unit crash", "35 unit crash")&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp;legend("topleft", b , cex=0.5, col=c("red", "green", "blue", "black"), lwd=2, bty="n")&lt;/div&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-align: left; text-indent: -18pt;"&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-V79iN8lDxRU/TrRDi7OZH9I/AAAAAAAAAP4/EdwW2OLGntQ/s1600/cdf.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;img border="0" height="316" src="http://4.bp.blogspot.com/-V79iN8lDxRU/TrRDi7OZH9I/AAAAAAAAAP4/EdwW2OLGntQ/s400/cdf.png" width="400" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; margin-left: 39.3pt; margin-right: 0cm; margin-top: 0cm; text-indent: -18pt;"&gt;&lt;span style="font-size: 13.5pt;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="text-align: justify;"&gt;&lt;span style="line-height: 115%;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;An interesting observation that we make (or ratherPerron made) is that the c.d.f of our autoregressive coefficient “rho” tendsmore towards unity with increase in the magnitude in crash. What this means isthat as the magnitude of crash increases the possibility of your accepting the (false)null of unit root increases. Why I say the false null is because I know thetrue DGP is a trend stationary one. &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="text-align: justify;"&gt;&lt;span style="line-height: 115%;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="text-align: justify;"&gt;&lt;span style="line-height: 115%;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;This idea of Perron was criticised on the groundthat he was specifying the break point (T&lt;sub&gt;b&lt;/sub&gt;) exogenously, that isfrom outside the DGP. Frankly speaking I do not understand why was this takenas a criticism. I think fixing the break point exogenously was a good way offixing it with an economic intuition and not making is a purely statisticalexercise. Some researchers (I don’t understand why) termed this (simulation) illustrationas a “data mining” exercise, and improved it by selecting the break point (T&lt;sub&gt;b&lt;/sub&gt;)endogenously (by Zivot and Andrews as mentioned in the lecture notes). &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="text-align: justify;"&gt;&lt;span style="line-height: 115%;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="text-align: justify;"&gt;&lt;span style="line-height: 115%;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;I would hate to impose my opinion here but I feelthis was a very elegant and logical way of driving home the point that the nullof unit root should be accepted for your sample if and only if your model standsthe test of extreme rigour and not otherwise, and the rigour could be imposedexogenously with economic intuition too.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="text-align: justify;"&gt;&lt;span style="line-height: 115%;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="text-align: justify;"&gt;&lt;span style="line-height: 115%;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;P.S. Perron did a similar simulation for breakingtrend model, i.e where the slope of the model had a structural break. The codeswould be quite similar to the ones given above, in fact it would be a goodpractice if you could do the similar simulation for a breaking trend. In case you do want to try but face any issues please feel free to post/email your queries.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;Criticism and discussions welcome. &amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-2765547462295538329?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/2765547462295538329/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/11/unit-root-versus-breaking-trend-perrons.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/2765547462295538329'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/2765547462295538329'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/11/unit-root-versus-breaking-trend-perrons.html' title='Unit root versus breaking trend: Perron&apos;s criticism'/><author><name>Shreyes</name><uri>http://www.blogger.com/profile/02952702110986035135</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='http://4.bp.blogspot.com/-qxonS7T4lDQ/Tnu9rDO6QhI/AAAAAAAAAOM/H6gH-qheti4/s220/DSC09046.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-6UA9VT_qmSo/TrRDRQjwCGI/AAAAAAAAAPw/oJUYLp-Z8RA/s72-c/Simulated+series.png' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-4502049248347957192</id><published>2011-11-03T23:03:00.000+05:30</published><updated>2011-11-03T23:03:28.127+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Harry Markowitz'/><category scheme='http://www.blogger.com/atom/ns#' term='Financial economics'/><category scheme='http://www.blogger.com/atom/ns#' term='Modern Portfolio Theory'/><category scheme='http://www.blogger.com/atom/ns#' term='Simulation'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><title type='text'>Modern Portfolio Optimization Theory: The idea</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;We were recently given a lecture (by &lt;a href="http://www.igidr.ac.in/faculty/susant/"&gt;Dr. Susan Thomas&lt;/a&gt;) on Harry Markowitz portfolio optimization theory, and I was really fascinating with the noble&amp;nbsp;laureate's&amp;nbsp;story of how he found it difficult to convince his guide about the importance of his thesis work. Little did anyone know that his thesis would get him the most respected award in academia 35 years down the lane. Let me try and illustrate what was the basic idea behind the modern portfolio theory in simple English language. The principal task of any financial advisory firm/individual is to try and solve the capital allocation problem of an agent, meaning how much money should be invested in what asset/security to get the desired rate of return with minimum risk. Lets try and visualize this problem of capital allocation from the individuals point of view. Suppose I want a 15% p.a rate of return on my investment and there is a security in the market that gives you an expected 15% annual rate of return with some risk(sigma or standard deviation) associated with that security/asset. One way for me to achieve the desired expected return is to invest in this security take the risk(sigma) associated with this security and hope that I am awarded for the risk I took and get a 15% return in the end. However, the beauty of financial markets is that since there are so many assets/securities being traded in the market, it is possible for me to diversify my investment, in simple words, park fraction of my money with different assets in a way that gives me approximately ~ 15% return but with substantially less risk. This is referred to as "diversification" in Finance parlance. The mathematics behind the results can be easily shown, one can find the maths in the lecture slides &lt;a href="http://www.igidr.ac.in/faculty/susant/TEACHING/FIN/pfin12.pdf"&gt;here&lt;/a&gt;. Let me try and illustrate this result through empirical simulation using R.&lt;br /&gt;&lt;br /&gt;Suppose you are in a universe of only risky assets, i.e all assets that have a positive sigma. Then what Harry Markowitz did, with mathematics, was to compute the set of Expected returns that you could achieve with minimum risk (sigma). To illustrate this result I took stock returns for these 7 random companies for about 251 days, computed their respective expected returns, and the variance-co-variance matrix of the returns. With this I have all the necessary fodder to compute the E(r) and sigma for all the possible combination of weights that I can assign between these securities, meaning all possible different combinations of parking my money among these&amp;nbsp;securities.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;# Map your working directory using setwd()&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;# Read the relevant file&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;r1 &amp;lt;- read.csv("Markowitz.csv")&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;mu = colMeans(r1[,-7]) &amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;## Calculate the column means and storing in "mu", I have removed the 7th column from the data frame as I have a risk free asset's return there, I will come back to this later in the post.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;bigsig = cov(r1[,-7]) &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;## Variance-co-variance matrix&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;m = nrow(bigsig)-1&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;w = diff(c(0,sort(runif(m)), 1));&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt; ## Assigning random weights between 0-1 to "w" which will have the dimension = no. of securities between which you have to divide your investment.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;rb = sum(w*mu); &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #c27ba0;"&gt;## Creating matrix "rb" which stores the E(r)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;sb = sum(w*bigsig*w); &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;## Creating matrix "sb" which stores sigma's&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;N = 2000 &amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;## Number of different combinations of "w" you want to look at&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;## Simulating the different combinations of weights "w's" and storing the E(r) and sigma^2&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;for (j in 2:N) {&lt;/div&gt;&lt;div style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;w = diff(c(0,sort(runif(m)), 1));&lt;/div&gt;&lt;div style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;r = sum(w*mu); rb = rbind(rb,r);&amp;nbsp;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;s = sum(w*bigsig*w); sb = rbind(sb,s); &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #c27ba0;"&gt;## Note this is sigma^2 (variance)&lt;/span&gt;&lt;/div&gt;&lt;div style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/div&gt;&lt;div style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;d = data.frame(rb, sb); &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #c27ba0;"&gt;## Merge all the E(r) and sigmas in one data.frame.&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #c27ba0;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;d$sb = sqrt(d$sb); &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #c27ba0;"&gt;## Square root the variance to get the sigmas&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #c27ba0;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;plot(d$sb, d$rb, ylab="E(r)", xlab="Sigma", col="blue", &amp;nbsp;xlim = c(0.5,10), ylim = c(-0.1,0.5), main = "E(r)- sigma (With risk free asset), N = 200000")&lt;/div&gt;&lt;div style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-yOz4DnX1PzM/TrLOQhUA4BI/AAAAAAAAAPg/6NwC_YaYYtE/s1600/Risky.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="316" src="http://4.bp.blogspot.com/-yOz4DnX1PzM/TrLOQhUA4BI/AAAAAAAAAPg/6NwC_YaYYtE/s400/Risky.png" width="400" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;With only Risky assets&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;div style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;I did this simulation for 200000 different values of weights too, for those who are curious the plot can be obtained from &lt;a href="http://dl.dropbox.com/u/20480592/2lac_rm.pdf"&gt;here.&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;If we close the envelope of the above mentioned points with a smooth curve (from the left hand side) we get the efficient portfolio frontier (EFF). So what the graph suggests is that one would want to choose the weights (allocations) that lie, more specifically, on the above half of the curve. It makes intuitive sense too, why would one take on more risk to get a lesser return (which is implied by the lower half of the curve). Also another observation that we can make from the above plot is that for&amp;nbsp;&lt;/span&gt;every additional unit of returns we &lt;b&gt;will have to take on an&amp;nbsp;increasingly higher&amp;nbsp;risk&lt;/b&gt; (illustrated by the upper half of the plot).&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&amp;nbsp;This risk-return trade off graph changes its&amp;nbsp;&lt;/span&gt;characteristics when a risk free assets comes into the picture. Lets see what happens is we incorporate the risk free asset in our data frame. Note that a risk free assets could be thought of as a government security, whose risk (sigma) is 0, hence even its co-variances with the other risky assets would be 0.&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&amp;nbsp;The only addition to the above codes would be to replace&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;r1[,-7] &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;with just&lt;/span&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt; r1 &lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;that is include the risk free asset too.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-lF-iF20NlOo/TrLObncwJhI/AAAAAAAAAPo/Qk8lr5b08-A/s1600/Risk_free.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="315" src="http://2.bp.blogspot.com/-lF-iF20NlOo/TrLObncwJhI/AAAAAAAAAPo/Qk8lr5b08-A/s400/Risk_free.png" width="400" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;With risk-free asset&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;Simulation with N = 200000 &lt;a href="http://dl.dropbox.com/u/20480592/2lac_rf.pdf"&gt;here.&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;Look what happened to our risk-return trade off relationship, its linear!! So its a straight line instead of a smooth curve. Meaning for every additional unit of return you will &lt;b&gt;have to take a fixed additional amount of risk&lt;/b&gt;, which will be given by the slope of the above half of the graph. This result, which sounds to be a trivial finding, can be extended to the entire universe of assets and one can compute the optimal portfolio that will give you a desired rate of return with minimum sigma. However the implementation of this methodology in the actual world is the real challenge. Computation of E(r), sigma, getting the up to date data for all the&amp;nbsp;&lt;/span&gt;securities or assets in the universe are some of the&amp;nbsp;challenges&amp;nbsp;that one would face. But&amp;nbsp;nevertheless it is inspiring to see how Harry Markowitz stuck to his idea and triumphed in the end with this path breaking research that shaped the financial advisory industry as we see it now.&lt;br /&gt;&lt;br /&gt;P.S In case you want to replicate the exercise&amp;nbsp;the data can be downloaded from &lt;a href="http://dl.dropbox.com/u/20480592/Markowitz.csv"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-4502049248347957192?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/4502049248347957192/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/11/modern-portfolio-optimization-theory.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/4502049248347957192'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/4502049248347957192'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/11/modern-portfolio-optimization-theory.html' title='Modern Portfolio Optimization Theory: The idea'/><author><name>Shreyes</name><uri>http://www.blogger.com/profile/02952702110986035135</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='http://4.bp.blogspot.com/-qxonS7T4lDQ/Tnu9rDO6QhI/AAAAAAAAAOM/H6gH-qheti4/s220/DSC09046.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-yOz4DnX1PzM/TrLOQhUA4BI/AAAAAAAAAPg/6NwC_YaYYtE/s72-c/Risky.png' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-4466747282505839196</id><published>2011-10-30T19:27:00.001+05:30</published><updated>2011-11-02T16:12:08.838+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='decision tree'/><category scheme='http://www.blogger.com/atom/ns#' term='data'/><category scheme='http://www.blogger.com/atom/ns#' term='modelling'/><category scheme='http://www.blogger.com/atom/ns#' term='econometrics'/><category scheme='http://www.blogger.com/atom/ns#' term='credit score'/><category scheme='http://www.blogger.com/atom/ns#' term='german credit'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><title type='text'>Modelling with R: part 5</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;div style="text-align: justify;"&gt;In our exercise of learning modelling in R, we have till now succeeded in doing the following:&lt;/div&gt;&lt;br /&gt;&lt;ol style="text-align: left;"&gt;&lt;li style="text-align: justify;"&gt;&lt;a href="http://programming-r-pro-bro.blogspot.com/2011/09/modelling-with-r-part-1.html"&gt;Importing the data&lt;/a&gt;&lt;/li&gt;&lt;li style="text-align: justify;"&gt;&lt;a href="http://programming-r-pro-bro.blogspot.com/2011/10/modelling-with-r-part-2.html"&gt;Preparing and transforming the data&lt;/a&gt;&lt;/li&gt;&lt;li style="text-align: justify;"&gt;&lt;a href="http://programming-r-pro-bro.blogspot.com/2011/10/modelling-with-r-part-3.html"&gt;Running a logistic regression&lt;/a&gt;&lt;/li&gt;&lt;li style="text-align: justify;"&gt;&lt;a href="http://programming-r-pro-bro.blogspot.com/2011/10/modelling-with-r-part-4.html"&gt;Creating a decision tree&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div style="text-align: justify;"&gt;Specifically, we created a decision tree using the &lt;a href="http://cran.r-project.org/web/packages/rpart/index.html"&gt;rpart&lt;/a&gt; package. The decision tree was built with no priors and gave an AUC value of 0.73475 as opposed to 0.80493 given by the logistic regression. So here the logistic regression outperforms the recursive partitioning methodology of the rpart package. In this post we will try to see how we can improve over the current performance level of our decision tree. As a way to gauge our performance in the future, we can consider the AUC of the logistic regression as a minimum benchmark over which we need to improve. So let's get started.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: center;"&gt;&amp;nbsp;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;## 2.2.2: Decision tree with priors &amp;nbsp;##&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;t1.prior &amp;lt;- rpart(default ~&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; amt.fac +&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; age.fac +&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; duration +&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; chk_acct +&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; history +&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; purpose +&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; sav_acct +&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; employment +&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; install_rate +&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; pstatus +&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; other_debtor +&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; time_resid +&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; property +&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; other_install +&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; housing +&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; other_credits +&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; job +&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; num_depend +&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; telephone +&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; foreign&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; , data = dev, parms = list(prior = c(0.9, 0.1)))&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: inherit;"&gt;# Not the difference in the commands for a tree with priors and a tree without one. Here we need to specify the priors along with the formula in the rpart() function command.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;Before we go further it is important to understand what "priors" exactly are.&amp;nbsp;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;I initially did not understand the concept of priors until I read &lt;a href="http://www.salford-systems.com/doc/criticalfeaturesdecisiontrees.pdf"&gt;this&lt;/a&gt; document. You need not go through the entire document.&amp;nbsp;&lt;/span&gt;I have made a little summary for myself that might help.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;Simply put, priors are nothing but a weighing scheme. They specify the weight that we put on the overall level of the dependent variable. Technically, all trees are built with priors. In cases where priors are not specified, they are automatically taken to be proportional response rate, i.e, for each class they are proportional to the to the number of records in that class.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;Take for example our data set. The dependent variable has two categories - 0 and 1. There are 300 1s and 700 0s, so the response rate is about 30% (&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: inherit; font-size: x-small;"&gt;300/(700 +300)&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;). Then most decision trees will automatically apply priors of 30% and 70% to the respective classes. Now a simple decision tree tries to maximize the number of cases it classifies correctly and hence a lot of effort will concentrated on classifying the non-defaulters instead of the&amp;nbsp;&lt;/span&gt;defaulters&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;. This is because&amp;nbsp;&lt;/span&gt;even if we incorrectly classify all the bad loans we will be wrong 30% of the time as opposed to 70% of time if we incorrectly classify the non-defaulters. As mentioned in the &lt;a href="http://www.salford-systems.com/doc/criticalfeaturesdecisiontrees.pdf"&gt;document&lt;/a&gt;, it is important to note that such a model will be, &lt;i&gt;"literally accurate but practically worthless."&lt;/i&gt;&amp;nbsp;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;How can we then deal with such situations. There are three possible options here&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;/div&gt;&lt;ol style="text-align: left;"&gt;&lt;li style="text-align: justify;"&gt;Increase the proportion of defaulters by copying the observations so that there are 700 instances of each.&lt;/li&gt;&lt;ul&gt;&lt;li style="text-align: justify;"&gt;Duplicating&amp;nbsp;observations, however, is not a very &lt;i&gt;neat &lt;/i&gt;idea.&lt;/li&gt;&lt;li style="text-align: justify;"&gt;Additionally, it can be a problem if the data set is large. Adding observations will only make the other process computationally intensive and/or plain inefficient.&lt;/li&gt;&lt;/ul&gt;&lt;li style="text-align: justify;"&gt;Randomly pick 300 non-defaulters and create a data set which has 300 instances of each.&lt;/li&gt;&lt;ul&gt;&lt;li style="text-align: justify;"&gt;This involves throwing out a large chuck data that contains valuable information and hence the results may not be accurate&lt;/li&gt;&lt;/ul&gt;&lt;li style="text-align: justify;"&gt;Use a smarter decision tree algorithm that let's you specify priors.&lt;/li&gt;&lt;/ol&gt;&lt;div style="text-align: justify;"&gt;In case you haven't already guessed we have taken the third option. If we can vary the priors based on our understanding, we can analyze&amp;nbsp;data without any special handling, regardless of how skewed&amp;nbsp;the distribution of the dependent variable may be.&lt;/div&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Coming back to our code&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;plot(t1.prior)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# Plots the trees&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;text(t1.prior)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# Adds the labels to the trees.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;We don't need to prune this model and can score it right away&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;val$t1.p.yhat &amp;lt;- predict(t1.prior, val, type = "prob")&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;We can plot the ROC curve for the tree with priors.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;t1.p.scores &amp;lt;- prediction(val$t1.p.yhat[,2], val$default)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;t1.p.perf &amp;lt;- performance(t1.p.scores, "tpr", "fpr")&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# Plot the ROC curve&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;plot(t1.p.perf, col = "blue", lwd = 1)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# Add the diagonal line and the ROC curve of the logistic model, ROC curve of the tree without priors&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;plot(m1.perf, col = "red", lwd = 1, add = TRUE)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;plot(t1.perf, col = "green", lwd = 1.5, add = TRUE)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;abline(0, 1, lty = 8, col = "grey")&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;legend("bottomright", legend = c("tree w/o prior", "tree with prior", "logit"),&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;col = c("green", "blue", "red"), lwd = c(1.5, 1, 1))&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;KS statistic&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;ks1.p.tree &amp;lt;- max(attr(t1.p.perf, "y.values")[[1]] -(attr(t1.p.perf, "x.values")[[1]]))&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;ks1.p.tree&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;AUC&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;t1.p.auc &amp;lt;- performance(t1.p.scores, "auc")&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;t1.p.auc&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-4466747282505839196?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/4466747282505839196/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/10/modelling-with-r-part-5.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/4466747282505839196'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/4466747282505839196'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/10/modelling-with-r-part-5.html' title='Modelling with R: part 5'/><author><name>MK</name><uri>http://www.blogger.com/profile/03144859563025317904</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-6530600034586260844</id><published>2011-10-28T01:33:00.000+05:30</published><updated>2011-10-28T01:33:05.382+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Time series'/><category scheme='http://www.blogger.com/atom/ns#' term='auto correlation function'/><category scheme='http://www.blogger.com/atom/ns#' term='Financial markets'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><title type='text'>Predictability of stock returns : Using acf()</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;In my previous &lt;a href="http://programming-r-pro-bro.blogspot.com/2011/10/predictability-of-stock-returns-using.html"&gt;post&lt;/a&gt;, I employed a rather crude and non-parametric approach to see if I could predict the direction of stock returns using the function &lt;a href="http://rss.acs.unt.edu/Rdoc/library/lawstat/html/runs.test.html"&gt;runs.test()&lt;/a&gt;. Lets go a step further and try modelling this with a parametric econometric approach. The company that I choose for the study is INFOSYS (NSE code INFY). Lets start by eyeballing the plot of the stock prices of INFY for the past one year.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;## Set the working&amp;nbsp;directory using setwd() ##&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;# Reading the relevant file.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;infy &amp;lt;- read.csv("01-10-2010-TO-01-10-2011INFYEQN.csv")&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;# Plotting the past one year's closing price of INFY&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;plot(as.Date(infy$Date, "%d-%b-%y"), infy$Close.Price, xlab= "Dates", ylab= "Adjusted closing price", type='l', col='red', main="Adjusted closing price of INFOSYS for past 1 year")&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; font-family: Times, 'Times New Roman', serif; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-ML45WPZGZRI/TqmDFcEqqqI/AAAAAAAAAPA/Zhoc9P5_Ki0/s1600/INFY_price.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="316" src="http://3.bp.blogspot.com/-ML45WPZGZRI/TqmDFcEqqqI/AAAAAAAAAPA/Zhoc9P5_Ki0/s400/INFY_price.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;Eyeballing the above plot suggests that the series is &lt;i&gt;NOT&lt;/i&gt; second order stationary. Meaning that the first two moments, of the distribution from which the data is drawn, changes with time. For a&amp;nbsp;stationary series,&amp;nbsp;the mean doesn't changes with time and the&amp;nbsp;co-variance with any "k" lag is independent of "t" and it just a function of "k". But we see that both the conditions are&amp;nbsp;violated&amp;nbsp;above.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;Let me attempt to explain the idea&amp;nbsp;stationary in simple English language.&amp;nbsp;For a moment suppose that you were to stand at time T = t and look at the value of the series, then look at the&amp;nbsp;neighbors values to the left and right of "t", if by doing this exercise you can make out the value of "t" that you are standing at then it is&amp;nbsp;possibly&amp;nbsp;a non-stationary series. On the other hand if you were placed at time T = t in any stationary series, by doing the above exercise you would not be able to figure out the value of "t". (This definition came up during a discussion with &lt;a href="http://www.blogger.com/profile/00017318258700020690"&gt;Utkarsh&lt;/a&gt;&amp;nbsp;some time ago).&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;A rule of thumb in any time series modelling is that we work with only stationary time series. If the series exhibits any non-stationarity, we have to remove that before we can employ any empirical analysis. In the above series the non-stationarity can be removed by using the returns instead of actual stock prices. (analogous to First&amp;nbsp;differencing)&amp;nbsp;.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;## Calculating the returns of stock prices&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;infy_ret &amp;lt;- 100*diff(log(infy[,2])) &amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;## Plotting the returns&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;plot(as.Date(infy$Date[-1], "%d-%b-%y"), infy_ret, xlab= "Dates", ylab= "Returns percentage(%)", type='l', col='red', main="Daily returns of INFOSYS for past 1 year")&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; font-family: 'Courier New', Courier, monospace; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-sQ3sh_rnOLo/TqmSK4OqdoI/AAAAAAAAAPI/igOrAcdYEig/s1600/INFY_ret.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="316" src="http://3.bp.blogspot.com/-sQ3sh_rnOLo/TqmSK4OqdoI/AAAAAAAAAPI/igOrAcdYEig/s400/INFY_ret.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;We see that in the above plot the mean is fixed at 0 and the fluctuations are around that mean, that doesn't change with time. Now that we have taken care of the non-stationarity lets proceed on our task.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;First we will plot the auto-correlation of the returns with the previous lags and see if there is any significant correlation that the returns have with the previous values.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;## Plotting the ACF of INFY returns for the past one years&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;acf(infy_ret, main = "ACF of INFOSYS returns for past one year")&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-Xd_KVlBrVcc/TqmVCYYwJfI/AAAAAAAAAPQ/mHYDaRLnYCQ/s1600/INFY_acf.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="315" src="http://1.bp.blogspot.com/-Xd_KVlBrVcc/TqmVCYYwJfI/AAAAAAAAAPQ/mHYDaRLnYCQ/s400/INFY_acf.png" width="400" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;The blue dotted line is the 95% confidence interval. We can see that there is the 4th and the 7th lag significant in the ACF plot (there is one significant at 19th lag too but I choose to ignore that). Now lets see what I get if I regress the value of returns on the lagged values till lag 8th.&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;## Regressing the returns till the 7th lag&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;summary(lm(infy_ret[8:length(infy_ret)] ~ infy_ret[8:length(infy_ret) - 1] + infy_ret[8:length(infy_ret) - 2]+ infy_ret[8:length(infy_ret) - 3] + infy_ret[8:length(infy_ret) - 4] + infy_ret[8:length(infy_ret) - 5] + infy_ret[8:length(infy_ret) - 6] +infy_ret[8:length(infy_ret) - 7] ))&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;## This is a simple OLS regression of the "inty_ret" starting from the 8th observation. I have started from the 8th observation to ensure that the number of obs. are same in the dependents and independent variables.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;Output:&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Lucida Console'; font-size: 13px; line-height: 17px; white-space: pre-wrap;"&gt;&lt;span class="Apple-style-span" style="color: #6aa84f;"&gt;Coefficients:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; border-collapse: separate; font-family: Arial;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Lucida Console'; font-size: 13px; line-height: 17px; text-align: -webkit-left; white-space: pre-wrap;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre class="GD40030CLR" style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: 'Lucida Console'; font-size: 10pt !important; line-height: 1.3; outline-color: initial; outline-style: none; outline-width: initial; white-space: pre-wrap !important;" tabindex="0"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #6aa84f;"&gt;                                 Estimate Std. Error t value Pr(&amp;gt;|t|)   &lt;br /&gt;(Intercept)                      -0.09316    0.11321  -0.823  0.41140   &lt;br /&gt;infy_ret[8:length(infy_ret) - 1]  0.08158    0.06479   1.259  0.20920   &lt;br /&gt;infy_ret[8:length(infy_ret) - 2] -0.04017    0.06537  -0.614  0.53950   &lt;br /&gt;infy_ret[8:length(infy_ret) - 3] -0.10049    0.06528  -1.539  0.12504   &lt;br /&gt;infy_ret[8:length(infy_ret) - 4]  0.20153    0.06457   3.121  0.00203 **&lt;br /&gt;infy_ret[8:length(infy_ret) - 5] -0.08566    0.06568  -1.304  0.19344   &lt;br /&gt;infy_ret[8:length(infy_ret) - 6] -0.06849    0.06584  -1.040  0.29928   &lt;br /&gt;infy_ret[8:length(infy_ret) - 7] -0.12395    0.06621  -1.872  0.06241 . &lt;br /&gt;---&lt;br /&gt;Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 &lt;br /&gt;&lt;br /&gt;Multiple R-squared: 0.08717, Adjusted R-squared: 0.05998&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #93c47d;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;Only the coefficient of the 4th lag is statistically significant, and the&amp;nbsp;&lt;span class="Apple-style-span" style="line-height: 17px; white-space: pre-wrap;"&gt;Adjusted R-squared is a small 0.05998 (i.e ~ 6% of the explanation is provided by the above regression).&lt;/span&gt;&lt;span class="Apple-style-span" style="line-height: 17px; white-space: pre-wrap;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;span class="Apple-style-span" style="line-height: 17px; white-space: pre-wrap;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="line-height: 17px; white-space: pre-wrap;"&gt;In the previous &lt;/span&gt;&lt;a href="http://programming-r-pro-bro.blogspot.com/2011/10/predictability-of-stock-returns-using.html" style="line-height: 17px; white-space: pre-wrap;"&gt;post&lt;/a&gt;&lt;span class="Apple-style-span" style="line-height: 17px; white-space: pre-wrap;"&gt; we had reached the conclusion that the returns series is completely random (using runs.test()). But here we have fit in a model that provides ~ 6% of the explanation, the important question that needs to be addressed now is that the can we use this model to predict the stock returns (and make some money using a trading strategy that employs the above regression).&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="line-height: 17px; white-space: pre-wrap;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="line-height: 17px; white-space: pre-wrap;"&gt;The model suggests that there is a statistically significant explanation that is being offered by the 4th lag in the above regression, but is this explanation economically significant? Now is when the economic intuition comes into play. The given sample data for the stock prices of INFY for the paste one year has confessed that the 4 days ago stock price provides a statistically significant explanation of today's stock prices. But a major point, perhaps the most important,  that we are missing in the above model is the&lt;/span&gt;&lt;i style="line-height: 17px; white-space: pre-wrap;"&gt; transaction costs &lt;/i&gt;&lt;span class="Apple-style-span" style="line-height: 17px; white-space: pre-wrap;"&gt;or &lt;/span&gt;&lt;a href="http://www.blogger.com/goog_1644715287" style="line-height: 17px; white-space: pre-wrap;"&gt;&lt;i&gt;market&lt;/i&gt; &lt;/a&gt;&lt;i style="line-height: 17px; white-space: pre-wrap;"&gt;&lt;a href="http://en.wikipedia.org/wiki/Market_microstructure"&gt;micro-structures&lt;/a&gt;.&amp;nbsp;&lt;/i&gt;&lt;br /&gt;&lt;i style="line-height: 17px; white-space: pre-wrap;"&gt;&lt;br /&gt;&lt;/i&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="line-height: 17px; white-space: pre-wrap;"&gt;Meaning that a statistically significant 4th lag does not mean that the explanation offered is economically significant too. To check if the relation is economically significant, we will have to adjust the prices for transaction costs and then do the regression and see if we get a similar result. Efficient market hypothesis that this statistical significant will disappear once you account for these transaction costs (impact cost or cost of trading). It seems to be intuitive too, because if we look at the ACF plotted above the auto-correlations are not significantly different from 0 and once we account for the transaction costs the 95% band will also broaden.&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;So the lesson is that a simple regression of current returns on the lagged returns (auto regressive model in time series parlance) might not be a reliable trading strategy :-)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;P.S. In case anyone wishes to replicate the exercise the data can be obtained from &lt;a href="http://dl.dropbox.com/u/20480592/01-10-2010-TO-01-10-2011INFYEQN.csv"&gt;here&lt;/a&gt;.&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-6530600034586260844?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/6530600034586260844/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/10/predictability-of-stock-returns-using_28.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/6530600034586260844'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/6530600034586260844'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/10/predictability-of-stock-returns-using_28.html' title='Predictability of stock returns : Using acf()'/><author><name>Shreyes</name><uri>http://www.blogger.com/profile/02952702110986035135</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='http://4.bp.blogspot.com/-qxonS7T4lDQ/Tnu9rDO6QhI/AAAAAAAAAOM/H6gH-qheti4/s220/DSC09046.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-ML45WPZGZRI/TqmDFcEqqqI/AAAAAAAAAPA/Zhoc9P5_Ki0/s72-c/INFY_price.png' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-7819443229257939951</id><published>2011-10-23T02:14:00.000+05:30</published><updated>2011-10-28T03:47:58.084+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='macroeconomics'/><category scheme='http://www.blogger.com/atom/ns#' term='Financial economics'/><category scheme='http://www.blogger.com/atom/ns#' term='Principal component analysis'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><title type='text'>Principal component analysis : Use extended to Financial economics : Part 2</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;My previous &lt;a href="http://programming-r-pro-bro.blogspot.com/2011/10/principal-component-analysis-use.html"&gt;post&lt;/a&gt; talked about how we can employ PCA on the data for multiple stock returns to reduce the number of variables in explaining the variance of the underlying data. But the idea was greeted with skepticism by many. A caveat to the application of PCA was that the meaning or the intuition behind the variable is lost in the computation of components, but since I was concerned with only explaining the variance in the underlying data it worked as a powerful method. This method was also adopted by Dr. Oyama in his &lt;a href="http://papers.ssrn.com/sol3/papers.cfm?abstract_id=882653"&gt;research paper&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;I came across this interesting&amp;nbsp;&lt;a href="http://onlinelibrary.wiley.com/doi/10.1002/ijfe.173/abstract"&gt;research paper&lt;/a&gt; published in one of the most coveted journals, (&lt;i&gt;International Journal on Finance and Economics&lt;/i&gt;)&lt;i&gt;&amp;nbsp;&lt;/i&gt;which has used the PCA on the macroeconomic variables. So relating it to my previous post and the question of investigating the factor affecting stock returns, what they do is that they take individual stocks and regresses it on the PCA of macroeconomic variables. At the first glance this would appear an inappropriate use of the tool, however the question that it intends to ask is also very different. There has been or infact still is this great battle between the Capital Asset Pricing (CAPM) school of thought and Arbitrage Pricing Theory (APT) school of thought. What the essential difference between these two schools is that the CAPM says that the returns to a stock are sensitive to only one factor, i.e the market rate of returns (single factor model) which captures the effect of all the various factors, however the APT guys scream that it depends on a number of factors and market rate of return would be just one of them. So in this battle to prove their point the APT folks take stock returns as a dependent variable and as independent variables they use the PCA of many macroeconomic variables. They do this essentially so that they can prove that there are more than one factors playing a significant part in the explaining the returns on a stock, they do not care whether the economic intuition is lost in the process on one-upmanship.&lt;br /&gt;&lt;br /&gt;Well anything is fair in war, but I think this path taken by APT guys of proving their point is a fair point. The variables that are thrown into the PCA are chosen with an economic intuition in mind, so its not correct to say this entire methodology is flawed on the pretext that the PCA of these macroeconomic variables have no economics intuition. If one really digs into the intuition behind PCA as explained in this &lt;a href="http://www.cs.gmu.edu/~ashehu/sites/default/files/cs444_Spring2011/PCAHowInRTo.pdf"&gt;article&lt;/a&gt;&amp;nbsp;one can visualize what the PCA of macroeconomics variables would be representing, the principal underlying components &lt;i&gt;that give rise to such a macroeconomic series (signals). &lt;/i&gt;This is an unconventional way of doing an econometric study, as in this is more of a qualitative than a quantitative study. I cannot make sense of a statement that 1 unit increase in my first PCA results in "x" unit increase in returns, this is an absurd statement, but nevertheless the methodology of answering the underlying question is not absurd.&lt;br /&gt;&lt;br /&gt;The methodology for calculating the PCA for macroeconomics is same as what was used in the &lt;a href="http://programming-r-pro-bro.blogspot.com/2011/10/principal-component-analysis-use.html"&gt;previous post&lt;/a&gt;. We can have a number of macroeconomic variables, however, I have done this demonstration with only 2 variables change in Mumbai inter-bank offer rates (MIBOR) rates and change in INR/USD exchange rates.&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;#### Calculating the PCA of macroeconomic variables ####&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;# Reading the relevant files&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;mibor &amp;lt;- read.csv("MIBOR.csv", na.strings="#N/A")&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;exchange &amp;lt;- read.csv("Exchange_rates.csv", na.strings="#N/A")&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;# Making sure that there are no missing values in the data, the missing values are replaced by linear interpolation&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;mibor[, 2] &amp;lt;- approx(as.Date(mibor$Dates, '%d-%b-%y'), mibor[ ,2], as.Date(mibor$Dates, '%d-%b-%y'))$y &amp;nbsp; &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# approx() returns the interpolated values in column 'y'&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;exchange[, 2] &amp;lt;- approx(as.Date(exchange$Year,'%d-%b-%y'), exchange[ ,2], as.Date(exchange$Year, '%d-%b-%y'))$y &amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# Similarly for exchange rates&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;# Now we will have to compute the change in MIBOR and exchange rates:&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;for(k in 2:nrow(mibor))&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; mibor$Change1[k] &amp;lt;- ((mibor$MIBOR[k] - mibor$MIBOR[k-1])/mibor$MIBOR[k-1])*100&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;for(j in 2:nrow(exchange))&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;exchange$Change[j] &amp;lt;- ((exchange$Exchange.rates[j] - exchange$Exchange.rates[j-1])/exchange$Exchange.rates[j-1])*100&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;# Creating matrix of the data&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;macro &amp;lt;- as.data.frame(rep(0, 2498)) &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;macro$ex &amp;lt;- exchange$Change&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;macro$rate &amp;lt;- mibor$Change1&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;macro.mat &amp;lt;- as.matrix(macro, nrow = 2498 , ncol = 2)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;# Calculating the principal components&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;prin.macro &amp;lt;- princomp(macro.mat)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;barplot(prin.macro$sdev[1:2]/prin.macro$sdev[1])&lt;/span&gt; &lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&amp;nbsp;# bar plot of the ratio is the standard deviation of the PC's with the standard deviation of the 1st PC, this can help us decide which PC to be used by simple eyeballing the plot.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;# Extract the 1st PC in a variable&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;load1 &amp;lt;- prin.macro$loadings[,1] &lt;/span&gt;&amp;nbsp; &lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# get the loadings of the 1st PC into 'load1'&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;pr.ma &amp;lt;- macro.mat %*% load1&lt;/span&gt; &lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# matrix multiply the loading with the original data to get the PC&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;pr1 &amp;lt;- as.numeric(pr.ma)&lt;/span&gt; &lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# And 'pr1' has your first principal component.&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Now if you have read &lt;a href="https://parmesan.mc.vanderbilt.edu/turnersd/ggd/2010-11-18-PCA-tutorial.pdf"&gt;this article&lt;/a&gt;&amp;nbsp;that I shared earlier, the first component gives me the principal underlying factor that gives rise to the series (signals) that I observe.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Caveats and cautions:&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div&gt;Before I am bombarded with question about the relevance of this methodology in this particular problem let me confess that in the above particular exercise its &lt;i&gt;not the best &lt;/i&gt;idea to use PCA. One should use PCA only if one has many macroeconomic variables like M1, M3, CPI, IIP, WPI, etc and there is high correlation between them. I have done the above only for illustrative purposes (since I am not employing PCA on macro-variables in my project).&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Assuming I had other macro-variables in the calculation of my PCA above, I could use them as an independent variable and regress them on the stock specific returns to understand &amp;nbsp;if there are more than one factors that come out to be significant in this regression. What this would essentially mean is that are there more than one factors explaining the stock returns. For the finance students, this is the classic argument between the Capital Asset Pricing (CAPM) guys and the Arbitrage Pricing Theory (APT) guys.&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;If in the regression of the PC's of macroeconomic variables on the returns we find that more than one factors are significant then we would have found evidence for or against one of the above theories.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;Limitations in the above method:&lt;/b&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;ol style="text-align: left;"&gt;&lt;li&gt;In the above exercise I have just taken 2 variables merely for illustration, I would have to include more variables if I really intent to find evidences against the CAPM.&lt;/li&gt;&lt;li&gt;Again the caveat of interpretation applies. Its difficult to interpret the PCA on macrovariables&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;b&gt;My take:&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Despite of the above caveats I think PCA does a good job of giving us a good empirical way to answer the question of whether CAPM or APT is a superior asset pricing theory. The main idea behind these asset pricing theories is to see what factors need to be incorporated to compute the asset specific risk premiums, so on that front, its not difficult to see what people still stick to CAPM, as its a lot simple and easier to work with. APT on the other hand makes a bold assertion of multiple factors governing the returns but it has always found it difficult to list out these factors. Well in most cases you would be able to conclude that the returns can be&amp;nbsp;modeled&amp;nbsp;best using a multi-factor model, but whats interesting to ask is how much additional&amp;nbsp;explanation&amp;nbsp;is offered by factors other than the 1st one. If my multi factor model offers just a marginally higher adjusted-R-squared,&amp;nbsp;is it reasonable for you to complicate your model is the question?&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;If it were only to prove this empirically why would you not choose the multi-factor model? The idea of modelling returns is to identify the factors that you need to take into account while computing the risk premiums, and if your factors do not have any economic intuition how can you assign risk premia looking at your principal component, that's the big question. But I would hate to impose my take here,&amp;nbsp;further&amp;nbsp;discussion is welcome.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;P.S : In case you wish to replicate the above exercise you can find the data here, &lt;a href="http://dl.dropbox.com/u/20480592/Exchange_rates.csv"&gt;exchange rates&lt;/a&gt;, &lt;a href="http://dl.dropbox.com/u/20480592/MIBOR.csv"&gt;MIBOR&lt;/a&gt;.&amp;nbsp;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-7819443229257939951?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/7819443229257939951/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/10/principal-component-analysis-use_23.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/7819443229257939951'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/7819443229257939951'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/10/principal-component-analysis-use_23.html' title='Principal component analysis : Use extended to Financial economics : Part 2'/><author><name>Shreyes</name><uri>http://www.blogger.com/profile/02952702110986035135</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='http://4.bp.blogspot.com/-qxonS7T4lDQ/Tnu9rDO6QhI/AAAAAAAAAOM/H6gH-qheti4/s220/DSC09046.JPG'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-3072241298241447683</id><published>2011-10-21T19:21:00.001+05:30</published><updated>2011-10-28T03:51:38.620+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='stock returns'/><category scheme='http://www.blogger.com/atom/ns#' term='Runs test'/><category scheme='http://www.blogger.com/atom/ns#' term='Financial markets'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><title type='text'>Predictability of stock returns : Using runs.test()</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;Financial market is interesting place, you find people taking positions (buying/selling) based on their expectations of what the security prices would be and are rewarded/penalized according to the accuracy of their expectations. The beauty of financial markets is that it provides a platform for &lt;i&gt;everyone&lt;/i&gt; to come in with their respective expectations and allows them to interact and exchange securities. I emphasize on &lt;i&gt;everyone &lt;/i&gt;because this everyone includes a auto-rickshaw driver, a clerk and also&amp;nbsp;sophisticated&amp;nbsp;econometricians and analysts. An obvious point then is that if your expectations are consistently correct, i.e you can predict the price movements before it happens on the exchange, you are a rich man. Assuming for all practical purposes that there is no &lt;i&gt;oracle&lt;/i&gt;&amp;nbsp;in our universe, who can do these predictions with 100% accuracy, the job of this prediction rests upon an econometrician/statistician. Lets see if they can do a good job too.&lt;br /&gt;&lt;br /&gt;I took the stock returns data for INFOSYS (INFY on NSE) for the past one year and tried to see if I could make this data confess its underlying linear/non-linear generating process. I started by employing a rather simple, straight forward and easy to interpret &lt;b&gt;&lt;a href="http://rss.acs.unt.edu/Rdoc/library/lawstat/html/runs.test.html"&gt;Runs test&lt;/a&gt;. &lt;/b&gt;Its a non-parametric statistical test&amp;nbsp;that will test the null hypothesis of whether the underlying series is identical and independent distributed. For those who are not too familiar with statistical parlance, non-parametric in simple term means that we have to make no assumptions about what &amp;nbsp;the underlying data should be like. There is a huge surge in the applications of non-parametric statistics to explain various processes, this is because the biggest&amp;nbsp;deterrence&amp;nbsp;to conducting these kinds of tests, i.e the computational issues, are no longer a problem in this generation of rapid computation. The idea of&amp;nbsp;empirical analysis is about trying to theorize a null hypothesis and then try your best to bring it down using empirical evidence.&amp;nbsp;(analogous to &lt;a href="http://en.wikipedia.org/wiki/Falsifiability"&gt;Karl Popper's idea of falsification&lt;/a&gt; of a theory, you hang on to a theory so long as it has not betrayed you yet)&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;## Doing runs test on INFY daily returns&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&amp;gt; infy &amp;lt;- read.csv("01-10-2010-TO-01-10-2011INFYEQN.csv") &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&amp;nbsp;## Reading the stock price data&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&amp;gt;&amp;nbsp;infy_ret &amp;lt;- 100*diff(log(infy[,2])) &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;## Since the second column in the data has the stock prices I have used&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;[&lt;/span&gt;log(P&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;t&lt;/span&gt;) - log(P&lt;span class="Apple-style-span" style="font-size: x-small;"&gt;t-1)&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: large;"&gt;]&lt;/span&gt;*100 as the returns.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&amp;gt; runs.test(factor(infy_ret &amp;gt; 0)) &amp;nbsp; &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;## what this has done is that it has created a category variable that takes&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;value 1 of infy_ret &amp;gt; 0 and 0 otherwise.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;What this does is that tells me whether the runs of the returns are predictable, i.e say if I represent possitive return by + and negative return by - then my series of returns would probably look like +,+,-, +, -, -, -, +, ...&lt;br /&gt;now that this test check is can I predict whether the next day will have &amp;nbsp;+ or &amp;nbsp;-&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Lucida Console'; font-size: 13px; line-height: 17px; white-space: pre-wrap;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Lucida Console'; font-size: 13px; line-height: 17px; white-space: pre-wrap;"&gt;Output:&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Lucida Console'; font-size: 13px; line-height: 17px; white-space: pre-wrap;"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6;"&gt;Runs Test&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-family: 'Lucida Console'; font-size: 13px; line-height: 17px; white-space: pre-wrap;"&gt;data:  factor(infy_ret &amp;gt; 0) &lt;/span&gt;&lt;br /&gt;&lt;pre class="GD40030CLR" style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-family: 'Lucida Console'; font-size: 10pt !important; line-height: 1.3; outline-color: initial; outline-style: none; outline-width: initial; white-space: pre-wrap !important;" tabindex="0"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6;"&gt;Standard Normal = 0.1308, p-value = 0.8959  &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;## High p-value means you cannot trash &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;your null hypothesis.&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;span class="Apple-style-span" style="color: #3d85c6; font-family: 'Lucida Console'; font-size: 13px; line-height: 17px; white-space: pre-wrap;"&gt;           &lt;/span&gt;&lt;br /&gt;For those not familiar with statistics, the p-value is nothing but the probability of you reject a null hypothesis when it is actually true. So in simple words it gives me the probability that I might end up rejecting a correct null hypothesis. (be very careful with the &lt;a href="http://en.wikipedia.org/wiki/P-value#Misunderstandings"&gt;interpretation of p-value&lt;/a&gt;, many times people end up misunderstanding it,&amp;nbsp;&amp;nbsp;many a times&amp;nbsp;even I have fallen prey to this). Therefore you cannot reject your null hypothesis under such a high probability of&amp;nbsp;committing this error or wrongly rejecting a correct hypothesis&amp;nbsp;, you just don't have enough evidence. Therefore your series is a random walk (you can understand this in the literal&amp;nbsp;English&amp;nbsp;language sense, but the definition is not so trivial in time series parlance).&lt;br /&gt;&lt;br /&gt;P.S In case you want to replicate this&amp;nbsp;exercise&amp;nbsp;the data can be obtained from &lt;a href="http://nse-india.com/live_market/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=INFY"&gt;here.&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-3072241298241447683?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/3072241298241447683/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/10/predictability-of-stock-returns-using.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/3072241298241447683'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/3072241298241447683'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/10/predictability-of-stock-returns-using.html' title='Predictability of stock returns : Using runs.test()'/><author><name>Shreyes</name><uri>http://www.blogger.com/profile/02952702110986035135</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='http://4.bp.blogspot.com/-qxonS7T4lDQ/Tnu9rDO6QhI/AAAAAAAAAOM/H6gH-qheti4/s220/DSC09046.JPG'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-7199157357261865072</id><published>2011-10-16T01:45:00.000+05:30</published><updated>2011-11-25T00:42:14.719+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Financial economics'/><category scheme='http://www.blogger.com/atom/ns#' term='Principal component analysis'/><category scheme='http://www.blogger.com/atom/ns#' term='highly correlated variables in data set'/><category scheme='http://www.blogger.com/atom/ns#' term='factor analysis'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><title type='text'>Principal component analysis : Use extended to Financial economics : Part 1</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;While working for my Financial economics project I came across this elegant tool called Principal component analysis (PCA)which is an extremely powerful tool when it comes to reducing the dimentionality of a data set comprising of highly correlated variables. This tool finds majority application in &lt;a href="http://gettinggeneticsdone.blogspot.com/2010/05/tutorial-principle-components-analysis.html"&gt;genetic research&lt;/a&gt;, which deals with data sets having many variables that are highly correlated.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;I will try and be as explicit and refrain from using statistical/mathematical jargons to explain what/how about this tool . To state a few stylized facts PCA is used mainly for:&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;/div&gt;&lt;ul style="text-align: left;"&gt;&lt;li&gt;&amp;nbsp;compressing the data&lt;/li&gt;&lt;li&gt;&amp;nbsp;filter some of the noise in the data&lt;/li&gt;&lt;/ul&gt;&lt;div style="text-align: left;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;b&gt;Problem at hand:&lt;/b&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;I was trying to investigate the factors that affect the returns of stocks in the Indian equity market, however I wanted to take into account all the &lt;a href="http://www.nseindia.com/content/indices/ind_cnx500.htm"&gt;S&amp;amp;P CNX 500&lt;/a&gt; companies. What would be really nice if I could somehow find a way of squeezing the 500 companies into say not more than 2-3 variables that can be representative of the entire set of 500 companies. This is precisely where PCA comes into play and does a fantastic job. What it gives me is just 1 variable that I can use instead of all the 500 companies!!!&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;Hats off and a bow of respect for the contributors/donors of packages to the &lt;a href="http://www.r-project.org/"&gt;CRAN servers&lt;/a&gt; that the above simplification can be achieved using just one line of script in R. Sounds easy, but what one really needs to do is to understand what PCA does and how the output from this script can be interpreted. Again, at the risk of over simplication (however trying hard to maintain my commandment of simplicity), I would illustrate in a crude manner the working of PCA.&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;b&gt;What PCA does:&lt;/b&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;Let me explain this relating to the above example, if I do a PCA on the returns data for the 500 companies, I would obtain 500 principal components. These components are nothing but the linear combination of the existing 500 variables(companies) arranged in the decreasing order of their variance. So 1st principal component (PC)has the maximum variance and 500th principal component (PC)has the least variance. The variance in the PCA represent nothing but the variance in the data. So 1st PC explains the maximum amount of variance in my data. One magical feature of PCA is that all these 500 components will be orthogonal to each other, meaning these components will be uncorrelated with each other. So essentially if we look at PCA as a black box it takes inputs as data set of highly correlated variables and gives as output PC's that explain the variance in the input data and they are uncorrelated with each other.(I don't leverage this feature in this particular problem, I would illustrate this use in other part of this blog)&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;b&gt;How PCA does it:&lt;/b&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;Since I have taken a vow of simplicity, I dont have much to say here.:-) However for the mathematically inclined and&amp;nbsp;certainty freaks like &lt;a href="http://www.blogger.com/profile/03144859563025317904"&gt;Madhav&lt;/a&gt;, &lt;a href="http://www.cs.princeton.edu/picasso/mats/PCA-Tutorial-Intuition_jp.pdf"&gt;this paper&lt;/a&gt; does a brilliant job of illustrating the matrix algebra that goes behind PCA computations. There are essentially 2 methods of calculating PCA, one is the&lt;a href="http://en.wikipedia.org/wiki/Eigendecomposition_of_a_matrix"&gt; eigenvalue decomposition&lt;/a&gt;&amp;nbsp;(done using &lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;princomp()&lt;/span&gt;&lt;/span&gt; command in R)and the other is &lt;a href="http://en.wikipedia.org/wiki/Singular_value_decomposition"&gt;singular value decomposition&lt;/a&gt;&amp;nbsp;(done using &lt;span class="Apple-style-span" style="color: blue;"&gt;prcomp()&lt;/span&gt; command using R).&amp;nbsp;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;b&gt;How this can be done in R:&lt;/b&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;####### Calculating Principal component of returns of S&amp;amp;P CNX 500 companies ########&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;## Access the relevant file ##&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;returns &amp;lt;- read.csv("Returns_CNX_500.csv")&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;One caveat that you need to keep in mind in that there should be no "NA" values in your data set. A presence of an NA would impede the computation of the var-covar matrix and hence their eigen vectors(i.e the factor loadings)&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;## Dealing with missing values in the returns data for companies&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;for(i in 2:ncol(returns))&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; returns1[, i] &amp;lt;- approx(returns$Year, returns1[ ,i], returns$Year)$y&lt;/span&gt; &amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;## approx function basically fits the&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;value of linear approximate&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;between the missing data points and the column $y stores the approximated values.&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: left;"&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;## Convert the data into matrix ##&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;ret &amp;lt;- as.matrix(returns1, nrow = dim(returns1)[1], ncol = dim(returns1)[2])&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;##Computing the principal component using eigenvalue decomposition ##&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;princ.return &amp;lt;- princomp(ret) &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #c27ba0;"&gt;## This is it.!!&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;## Identifying what components to be used ##&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;barplot(height=princ.return$sdev[1:10]/princ.return$sdev[1]) &amp;nbsp; &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #c27ba0;"&gt;## I am plotting the standard deviation of the PC's divided by standard deviation of PC 1, this can help us decide on a benchmark that we can use to select the relevant components.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #c27ba0;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-ojBf3b6Llfk/TpndvNKVp6I/AAAAAAAAAOs/EzeaQYFaTsk/s1600/PC_blog1.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" height="315" src="http://2.bp.blogspot.com/-ojBf3b6Llfk/TpndvNKVp6I/AAAAAAAAAOs/EzeaQYFaTsk/s400/PC_blog1.png" width="400" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Standard deviation of the first 10 components compared to 1st PC&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #c27ba0;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;We can clearly see from the above figure that as expected the first PC does the majority of the variance&amp;nbsp;explanation in the returns data for the 500 companies. So if we want to identify factors that influence the returns of S&amp;amp;P CNX 500 companies I can use the 1st PC as a variable in my regression. So far we have calculated the principal components, now we will extract out 1st PC as a numeric variable from the matrix.(&lt;span class="Apple-style-span" style="color: blue;"&gt;princ.return&lt;/span&gt;)&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #c27ba0;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #c27ba0; font-family: 'Courier New', Courier, monospace;"&gt;## To get the first principal component in a variable ##&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;load &amp;lt;- loadings(princ.return)[,1] &amp;nbsp; &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #c27ba0;"&gt;## loadings() gives the linear combination by which our input variables will be linearly weighted to compute the components, and this command gives us the loading for 1st PC.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;pr.cp &amp;lt;- ret %*% load &amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #c27ba0;"&gt;## Matrix multiplication of the input data with the loading for the 1st PC gives us the 1st PC in matrix form.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;pr &amp;lt;- as.numeric(pr.cp) &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #c27ba0;"&gt;## Gives us the 1st PC in numeric form in pr.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: #c27ba0;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;One question that might be raised is why not just use the S&amp;amp;P CNX 500 index returns as an input to the regression? The simple answer to that question would be that PC 1 gives you a relatively clear signal of the returns as opposed to the index which would have a lot of noise. This question would have made sense in the 1900's when the technology was not so efficient in terms of computation. Since now computational time and effort finds minimum weight in any researchers mind there is no reason to settle for anything but the best.&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;There is an important caveat that must be kept in mind while doing analysis using PCA, though PCA has a clear mathematical intuition it lacks an economic intuition. That is, one unit change in PC 1 of returns has a mathematical meaning but no economic meaning, you cannot make sense of this statement that PC 1 of returns for the 500 companies has gone up by "x" amount. Therefore the use of this analysis should be limited to factor analysis and not to be extended to predictive analysis.&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;In case you wish to replicate the above exercise the data can be obtained from &lt;a href="http://dl.dropbox.com/u/20480592/Returns_CNX_500.csv"&gt;here.&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-7199157357261865072?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/7199157357261865072/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/10/principal-component-analysis-use.html#comment-form' title='16 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/7199157357261865072'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/7199157357261865072'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/10/principal-component-analysis-use.html' title='Principal component analysis : Use extended to Financial economics : Part 1'/><author><name>Shreyes</name><uri>http://www.blogger.com/profile/02952702110986035135</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='http://4.bp.blogspot.com/-qxonS7T4lDQ/Tnu9rDO6QhI/AAAAAAAAAOM/H6gH-qheti4/s220/DSC09046.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-ojBf3b6Llfk/TpndvNKVp6I/AAAAAAAAAOs/EzeaQYFaTsk/s72-c/PC_blog1.png' height='72' width='72'/><thr:total>16</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-5631674200961103268</id><published>2011-10-13T15:38:00.002+05:30</published><updated>2011-10-26T10:26:49.651+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='decision tree'/><category scheme='http://www.blogger.com/atom/ns#' term='data'/><category scheme='http://www.blogger.com/atom/ns#' term='econometrics'/><category scheme='http://www.blogger.com/atom/ns#' term='credit score'/><category scheme='http://www.blogger.com/atom/ns#' term='german credit'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><title type='text'>Modelling with R: part 4</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;In &lt;a href="http://programming-r-pro-bro.blogspot.com/2011/10/modelling-with-r-part-3.html"&gt;part 3&lt;/a&gt;, we ran a logistic model to determine the probability of default of a customer. We also saw the outputs and tried to judge the performance of the model b plotting the ROC curve. Let's try a different approach today. How about a decision tree? For starters, we being a simple decision tree with no priors.&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div style="text-align: center;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: center;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;##########-------------2.2: Recursive partitioning------------############### &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: center;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: center;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;## 2.2.1: Decision tree with no priors &amp;nbsp;##&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;What is recursive? How is partitioning done? How does partitioning translate to predictions? What are priors?&amp;nbsp;Let's try and decode these big words that we have written above.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Partitioning means splitting the data into subsets based on attributes. Partitioning typically happens in a top-down approach by choosing a variable, or an attribute, that is the "best" attribute to split the data. This "best" variable would be one that splits the data into homogeneous subsets with each subset having the same category of the target variable, i.e, either all zeros or all ones. It is in the way of choosing this "best" variable that different tree construction algorithms differ. For example, CART, uses Gini coefficient to choose the best variable. I read that ID3 and C4.5 use the concept of entropy, but I really don't know how these function.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;After a subset is formed, the splitting process continues in a "recursive" manner and the recursion halts when the subset at a node has the same value for the target variable.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;To run a decision tree we need to call the "rpart" library.&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;library(rpart)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;We use a recursive partioning tree model to predict the likelihood of default.&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;t1.noprior &amp;lt;- rpart(default ~&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; amt.fac +&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; age.fac +&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; duration +&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; chk_acct +&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; history +&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; purpose +&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; sav_acct +&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; employment +&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; install_rate +&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; pstatus +&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; other_debtor +&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; time_resid +&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; property +&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; other_install +&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; housing +&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; other_credits +&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; job +&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; num_depend +&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; telephone +&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; foreign&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; , data = dev)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;plot(t1.noprior)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;# Plots the trees&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;text(t1.noprior)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;# Adds the labels to the trees.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Before we score the validation data, we need to prune the tree to ensure that we haven't&amp;nbsp;over fitted the data.&lt;/div&gt;&lt;div&gt;In order to prune the data we need to find the appropriate complexity parameter.&amp;nbsp;The ideal value of the complexity parameter in this case would then be the one that minimizes&amp;nbsp;the X-error.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;We can find this value as&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;printcp(t1.noprior)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;# Here we see that the value is 0.013514.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;We can write a small script to find this value&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;t1.noprior.cp &amp;lt;- t1.noprior$cptable[which.min(t1.noprior$cptable[, "xerror"]), "CP"]&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;t1.noprior.prune &amp;lt;- prune(t1.noprior,t1.noprior.cp)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;We need to score the pruned tree model the same way we did for the Logistic model.&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;val$t1.yhat &amp;lt;- predict(t1.noprior.prune, val, type = "prob")&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Now notice the difference between the two predictions- one of the Logistic model and&amp;nbsp;the other of the tree model.&amp;nbsp;The logistic model predictions have only column, which gives the probability that the&amp;nbsp;person will default, or mathematically, the probability of y being equal to 1.&amp;nbsp;On the other hand the tree model predictions give two columns, first for y being&amp;nbsp;equal to zero and second column for y being equal to 1.&amp;nbsp;Hence, for plotting the ROC curve we need to take only the probability of y being equal to 1.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;We will also plot the ROC curve for this tree.&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;t1.scores &amp;lt;- prediction(val$t1.yhat[,2], val$default)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;t1.perf &amp;lt;- performance(t1.scores, "tpr", "fpr")&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# Plot the ROC curve&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;plot(t1.perf, col = "green", lwd = 1.5)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Now how do we compare the performance of this decision tree model with our earlier logistic model. One simple way would be to plot the two ROCs in the same graph and visually guess it.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# Add the ROC curve of the logistic model and the diagonal line&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;plot(m1.perf, col = "red", lwd = 1, add = TRUE)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;abline(0, 1, lty = 8, col = "grey")&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;legend("bottomright", legend = c("tree", "logit"), col = c("green", "red"), lwd = c(1.5,1))&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Similarly we can calculate the KS statistic as well&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;ks1.tree &amp;lt;- max(attr(t1.perf, "y.values")[[1]] - (attr(t1.perf, "x.values")[[1]]))&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;ks1.tree&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Another way to compare the performance of two models, especially when the dependent variable is&amp;nbsp;binary, is the Area Under the Curve or the "AUC"&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# AUC for the logistic model&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;m1.auc &amp;lt;- performance(m1.scores, "auc")&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;m1.auc&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# AUC for the decision tree&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;t1.auc &amp;lt;- performance(t1.scores, "auc")&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;t1.auc&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-5631674200961103268?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/5631674200961103268/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/10/modelling-with-r-part-4.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/5631674200961103268'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/5631674200961103268'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/10/modelling-with-r-part-4.html' title='Modelling with R: part 4'/><author><name>MK</name><uri>http://www.blogger.com/profile/03144859563025317904</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-770903156452193870</id><published>2011-10-05T18:40:00.001+05:30</published><updated>2011-10-05T18:41:02.639+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='logit'/><category scheme='http://www.blogger.com/atom/ns#' term='data'/><category scheme='http://www.blogger.com/atom/ns#' term='modelling'/><category scheme='http://www.blogger.com/atom/ns#' term='econometrics'/><category scheme='http://www.blogger.com/atom/ns#' term='credit score'/><category scheme='http://www.blogger.com/atom/ns#' term='german credit'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><title type='text'>Modelling with R: part 3</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;div style="text-align: justify;"&gt;The previous posts, &lt;a href="http://programming-r-pro-bro.blogspot.com/2011/09/modelling-with-r-part-1.html"&gt;part 1&lt;/a&gt; and &lt;a href="http://programming-r-pro-bro.blogspot.com/2011/10/modelling-with-r-part-2.html"&gt;part 2&lt;/a&gt;, detailed the procedure to successfully import the data and transform the data so that we can extract some useful&amp;nbsp;information&amp;nbsp;from them. Now it's time to get our hands dirty with some predictive modelling.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;The dependent variable here is a binary variable taking values "0" and "1", indicating whether the customer defaulted or not. There are specific, but many, modelling&amp;nbsp;techniques&amp;nbsp;that help us with binary dependent variables. Let's start with the one of the simplest, the Logistic Regression model. And this time, let's just dive straight into it and try to make sense as we go along.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="text-align: center;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;##########-------------2.1: Logistic regression------------###############&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;m1.logit &amp;lt;- glm(default ~&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; amt.fac +&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; age.fac +&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; duration +&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; chk_acct +&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; history +&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; purpose +&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; sav_acct +&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; employment +&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; install_rate +&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; pstatus +&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; other_debtor +&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; time_resid +&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; property +&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; other_install +&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; housing +&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; other_credits +&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; job +&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; num_depend +&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; telephone +&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; foreign&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; , family = binomial(link = "logit"), data = dev)&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# The glm command is for "Generalized Linear Models"&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# "~" is the separator between the dependent (or target variable) and the independent variables&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# Independent variables are separated by the "+" sign&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# "data" requires the data frame in the which the variables are stored&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# "family" is used to specify the assumed distribution.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;The above code is pretty much self-explanatory. We are running a logistic model on our transformed data. We have specified the "logit" link which means that the estimates (or coefficient or weights) will be for the log of odds ratio and not the direct probabilities. Additionally, note that in case we had to regress the dependent variable, "default" on all the independent variables, we could have simply written&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;# m1.logit &amp;lt;- glm(default ~ .,&amp;nbsp;family = binomial(link = "logit"), data = dev)&lt;/span&gt;&lt;br /&gt;Here the "." indicates all the variables in the data set except for the dependent variable. We are not using the command here is because we have the original "amount" and "age" variables as well as the transformed "amt.fac" and "age.fac" variables as well and we should use only one form, either the&amp;nbsp;continuous&amp;nbsp;form or the factor form but not both.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;We can check the output of the model as&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;summary(m1.logit)&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;You will see an output that looks like this&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #274e13; font-family: 'Courier New', Courier, monospace; font-size: 13px; line-height: 17px; white-space: pre-wrap;"&gt;Call:&lt;/span&gt;&lt;br /&gt;&lt;pre class="GD40030CKR" style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; font-size: 10pt !important; line-height: 1.3; outline-color: initial; outline-style: none; outline-width: initial; white-space: pre-wrap !important;" tabindex="0"&gt;&lt;span class="Apple-style-span" style="color: #274e13; font-family: 'Courier New', Courier, monospace;"&gt;glm(formula = default ~ amt.fac + age.fac + duration + chk_acct + &lt;br /&gt;    history + purpose + sav_acct + employment + install_rate + &lt;br /&gt;    pstatus + other_debtor + time_resid + property + other_install + &lt;br /&gt;    housing + other_credits + job + num_depend + telephone + &lt;br /&gt;    foreign, family = binomial(link = "logit"), data = dev)&lt;br /&gt;&lt;br /&gt;Deviance Residuals: &lt;br /&gt;    Min       1Q   Median       3Q      Max  &lt;br /&gt;-1.9970  -0.7118  -0.3821   0.6864   2.5393  &lt;br /&gt;&lt;br /&gt;Coefficients:&lt;br /&gt;                  Estimate Std. Error z value Pr(&amp;gt;|z|)    &lt;br /&gt;(Intercept)        0.36717    1.28298   0.286  0.77474    &lt;br /&gt;amt.fac2600-5000  -0.39145    0.28742  -1.362  0.17322    &lt;br /&gt;amt.fac5000+       0.25237    0.40440   0.624  0.53258    &lt;br /&gt;age.fac30-40      -0.12406    0.26915  -0.461  0.64486    &lt;br /&gt;age.fac40+        -0.40640    0.30975  -1.312  0.18950    &lt;br /&gt;duration           0.03454    0.01152   2.998  0.00272 ** &lt;br /&gt;chk_acctA12       -0.26207    0.26204  -1.000  0.31725    &lt;br /&gt;chk_acctA13       -0.71081    0.43527  -1.633  0.10246    &lt;br /&gt;chk_acctA14       -1.66798    0.27841  -5.991 2.08e-09 ***&lt;br /&gt;historyA31         0.51820    0.68122   0.761  0.44684    &lt;br /&gt;historyA32        -0.44583    0.54524  -0.818  0.41354    &lt;br /&gt;historyA33        -0.36003    0.58949  -0.611  0.54137    &lt;br /&gt;historyA34        -0.89606    0.54235  -1.652  0.09850 .  &lt;br /&gt;purposeA41        -1.30443    0.44483  -2.932  0.00336 ** &lt;br /&gt;purposeA410       -0.68765    0.79700  -0.863  0.38825    &lt;br /&gt;purposeA42        -0.50703    0.31109  -1.630  0.10314    &lt;br /&gt;purposeA43        -0.95899    0.29629  -3.237  0.00121 ** &lt;br /&gt;purposeA44        -1.07335    0.95318  -1.126  0.26013    &lt;br /&gt;purposeA45        -0.40166    0.64852  -0.619  0.53569    &lt;br /&gt;purposeA46         0.07051    0.48321   0.146  0.88399    &lt;br /&gt;purposeA48        -0.79836    1.32004  -0.605  0.54531    &lt;br /&gt;purposeA49        -0.63881    0.40523  -1.576  0.11493    &lt;br /&gt;sav_acctA62       -0.20567    0.35346  -0.582  0.56064    &lt;br /&gt;sav_acctA63       -0.29441    0.47750  -0.617  0.53752    &lt;br /&gt;sav_acctA64       -2.03887    0.74864  -2.723  0.00646 ** &lt;br /&gt;sav_acctA65       -0.95937    0.32147  -2.984  0.00284 ** &lt;br /&gt;employmentA72      0.24498    0.51552   0.475  0.63463    &lt;br /&gt;employmentA73      0.01948    0.50334   0.039  0.96913    &lt;br /&gt;employmentA74     -0.41741    0.54075  -0.772  0.44016    &lt;br /&gt;employmentA75      0.01317    0.50928   0.026  0.97936    &lt;br /&gt;install_rate       0.22383    0.10671   2.098  0.03594 *  &lt;br /&gt;pstatusA92        -0.71859    0.53450  -1.344  0.17882    &lt;br /&gt;pstatusA93        -1.19509    0.52355  -2.283  0.02245 *  &lt;br /&gt;pstatusA94        -0.95261    0.60277  -1.580  0.11402    &lt;br /&gt;other_debtorA102   0.71012    0.48442   1.466  0.14267    &lt;br /&gt;other_debtorA103  -0.84087    0.51928  -1.619  0.10538    &lt;br /&gt;time_resid         0.06092    0.10684   0.570  0.56851    &lt;br /&gt;propertyA122       0.43538    0.31755   1.371  0.17035    &lt;br /&gt;propertyA123       0.45504    0.29062   1.566  0.11740    &lt;br /&gt;propertyA124       0.95268    0.52675   1.809  0.07051 .  &lt;br /&gt;other_installA142  0.54490    0.47279   1.153  0.24911    &lt;br /&gt;other_installA143 -0.45903    0.28664  -1.601  0.10929    &lt;br /&gt;housingA152       -0.58427    0.29480  -1.982  0.04749 *  &lt;br /&gt;housingA153       -0.95369    0.59035  -1.615  0.10621    &lt;br /&gt;other_credits     -0.08412    0.24145  -0.348  0.72753    &lt;br /&gt;jobA172            0.51201    0.80390   0.637  0.52419    &lt;br /&gt;jobA173            0.56316    0.77175   0.730  0.46556    &lt;br /&gt;jobA174            0.09850    0.77019   0.128  0.89823    &lt;br /&gt;num_depend         0.38802    0.29445   1.318  0.18758    &lt;br /&gt;telephoneA192     -0.19916    0.24766  -0.804  0.42131    &lt;br /&gt;foreignA202       -1.35004    0.85752  -1.574  0.11541    &lt;br /&gt;---&lt;br /&gt;Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 &lt;br /&gt;&lt;br /&gt;(Dispersion parameter for binomial family taken to be 1)&lt;br /&gt;&lt;br /&gt;    Null deviance: 853.51  on 699  degrees of freedom&lt;br /&gt;Residual deviance: 628.93  on 649  degrees of freedom&lt;br /&gt;AIC: 730.93&lt;br /&gt;&lt;br /&gt;Number of Fisher Scoring iterations: 5&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;We can see that we have five variables which are significant at 1 percent level of significance&amp;nbsp;and one variable significant at 0.1 percent level of&amp;nbsp;significance. Great! Or not so great! How do we judge how good/bad our model is? Have no fear, R is here.&lt;br /&gt;&lt;br /&gt;One of the most popular methods to check the performance of a model, which has a binary&amp;nbsp;independent variable, is by plotting the Receiver Operating&amp;nbsp;Characteristic&amp;nbsp;(ROC) curve&amp;nbsp;which is a plot of the&amp;nbsp;sensitivity, or the true positive rate vs. the false positive rate.&amp;nbsp;In simpler terms, it plots the no. of 1s in the model correctly identified as 1 vs. the no.&amp;nbsp;of 0s in the model which are incorrectly identified as 1.&lt;br /&gt;&lt;br /&gt;Also, it makes more sense to plot the ROC curve on the validation sample.&amp;nbsp;For this, we would need to score that validation data set. By "scoring" we mean that with the values of the independent variables given in the validation sample, we will run the model equation on each row of the data set and get the probability of the customer defaulting. Simply, use all the Xs and the Y-hats or the predicted Ys.&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;val$m1.yhat &amp;lt;- predict(m1.logit, val, type = "response")&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# The predict command runs the regression model on the "val" dataset and stores the estimated&amp;nbsp;&amp;nbsp;y-values, i.e, the yhat.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;library(ROCR)&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;m1.scores &amp;lt;- prediction(val$m1.yhat, val$default)&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: inherit;"&gt;# The prediction function of the ROCR library basically creates a structure to validate&amp;nbsp;our predictions, "val$yhat" with respect to the actual y-values "val$default"&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We can then plot the ROC curve by plotting the performance fuction which checks how our&amp;nbsp;model is performing.&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;plot(performance(m1.scores, "tpr", "fpr"), col = "red")&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;abline(0,1, lty = 8, col = "grey")&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# "tpr" and "fpr" are arguments of the "performance" function indicating that the plot is&amp;nbsp;between the true positive rate and the false positive rate.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# "col" is an argument to the plot function and indicates the colour of the line&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# "abline" plots the diagonal, "lty" is the line type which is used to create the dashed line&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The ROC curve's performance is judged by the area under the curve formed by the ROC curve and the 45' line. The idea is that if we randomly try to classify the customers who will default, we will get the 45' line. And if we use a predictive approach, then the approach should try to classify all the 1s and 1, i.e, correctly identify all the people who defaulted. Such a curve would then be a right triangle with vertices at (0,0), (0,1), (1,1). But since predictive modelling is usually accompanied with some error, we get a curve like we see in the graph. And the rule of thumb is, more the area under the curve, better the model.&lt;br /&gt;&lt;br /&gt;In addition to the ROC curve, the KS statistic is also a popular metric used to judge model&amp;nbsp;performance, the Kolmogorov-Smirnov statistic. It is the maximum difference between the cumulative true&amp;nbsp;positive rate and the cumulative false positive rate&lt;br /&gt;&lt;br /&gt;For this let's store the output of the "performance" function above to an R object&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;m1.perf &amp;lt;- performance(m1.scores, "tpr", "fpr")&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;ks1.logit &amp;lt;- max(attr(m1.perf, "y.values")[[1]] - (attr(m1.perf, "x.values")[[1]]))&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;ks1.logit&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;Well, the ROC shows that the model is not so great but not so bad either. So what do we do to improve our model? Do some more transformations, tweak it a bit, or just try a different technique?&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;I'll leave this as an open question to make it interactive. Let's try all the "feasible" approaches that people suggest that will help us improve the model.&lt;/span&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-770903156452193870?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/770903156452193870/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/10/modelling-with-r-part-3.html#comment-form' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/770903156452193870'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/770903156452193870'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/10/modelling-with-r-part-3.html' title='Modelling with R: part 3'/><author><name>MK</name><uri>http://www.blogger.com/profile/03144859563025317904</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-2687725726484719933</id><published>2011-10-04T22:30:00.002+05:30</published><updated>2011-10-05T03:49:09.466+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Time series'/><category scheme='http://www.blogger.com/atom/ns#' term='missing data in plot'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><title type='text'>Simple time series plot using R : Part 2</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;I would like to share my experience of plotting different time series in the same plot for comparison. As an assignment I had to plot the time series of Infant mortality rate(IMR) along with the SOX emission(sulphur emission) for the past 5 decades in the same graph and compare how the intensities have been varying in the past 5 decades.Well to start with there is a problem of how to get these plots in the same graph as one is a mortality rate and other is an emission rate, having different units of measurements!&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;What we essentially want to do is to see how the intensity of these problem has been changing, whether the intensity has increased/decreased. From a policy makers standpoint, whether it requires immediate attention or not. So what we can do instead is divide all the IMR values by the maximum IMR value that we have for the past 5 decade and store them as "IMR.std". Similarly divide all the SOX values by the maximum SOX value and store it in "SOX.Std". What we have achieved is a parsimonious way of representing the 2 variables that have values between "0 and 1".(Achieving the desired standardization).&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;Now that we have "IMR.Std." and "SOX.Std." both with values between "0-1" I can plot them in the same graph. Recalling from the &lt;a href="http://www.blogger.com/goog_224688146"&gt;previous post&lt;/a&gt;&lt;a href="http://for%20the%20past%206%20decades%20/"&gt;:&amp;nbsp;&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;# Make sure the working directory is set to where the file is, in this case "Environment.csv":&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;a &amp;lt;- read.csv("Environment.csv")&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;# Plotting the "IMR.Std" on a graph &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;plot(a$Year, a$IMR.Std., type="l", xlab= "Years", ylab= "Intensity of problem(normalized between 0-1)", col="green" , lwd=2)&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;# Adding the plot for "SOX.Std."&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;# &lt;span class="Apple-style-span" style="color: blue;"&gt;lines(...)&lt;/span&gt; command basically adds the argument variable to the existing plot. &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;lines(a$Year, a$SOX.std., type="l", col="black", lwd=2)&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;Ideally this should have done the job. Giving me the IMR.Std(in green) and SOX.Std.(in red) in the same plot. But this dint happen for the reason that the data for SOX was available only after 1975 and also the data was not available for alternate years. Well I thought that R would treat it trivially and just plot the "non-NA" values of SOX.std. that were there but as it happens that this was not such a trivial thing for R. It demands a lot more rigor(just like a mathematical proof), to execute a command, not taking anything for granted. Hence to get the desired result I had to specify that it considers only the "non-NA" values for SOX.Std.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;The code for SOX.std had to be altered a bit:&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;a &amp;lt;- read.csv("Environment.csv")&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;plot(a$Year, a$IMR.Std., type="l", xlab= "Years", ylab= "Intensity of problem(normalized between 0-1)", col="green" , lwd=2)&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;# All I need to make sure now is that I direct R to refer to only the "non-NA" values in the SOX.Std. variable.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;lines( a$Year[ !is.na(a$SOX.std.) ], a$SOX.std.[ !is.na(a$SOX.std.) ], type="l", col="black", lwd=2)&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;# If there are 2 separate graphs on the same plot to label the different graphs(depending on their color in this case) we use the&lt;span class="Apple-style-span" style="color: blue;"&gt; legend(...)&lt;/span&gt; command :&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;b &amp;lt;- c("IMR rate", "SOX")&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;legend("bottomleft", b , cex=0.8, col=c("green", "black"), lwd=2, bty="n")&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;# &lt;span class="Apple-style-span" style="color: blue;"&gt;title(...)&lt;/span&gt; command gets a main title to the plot&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;title(main="Stage model for country X", col.main="Black", font.main=4)&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-wuFbUHwMb80/Tos6SPkAxLI/AAAAAAAAAOo/bJNRPHfHTBQ/s1600/Rplot.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;img border="0" height="315" src="http://4.bp.blogspot.com/-wuFbUHwMb80/Tos6SPkAxLI/AAAAAAAAAOo/bJNRPHfHTBQ/s400/Rplot.png" width="400" /&gt;&lt;/span&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;This is how the plot finally looks like.&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;It was&amp;nbsp;&lt;a href="http://utkarshu.in/"&gt;Utkarsh's&lt;/a&gt;&amp;nbsp;generosity, who gave me the codes, that saved me a lot of time in solving this small issue, I wish to pass this on as it might save someone else's.&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-2687725726484719933?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/2687725726484719933/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/10/simple-time-series-plot-using-r-part-2.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/2687725726484719933'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/2687725726484719933'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/10/simple-time-series-plot-using-r-part-2.html' title='Simple time series plot using R : Part 2'/><author><name>Shreyes</name><uri>http://www.blogger.com/profile/02952702110986035135</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='http://4.bp.blogspot.com/-qxonS7T4lDQ/Tnu9rDO6QhI/AAAAAAAAAOM/H6gH-qheti4/s220/DSC09046.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-wuFbUHwMb80/Tos6SPkAxLI/AAAAAAAAAOo/bJNRPHfHTBQ/s72-c/Rplot.png' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-8877741654802198451</id><published>2011-10-02T19:39:00.002+05:30</published><updated>2011-10-04T14:44:59.084+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='data'/><category scheme='http://www.blogger.com/atom/ns#' term='example'/><category scheme='http://www.blogger.com/atom/ns#' term='modelling'/><category scheme='http://www.blogger.com/atom/ns#' term='econometrics'/><category scheme='http://www.blogger.com/atom/ns#' term='credit score'/><category scheme='http://www.blogger.com/atom/ns#' term='german credit'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><title type='text'>Modelling with R: part 2</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;div style="text-align: justify;"&gt;I apologize for the delay in the second post (just in case anybody was waiting), I had been vary involved with work the past week. I shall try to be more regular. Well, in the &lt;a href="http://programming-r-pro-bro.blogspot.com/2011/09/modelling-with-r-part-1.html"&gt;previous&lt;/a&gt; post, we successfully imported data into R and got a basic "feel" of it by looking at the various variables present and their types as well. Now we will try to process the data to make sense of it. Data by themselves are just space hogging particles,&amp;nbsp;aesthetically&amp;nbsp;challenged, and practically worthless... unless... well... unless... we can get some information out of them. And to get that information, they need to be processed, transformed, and at times coerced. This post will describe how we can start to do that. So, let's grab its throat and make it spit out the ugly truth (excuse me for the histrionics). Also, this last sentence bore no relation &lt;a href="http://www.imdb.com/title/tt1142988/"&gt;this&lt;/a&gt; horrible Gerad Butler movie.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;div style="text-align: center;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;##########----------------1.2: Processing the data------------###############&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&amp;nbsp;There are three main steps involved here:&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 1. Preliminary visulaization&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 2. Data transformation and/or variable creation&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 3. Development-validation division of data set&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Let's start with 1.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Suppose we want to check the distribution of amount in the given data. We can use&amp;nbsp;a simple plot command.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;plot(amount, type = "l", col = "royalblue")&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;plot(age, type = "l", col = "brown")&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Now the plots we have created present the variation in these variables in a manner&amp;nbsp;which neither easily&amp;nbsp;discernible&amp;nbsp;nor is it clean.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;It would be better if created histograms to check the frequency distribution of these&amp;nbsp;variables.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;hist(amount)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;hist(age)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# The "hist" command has a lot of options that help extend the features of the plot.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Now, with the hist command, we are able to see the picture clearly (literally). But it is not always appropriate to plot a histogram. What is the best way depends largely on the problem at hand. Suppose we had a&amp;nbsp;multiple&amp;nbsp;time&amp;nbsp;series and we wanted to check the behaviour, then it would be better to use the plot command which will not only present the data in a much neater way but also enable us to compare different time series in a single plot. For a simple example, you can check Shreyes' &lt;a href="http://programming-r-pro-bro.blogspot.com/2011/09/simple-plot-using-r.html"&gt;post&lt;/a&gt;.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Coming back, we can similarly visualize the pattern, frequency and distributions of other variables&amp;nbsp;as well.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Now, in case you have ever worked on a credit scoring exercise before, you might have heard&amp;nbsp;that it is better to create categories out continuous variables.&amp;nbsp;This helps a lot while implementing the model that we build because it is more convenient&amp;nbsp;to come with strategies for individuals belonging to a particular income group&amp;nbsp;rather than for all individuals with specific incomes.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;For this we need to bin some&amp;nbsp;variables&amp;nbsp;like amount and age. One approach to do this is to run the following code&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;b&gt;&lt;span class="Apple-style-span" style="color: red;"&gt;DO NOT&lt;/span&gt;&lt;/b&gt; run this chunk of code. I will explain later why. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;# g.data$amount &amp;lt;- as.factor(ifelse(amount &amp;lt;= 2500, "0-2500",&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; ifelse(amount &amp;lt;= 5000, "2600-5000", "5000+")))&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Here we are creating three categories for the variable "amount". One for those with&amp;nbsp;income level less than 2500, one for income between 2500 and 5000, and one for income&amp;nbsp;greater than 5000.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;There is an important point to note here. Above, while creating the category variable, we&amp;nbsp;overwrote the original variable "amount" in the R object "g.data". Ideally this process is not&amp;nbsp;well advised because if we later find that there was an error in our code or there was some&amp;nbsp;flaw in the logic and we need to change it, we will have to re-do all the steps that we have done&amp;nbsp;till this point. But, there is another side here as well. R, while working, stores all the data and the&amp;nbsp;objects that we create in the RAM and hence if the data set is of considerably large size then&amp;nbsp;creating additional variables by transformation is not a very wise idea either. This trade-off&amp;nbsp;&amp;nbsp;needs to be balanced.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;In this case, the data set is quite small and hence it would be better if we create an additional&lt;/div&gt;&lt;div style="text-align: justify;"&gt;object instead of overwriting the original one.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;g.data$amt.fac &amp;lt;- as.factor(ifelse(amount &amp;lt;= 2500, "0-2500",&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; ifelse(amount &amp;lt;= 5000, "2600-5000", "5000+")))&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;head(g.data$amt.fac)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Similarly, we can do so for "age".&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;g.data$age.fac &amp;lt;- as.factor(ifelse(age&amp;lt;=30, '0-30', ifelse(age &amp;lt;= 40, '30-40', '40+')))&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;head(g.data$age.fac)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Here our dependent variable is "response". It is a factor variable and has&amp;nbsp;"1" and "2" as the factor levels. Now, R by itself can handle factor variables and so we do not need to transform them unless we plan to combine categories. But I like to keep the response category coded as "1" (this is just because of habit and nothing else). Hence, I reassign the levels to "0" and "1".&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;g.data$default &amp;lt;- as.factor(ifelse(response == 1, "0", "1"))&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;is.factor(g.data$default)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;contrasts(g.data$default)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;head(g.data$default)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;We attach the data again to include the newly created variables.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;attach(g.data)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;In the&amp;nbsp;&lt;a href="http://programming-r-pro-bro.blogspot.com/2011/09/modelling-with-r-part-1.html"&gt;previous&lt;/a&gt;&amp;nbsp;post, one the &lt;a href="http://programming-r-pro-bro.blogspot.com/2011/09/modelling-with-r-part-1.html?showComment=1317456477493#c5554271930711081372"&gt;comments&lt;/a&gt;&amp;nbsp;introduced me to the &lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;with&lt;/span&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;()&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&amp;nbsp;command as a substitute for the&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;attach()&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;. The&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;with&lt;/span&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;()&amp;nbsp;&lt;/span&gt;command also serves the purpose quite well. It reduces the pain of writing the object name with the $ sign before we can refer to a variable but it needs to be included for every operation that we perform on the object.&amp;nbsp;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Now, we saw that there are a lot of categorical variables present in the data.&amp;nbsp;R provides many functions to plot categorical data.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Let's see an example.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;mosaicplot(default ~ age.fac, col = T)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;mosaicplot(default ~ job, col = T)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;mosaicplot(default ~ chk_acct, col = T)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;We can also use a&amp;nbsp;spine plot.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;spineplot(default ~ age.fac)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;We can also check the&amp;nbsp;relations&amp;nbsp;between variables&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;library(lattice)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;xyplot(amount ~ age)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;In case you don't have the "lattice" library installed, you can download it by running&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;install.packages("lattice")&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;We can also condition on a variable and see the interaction&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;xyplot(amount ~ age | default)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;"lattice" package also has the option for a barchart and it lets you plot the barchart and a histogram for&amp;nbsp;factor variable type as well.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;barchart(age.fac, col = "grey")&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;barchart(amt.fac, col = "grey")&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;histogram(employment, col = "grey")&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;histogram(sav_acct, col = "grey")&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;As a last step in this stage, we need to create a development sample and a validation sample.&amp;nbsp;We take about 70%&amp;nbsp;percent&amp;nbsp;of the data as development sample and 30% as validation sample.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;d &amp;lt;- sort(sample(nrow(g.data), nrow(g.data)*0.7))&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# The sample command here creates a random sample of the number of rows in "g.data" and then&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# selects 70% of this random sample and assigns it to object "d".&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Note that here the sample is being generated from the row numbers and not the exact rows of data&lt;/div&gt;&lt;div style="text-align: justify;"&gt;so that if you see the object "d", you will see 700 randomly selected natural numbers between 1 and 1000 which are nothing but the row numbers in the data frame "g.data".&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# The "sort"&amp;nbsp;command in the beginning just sorts these randomly generated row numbers in an ascending order.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&amp;nbsp;Then to create the development sample, we use the vector properties of R and assign the "d" rows&amp;nbsp;to the R object "dev", and the remaining to the R object "val".&amp;nbsp;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;dev&amp;lt;-g.data[d,]&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;val&amp;lt;-g.data[-d,]&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;After creating the sample, we can check the size of the two samples vis-a-vis the original data.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;dim(g.data)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;dim(dev)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;dim(val)&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;Finally we have been able to domesticate the data. We have sliced and diced them according to our needs. In the next post we will try to cook them in the modelling pan.&lt;/span&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-8877741654802198451?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/8877741654802198451/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/10/modelling-with-r-part-2.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/8877741654802198451'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/8877741654802198451'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/10/modelling-with-r-part-2.html' title='Modelling with R: part 2'/><author><name>MK</name><uri>http://www.blogger.com/profile/03144859563025317904</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-601764821324059020</id><published>2011-09-30T03:32:00.000+05:30</published><updated>2011-10-05T17:29:15.884+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='data'/><category scheme='http://www.blogger.com/atom/ns#' term='modelling'/><category scheme='http://www.blogger.com/atom/ns#' term='econometrics'/><category scheme='http://www.blogger.com/atom/ns#' term='credit score'/><category scheme='http://www.blogger.com/atom/ns#' term='german credit'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><title type='text'>Modelling with R: part 1</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;div style="text-align: justify;"&gt;When I started work about 3 months ago, I didn't know much more than loading data and executing standard Econometric commands in R. But now I feel much much much more confident in using R for work, for research, for puzzles, and sometimes just for fun. I have learnt a lot of R, statistics, and real world application of Econometrics than I ever did at &lt;a href="http://www.igidr.ac.in/"&gt;school&lt;/a&gt; (this in spite of the fact that I graduated from a &lt;a href="http://www.igidr.ac.in/"&gt;school&lt;/a&gt; which has a very strong foundation in the field).&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Much R that I have learnt has come from various R enthusiasts who have shared their knowledge and creativity of this elegant language over the internet. And as I grow with R, I found it quite compelling to share my learning journey as well.&amp;nbsp;So, I have decided to describe some of the modelling techniques that are available in R. Along with this, I shall keep giving a few insights regarding the foundation and the need for the technique applied. Since all the techniques are quite big for one blog post, I have decided to divide entire journey into small,&amp;nbsp;channelized, understandable and presumably fun exercises that will help the reader navigate through the models easily and efficiently.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;For illustrating these techniques, I have taken the quite popular German credit data set which can be downloaded from &lt;a href="http://archive.ics.uci.edu/ml/machine-learning-databases/statlog/german/"&gt;here&lt;/a&gt; and &lt;a href="ftp://ftp.ics.uci.edu/pub/machine-learning-databases/statlog/german"&gt;here&lt;/a&gt;. Additionally, most of the techniques that I describe here are taken from &lt;a href="http://cran.r-project.org/doc/contrib/Sharma-CreditScoring.pdf"&gt;this&lt;/a&gt; excellent guide. These blog posts can be thought of an extension to this guide. The German credit data has 1000 rows and 21 columns including the dependent variable, which in this case is binary- 1 means "good credit" and 2 means "bad credit". We need to predict whether a given case example will be a "good credit" or a "bad credit".&amp;nbsp;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Now, there are a lot of techniques that can be used for such cases which have a dichotomous dependent variable. We will go through some of them in the following posts. For starters, here we will just try to import the data successfully and try to understand what kind of variables do we have.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;###########================================================###############&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Section 1: Data preparation &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;#&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;###########================================================###############&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;##########----------------1.1: Reading the data------------###############&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Set the directory to where the german data file is located&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;setwd("D:/Softwares/R/Training/")&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Read the data by importing the csv file&amp;nbsp;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;g.data &amp;lt;- read.csv("german.data.csv", header = F)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Note that by default the read.csv file has header the option set to TRUE. In this&amp;nbsp;case however, since we don't have the variable names and hence the header row, we&amp;nbsp;need to change the default option and set it to FALSE otherwise the first row of data&amp;nbsp;will be taken as the header row.&lt;br /&gt;&lt;br /&gt;# There is a slight mistake in the above code. I came to realize this when it was pointed out in one of the &lt;a href="http://programming-r-pro-bro.blogspot.com/2011/09/modelling-with-r-part-1.html?showComment=1317686185296#c9026100316075262376"&gt;comments&lt;/a&gt;. As it happens, the data set available at the two links above is in a "space" delimited format. So the usual csv command will not work. I imported it as a csv because I had converted the tab delimited format to csv using a spread sheet (quite "uncool" I know, but I didn't know much R then). Anyway, thanks to another &lt;a href="http://programming-r-pro-bro.blogspot.com/2011/09/modelling-with-r-part-1.html?showComment=1317699586532#c6751705972295890275"&gt;comment&lt;/a&gt;, we can use this command to import the file.&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white; line-height: 18px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;g.data &amp;lt;- read.delim("german.data",header=F,sep=" ")&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;It's always good practice to check the first few observations and see that the data&amp;nbsp;were read in correctly.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;head(g.data)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;# "head" displays the first six observations&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;We can and should also check the number of rows of data and the number of variables present.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;dim(g.data)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Now, in order to make our analysis easier to understand and implement, it is advisable&amp;nbsp;to identify all the variables by specifying the column names.&amp;nbsp;The column names can be found in the data dictionary.&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;names(g.data) &amp;lt;- c("chk_acct", "duration", "history", "purpose", "amount", "sav_acct", "employment",&lt;/span&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp;"install_rate", "pstatus", "other_debtor", "time_resid", "property", "age",&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;"other_install", "housing", "other_credits", "job", "num_depend", "telephone",&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;"foreign", "response")&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;head(g.data)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;# The "names" function gives the column names of the data frame. Here we are assigning names using this&amp;nbsp;function in a serial manner.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Well, if I may use some corporate jargon, it is advisable to do a DQ and DI check.&amp;nbsp;For starters, it is important to find out the characteristic of all the variables present&amp;nbsp;in the data.&amp;nbsp;We can start with checking the type for each variable.&amp;nbsp;For variables that we expect to be numeric or factor we can check them as&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;is.numeric(g.data$property)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;is.factor(g.data$property)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;is.numeric(g.data$age)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;is.double(g.data$amount)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;is.numeric(g.data$amount)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;This process becomes quite a tedious exercise if we have a large number of variables&amp;nbsp;R can make the task easier for us.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;str(g.data)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;# "str" will compactly display the structure of any R object&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Additionally, to make the code more legible and easier to write, we can attach the object "g.data".&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Then we can refer to g.data$amount simply as amount.&amp;nbsp;But we need to ensure that there are not more than one objects with same name, i.e, if you have&amp;nbsp;say two data sets data1 and data2, and both the data sets have the variable "amount", then if we&amp;nbsp;attach both the data sets and call "amount", then "amount" will refer to the former object we&amp;nbsp;attached.&amp;nbsp;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;attach(g.data)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;head(amount)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Well, this completes the data import. We have successfully manged to import the data and correctly identify all the variables present. We will continue playing with data in the &lt;a href="http://programming-r-pro-bro.blogspot.com/2011/10/modelling-with-r-part-2.html"&gt;next&lt;/a&gt; post. &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-601764821324059020?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/601764821324059020/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/09/modelling-with-r-part-1.html#comment-form' title='13 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/601764821324059020'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/601764821324059020'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/09/modelling-with-r-part-1.html' title='Modelling with R: part 1'/><author><name>MK</name><uri>http://www.blogger.com/profile/03144859563025317904</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>13</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-7187624405245032621</id><published>2011-09-27T20:07:00.001+05:30</published><updated>2011-09-27T20:07:08.568+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='problem'/><category scheme='http://www.blogger.com/atom/ns#' term='solution'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><category scheme='http://www.blogger.com/atom/ns#' term='Project Euler'/><title type='text'>Project Euler: problem 6</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="background-color: white;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: 'Trebuchet MS', sans-serif; font-size: 16px; font-style: italic;"&gt;The sum of the squares of the first ten natural numbers is,&lt;/div&gt;&lt;div style="font-family: 'Trebuchet MS', sans-serif; font-size: 16px; font-style: italic; text-align: center;"&gt;1&lt;sup&gt;2&lt;/sup&gt;&amp;nbsp;+ 2&lt;sup&gt;2&lt;/sup&gt;&amp;nbsp;+ ... + 10&lt;sup&gt;2&lt;/sup&gt;&amp;nbsp;= 385&lt;/div&gt;&lt;div style="font-family: 'Trebuchet MS', sans-serif; font-size: 16px; font-style: italic;"&gt;The square of the sum of the first ten natural numbers is,&lt;/div&gt;&lt;div style="font-family: 'Trebuchet MS', sans-serif; font-size: 16px; font-style: italic; text-align: center;"&gt;(1 + 2 + ... + 10)&lt;sup&gt;2&lt;/sup&gt;&amp;nbsp;= 55&lt;sup&gt;2&lt;/sup&gt;&amp;nbsp;= 3025&lt;/div&gt;&lt;div style="font-family: 'Trebuchet MS', sans-serif; font-size: 16px; font-style: italic;"&gt;Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025&amp;nbsp;-&amp;nbsp;385 = 2640.&lt;/div&gt;&lt;div style="font-family: 'Trebuchet MS', sans-serif; font-size: 16px; font-style: italic;"&gt;Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.&lt;/div&gt;&lt;div style="font-family: 'Trebuchet MS', sans-serif; font-size: 16px; font-style: italic;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: 'Trebuchet MS', sans-serif; font-size: 16px;"&gt;This is one quite simple.&lt;/div&gt;&lt;div style="font-family: 'Trebuchet MS', sans-serif; font-size: 16px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: 'Trebuchet MS', sans-serif; font-size: 16px;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;# Create a vector with the first hundred natural numbers&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;x.1 &amp;lt;- (1:100)&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;# Square each element of the vector of natural numbers, i.e, square each natural number and store in another vector&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;y.1 &amp;lt;- x.1^2&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;head(y.1)&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;# Sum the first hundred natural numbers and the squares of each of the first hundred natural numbers and take the difference of these sums&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;a.1 &amp;lt;- sum(y.1)&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;b.1 &amp;lt;- (sum(x.1))^2&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;b.1 - a.1&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;b.1&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #6aa84f; font-family: inherit;"&gt;Answer: 25164150&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="MsoNormal"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;span style="color: black; line-height: 115%;"&gt;After solving a couple of these problems, andafter reading some solutions posted by&amp;nbsp;&lt;a href="http://aatrujillo.wordpress.com/"&gt;&lt;span style="color: blue;"&gt;aatrujillo&lt;/span&gt;&lt;/a&gt;&amp;nbsp;&lt;a href="http://programming-r-pro-bro.blogspot.com/2011/09/project-euler-problem-3.html?showComment=1316652902830#c5500738793886170979"&gt;&lt;span style="color: blue;"&gt;here&lt;/span&gt;&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="http://programming-r-pro-bro.blogspot.com/2011/09/project-euler-problem-1.html?showComment=1317001592135#c7038322922041444892"&gt;&lt;span style="color: blue;"&gt;here&lt;/span&gt;&lt;/a&gt;, I realize that my solutions are notgeneral, i.e., they only cater to the problem at hand and hence their scope isvery limited. For example, consider the above problem. Had the question askedto do the same analysis on the first 200 natural numbers, I would have torewrite the entire loop again. I understand that in this case it does notinvolve much more than changing the size of the x.1 vector, but for a problemthat involves more than one loop, it seems to be very "uncool". As aresult, I have decided to orient my results towards general solutions and thensolve the problem by specifying the parameters. Let's see how that goes. :)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="font-family: 'Trebuchet MS', sans-serif; font-size: 16px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-7187624405245032621?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/7187624405245032621/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/09/project-euler-problem-6.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/7187624405245032621'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/7187624405245032621'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/09/project-euler-problem-6.html' title='Project Euler: problem 6'/><author><name>MK</name><uri>http://www.blogger.com/profile/03144859563025317904</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-1678279693743977911</id><published>2011-09-21T23:15:00.001+05:30</published><updated>2011-10-04T20:19:37.125+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Time series'/><category scheme='http://www.blogger.com/atom/ns#' term='plot'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><category scheme='http://www.blogger.com/atom/ns#' term='Overnight MIBOR'/><title type='text'>Simple time series plot using R : Part 1</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;As a task for my Financial eco assignment&amp;nbsp;I had to plot a simple time series of the &lt;a href="http://www.debtonnet.com/newdon/files/MarketInformation/NSEReports.asp"&gt;overnight MIBOR&lt;/a&gt;(Mumbai interbank offer rates) for the past one year . The job could very well have been done easily in MS-Excel but I choose to plot it in R instead and the quality of the graph, pixel-wise and neatness wise, was way better than what I could have obtained with MS-Excel. All this at the cost of a minimal 3 lines of code:&lt;br /&gt;&lt;br /&gt;# The overnight MIBOR rates were stored in a file name "Call_Rates_2011.csv", this is just a normal Excel file saved in a CSV(comma separated delimited) format that R can read.&lt;br /&gt;# The way in which R conceptualizes the data is similar to that in Excel, to draw a simple analogy you can assume that the variable "a" now stores the entire Excel spreed sheet in it.&lt;br /&gt;# You will have to make sure that the working directory is the one that contains the file "Call_Rates_2011.csv"&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;a &amp;lt;- read.csv("Call_Rates_2011.csv")&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;# The 2 column headers in my CSV file were "date" and "mibor", so the below code plots "date" on the x-axis and "mibor" on the y-axis. The as.Date() tells R that the column "date" contains dates in the format "day-month-year"('%d-%b-%y').&lt;br /&gt;# a$(column header) is the standard way of referring to a column in the "spreadsheet" contained in "a"&lt;br /&gt;# xlab : x-axis label&lt;br /&gt;# ylab : y- axis label&lt;br /&gt;# type : line(l)&lt;br /&gt;# col : color of the line&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;plot(as.Date(a$date,'%d-%b-%y'), a$mibor, xlab= "Months", ylab= "MIBOR overnight rates(percentage)", type='l', col='red')&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;# This is to get the titles in place&lt;br /&gt;# main : main title&lt;br /&gt;# col.main : color of the main title&lt;br /&gt;#font.main : font size of the title&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;title(main="Overnight MIBOR rates for last one year", col.main="black", font.main=4)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;And the plot hence obtained thus looks like:&lt;br /&gt;&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-k36Kt8KMY0o/TnodGPNE0OI/AAAAAAAAAOE/CJ9RVt54csU/s1600/Rplot_Mibor.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="253" src="http://1.bp.blogspot.com/-k36Kt8KMY0o/TnodGPNE0OI/AAAAAAAAAOE/CJ9RVt54csU/s320/Rplot_Mibor.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Incase you can't make out the difference in the quality of the plot obtained just drop in your comments and email address and I will mail you the pdf and the jpg image of the plot. You can pull/stretch&amp;nbsp;it to see that the pixels don't get distorted and it looks way neater if you present it in your slide in a presentation.&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-1678279693743977911?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/1678279693743977911/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/09/simple-plot-using-r.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/1678279693743977911'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/1678279693743977911'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/09/simple-plot-using-r.html' title='Simple time series plot using R : Part 1'/><author><name>Shreyes</name><uri>http://www.blogger.com/profile/02952702110986035135</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='http://4.bp.blogspot.com/-qxonS7T4lDQ/Tnu9rDO6QhI/AAAAAAAAAOM/H6gH-qheti4/s220/DSC09046.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/-k36Kt8KMY0o/TnodGPNE0OI/AAAAAAAAAOE/CJ9RVt54csU/s72-c/Rplot_Mibor.png' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-2648750509708341353</id><published>2011-09-21T22:01:00.001+05:30</published><updated>2011-09-21T22:02:14.155+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='problem'/><category scheme='http://www.blogger.com/atom/ns#' term='solution'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><category scheme='http://www.blogger.com/atom/ns#' term='Project Euler'/><title type='text'>Project Euler: problem 3</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="background-color: white; font-size: 16px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;i&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;i&gt;The prime factors of 13195 are 5, 7, 13 and 29.&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;i&gt;What is the largest prime factor of the number 600851475143 ?&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This one was quite easy, and much easier in R as it turns out.&lt;br /&gt;&lt;br /&gt;The GNU Multi-Precision Library (&lt;a href="http://gmplib.org/"&gt;GMP&lt;/a&gt;) is available as a package in R. So the only thing I had to do is install the library. Rest... well... not much...&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;library(gmp)&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;factorize(600851475143)&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;# The factorize function lists down all the prime factors of the number in the parentheses in ascending order.&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #38761d; font-family: inherit;"&gt;Answer:&amp;nbsp;6857&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #38761d; font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;Well, I understand I did not do much here than just being aware of something called the&amp;nbsp;&lt;/span&gt;&lt;a href="http://gmplib.org/"&gt;GMP&lt;/a&gt;. Don't blame me, blame &lt;a href="http://www.google.co.in/"&gt;them&lt;/a&gt;&amp;nbsp;or &lt;a href="http://utkarshu.in/"&gt;him&lt;/a&gt;.&amp;nbsp;But in due respect for the only concrete language known to humankind (Math not R), I shall try to come up with a more genuine approach.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-2648750509708341353?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/2648750509708341353/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/09/project-euler-problem-3.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/2648750509708341353'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/2648750509708341353'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/09/project-euler-problem-3.html' title='Project Euler: problem 3'/><author><name>MK</name><uri>http://www.blogger.com/profile/03144859563025317904</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-1486528605668322981</id><published>2011-09-17T18:19:00.001+05:30</published><updated>2011-09-20T01:34:00.717+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='example'/><category scheme='http://www.blogger.com/atom/ns#' term='beamer'/><category scheme='http://www.blogger.com/atom/ns#' term='LaTeX'/><category scheme='http://www.blogger.com/atom/ns#' term='presentation'/><title type='text'>Introduction to Beamer</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;div style="text-align: justify;"&gt;A friend of mine, who is quite smart by the way (she is a PhD. student in Physics at &lt;a href="http://www.cam.ac.uk/"&gt;Cambridge&lt;/a&gt;), recently asked me for some help with &lt;a href="http://en.wikipedia.org/wiki/Beamer_(LaTeX)"&gt;Beamer&lt;/a&gt;. Well most of my knowledge and code came from &lt;a href="http://utkarshu.in/"&gt;Utkarsh&lt;/a&gt;&amp;nbsp;when I had started about a year ago. Initially, I had just asked him for some help with &lt;a href="http://www.ubuntu.com/"&gt;Ubuntu&lt;/a&gt;, then a little bit with &lt;a href="http://www.r-project.org/"&gt;R&lt;/a&gt;, and then some with&amp;nbsp;&lt;a href="http://www.latex-project.org/"&gt;LaTeX&lt;/a&gt;, followed a bit by &lt;a href="http://www.python.org/"&gt;Python&lt;/a&gt;. This was all accompanied with constant questions from my side regarding mathematics, programming, web scarping, regular expressions etc. Most of the times he ended up writing the entire code for me. Though this was more or less like spoon-feeding a kid, he made it a point to ensure that I understood what the code was doing. After writing a lot of code for me and fixing a lot of bugs in the code that I wrote, I finally started to get some confidence while writing my own scripts. A year later I realize that much of teaching much did not emphasize that I get the "grammar" of script right, but that I got the "approach" of the script right. To be more clear, it's more important to know exactly what we want are trying achieve through the code rather than the way to do it. If you know what you want, you'll figure out a way to get it.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Well, when my friend asked for some help with&amp;nbsp;&lt;a href="http://en.wikipedia.org/wiki/Beamer_(LaTeX)"&gt;Beamer&lt;/a&gt;, I didn't know how to exactly go about it. Should I forward her some documentation that I have stored in my computer which I never read myself? Well, I did do that. But the idea was a bit discomforting. Also, all the time that&amp;nbsp;&lt;a href="http://icwww.epfl.ch/~upadhyay/"&gt;Utkarsh&lt;/a&gt;&amp;nbsp;spent with an idiot like me should not go waste. So I tried to recollect some of the things that he taught me, scrapped through some of my beamer presentations that I had made in college and organized it into a TeX document.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;Hope it helps...&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;% This is a very humble attempt to introduce fellow students to amazing world of LaTeX, and more specifically Beamer.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;% This by no means is a guide or a reference manual for Beamer. It is simply a beginning. If you are planning your first&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;presentation, this should serve the purpose and at the same time arouse&amp;nbsp;curiosity&amp;nbsp;to delve deeper.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: justify; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;% Also, I would like to point out that most of the code here has been acquired from various secondary sources and are not&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;% my creation. I have just assembled some information that I have into a organized document. I sincerely thank all those&lt;/span&gt;&amp;nbsp;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;creative individuals you were generous enough to share their solutions with the world.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\documentclass[]{beamer}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\usepackage{graphicx}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\usepackage{amsmath}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\usepackage{tabls}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\usepackage{booktabs}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\usepackage{float}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\usepackage{array}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\usepackage[english]{babel}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\usepackage{hyperref}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\usepackage{subfigure}&lt;/span&gt;&amp;nbsp;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;% This package is a very neat way to&amp;nbsp;accommodate&amp;nbsp;more than one graphic on one slide.&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;&amp;nbsp;A sample command&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;is included for reference.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\newenvironment{wideitemize}{\itemize\addtolength{\itemsep}{10pt}}{\enditemize}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;% Typically, the itemize enviroment does not provide adequate space between bullet points&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;% and hence can look a bit cluttered at times. The new "wideitemize" problem solves this issue.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;% To use it just put your regular "\item" between "\begin{wideitemize}" and "\end{wideitemize}" &lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\hypersetup{&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;% This can be left blank if information is not available&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt; pdfauthor = {Author's name},&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt; pdftitle = {Title for the pdf file},&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt; pdfsubject = {},&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt; pdfkeywords = {},&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt; pdfcreator = {LaTeX with hyperref package},&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt; pdfproducer = {dvips + ps2pdf}}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: right; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\mode&amp;lt;presentation&amp;gt; &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;% This is a representative list of themes available is Beamer. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Shreyes prefers the Warsaw theme, &amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;I prefer&lt;/span&gt;&amp;nbsp;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;Pittsburg; it's much neater.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: right; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&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;Also, CambridgeUS is quite&amp;nbsp;popular among academics.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt; {&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%\usetheme{Warsaw}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%\usetheme{Rochester}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%\usetheme{Madrid}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%\usetheme{Pittsburgh}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%\usetheme{Antibes}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%\usetheme{Montpellier}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%\usetheme{Berkeley}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%\usetheme{PaloAlto}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%\usetheme{Goettingen}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%\usetheme{Marburg}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%\usetheme{Hannover}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%\usetheme{Berlin}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%\usetheme{Ilmenau}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%\usetheme{Dresden}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%\usetheme{Darmstadt}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%\usetheme{Frankfurt}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%\usetheme{Singapore}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%\usetheme{Szeged}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%\usetheme{Copenhagen}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%\usetheme{Malmoe}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; \usetheme{CambridgeUS}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; \setbeamercovered{transparent = 28}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt; }&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\usefonttheme{professionalfonts}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\usecolortheme[rgb={0.01,0.18,0.42}]{structure} &lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\useinnertheme{rounded}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\title{Sample Presentation}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\author{Author} \institute{My Institute}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\setbeamertemplate{footline}[frame number]&lt;/span&gt;&amp;nbsp;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;% This shows the slide number at the bottom of the page. May or may not be&amp;nbsp;required&amp;nbsp;depending on the theme you use.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\beamertemplatenavigationsymbolsempty&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;% This disables the navigation button in the lower right corner of the generated pdf file.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\begin{document}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\begin{frame} &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;% Just so we are clear, a frame here means a slide.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt; \titlepage&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\end{frame}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: center; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\AtBeginSection[] &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;% This will display the list of contents before the beginning of each section&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;{&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;   \begin{frame}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; \frametitle{Outline}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; \tableofcontents[currentsection]&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;   \end{frame}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: center; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\begin{frame} &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;% You can use this if you just want the list of contents to be displayed once at the beginning.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; \tableofcontents&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\end{frame}             &lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\section{Introduction}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\begin{frame}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; \frametitle{A simple sample frame}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \begin{wideitemize}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\item Point 1&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\pause&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\item Point 2&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\pause&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\item Notice the space between the points.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\pause &lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\item Comparatively, more generous than the &amp;nbsp; &amp;nbsp;normal ``itemize" enviroment&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\pause&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \item Also, the sequential appearance of points is handeled with \textbackslash pause command.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \end{wideitemize}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\end{frame}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\section{Part I}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\begin{frame}{Frames you can replicate} &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;% You can include the frame title here as well.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; \begin{itemize}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \item 1&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \item 2&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \item 3&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \item 4&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \item 5&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; \end{itemize}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\end{frame}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\begin{frame}{Another one you can replicate} &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;% This one uses wideitemize&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; \begin{wideitemize}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \item 1&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \item 2&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \item 3&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \item 4&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \item 5&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; \end{wideitemize} &lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\end{frame}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\begin{frame}{Nested environments}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; \begin{wideitemize}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\item This is wideitemize environment&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: center; text-indent: 0px;"&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\item You can nest environments to formulate a&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;hierarchical&lt;/span&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp;structure&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \item See below&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\begin{itemize}     &lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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;\item This is itemize&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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;\pause&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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;\item Notice the space between bullets&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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;\pause &lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: right; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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;\item The bullets can be changed to number using the enumerate environment&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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;\pause&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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;\item \LaTeX is cool&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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;\pause&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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;\item I am glad you chose to give it a&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: center;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;thought&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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;\pause&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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;\item It will change the way you work&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\end{itemize}  &lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; \end{wideitemize}   &lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\end{frame}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\section{Part II}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\begin{frame}{subfigure example}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; \begin{figure}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \centering&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \subfigure[Figure 1]{&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \includegraphics[scale=0.4]{name_of_figure1_file.pdf}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \label{model1}} &amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;% Try putting labels for every figure, table, and equation. It will become easier to navigate&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;if the document is huge.&lt;/span&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \subfigure[Figure 2]{&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \includegraphics[scale=0.4]{name_of_figure2_file.pdf}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \label{model2}&lt;/span&gt;}&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; \end{figure}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\end{frame}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\begin{frame}{Math in \LaTeX}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; \begin{enumerate}&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; \item Use the ``align" or ``gather" environment for math&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\item They are neater&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\item You can also use the ``equation" and ``eqnarray" envrionments&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\item Also, notice that this is the enumerate environment&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\item Hence the numbers instead of bullets &lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\end{enumerate} &lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\end{frame}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\centering&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;\frame[plane]{&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\vspace{2cm}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {\huge Thank you} &lt;/span&gt;&lt;span class="Apple-style-span" style="color: #a64d79; font-family: Arial, Helvetica, sans-serif;"&gt;% Huge makes the text size, well, Huge&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\vspace{3cm}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\begin{flushright}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Author name and contact&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;\end{flushright}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left; text-indent: 0px;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-indent: 0px;"&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;\end{document}&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: left;"&gt;&amp;nbsp; &amp;nbsp;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-1486528605668322981?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/1486528605668322981/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/09/introduction-to-beamer.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/1486528605668322981'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/1486528605668322981'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/09/introduction-to-beamer.html' title='Introduction to Beamer'/><author><name>MK</name><uri>http://www.blogger.com/profile/03144859563025317904</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-4595668682947021240</id><published>2011-09-16T17:15:00.000+05:30</published><updated>2011-09-16T17:15:07.335+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='problem'/><category scheme='http://www.blogger.com/atom/ns#' term='solution'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><category scheme='http://www.blogger.com/atom/ns#' term='Project Euler'/><title type='text'>Project Euler: problem 2</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: Arial, Helvetica, sans-serif; font-size: 16px;"&gt;&lt;i&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;div style="text-align: center;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;i&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;# Inititae a vector x with two values 1 and 2, the starting points for the&lt;/span&gt;&amp;nbsp;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;Fibonacci series&lt;/span&gt;&amp;nbsp;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;x &amp;lt;- c(1,2)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;length(x)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;# Take an object "i", with a starting value of 1.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;# This object will be&amp;nbsp;used to as an index for the vector "x".&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;# We continue to add&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;# the (n - 1)&lt;sup&gt;th&lt;/sup&gt; term&amp;nbsp;&lt;/span&gt;&amp;nbsp;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;and the (n - 2)&lt;sup&gt;th&lt;/sup&gt; term&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;# to get the n&lt;sup&gt;th&lt;/sup&gt; term.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;# This process continues as long as an element of&lt;/span&gt;&amp;nbsp;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;vector x with&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;# index value&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;"i"&lt;/span&gt;&amp;nbsp;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;just crosses the 4,000,000 mark.&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;i &amp;lt;- 1&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;while (x[i] &amp;lt; 4000000){i &amp;lt;- i + 1&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; x.index &amp;lt;- length(x)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&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; x[x.index + 1] &amp;lt;- x[x.index] + x[x.index - 1]}&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;x&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;# Sum the even values of the Fibonacci series thus obtained&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;sum(x[x %% 2 == 0])&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #38761d; font-family: inherit;"&gt;Answer :&amp;nbsp;&lt;span class="Apple-style-span" style="background-color: #f1f2f3;"&gt;4613732&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-4595668682947021240?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/4595668682947021240/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/09/project-euler-problem-2.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/4595668682947021240'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/4595668682947021240'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/09/project-euler-problem-2.html' title='Project Euler: problem 2'/><author><name>MK</name><uri>http://www.blogger.com/profile/03144859563025317904</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-1676067494357977985</id><published>2011-09-15T12:44:00.000+05:30</published><updated>2011-09-15T14:31:19.525+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='problem'/><category scheme='http://www.blogger.com/atom/ns#' term='solution'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><category scheme='http://www.blogger.com/atom/ns#' term='Project Euler'/><title type='text'>Project Euler: problem 1</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;To be fairly honest (assuming there are degrees of honesty), I do know a little about math and programming but I don't know much math or any programming. I've loved math for a long time, but started to learn and understand fairly recently. So during the process of learning and understanding math and a little bit of programming, &lt;a href="http://www.blogger.com/profile/02952702110986035135"&gt;Shreyes&lt;/a&gt;&amp;nbsp;and I thought of sharing our procedures. Most of these procedures aren't the most elegant solutions and at times are plain clumsy and inefficient. We plan to improve our programming skills as we go along.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;We have recently started with &lt;a href="http://projecteuler.net/index.php?section=problems"&gt;Project Euler&lt;/a&gt; problems and will be posting some of the methods that we have used to arrive at a solution for each of the problems. We know only one language, &lt;a href="http://www.r-project.org/"&gt;R&lt;/a&gt;&amp;nbsp;and hence our solutions are written in R.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;So let's start with &lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=1"&gt;problem 1&lt;/a&gt;.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: 'Trebuchet MS', sans-serif;"&gt;If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="background-color: white; font-family: 'Trebuchet MS', sans-serif;"&gt;Find the sum of all the multiples of 3 or 5 below 1000.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;I tried two approaches to solve this problem and have detailed both of them below. By the time I was through with the problem, I found another approach.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;First, let's see the one that I thought was quite efficient.&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;# Approach 1&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;# Create an empty vector of desired length where the results of the division&amp;nbsp;&lt;/span&gt;will be stored&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;x.1 &amp;lt;- rep(NA, 999)&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;#NA is replicated 999 times&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;# For each integer from 1 to 999 (we want numbers below 1000), divide the integer # by either 3 or 5 and take&amp;nbsp;the modulus.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;# If the integer, say "i" is completely divisible by either 3 or 5, assign &amp;nbsp;that value to the ith element of vector x; otherwise assign i'th value of x to equal to&amp;nbsp;zero&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;for (i in 1:999){&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if (i %% 3 == 0 || i %% 5 == 0) {x.1[i] &amp;lt;- i} else {x.1[i] &amp;lt;- 0}&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;}&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;# "%%" is the modulus function, "||" is the symbol for the "or" command, x.1[i] calls the ith element of vector x.1&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;# Take all the non-zero values of x, i.e. those values for which the integer was&amp;nbsp;&lt;/span&gt;perfectly divisible by either 3 or 5 and assign it to a separate vector&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;y.1 &amp;lt;- x.1[x.1 != 0]&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;# This assigns all the nonzero rows of vector x.1 to a vector y.1&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;# Take a summation of these values and this is the desired output.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;sum(y.1)&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;# "sum" finds the sum of all the elements of vector y.1&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #38761d; font-family: 'Courier New', Courier, monospace;"&gt;Answer: 233168&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #38761d; font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;Also, just to make sure that there is no confusion, the ".1" in x.1 and y.1 does not denote anything special, it is merely an assigning convention indicating that the variables were created for the first approach.&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;# Approach 2&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;# Take numbers from 1 to 333 and multiply each by 3 to get multiples of 3.&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;# We only take numbers till 333 because we have to find the sum of multiples of&amp;nbsp;3 (and 5) that are less than 1000.&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;x.2 &amp;lt;- 0&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;for (i in 1:333){x.2[i] &amp;lt;- i*3}&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;head(x.2)&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;# head shows the first few rows of the object&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;# For multiples of 5, we take numbers till 199 and the last multiple of 5 below&amp;nbsp;1000 comes to be 995&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;y.2 &amp;lt;- 0&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;for (j in 1:199){y.2[j] &amp;lt;- j*5}&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;head(y.2)&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;# 15 is a common multiple of 3 and 5, and hence it will get included twice - once&amp;nbsp;when we add the numbers that are multiples of 3 and once when we add numbers&amp;nbsp;that are multiples of 5. So we need to subtract these 15 and its&amp;nbsp;multiples.&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;z.2 &amp;lt;- 0&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;for (k in 1:66){z.2[k] &amp;lt;- k*15}&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;head(z.2)&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;# Final sum&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;sum(x.2) + sum(y.2) - sum(z.2)&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;Answer: 233168&lt;/span&gt;&lt;/div&gt;&lt;div style="font-family: 'Courier New', Courier, monospace; text-align: justify;"&gt;&lt;span class="Apple-style-span" style="color: #38761d;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="text-align: justify;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;There is another approach, which uses the funda of arithmetic progressions. Let's see if you can figure that out.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-1676067494357977985?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/1676067494357977985/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/09/project-euler-problem-1.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/1676067494357977985'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/1676067494357977985'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/09/project-euler-problem-1.html' title='Project Euler: problem 1'/><author><name>MK</name><uri>http://www.blogger.com/profile/03144859563025317904</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5049613389055921243.post-2257764409652263874</id><published>2011-06-30T20:12:00.003+05:30</published><updated>2011-09-22T05:54:34.222+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='problem'/><category scheme='http://www.blogger.com/atom/ns#' term='solution'/><category scheme='http://www.blogger.com/atom/ns#' term='R'/><category scheme='http://www.blogger.com/atom/ns#' term='Project Euler'/><title type='text'>Project Euler: problem 5</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;i&gt;Calculate LCM of 'n' consecutive natural numbers using R&lt;/i&gt;&lt;br /&gt;&lt;i&gt;What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?&lt;/i&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;&lt;br /&gt;Well I shall hit the nail right on the head and not beat around the bush. I am taking programming lessons on R from my pro bro(Utkarsh Upadhyay) who agreed on teaching me only if I would disseminate my learning(a paranoia all the open-source advocates share). Hence I shall populate the web with another link, which might help other dumb programmers like me.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;Question:What is the smallest positive number that is&amp;nbsp;evenly divisible&amp;nbsp;by all of the numbers from 1 to 20?(Which is essentially the LCM of all the numbers from 1 to 20)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;I came across this problem&amp;nbsp;&lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=5"&gt;http://projecteuler.net/index.php?section=problems&amp;amp;id=5&lt;/a&gt;&amp;nbsp;here. You can choose any programming language to solve the problems given. I choose R.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;My first attempt to solve this:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;a &amp;lt;- 20&lt;u1:p&gt;&lt;/u1:p&gt;&amp;nbsp; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;c &amp;lt;- 0&amp;nbsp;&amp;nbsp; &lt;br /&gt;while ( c &amp;lt; 20){&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="font-family: inherit; font-size: 11pt;"&gt;// this loop stops when the value of ‘c’ becomes 20, i.e ‘a’ is divisible by all the numbers from 1 to 20&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c &amp;lt;- 0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n &amp;lt;- 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a &amp;lt;- a + 1&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while ( n &amp;lt; 21 ){ &amp;nbsp;&lt;/span&gt;&amp;nbsp;// check if ‘a’ is divisible by all numbers from 1 to 20&lt;span class="Apple-style-span" style="color: blue;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue;"&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;&amp;nbsp;&amp;nbsp; if (a%%n == 0) c &amp;lt;- c + 1&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue;"&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;&amp;nbsp;&amp;nbsp; n &amp;lt;- n+1&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt; }&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt; print (a)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;&lt;b&gt;Brief note on the program above:&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;What I am essentially doing is checking if the number in the variable ‘a’ is divisible by all the numbers from 1 to 20, if its not then I am incrementing the value of ‘a’ and proceeding again with the loop(first while loop). So I would be checking for all the numbers starting 21 whether they are divisible by all the numbers from 1 to 20 and the program runs till ‘a’ takes the desired value.(which came out to be 232,792,560, after the computation was over).&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;This was a conservative way of getting the job done. It however took &lt;b&gt;9 hours&lt;/b&gt; of computation to blurt out the answer. Hmmm, well I could live with that number but just out of&amp;nbsp;curiosity&amp;nbsp;I asked Utkarsh if there was anything that I was missing. I just wish I were there to see the expression on his face, it would probably have been that of despair, or could also have been a hysterical laughter, I would never get to know that(sigh, Schrodinger's cat) but nevertheless lets focus on the task at hand. The suggestion Utkarsh gave was to use "recursive functions".&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;Revised program using&amp;nbsp;recursive&amp;nbsp;function and pro bro's help:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;We essentially define 2 function and call one in the other. Its easier when you look at the code:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;b&gt;&lt;span style="color: black; font-size: 11pt;"&gt;Defining a function lcm(a,b) and storing the codes in a file "LCM.R":&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-size: 11pt;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;lcm &amp;lt;- function(a, b) {&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;if(a &amp;gt; b) { &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style="font-family: inherit; font-size: 11pt;"&gt; # Swap the numbers to keep the smallest number in ‘a’&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;a &amp;lt;- a + b&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;b &amp;lt;- a - b&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;a &amp;lt;- a - b&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;&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;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;i &amp;lt;- 2&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;comb &amp;lt;- 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;while(i &amp;lt;= a) {&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(a %% i == 0 &amp;amp;&amp;amp; b %% i == 0) { &amp;nbsp; &amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style="font-family: inherit; font-size: 11pt;"&gt; # Accout for all the common factors&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a &amp;lt;- a / i&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; b &amp;lt;- b / i&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style="font-family: inherit; font-size: 11pt;"&gt;# Count common factors only once.&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;comb &amp;lt;- comb * i&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&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;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;else {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: inherit; font-size: 11pt;"&gt; # i is not a common factor, carry on to the next number&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; i &amp;lt;- i + 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;&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;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;return (comb * a * b) &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style="font-family: inherit; font-size: 11pt;"&gt;# For the non common factors, count all of them&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="font-family: inherit; font-size: 11pt;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;}&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;&lt;b&gt;A brief note on the above program:&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;What we have done here is we have defined a function lcm(a,b) as per our convenience and defined it such that it returns the LCM of ‘a’ and ‘b’. The logic used to calculate the LCM is what most of us have already used in class 5&lt;sup&gt;th&lt;/sup&gt;. Identify the common factors(which would be contained in ‘i’) and then to compute the LCM just use “comb &amp;lt;- comb * i&lt;u1:p&gt;&lt;/u1:p&gt;”. Note that whenever I come across a common factor I am dividing both ‘a’ and ‘b’ by ‘i’ thus the values of ‘a’ and ‘b’ left in the end of the loop would be co-prime.(Think about this.!!). Therefor I am returning (comb * a * b), which would return the LCM of ‘a’ and ‘b’.&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;This program would be stored in a file let’s say “LCM.R”. Now whenever I have to refer to this function lcm(a,b) all I need to do is source this file “LCM.R” and I can conveniently use the function lcm(a,b) to get the LCM of ‘a’ and ‘b’. It would be as if the function lcm(a,b) always existed.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;Now I can address a question for the novice programmers. Where do the 'a' and 'b' come from?&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;So whenever I use this self defined function lcm(a,b) I will use it in a program right.? so if I write&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit; font-size: 15px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: inherit; font-size: 15px;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;source('LCM.R')&lt;/span&gt; // this would allow you to use the function you defined in "LCM.R"&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;l &amp;lt;- lcm(6,8) &lt;/span&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;// the value of 'a' would be 6 and 'b' would be 8&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit; font-size: 15px;"&gt;print (l)&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit; font-size: 15px;"&gt;I would get 24.&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;b&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;Defining another function lcm1(list.num) and storing the codes in "LCM2.R"&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;span style="color: black; font-size: 11pt;"&gt;Now we come to the tricky part. What we will do now is use the function lcm(a,b), that we defined, and use it to compute LCM of a list of numbers.&lt;/span&gt;&lt;span style="color: black; font-size: 11pt;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;source('LCM')&lt;/span&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt; //calling the file that stores the function lcm(a,b)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;lcm1 &amp;lt;- function(list.num) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&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; LCM.so.far &amp;lt;- 1&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&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; for(next.number in list.num) {&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&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; &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;LCM.so.far &amp;lt;- lcm(LCM.so.far, next.number) // Here lies the beauty&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&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; &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; &amp;nbsp; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&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; &amp;nbsp;return (LCM.so.far)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="font-family: inherit; font-size: 11pt;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&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; }&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;&lt;b&gt;A brief about the above program:&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;What we have done above is defined another function lcm1(list.num) which will take a list of numbers and blurt out the LCM.(Which is exactly what we want.!!). If we look at the ‘for’ loop defined we are running the loop for all the values in the list of numbers. Now the beautiful logic is in the line&amp;nbsp;“LCM.so.far &amp;lt;- lcm(LCM.so.far, next.number)”, we have cleverly used the function defined earlier lcm(a,b) here. LCM.so.far would keep on updating it self as the loop runs with the next number in the list. Finally this function returns the LCM of the list of numbers that would be stored in ‘LCM.so.far’ at the end of the loop.(Think why.!!)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;Now this function lcm1(list.num) and its definition would be stored in another file say “LCM2.R”. Similar to how we used the file “LCM.R” to use the function lcm(a,b) we can now source “LCM2.R” to use the function lcm1(list.num) that we defined.!!&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;So basically we have 2 function defined in 2 different files. To use the functions wll we need to do is source the files they are stored in.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;b&gt;&lt;span style="color: black; font-size: 11pt;"&gt;Main Program:&lt;/span&gt;&lt;/b&gt;&lt;span style="color: black; font-size: 11pt;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;source('LCM2')&lt;/span&gt;&lt;span style="font-family: inherit; font-size: 11pt;"&gt; // this will call both the files(think why.!!)&lt;span class="Apple-style-span" style="color: blue;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: blue; font-family: inherit; font-size: 11pt;"&gt;l &amp;lt;- lcm1(1:20)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="color: blue; font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="font-family: inherit; font-size: 11pt;"&gt;&lt;span class="Apple-style-span" style="color: blue;"&gt;print (l)&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;Here we have sourced the file “LCM2.R” which would automatically open “LCM.R” too, since “LCM2.R” has to use lcm(a,b) defined in “LCM.R”(hope you catch the drift). Now we stored the LCM of the list of numbers from 1 to 20 (1:20) in ‘l’ and displayed ‘l’.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;and TADA.!!!&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;The approximate computation time is &amp;lt;1 sec. And also we get a flexible functionality to compute the LCM of any consecutive list of natural numbers however long(not literally.!!)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;Even if I consider that the computation took 1 sec, the program that I came up with took 9*60*60= 32,400 secs. Therefore the approximate efficiency enhancement achieved via this transition is&lt;b&gt; 32,39,900% &lt;/b&gt;which is not bad I say..:-)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;Critiques, abuses, banters, blessings are welcome.&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: inherit;"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;  &lt;/span&gt;&lt;br /&gt;&lt;div style="margin-bottom: .0001pt; margin: 0cm;"&gt;&lt;span style="color: black; font-family: inherit; font-size: 11pt;"&gt;P.S: Pardon me for the poor presentation and grammatical errors if any.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5049613389055921243-2257764409652263874?l=programming-r-pro-bro.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programming-r-pro-bro.blogspot.com/feeds/2257764409652263874/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/06/programming-lessons-on-r-by-pro-bro.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/2257764409652263874'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5049613389055921243/posts/default/2257764409652263874'/><link rel='alternate' type='text/html' href='http://programming-r-pro-bro.blogspot.com/2011/06/programming-lessons-on-r-by-pro-bro.html' title='Project Euler: problem 5'/><author><name>Shreyes</name><uri>http://www.blogger.com/profile/02952702110986035135</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='http://4.bp.blogspot.com/-qxonS7T4lDQ/Tnu9rDO6QhI/AAAAAAAAAOM/H6gH-qheti4/s220/DSC09046.JPG'/></author><thr:total>2</thr:total></entry></feed>
