<?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 Martina Giron on Medium]]></title>
        <description><![CDATA[Stories by Martina Giron on Medium]]></description>
        <link>https://medium.com/@martstats?source=rss-fe61eb5b0652------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*z5hwh4ePC8otWzewo851Kw.jpeg</url>
            <title>Stories by Martina Giron on Medium</title>
            <link>https://medium.com/@martstats?source=rss-fe61eb5b0652------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 12:48:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@martstats/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[Working with Survey Data — Clean and Visualize Likert Scale Questions in R]]></title>
            <link>https://medium.com/data-science/working-with-survey-data-clean-and-visualize-likert-scale-questions-in-r-6a78e3b9c7b2?source=rss-fe61eb5b0652------2</link>
            <guid isPermaLink="false">https://medium.com/p/6a78e3b9c7b2</guid>
            <category><![CDATA[data-cleaning]]></category>
            <category><![CDATA[surveys]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[r]]></category>
            <dc:creator><![CDATA[Martina Giron]]></dc:creator>
            <pubDate>Wed, 17 Aug 2022 17:15:39 GMT</pubDate>
            <atom:updated>2022-08-18T04:47:37.610Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Working with Survey Data — Clean and Visualize Likert Scale Questions in R</strong></h3><h4>How to format your data for quantitative analysis</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JBUNHHY41nn2Elx-" /><figcaption>Photo by <a href="https://unsplash.com/@celpax?utm_source=medium&amp;utm_medium=referral">Celpax</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>If you’ve ever read or written a survey, you’ve probably encountered Likert type questions. These are questions that are answerable on some scale like <em>strongly disagree</em> to <em>strongly agree</em>, or <em>always</em> to <em>never</em>. This scale allows us to quantify sentiments that are otherwise subjective. Today, we’ll discuss how to clean, aggregate, and visualize this type of data.</p><h3><strong>About the dataset</strong></h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a601fb5ad4763058bee47a3001e0e599/href">https://medium.com/media/a601fb5ad4763058bee47a3001e0e599/href</a></iframe><p>We’ll be using data from a product satisfaction survey with 1000 respondents. Our goals are to recode and reshape this data to make them suitable for calculating summary statistics and creating visualizations. You can download the dataset <a href="https://github.com/MartinaGiron/TDS-code/tree/main/working-with-surveys">here</a>.</p><h3><strong>Load the data</strong></h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ebb406dfe1ac3b70d7d6a131df00580/href">https://medium.com/media/4ebb406dfe1ac3b70d7d6a131df00580/href</a></iframe><p>This dataset gives us the applicant ID, as well as their responses to each question encoded in one column each. As you can see, the column heads aren’t proper variable names. This would make it difficult to call them in functions. So let’s use colnames() to rename them by category and item number. Be sure to save the original names to a separate file for future reference. We’ll also need to load the tidyverse package to perform our data wrangling and visualization.</p><h3><strong>Recode the likert responses</strong></h3><p>Our responses are still in text form. We need to recode them with a numerical scale to perform quantitative analysis. We can write a function using case_when() to accomplish this. This can also be done with gsub(), but I prefer this method because it makes our code more compact and readable.</p><p>Sometimes, phrases are stated negatively to ensure that the person answering the survey is not indiscriminately inputting all high or all low-scoring answers. Thus, we would have to:</p><ol><li>Identify which items are scaled positively or negatively.</li></ol><ul><li>In our case, all items are scaled positively except for the following:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/1*U4d9xb5pJsbm2NyWhfZMzg.png" /><figcaption>Negatively Stated Items (Image by Author)</figcaption></figure><p>2. Apply the function as is for items scaled positively, and flip the scale for items scaled negatively.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83a627cb5639380553f50a495cf419fd/href">https://medium.com/media/83a627cb5639380553f50a495cf419fd/href</a></iframe><ul><li>Here, I wrote two separate functions for the positively and negatively scaled items.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0de1fe018521409b0a4527e9131c92f8/href">https://medium.com/media/0de1fe018521409b0a4527e9131c92f8/href</a></iframe><ul><li>To apply the positive one, I removed the id column and all negatively scaled items using select(), so I can easily mutate_all() the positive ones that remained.</li><li>To apply the negative one, I did the opposite and retained only the negatively scaled items before applying the mutate_all() .</li><li>After applying both functions, we must recombine our data using cbind() .</li></ul><h3><strong>Calculate Totals</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/379/1*IiBnyVSuNHW49dAU69ifww.png" /><figcaption>First 6 rows of the overall total and category totals per respondent (Image by Author)</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/946b4a2494c67390fcd6c31662bc7f4f/href">https://medium.com/media/946b4a2494c67390fcd6c31662bc7f4f/href</a></iframe><ul><li>Since this survey measures overall product satisfaction<strong>, </strong>we want to know the total scores of each respondent for all items. We can do this with the rowSums() function on the recoded data.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c937dcaaaeecc04c87775c70318c519d/href">https://medium.com/media/c937dcaaaeecc04c87775c70318c519d/href</a></iframe><ul><li>Now, let’s get the total scores for each category. We can use mutate() to create new columns with the category totals. The rowwise() function allows us to perform operations across columns. Remember to use ungroup() after performing all operations to revert your data into <em>tidy </em>format.</li><li>As an exercise, you can try calculating the category means yourself!</li></ul><h3><strong>Reshape the data</strong></h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01a6017dc61d9290987a2b670ec94a1a/href">https://medium.com/media/01a6017dc61d9290987a2b670ec94a1a/href</a></iframe><p>We want our data to be in <em>tidy </em>format to analyze and visualize it. In this case, it means pivoting our data so that our new columns will contain just the ID number, question number, category, and response. First, we’ll pivot on question number and response. Then, we can assign their categories with mutate(). We’ll use str_match() together with <em>regular expressions</em>, or <em>regex</em>, to get the category and question number from the question column. Here, “^[^_]+(?=_)” means<em> get all characters before the underscore</em>, and “[0–9]$” means <em>get the last digit of the string</em>.</p><h3><strong>Aggregate the data</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/434/1*nCOv-ZGbKga2mlFVapfSUQ.png" /><figcaption>Average and standard deviation of responses per category (Image by Author)</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b106f10ee3904f08c53c1640ff476b09/href">https://medium.com/media/b106f10ee3904f08c53c1640ff476b09/href</a></iframe><p>We can now calculate summary statistics for our data. Since we want to calculate statistics by category, we’ll group by this variable. Then, we’ll pass our data into the summarise() function. Here is our expected output:</p><h3><strong>Visualize the data</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/710/1*2pr_s_Yjj89cSzOvdLXq8Q.png" /><figcaption>Histogram of Total Scores (Image by Author)</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7cd7f5bcc614b2cb5af39f223dee63c8/href">https://medium.com/media/7cd7f5bcc614b2cb5af39f223dee63c8/href</a></iframe><p>First, let’s get an overview of the distribution of the total score. We used the survey_recoded data because this dataset contains the total score per respondent. Notice that the data appears to approximately follow a normal distribution. This is expected, because of our large sample size of 1000.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/710/1*x-c3KpiSOLvdqE58OOV9gQ.png" /><figcaption>Histograms of Each Category’s Scores (Image by Author)</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5cfaec7d95538d5ffa1a5c27b44baf8f/href">https://medium.com/media/5cfaec7d95538d5ffa1a5c27b44baf8f/href</a></iframe><p>Now, let’s look at the category-level distributions. We’ll use the survey_long for our data, and pass category to facet_wrap() to do this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/710/1*_trCXGFlubaTS68RuMsiJg.png" /><figcaption>Histogram of each Question’s Scores (Image by Author)</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8cd3abc6b544a8930f8bbb7bbcb065c3/href">https://medium.com/media/8cd3abc6b544a8930f8bbb7bbcb065c3/href</a></iframe><p>We can also look at the question-level distributions through the same method. Here, I added an extra argument nrow = 3, so that all questions from the same category appear in one row.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/710/1*NT4Jxr3BNrIJKuDeWo15RQ.png" /><figcaption>Average Score fo Each Question (Image by Author)</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6416ac7e83c50ecc5958135fcf48d9d9/href">https://medium.com/media/6416ac7e83c50ecc5958135fcf48d9d9/href</a></iframe><p>Lastly, let’s look at the average response level per question. We divided the variable response by 1000 because without it, our graph would just give us the sum of the scores per question. We also added ylim(0,5), since otherwise, our graph would only have 0 to 4 on its y-axis. This is because the maximum average scores for the questions are never higher than 4.</p><h3><strong>Next Steps</strong></h3><p>This tutorial focused on preparing survey data for exploratory analyses. The bar graphs we created were insightful, but we can work on their aesthetics to make them more professional and attention-grabbing. Read my tutorial on how you can do this:</p><p><a href="https://towardsdatascience.com/customize-your-ggplot2-bar-graph-5-ways-to-instantly-improve-your-r-data-visualizations-f9c11dfe0163">Customize Your ggplot2 Bar Graph — 5 ways to instantly improve your R data visualizations</a></p><p>For a deeper analysis, we can also perform statistical tests to test for correlations and measure the survey’s reliability. You can read more about these concepts and try applying them with R’s cor.test() function and cronbach.alpha() from the ltm package.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6a78e3b9c7b2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/working-with-survey-data-clean-and-visualize-likert-scale-questions-in-r-6a78e3b9c7b2">Working with Survey Data — Clean and Visualize Likert Scale Questions 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[Customize Your ggplot2 Bar Graph — 5 ways to instantly improve your R data visualizations]]></title>
            <link>https://medium.com/data-science/customize-your-ggplot2-bar-graph-5-ways-to-instantly-improve-your-r-data-visualizations-f9c11dfe0163?source=rss-fe61eb5b0652------2</link>
            <guid isPermaLink="false">https://medium.com/p/f9c11dfe0163</guid>
            <category><![CDATA[data-analytics]]></category>
            <category><![CDATA[r]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[ggplot2]]></category>
            <dc:creator><![CDATA[Martina Giron]]></dc:creator>
            <pubDate>Tue, 09 Aug 2022 16:50:49 GMT</pubDate>
            <atom:updated>2022-08-09T16:50:49.150Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Customize Your ggplot2 Bar Graph — 5 ways to instantly improve your R data visualizations</strong></h3><h4>How to grab your reader’s attention and make professional-looking graphs</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SFTQa2FI8QGep1CrY07T8g.jpeg" /><figcaption>Image by Author</figcaption></figure><p>Bar graphs are the bread and butter of data visualization. They are useful for comparing values of categorical data.</p><ul><li>In tidyverse, you can construct the bar graph below with only two lines of code.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/775/1*k_KWiJCowiElXdTH0vCbng.jpeg" /><figcaption>Image by Author</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5756711c34db5e90156d696fdc2ba619/href">https://medium.com/media/5756711c34db5e90156d696fdc2ba619/href</a></iframe><p>Despite its simple appearance, this graph tells you everything you need to know about the data. If you’re just doing an exploratory data analysis, its modest appearance may be sufficient. However, if you plan to communicate your data through a report or presentation, it may not be enough to draw your reader’s attention. Customizing your graph’s aesthetics can improve its readability, draw emphasis to interesting trends, and help your visualization look more professional.</p><p><strong>Here are 5 quick and easy ways to improve your graphs.</strong></p><p>For this tutorial, we’ll be using the same data we used to generate the graph above. It contains the profits for each computer accessory sold in a hypothetical store. You can generate the dataset with this code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a7f6d96ee816d65b27f01fb37a149710/href">https://medium.com/media/a7f6d96ee816d65b27f01fb37a149710/href</a></iframe><ol><li><strong>Reorder factors</strong></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/775/1*u41_6tkwoF0DNGTuu6Av0Q.jpeg" /><figcaption>Image by Author</figcaption></figure><ul><li>Draw attention to outstanding data points by reordering the bars in descending order.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/befbe2259e86fe2ce0b90f8f6d1268b2/href">https://medium.com/media/befbe2259e86fe2ce0b90f8f6d1268b2/href</a></iframe><ul><li>For other datasets, it also may be useful to group the data by geographical location or order them alphabetically. Master fct_reorder by reading its <a href="https://forcats.tidyverse.org/reference/fct_reorder.html">documentation</a>, and you can do these as well.</li></ul><p><strong>2. Add labels</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/775/1*-_RGIyctG4ccOGdsWrLPwA.jpeg" /><figcaption>Image by Author</figcaption></figure><ul><li>Edit your title, x-, and y- axes labels to give your reader context and ensure variables are easily understood.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a246d753d7770660af17f163f6ff795b/href">https://medium.com/media/a246d753d7770660af17f163f6ff795b/href</a></iframe><p><strong>3. Reposition the tick labels</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/874/1*yUK0PTFaDNstI04r5nP7sQ.jpeg" /><figcaption>Image by Author</figcaption></figure><ul><li>Tick labels get harder to read as they increase in number and length. Overlapping and alignment issues can make them incomprehensible. To solve this problem you can rotate them slightly.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6256373a8134c233791ca0a2e7d7a1e/href">https://medium.com/media/f6256373a8134c233791ca0a2e7d7a1e/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/874/1*95zLHgSW9XG05GWu4YjRvg.jpeg" /><figcaption>Image by Author</figcaption></figure><ul><li>Or flip your coordinate axes.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e3f42943d88c89e2ce82414b398fd76f/href">https://medium.com/media/e3f42943d88c89e2ce82414b398fd76f/href</a></iframe><p><strong>4. Reclassify less significant categories as “others”</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/775/1*JSB2udfWtzzC47JRe_qI7Q.jpeg" /><figcaption>Image by Author</figcaption></figure><ul><li>Emphasize the extremity of top data points by regrouping the lowest ones together. You can do this by creating a new column with mutate(). Here, we used ifelse to reclassify all items with profits less than 3000 USD to others .</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6bd722822c210d91abfecdf7c6207b4/href">https://medium.com/media/b6bd722822c210d91abfecdf7c6207b4/href</a></iframe><p><strong>5. Customize your color palette</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/874/1*Fv7ZXfAdYRc0fz9veJQtmA.jpeg" /><figcaption>Image by Author</figcaption></figure><ul><li>R has a variety of in-built color palettes you can choose from. Consider colorblind-friendly palettes. The package viridis is a good place to start.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d5db6ab596c6c882690ade90247eaa0/href">https://medium.com/media/8d5db6ab596c6c882690ade90247eaa0/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/874/1*v70QlpUHKVH-kOBNqMtJ3A.jpeg" /><figcaption>Image by Author</figcaption></figure><ul><li>You may want to use your company’s color palette when making visualizations for work. Since profit is continuous data, I used scale_fill_gradient. You can specify the end colors of your gradient with high and low.</li><li>R also has custom themes you can use to instantly change your graph’s layout and background colors. I used theme_minimal() here to get a nice and clean look.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d399c52102bb7107a7ce731859a0bada/href">https://medium.com/media/d399c52102bb7107a7ce731859a0bada/href</a></iframe><p><strong>Next Steps</strong></p><p>A while ago, we introduced theme_minimal, one of R’s many theme presets that you can apply to instantly beautify your graph. Experiment with other default themes <a href="https://ggplot2.tidyverse.org/reference/ggtheme.html">here</a> and consider exploring <a href="https://www.rdocumentation.org/packages/ggplot2/versions/3.3.6/topics/theme">custom themes</a> to apply your own branding.</p><p>Improving your visualizations is a task worth investing your time in. In my experience, sharing advanced animated visualizations like the one below helped me build a following on Tech Twitter. Take a deeper dive into the libraries and functions we learned earlier to bring your graphs to the next level.</p><h3>Martina Giron on Twitter: &quot;#PHInflation | It can be hard to grasp what an increase in #inflation can mean in our daily life as Filipinos. So this week, I decided to visualize the increase in prices of meat (and fish) in NCR, Philippines since 2008.#rstats #dataviz #datascience #data pic.twitter.com/p0XCEoV2Wm / Twitter&quot;</h3><p>PHInflation | It can be hard to grasp what an increase in #inflation can mean in our daily life as Filipinos. So this week, I decided to visualize the increase in prices of meat (and fish) in NCR, Philippines since 2008.#rstats #dataviz #datascience #data pic.twitter.com/p0XCEoV2Wm</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f9c11dfe0163" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/customize-your-ggplot2-bar-graph-5-ways-to-instantly-improve-your-r-data-visualizations-f9c11dfe0163">Customize Your ggplot2 Bar Graph — 5 ways to instantly improve your R data visualizations</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[How to Create a Custom Dataset in R]]></title>
            <link>https://medium.com/data-science/how-to-create-a-custom-dataset-in-r-cf045e286656?source=rss-fe61eb5b0652------2</link>
            <guid isPermaLink="false">https://medium.com/p/cf045e286656</guid>
            <category><![CDATA[r]]></category>
            <category><![CDATA[dataset]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[tidyverse]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Martina Giron]]></dc:creator>
            <pubDate>Sat, 30 Jul 2022 03:43:01 GMT</pubDate>
            <atom:updated>2022-08-04T14:51:27.838Z</atom:updated>
            <content:encoded><![CDATA[<h4>Make your own synthetic dataset to analyze for your portfolio</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MhyeclCkPKG3M5F0" /><figcaption>Photo by <a href="https://unsplash.com/@homajob?utm_source=medium&amp;utm_medium=referral">Scott Graham</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In your data science journey, you might have come across <em>synthetic datasets</em>, sometimes called <em>toy </em>or<em> dummy datasets. </em>These are useful for practicing data wrangling, visualizing, and modelling techniques. Compared to real world data, they are often clean and easy to understand. This makes working with synthetic data an appealing idea for beginners. Today, I’ll walk you through how you can make your own synthetic data. After reading this article, you’ll be able to create your own big data, with as many variables as you like.</p><p>This tutorial will tackle how I created this Human Resources dataset. It contains how well 300 data scientists performed in a hypothetical company’s hiring exam, and their performance evaluation scores after working in the company for 1 year.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a67dff0294902a05e6afeccee570c6e/href">https://medium.com/media/8a67dff0294902a05e6afeccee570c6e/href</a></iframe><h3><strong>Why create your own dataset?</strong></h3><p>Aside from being ready to analyze, synthetic datasets offer additional advantages over real world data. Here are some scenarios to illustrate:</p><p><strong>The data you are looking for is highly niche, specific, or confidential</strong></p><p>At first, I tried to find datasets for my project in mind. After a couple hours of searching, I was empty handed. I realized that there may simply not be one online because companies don’t go around publishing their applicants’ data.</p><p><strong>You want to create your own tutorial</strong></p><p>Let’s say you want to write a tutorial on performing a linear regression (like I will!). You want a guarantee that some predictors will be linearly related to your independent variable, and some will not. If you looked for your own dataset, you would have to test this for each of them. On the other hand, if you created your own, you can design it such that you are guaranteed that this assumption will hold.</p><p><strong>You want to own all the rights to your data</strong></p><p>No more worrying about copyright, data privacy, or terms of use, since the data is completely yours.</p><p><strong>Your goal is to practice your data visualization and communication skills</strong></p><p>In my case, my purpose is to showcase my knowledge in HR analytics. Specifically, I want to show my knowledge in performing statistical tests and communicating the results to business managers.</p><h3>What you’ll need</h3><ul><li>An idea what your dataset will describe</li><li>Knowledge on the nature of the data</li><li>Some knowledge on common probability distributions</li><li>Working knowledge of R</li></ul><h3>Making the Dataset</h3><p>R has several functions that allow you to instantly generate random data. The following steps will guide you through choosing the right functions, organizing their outputs, and exporting your finished dataset.</p><h4>Step 1: List down all variables you want to include</h4><figure><img alt="Table of variables to be generated for synthetic Human Resources Dataset. Includes the following variables: Applicant ID, management experience, situational judgment test score, coding exam scores for data cleaning, data visualization, and machine learning, interview scores from three panelists, number of absences, and performance evaluation scores" src="https://cdn-images-1.medium.com/max/838/1*chlkJopG2I9ve59ulsYHiw.png" /></figure><p>Note down how many units or rows of data you want. For this project, I want a total of 320 applicants/rows of data.</p><h4>Step 2: Describe the requirements of each variable</h4><figure><img alt="Table of data requirements for each variable. Specifies minimum and maximum scores, lists of categories, and correlated variables." src="https://cdn-images-1.medium.com/max/694/1*AjN3NKf2RtEtg5D_rmlg8Q.png" /></figure><p>This is where context and your field experience will come in. For example, interview scores may lie between 0 and 10. But as a recruiter, you know that scores from 0 to 3 are unrealistic, so you set 4 and 10 as the minimum and maximum for this variable. Meanwhile, for scores on a machine learning exam, the realistic minimum and maximum scores are 40 and 100. However, you also know that the average score is 80, most applicants have scores near that, so you also want your data to reflect that trend. If you want some variables to be correlated with one another, indicate which variables as well.</p><h4>Step 3: Determine an appropriate distribution for your variables</h4><figure><img alt="Table of probability distributions for each variable" src="https://cdn-images-1.medium.com/max/694/1*jIbMDdgs8DRN1LtAlAT5PQ.png" /></figure><p>This is the trickiest part because it’s where you will be using your knowledge on common probability distributions. If you’re just using your dataset to practice wrangling or visualization, you could probably get away with only knowing the uniform and normal distribution. In my case though, I want my data to have all sorts of relationships and peculiarities, so I use a whole bunch.</p><p>I used a discrete uniform distribution for interview scores because I want the probability of an applicant getting a 4 to be equal to getting a 4.1, or 4.2, all the way to 10. Meanwhile, I used a multivariate normal distribution for my coding exam scores and performance scores because I wanted them all to be normally distributed and correlated to one another with different strengths.</p><p>Take note that each distribution is characterized by its own set of parameters, and that’s where our requirements from step 2 will come in. You can read more about common probability distributions <a href="https://en.wikipedia.org/wiki/List_of_probability_distributions">here</a>.</p><h4>Step 4: Writing the Code</h4><p>Of course, this part is easier said than done. So let’s work at it one variable (or group of variables) at a time.</p><p><strong>Packages</strong></p><pre>library(MASS)<br>library(tidyverse)<br>library(writexl)</pre><p>First, let’s load all the packages we’ll need. Tidyverse for data manipulation, MASS for generating our multivariate normal distribution, writexl to save our work as an excel file.</p><p><strong>Applicant ID Number</strong></p><pre>id &lt;- paste0(&quot;Applicant #&quot;, 1:320)</pre><p>The simplest way to generate unique ID numbers is to create a sequence of whole numbers. Here, we also appended “Applicant #” with paste0().</p><p><strong>Management</strong></p><pre># Assign Management Experience Randomly<br>set.seed(0717)<br>management_random &lt;- rbinom(320, 1, 0.5)<br>management &lt;- ifelse(management_random == 0, &quot;manager&quot;, &quot;non-manager&quot;)</pre><p>One way to randomly generate categorical variables is to first draw a random set of numbers, then assign a category to each number. Here, we randomly selected 0’s and 1’s using rbinom, then assigned Male to 0 and Female to 1</p><p><strong>Situational Judgment Test Scores, Number of Absences, and Data Visualization Exam Scores</strong></p><pre># Situational Judgment Test Scores<br>set.seed(0717)<br>situational &lt;- round(runif(320, 60, 100))</pre><pre># Number of Absences<br>set.seed(0717)<br>absences &lt;- rpois(320, lambda = 1)</pre><pre># Data Viz Exam Scores<br>set.seed(0717)<br>coding_viz &lt;- rnorm(320, 85, 5) %&gt;%<br>  cap_score(., 100) %&gt;% <br>  round(2)</pre><p>Since we already specified the distribution and parameters we want to use for these variables, all we need to do now is plug them into their respective functions runif for the uniform distribution, rpois for poisson, and rnorm for the univariate normal. We’ll also use that function we created earlier, cap_score, to impose a maximum score of 100 on the Data Visualization Exam Scores.</p><p><strong>Correlated Variables</strong></p><pre>cor_var_means &lt;- c(6.8, 7.2, 8.4, 77, 84, 80)<br>cor_var_matrix &lt;- matrix(<br>  c(<br>    0.87, 0.6, 0.7, 0.36, 1.55, 0.57,<br>    0.6, 1.2, 0.52, 0.5, 1.2, 2.34,<br>    0.7, 0.52, 0.68, 0.45, 0.89, 0.75,<br>    0.36, 0.5, 0.45, 15.2, 1.09, 1.64,<br>    1.55, 1.2, 0.89, 1.09, 17.2, 1.88,<br>    0.57, 2.34, 0.75, 1.64, 1.88, 9.3<br>  ), byrow = T, nrow = 6<br>)</pre><pre>set.seed(0717)<br>correlated_vars_df &lt;- as.data.frame(mvrnorm(n = 320, mu = cor_var_means, Sigma = cor_var_matrix))</pre><p>We will need a vector of means and a variance-covariance matrix to generate our multivariate normal distribution. We have already decided our means from earlier steps, but now, we have to calculate the covariances with the following formula. You might find it easier to do this step in Excel.</p><pre>correlated_vars_df_cols &lt;- c(&quot;interview_p1&quot;, &quot;interview_p2&quot;, &quot;interview_p3&quot;, &quot;coding_cleaning&quot;, &quot;coding_ml&quot;, &quot;performance&quot;)<br>colnames(correlated_vars_df) &lt;- correlated_vars_df_cols</pre><pre>correlated_vars_df &lt;- correlated_vars_df %&gt;%<br>  mutate(interview_p1 = round(cap_score(interview_p1, 10), 1),<br>         interview_p2 = round(cap_score(interview_p2, 10), 1),<br>         interview_p3 = round(cap_score(interview_p3, 10), 1),<br>         coding_cleaning = round(cap_score(coding_cleaning, 100), 2),<br>         coding_ml = round(cap_score(coding_ml, 100), 2),<br>         performance = round(cap_score(performance, 100))<br>         )</pre><p>Now, we have our randomly generated interview, coding exam, and performance scores. Let’s clean it up a little. First, we assigned them all column names. Next, let’s round off interview scores off to one decimal place and place a maximum score of 10. I created my own function cap_score, to do this. It takes the column and the maximum score as its arguments. For the coding exam scores, 2 decimal places and a maximum of 100. For performance, we’ll make them whole numbers with a maximum of 100 as well.</p><h4>Step 5: Gather and Save Your Data</h4><pre>applicant_scores &lt;- cbind(<br>  id, management, situational, coding_viz, correlated_vars_df, absences)</pre><pre>applicant_final &lt;- applicant_scores[1:300, ]</pre><pre>write_xlsx(applicant_final, &quot;Employee Selection Scores.xlsx&quot;)</pre><p>We can easily gather the variables and datasets we generated using cbind. I’m saving only the first 300 rows of data, since I plan to use this as a training set. When you’re ready, you can save the final dataset as your desired format. I chose the Excel .xlsx</p><h4>(optional) Step 6: Share it!</h4><p>You created this data set probably because you had no access to any alternatives. That means your fellow data analyst in training probably don’t either. Give back to the community by sharing! This is also an opportunity to showcase your R skills and domain knowledge.</p><p>You can access my dataset here.</p><p><a href="https://github.com/MartinaGiron/R-Portfolio/tree/main/Employee-Selection">R-Portfolio/Employee-Selection at main · MartinaGiron/R-Portfolio</a></p><h3>Some final words</h3><p>Today, we tackled how to create your own dataset with 320 data points and variables following the normal, poisson, and discrete uniform probability distributions. You can now analyze this data and add this to your portfolio. Next time, try adding more data points (you can have hundreds of thousands, or even millions of rows!). You can also experiment by having your variables follow different distributions, such as the beta or geometric distributions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cf045e286656" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/how-to-create-a-custom-dataset-in-r-cf045e286656">How to Create a Custom Dataset 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>