<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Catherine Williams on Medium]]></title>
        <description><![CDATA[Stories by Catherine Williams on Medium]]></description>
        <link>https://medium.com/@cat.williams.crw?source=rss-b8e0c42f44a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*GHazz1hYDJWHlP36.jpg</url>
            <title>Stories by Catherine Williams on Medium</title>
            <link>https://medium.com/@cat.williams.crw?source=rss-b8e0c42f44a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 03:22:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cat.williams.crw/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Create a Correlation Matrix with Too Many Variables in R]]></title>
            <link>https://medium.com/data-science/how-to-create-a-correlation-matrix-with-too-many-variables-309cc0c0a57?source=rss-b8e0c42f44a------2</link>
            <guid isPermaLink="false">https://medium.com/p/309cc0c0a57</guid>
            <category><![CDATA[r-programming]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[variable-selection]]></category>
            <category><![CDATA[correlation]]></category>
            <category><![CDATA[towards-data-science]]></category>
            <dc:creator><![CDATA[Catherine Williams]]></dc:creator>
            <pubDate>Sun, 23 Feb 2020 23:38:18 GMT</pubDate>
            <atom:updated>2020-03-01T05:30:19.565Z</atom:updated>
            <content:encoded><![CDATA[<h4>Efficiently filter out uncorrelated variables to see more relevant results.</h4><p>I used the <a href="https://www.kaggle.com/c/house-prices-advanced-regression-techniques">Kaggle House Prices Dataset</a> which has 79 explanatory variables. In my analysis, I tried to look at correlations between all of the variables and realized there are just too many variables to make sense of any typical visual aid.</p><p>I tried several different packages and tools and decided that I can manipulate <a href="https://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.html">corrplot</a> to do what I want the best. Without any manipulations, this is what the correlation matrix looks like. This is obviously a very unhelpful visualization. There are definitely ways to change this view built into the package, but none of them can really handle this many variables.</p><pre>library(corrplot)</pre><pre>df_cor &lt;- cor(df)<br>corrplot(df_cor)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/433/1*i_BNM9z4eUb0pf0dzhuBAQ.png" /><figcaption>Initial chart from corrplot</figcaption></figure><p>With a lot of trial and error and skimming Stack Overflow, I was able to create a function to make this process much easier. First, it will convert all variables to numeric values (if not already). Then, it will drop duplicates and perfect correlations (correlations with itself). These are obviously not useful.</p><pre>corr_simple &lt;- function(data=df,sig=0.5){<br>  #convert data to numeric in order to run correlations<br>  #convert to factor first to keep the integrity of the data - each value will become a number rather than turn into NA<br>  df_cor &lt;- data %&gt;% mutate_if(is.character, as.factor)<br>  df_cor &lt;- df_cor %&gt;% mutate_if(is.factor, as.numeric)</pre><pre>  #run a correlation and drop the insignificant ones<br>  corr &lt;- cor(df_cor)<br>  #prepare to drop duplicates and correlations of 1     <br>  corr[lower.tri(corr,diag=TRUE)] &lt;- NA <br>  #drop perfect correlations<br>  corr[corr == 1] &lt;- NA </pre><pre>  #turn into a 3-column table<br>  corr &lt;- as.data.frame(as.table(corr))<br>  #remove the NA values from above <br>  corr &lt;- na.omit(corr) </pre><pre>  #select significant values  <br>  corr &lt;- subset(corr, abs(Freq) &gt; sig) <br>  #sort by highest correlation<br>  corr &lt;- corr[order(-abs(corr$Freq)),] </pre><pre>  #print table<br>  print(corr)</pre><pre>  #turn corr back into matrix in order to plot with corrplot<br>  mtx_corr &lt;- reshape2::acast(corr, Var1~Var2, value.var=&quot;Freq&quot;)<br>  <br>  #plot correlations visually<br>  corrplot(mtx_corr, is.corr=FALSE, tl.col=&quot;black&quot;, na.label=&quot; &quot;)<br>}</pre><pre>corr_simple()</pre><p>Next, it will set up the data frame to see the raw correlations in a table since raw numbers can be helpful. The data frame is sorted by the highest correlation first.</p><figure><img alt="Table showing raw numbers for correlations between two variables." src="https://cdn-images-1.medium.com/max/833/1*a1GDj_DwOvUY9gFNNYxOVg.png" /><figcaption>Correlation Table</figcaption></figure><p>In order to reduce the sheer quantity of variables (without having to manually pick and choose), Only variables above a specific significance level threshold are selected. It is set to 0.5 as the initial default.</p><p>After the table is produced, it will return the following, filtered out, correlation matrix chart. Only the correlations with a high enough significance level will have a colored circle. This further helps to cut out the noise if there are still a lot of remaining variables.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/433/1*lOCqw2i558hUZb5qS1yIxA.png" /><figcaption>Filtered down, useful correlation matrix using corr_simple function</figcaption></figure><h4>Conclusion</h4><p>This ‘corr_simple’ function can be run again and again after some feature engineering, or with different significance levels. This really helps for faster analysis and to see only the relevant data. With more variables, it may be necessary to play with different significance levels and/or use more feature engineering to reduce the number of correlated variables, and then re-run the function until results are readable and useful.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=309cc0c0a57" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/how-to-create-a-correlation-matrix-with-too-many-variables-309cc0c0a57">How to Create a Correlation Matrix with Too Many Variables in R</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fastest Way to Install & Load Libraries in R]]></title>
            <link>https://medium.com/data-science/fastest-way-to-install-load-libraries-in-r-f6fd56e3e4c4?source=rss-b8e0c42f44a------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6fd56e3e4c4</guid>
            <category><![CDATA[data-analytics-tools]]></category>
            <category><![CDATA[r]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[package]]></category>
            <category><![CDATA[libraries]]></category>
            <dc:creator><![CDATA[Catherine Williams]]></dc:creator>
            <pubDate>Thu, 29 Aug 2019 01:50:54 GMT</pubDate>
            <atom:updated>2019-08-30T23:38:12.676Z</atom:updated>
            <content:encoded><![CDATA[<p>After working collaboratively with a classmate, it became apparent that I needed a new way of loading libraries from what I was taught in school. Not everyone has the same libraries installed and this can run into errors. I wanted the code to run seamlessly for everyone. Additionally, it is painful to have to write the same functions over and over again to install and load different libraries. My classmate and I worked to find a simple way to do this. I then used a package called <a href="https://cran.r-project.org/web/packages/tictoc/index.html">tictoc</a> to measure the speed of different methods.</p><p>Note: The testing was done with a clean global environment. All of the packages have already been installed and loaded as well so that testing can stay consistent (the packages will just re-load).</p><p>First, the tictoc package needs to be installed and loaded in order to do the analysis. I will also define a variable for the list of packages to be loaded.</p><pre>install.packages(&quot;tictoc&quot;)<br>library(tictoc)</pre><pre>packages &lt;- c(&quot;tidyverse&quot;, &quot;dplyr&quot;, &quot;stringr&quot;, &quot;zoo&quot;, &quot;ROCR&quot;, &quot;caret&quot;, &quot;class&quot;, &quot;gmodels&quot;, &quot;randomForest&quot;)</pre><p><strong>Method 1 — Load and install each library separately:</strong></p><p><em>I commented out install.packages because it will reinstall whether the package exists or not. This is not the most efficient method since you will either reinstall everything or get an error if install.packages is omitted and the package does not exist in the current installation.</em></p><p>Total time: 0.11 sec</p><pre>tic(&quot;basic&quot;)</pre><pre>#install.packages(packages)<br>library(tidyverse)<br>library(dplyr)<br>library(stringr)<br>library(zoo)<br>library(ROCR)<br>library(caret)<br>library(class)<br>library(gmodels)<br>library(randomForest)</pre><pre>toc(log = TRUE)<br>log.txt &lt;- tic.log(format = TRUE)</pre><p><strong>Method 2 — Use a for loop</strong></p><p>Total time: 0.13 sec</p><pre>tic(&quot;for loop&quot;)</pre><pre>libraries &lt;- function(packages){<br>  for(package in packages){<br>    #checks if package is installed<br>    if(!require(package, character.only = TRUE)){<br>      #If package does not exist, then it will install<br>      install.packages(package, dependencies = TRUE)<br>      #Loads package<br>      library(package, character.only = TRUE)<br>    }<br>  }<br>}</pre><pre>libraries(packages)</pre><pre>toc(log = TRUE)<br>log.txt &lt;- tic.log(format = TRUE)</pre><p><strong>Method 3 — Use apply method</strong></p><p>Total time: 0.07 sec</p><pre>tic(&quot;apply&quot;)</pre><pre>ipak &lt;- function(pkg){<br>  new.pkg &lt;- pkg[!(pkg %in% installed.packages()[, &quot;Package&quot;])]<br>  if (length(new.pkg))<br>    install.packages(new.pkg, dependencies = TRUE)<br>  sapply(pkg, require, character.only = TRUE)<br>}</pre><pre>ipak(packages)</pre><pre>toc(log = TRUE)<br>log.txt &lt;- tic.log(format = TRUE)</pre><p><strong>Method 4 — Use the </strong><a href="https://cran.r-project.org/web/packages/install.load/README.html"><strong>install.load</strong></a><strong> package</strong></p><p><em>For the purposes of this post, I calculated the time with the assumption that the user already has install.load installed, since this would only have to happen once. The code to install is just there for reproducibility purposes.</em></p><p>Total time: 0.12 sec</p><pre>tic(&quot;install.load&quot;)</pre><pre>if (!require(&quot;install.load&quot;)) install.packages(&quot;install.load&quot;)<br>library(install.load)</pre><pre>install_load(packages)</pre><pre>toc(log = TRUE)<br>log.txt &lt;- tic.log(format = TRUE)</pre><p><strong>Method 5 — Use the </strong><a href="https://cran.r-project.org/web/packages/pacman/index.html"><strong>pacman</strong></a><strong> package</strong></p><p><em>This was also calculated with the assumption that the user already has pacman installed.</em></p><p>Total time: 0.06 sec</p><pre>tic(&quot;pacman&quot;)</pre><pre>if (!require(&quot;pacman&quot;)) install.packages(&quot;pacman&quot;)<br>#pacman will not accept a character vector so the same packages are repeated<br>pacman::p_load(&quot;tidyverse&quot;, &quot;dplyr&quot;, &quot;stringr&quot;, &quot;zoo&quot;, &quot;ROCR&quot;, &quot;caret&quot;, &quot;class&quot;, &quot;gmodels&quot;, &quot;randomForest&quot;)</pre><pre>toc(log = TRUE)<br>log.txt &lt;- tic.log(format = TRUE)</pre><p><strong>Final Verdict</strong></p><p>The fastest way to install and/or load many packages is to use the pacman package. However, if you did not want to install an extra package, using the apply method will be the best. It is only minutely slower.</p><p>It is important to also mention that with the “basic” test, if the user is unsure of the packages they currently have installed, this method can become drastically slower for the reasons mentioned above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DNlLvQA2qi1K3MKvevcGGA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6fd56e3e4c4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/fastest-way-to-install-load-libraries-in-r-f6fd56e3e4c4">Fastest Way to Install &amp; Load Libraries in R</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>