<?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 Mohsin Ramay on Medium]]></title>
        <description><![CDATA[Stories by Mohsin Ramay on Medium]]></description>
        <link>https://medium.com/@mohsinramay?source=rss-21e59c6e1c3b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*L-XLNesjFcRD1IvM_p8qEQ.jpeg</url>
            <title>Stories by Mohsin Ramay on Medium</title>
            <link>https://medium.com/@mohsinramay?source=rss-21e59c6e1c3b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 19:40:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mohsinramay/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[Navigating the Transition from Academia to Industry: A Data Scientist’s Reflections]]></title>
            <link>https://mohsinramay.medium.com/navigating-the-transition-from-academia-to-industry-a-data-scientists-reflections-7668c395923c?source=rss-21e59c6e1c3b------2</link>
            <guid isPermaLink="false">https://medium.com/p/7668c395923c</guid>
            <category><![CDATA[industry]]></category>
            <category><![CDATA[corporate-culture]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[experience]]></category>
            <category><![CDATA[skills]]></category>
            <dc:creator><![CDATA[Mohsin Ramay]]></dc:creator>
            <pubDate>Mon, 15 Jan 2024 15:25:29 GMT</pubDate>
            <atom:updated>2024-01-15T15:25:29.188Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Adapting to the Corporate Pace After a Decade in Academia</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LTJ-3x7TgNQN51Xz" /><figcaption>Photo by <a href="https://unsplash.com/@lukechesser?utm_source=medium&amp;utm_medium=referral">Luke Chesser</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>With a robust academic foundation and extensive research tenure across the United States, Pakistan, and England, my venture into the industry was not just a career shift; it was a strategic realignment toward stability and growth. My decision was underpinned by the burgeoning competition in academia, a saturated job market for graduates, the relentless pressure of publication, and the pursuit of grant funding. Moreover, the promise of a more substantial remuneration package was an undeniable allure, particularly in this epoch.</p><p>The industry’s rhythm is unrelentingly brisk — a contrast to the measured cadence of academic research, particularly in the agricultural domain where the pace is dictated by the gradual nature of experiments.</p><p>In early 2023, I embarked on my corporate journey at FMC Corporation as a Data Scientist. My doctoral studies equipped me with a robust statistical foundation and a secondary specialization in Geographic Information Systems (GIS) and remote sensing, augmenting my skill set to an advanced level. Proficiencies in programming languages like R and Python were honed and further refined through freelance opportunities and fellowships at the University of Exeter and the Alan Turing Institute in London.</p><p>The inaugural year in the fast-paced corporate environment was both exhilarating and enlightening, replete with invaluable insights:</p><ul><li><strong>Practicality over Perfection:</strong> Success in the industry doesn’t necessitate one to be exceptionally ingenious; it demands the completion of tasks. While perfection is a noble pursuit, pragmatism is more valued.</li><li><strong>Navigating Corporate Dynamics:</strong> Politics permeate every professional sphere, yet the ability to collaborate and provide support within a team is paramount.</li><li><strong>Broadening Technical Expertise:</strong> A grasp of R and Python is fundamental, but proficiency in SQL, cloud computing, workflow construction, Docker, and the creation of scalable pipelines is indispensable.</li><li><strong>Mastery of Time:</strong> The industrial landscape is characterized by stringent deadlines. Effective time management, coupled with expertise, is crucial.</li><li><strong>Cross-Cultural Collaboration:</strong> Be equipped for early morning interactions with global teams across time zones, bringing an attentive and engaged mindset to each meeting.</li><li><strong>Strategic Communication: </strong>Regular interaction with colleagues and understanding organizational structure is vital. Comprehending how teams interlink and rely on one another facilitates smoother operations and more cohesive work.</li><li><strong>Embrace Continuous Learning:</strong> The industry demands adaptability. Being open to learning and proactively seeking assistance when necessary is encouraged. Transparency regarding obstacles is welcomed, fostering a supportive team environment.</li><li><strong>Ownership and Accountability: </strong>Taking charge of projects with a sense of personal investment leads to recognition and respect within the company.</li><li><strong>Proactivity and Planning: </strong>Anticipating challenges and addressing them promptly is essential. In industry, timely completion is not just an expectation but a mandate.</li><li><strong>Continuous Skill Development:</strong> Leveraging one’s entire skill set, including my own in Statistics, Agriculture, Programming, GIS, and Remote Sensing, to deliver impressive results can earn the admiration of your peers and superiors.</li><li><strong>Workplace Equilibrium: </strong>Stress is inevitable, but it should not overshadow the work. Timely completion, seeking and offering help, and finding joy in your role are the essence of a fulfilling career in the industry.</li></ul><p>As a seasoned academic turned data scientist, these learnings have not only shaped my professional philosophy but also underscored the importance of agility and resilience in a corporate setting. The journey is challenging yet rewarding, proving that the industry can indeed be a landscape of opportunity for those who adapt and thrive.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7668c395923c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Analyzing and Visualizing Geospatial Data with H3 Hexagons]]></title>
            <link>https://mohsinramay.medium.com/analyzing-and-visualizing-geospatial-data-with-h3-hexagons-2e7bf048194?source=rss-21e59c6e1c3b------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e7bf048194</guid>
            <category><![CDATA[h3]]></category>
            <category><![CDATA[mapping]]></category>
            <category><![CDATA[geospatial]]></category>
            <category><![CDATA[geospatial-data]]></category>
            <category><![CDATA[ggplot2]]></category>
            <dc:creator><![CDATA[Mohsin Ramay]]></dc:creator>
            <pubDate>Sun, 27 Aug 2023 22:04:42 GMT</pubDate>
            <atom:updated>2023-08-27T22:04:42.706Z</atom:updated>
            <content:encoded><![CDATA[<p>Geospatial data analysis and visualization have never been easier thanks to the use of H3 hexagons. H3 data offers more detail, consistency across space, and global coverage, making spatial analysis and visualization easier than ever before.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/577/1*Ou4zJP9sfqz2vDQmFzXe3Q.png" /><figcaption>H3 hexagons covering the globe at different resolutions.</figcaption></figure><p>To illustrate the power of H3 data, let’s take a look at the Agricultural Production Area Map of the USA. Comparing it at the county level versus the H3 resolution level 5, we can see that the H3 data offers a higher level of spatial detail, revealing distinct trends that were not visible before.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dH2fsvvGUv8tzQ7vQPgUNg.jpeg" /><figcaption>The area under Agricultural Production at the county level in the USA.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*geYJdPKJHdjztiyxfsxCUA.png" /><figcaption>The area under Agricultural Production at the H3 resolution level 5 in the USA.</figcaption></figure><p>Another example of the power of H3 data is the bivariate map that shows the intricate relationship between the major rivers and the heartland of American agriculture. The level of detail provided by H3 data is simply astounding.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RiUPTyyIOS-P7vn4ciq-_Q.png" /><figcaption>Bivariate map depicting the dynamic bond between Major Rivers and the heartland of American agriculture at the county level.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dCP2wQvUiNTEahdesWbemQ.png" /><figcaption>Bivariate map depicting the dynamic bond between Major Rivers and the heartland of American agriculture at the H3 resolution level 5.</figcaption></figure><p>H3 hexagons are revolutionizing the way we analyze and visualize geospatial data. They offer a level of detail and consistency that was previously impossible, making it easier than ever before to uncover trends and patterns in our data. Whether you’re a geospatial analyst or a data scientist, H3 hexagons are a powerful tool that should not be overlooked.</p><p>If you’re looking to take your geospatial analysis and visualization to the next level, consider incorporating H3 hexagons into your workflow. With their global coverage, consistent resolution, and unparalleled detail, H3 hexagons are sure to be a game-changer for anyone working with geospatial data.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e7bf048194" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exploring Global Aridity Changes: A Data Science Adventure | DataWim]]></title>
            <link>https://mohsinramay.medium.com/exploring-global-aridity-changes-a-data-science-adventure-datawim-50aec625a6f0?source=rss-21e59c6e1c3b------2</link>
            <guid isPermaLink="false">https://medium.com/p/50aec625a6f0</guid>
            <category><![CDATA[climate-change]]></category>
            <category><![CDATA[remote-sensing]]></category>
            <category><![CDATA[drought]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[global-scale-analysis]]></category>
            <dc:creator><![CDATA[Mohsin Ramay]]></dc:creator>
            <pubDate>Tue, 15 Aug 2023 00:00:35 GMT</pubDate>
            <atom:updated>2023-08-16T02:33:25.717Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iUDPjF3ZJQTu3GJOWiadGg.gif" /></figure><p>Have you ever wondered how our planet’s climate is changing over time? The impacts of climate change are felt in various ways, and one important aspect is aridity. Aridity, which refers to the dryness of an environment, has significant implications for ecosystems, water availability, and biodiversity. In this data science blog, we will embark on a journey to visualize and understand changes in aridity using R and a fascinating dataset that spans from 1950 to 2100.</p><h3>Unveiling the Data</h3><p>Our adventure begins with data acquisition. We’ve sourced a comprehensive dataset of global bioclimatic indicators from climate projections. These indicators provide us with valuable insights into various climatic factors, including aridity. You can access the Copernicus Climate Change Service (C3S) dataset at the following link: <a href="https://cds.climate.copernicus.eu/cdsapp#!/dataset/sis-biodiversity-cmip5-global?tab=overview">Global Bioclimatic Indicators</a>.</p><p>The dataset contains aridity data for different years, allowing us to explore how aridity has evolved over time. We’ll use the powerful capabilities of R and several libraries, including tidyverse, sf, terra and gganimate, to analyze and visualize this dataset.</p><h3>Loading and Preparing the Data</h3><p>Let’s start by loading the necessary libraries and importing the aridity raster data. We’ll also perform some initial exploratory data analysis (EDA) to better understand the data structure and characteristics. The code snippet below demonstrates this process:</p><pre># Load libraries <br>library(tidyverse) <br>library(sf) <br>library(gganimate) <br>library(raster) <br>library(terra) <br>library(RColorBrewer) <br><br># Load the aridity raster data<br>aridity_rcp85 &lt;- rast(&quot;dataset-sis-biodiversity-cmip5-global-8a76744d-4fd0-407f-b272-1cfcc91c4aef/aridity_annual-mean_gfdl-esm2m_rcp85_r1i1p1_1950-2100_v1.0.nc&quot;)<br><br># Rename raster layers using their corresponding time stamps<br>names(aridity_rcp85) &lt;- time(aridity_rcp85)<br><br># EDA: Basic visualization<br>layer_number &lt;- 75<br>my_palette &lt;- colorRampPalette(brewer.pal(11, &quot;RdYlBu&quot;))<br><br># Plot the raster layer using a custom palette<br>plot(aridity_rcp85[[layer_number]], <br>     col = rev(my_palette(100)),<br>     main = paste(&quot;Aridity - Layer&quot;, layer_number),<br>     legend.args = list(text = &quot;Aridity Index&quot;, <br>                        side = 1, <br>                        font = 2, <br>                        line = 2.5, <br>                        cex = 0.8))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Yk4sKNcgMB8HN43l.png" /></figure><h3>Animated Insights</h3><p>Animation is one of the most compelling ways to convey changes over time. By visualizing aridity data as an animation, we can effectively showcase its evolution on a global scale. To achieve this, we’ll employ the gganimate library along with geographic information from the Natural Earth dataset. The animation will elegantly illustrate how aridity varies across different years.</p><p><strong>Note:</strong> For the sake of space, I’ll just use the first 10 rasters in the stack for animation.</p><pre># Load necessary libraries<br>library(gganimate)<br>library(rnaturalearth)<br><br># Custom fonts<br>library(showtext)<br>font_add_google(&quot;Prata&quot;, regular.wt = 400)<br>showtext_auto()<br><br># Download global coastline data from naturalearth<br>countries &lt;- ne_countries(scale = 110, returnclass = c(&quot;sf&quot;))<br><br># Define the output directory and animation file<br>output_dir &lt;- &quot;Animation/&quot;<br>animation_file &lt;- &quot;aridity_animation.gif&quot;<br><br># Create the output directory if it doesn&#39;t exist<br>if (!dir.exists(output_dir)) {<br>  dir.create(output_dir)<br>}<br><br># Convert raster data to a dataframe<br>g_df &lt;- aridity_rcp85[[1:10]] %&gt;% # first 10 rasters only<br>  as.data.frame(xy = TRUE) %&gt;%<br>  pivot_longer(!c(x, y)) %&gt;%<br>  filter(!is.na(value)) %&gt;%<br>  mutate(name = ymd(name),<br>         Year = year(name))<br><br># Get unique years<br>Years &lt;- unique(g_df$Year)<br><br># Loop over each unique year<br>purrr::map(Years, ~{<br>  # Filter data for the current year<br>  year_data &lt;- g_df %&gt;% filter(Year == .x)<br>  <br>  # Formulate the graphing element<br>  world_map &lt;- ggplot() +<br>    theme_void() +<br>    geom_raster(data = year_data, aes(x = x, y = y, fill = value, group = name), color = NA) +<br>    scale_fill_viridis_c(option = &quot;B&quot;) +<br>    geom_sf(data = countries, color = &#39;white&#39;, linetype = &#39;solid&#39;, fill = NA, size = 0.2) +<br>    theme(<br>      plot.title = element_text(family = &quot;Prata&quot;, size = 40, hjust = 0.5),<br>      plot.subtitle = element_text(hjust = 0.5, size = 16), <br>      plot.caption = element_text(color = &quot;black&quot;, size = 26, hjust = 0.9),<br>      legend.position = &quot;bottom&quot;, <br>      legend.text = element_text(size = 30), <br>      legend.box.spacing = unit(-12, &quot;pt&quot;)<br>    ) +<br>    labs(title = paste(&quot;Aridity:&quot;, round(.x)),<br>         caption = &quot;Data source: Global bioclimatic indicators derived from CMIP5 climate projections&quot;,<br>         fill = NULL) +<br>    guides(<br>      fill = guide_colourbar(<br>        direction = &#39;horizontal&#39;,<br>        title.position=&#39;top&#39;,<br>        title.hjust=0.5,<br>        ticks.colour=&#39;#f5f5f2&#39;,<br>        barwidth = 32,<br>        barheight = 0.75<br>      )<br>    )<br>  <br>  # Define the file path for the PNG image<br>  file_path &lt;- file.path(output_dir, sprintf(&quot;aridity_%d.png&quot;, .x))<br>  <br>  # Save the plot as a PNG image<br>  ggsave(file_path, world_map, dpi = 300, width = 7, height = 4)<br>})<br><br># Combine PNG images into a GIF animation<br># Get the list of PNG files in the directory<br>image_files &lt;- list.files(output_dir, pattern = &quot;*.png&quot;, full.names = TRUE)<br><br># Read the images<br>library(magick)<br>images &lt;- lapply(image_files, image_read)<br><br># Set the frames per second (fps) for the animation<br>fps &lt;- 5<br><br># Create the animation by setting the duration for each frame<br>animation &lt;- image_animate(image_join(images), fps = fps)<br><br># Save the animation as a GIF file<br>animation_file &lt;- &quot;aridity_animation.gif&quot;<br>image_write(animation, animation_file)<br><br># animation<br>animation</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*s1RYG3mSXb9t4trz.gif" /></figure><h3>Conclusion</h3><p>Through this data science adventure, we’ve explored the captivating realm of global aridity changes. With the help of R and a rich dataset, we’ve visualized the evolution of aridity over time, providing an engaging and informative animation. This journey reminds us of the importance of understanding climate change’s impact on our environment and the critical role that data science plays in uncovering such insights. As we continue to innovate and explore, we can use data-driven approaches to shape a more sustainable future for our planet.</p><p><strong>That’s it!</strong></p><p><strong>Feel free to reach me out if you got any questions.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/0*H8vl8gA75VIktuDf.jpg" /></figure><p>Data Science Fellow</p><p>My research interests include disease modeling in space and time, climate change, GIS and Remote Sensing and Data Science in Agriculture.</p><p><em>Originally published at </em><a href="https://www.datawim.com/post/exploring-global-aridity-changes-a-data-science-adventure/"><em>https://www.datawim.com</em></a><em> on August 15, 2023.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=50aec625a6f0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[From One Sample to Many: Estimating Distributions with Bootstrapping and Permutation | DataWim]]></title>
            <link>https://mohsinramay.medium.com/from-one-sample-to-many-estimating-distributions-with-bootstrapping-and-permutation-datawim-7f31c72c5531?source=rss-21e59c6e1c3b------2</link>
            <guid isPermaLink="false">https://medium.com/p/7f31c72c5531</guid>
            <category><![CDATA[ggplot2]]></category>
            <category><![CDATA[permutations]]></category>
            <category><![CDATA[bootstrapping]]></category>
            <category><![CDATA[sampling]]></category>
            <category><![CDATA[statistics]]></category>
            <dc:creator><![CDATA[Mohsin Ramay]]></dc:creator>
            <pubDate>Sun, 05 Mar 2023 00:00:00 GMT</pubDate>
            <atom:updated>2023-03-06T02:30:33.419Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qKEbfFW_KH6mFtansg6izw.png" /></figure><p>We often use randomization methods in statistics to make inferences about a population based on a sample. Two of the most commonly used randomization methods are bootstrapping and permutation. In this blog post, I will discuss what bootstrapping and permutation are and how to use them in R. I will also provide examples of when each method is appropriate.</p><h3>Bootstrapping</h3><p>Bootstrapping is a resampling technique that involves creating new samples by drawing observations from the original sample with replacement. The new samples are of the same size as the original sample, and multiple samples are created to estimate the sampling distribution of a statistic. This method is used when obtaining new samples from the population of interest is difficult or impossible.</p><p>For example, suppose we are interested in estimating the mean height of all adults in a city, but we only have data for a sample of 50 adults. We can use bootstrapping to estimate the sampling distribution of the mean height and calculate confidence intervals.</p><p>Let’s generate a sample of 50 heights using the rnorm() function in R:</p><pre>set.seed(123) # for reproducibility<br><br>heights &lt;- rnorm(n = 50, mean = 170, sd = 10)</pre><p>We can then use the boot() function in the boot package to create new samples using bootstrapping and estimate the mean height:</p><pre>library(boot)<br><br># Define function to estimate mean<br>mean_func &lt;- function(data, index) {<br>  return(mean(data[index]))<br>}<br><br>set.seed(123)<br># Use bootstrapping to estimate mean<br>boot_mean &lt;- boot(data = heights,<br>                  statistic = mean_func,<br>                  R = 1000)<br>boot_mean<br><br>ORDINARY NONPARAMETRIC BOOTSTRAP<br><br><br>Call:<br>boot(data = heights, statistic = mean_func, R = 1000)<br><br><br>Bootstrap Statistics :<br>    original      bias    std. error<br>t1*  170.344 -0.05647046    1.271634</pre><p>The output shows that the estimated mean height is 170.3. We can visualize the sampling distribution of the mean height using a histogram.</p><pre># creating dataframe<br>boot_mean &lt;- data.frame(boot_mean$t)<br><br>library(ggplot2)<br><br># Create histogram of bootstrapped means<br>p1 &lt;- boot_mean |&gt;<br>  ggplot(aes(x = boot_mean.t)) +<br>  geom_histogram(bins = 20, fill = &quot;orange&quot;, alpha = 0.75) +<br>  geom_vline(<br>    xintercept = median(boot_mean$boot_mean.t, na.rm = TRUE),<br>    lty = 2,<br>    size = 1,<br>    colour = &quot;blue&quot;<br>  ) +<br>  labs(title = &quot;Sampling Distribution of Mean Height (Bootstrapping)&quot;,<br>       x = &quot;Mean Height (cm)&quot;, y = &quot;Frequency&quot;) +<br>  theme_bw()<br><br>p1</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*WfXt5e0BVu5BM7JY.png" /></figure><p>The output shows a bell-shaped curve that is centered around the estimated mean height of 170.3.</p><h3>Permutation</h3><p>Permutation, also known as randomization tests or exact tests, involves randomly shuffling the values of one or more variables and recalculating a statistic of interest. This is done repeatedly to create a null statistic distribution under the assumption of no association between the variables.</p><p>Permutation is often used when testing hypotheses about the relationship between variables or when comparing two groups. For example, suppose we are interested in whether there is a significant difference in the mean height between males and females. We can use permutation to test this hypothesis.</p><p>Let’s generate two samples of heights, one for males and one for females. We can visualize the null distribution of the F statistic using a density plot.</p><p>In this example, we first generate two random groups of data (group1 and group2) with means of 10 and 12, respectively. We then calculate the observed difference in means between the two groups.</p><p>We define a function diff_means that takes a list of two vectors and returns the difference in means between the two vectors. We then use this function with the replicate function to perform a permutation test and generate a null distribution of the difference in means between the two groups.</p><p>Finally, we create a histogram of the null distribution using ggplot and add a vertical line at the observed difference in means. This allows us to visually compare the observed difference in means to the null distribution of differences in means generated by the permutation test.</p><pre># Generate some random data for two groups<br>set.seed(123)<br>group1 &lt;- rnorm(50, mean = 10, sd = 2)<br>group2 &lt;- rnorm(50, mean = 12, sd = 2)<br><br># Calculate observed difference in means<br>obs_diff &lt;- mean(group2) - mean(group1)<br><br># Define function to calculate difference in means between two groups<br>diff_means &lt;- function(x) {<br>  return(mean(x[[2]]) - mean(x[[1]]))<br>}<br><br># Use permutation to estimate null distribution of difference in means<br>perm_diff &lt;-<br>  replicate(1000, diff_means(list(<br>    group1 = sample(group1), group2 = sample(group2)<br>  )))<br><br># making dataframe<br>perm_diff &lt;- data.frame(perm_diff)<br><br># Create histogram of null distribution<br>p2 &lt;- perm_diff |&gt;<br>  ggplot(aes(x = perm_diff)) +<br>  geom_histogram(fill = &quot;orange&quot;, alpha = 0.75, bins = 30) +<br>  geom_vline(xintercept = obs_diff, lty = 2, size = 1, color = &quot;blue&quot;) +<br>  labs(title = &quot;Null Distribution of Difference in Means (Permutation)&quot;,<br>       x = &quot;Difference in Means&quot;, y = &quot;Frequency&quot;) +<br>  theme_bw()<br><br>p2</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eNykiS5K-8CFQcfi.png" /></figure><p><strong>That’s it!</strong></p><p><strong>Feel free to reach me out if you got any questions.</strong></p><p><em>Originally published at </em><a href="https://www.datawim.com/post/from-one-to-mayestimating-distributions-with-bootstrapping-and-permutation/"><em>https://www.datawim.com</em></a><em> on March 5, 2023.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7f31c72c5531" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Art with R]]></title>
            <link>https://blog.devgenius.io/art-with-r-d3534144cc91?source=rss-21e59c6e1c3b------2</link>
            <guid isPermaLink="false">https://medium.com/p/d3534144cc91</guid>
            <category><![CDATA[art]]></category>
            <category><![CDATA[statistics]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[creativity]]></category>
            <category><![CDATA[r]]></category>
            <dc:creator><![CDATA[Mohsin Ramay]]></dc:creator>
            <pubDate>Mon, 26 Sep 2022 22:00:01 GMT</pubDate>
            <atom:updated>2022-09-29T19:06:49.444Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://www.datawim.com/post/art-with-r/">In this blog post, I’ll show how we can create art pieces (target boards) in R using base plotting features and functions.</a></p><p>There is a lot we can do with programming languages and so do with R. In my last post, I shared how we can generate different spatial patterns in R using some simple base functions like matrix and then visualize them using the ggplot2 package.</p><p>In this post, I’ll do some more creativity and show how we can use R to do artistic work and create beautiful target boards with points on them.</p><p>Accuracy and precision are vital concepts in machine learning, especially when assessing model quality for its prediction. <strong>Accuracy</strong> refers to how close a measurement is to the truth. In contrast, <strong>precision</strong> refers to how close measurements of the same class are to each other. Of course, both are independent of each other.</p><p>And those of you who are familiar with these terms might have come across the following figure that beautifully visualize these concepts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/785/0*wRid9FEsDHL0Q98S.png" /></figure><p>In this blog post, I’ll recreate these shapes with R using base plotting functions.</p><h3>Simple shapes</h3><p>We can use the base plot function in R to draw different shapes like circles, squares, triangles, etc. And using these shapes, we can do a lot.</p><h3>A circle</h3><pre>plot(0, 0, cex = 12)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ok02OFba47RAnv7v.png" /></figure><h3>A triangle</h3><pre>plot(0, 0, cex = 12, pch = 2)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*VdpuhnQKPYtS4M2n.png" /></figure><h3>A square</h3><pre>plot(0, 0, cex = 12, pch = 0)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8J_qL8H_MStzB2iH.png" /></figure><p>We can also combine these shapes in the same graph by plotting them together.</p><pre>plot(0, 0, cex = 12)<br>points(0.5, 0.5, cex = 12, pch = 2)<br>points(-0.5, -0.5, cex = 12, pch = 0)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HNHMG7VfIX4htvw1.png" /></figure><p>To make these colorful, we can fill them with colors of our choice.</p><pre>plot(0, 0, cex = 12, pch = 21, bg = &quot;orange&quot;)<br>points(0.5, 0.5, cex = 12, pch = 24, bg = &quot;green&quot;)<br>points(-0.5, -0.5, cex = 12, pch = 22, bg = &quot;blue&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qdeO2iM-X6F_kCcf.png" /></figure><p>Coming back to the business, i.e., target board. For that purpose, we can use circles of different sizes, lay them over each other, and fill them with different colors, i.e., orange and white. At first, we can start with a large orange circle (cex = 45) followed by correspondingly smaller circles with different colors.</p><pre><em># target board</em><br>plot(0, 0, cex = 45, pch = 21, bg = &quot;orange&quot;, col = &quot;black&quot;)<br>points(0, 0, cex = 39, pch = 21, bg = &quot;white&quot;, col = &quot;white&quot;)<br>points(0, 0, cex = 33, pch = 21, bg = &quot;orange&quot;, col = &quot;orange&quot;)<br>points(0, 0, cex = 27, pch = 21, bg = &quot;white&quot;, col = &quot;white&quot;)<br>points(0, 0, cex = 21, pch = 21, bg = &quot;orange&quot;, col = &quot;orange&quot;)<br>points(0, 0, cex = 15, pch = 21, bg = &quot;white&quot;, col = &quot;white&quot;)<br>points(0, 0, cex = 9, pch = 21, bg = &quot;orange&quot;, col = &quot;orange&quot;)<br>points(0, 0, cex = 3, pch = 21, bg = &quot;white&quot;, col = &quot;white&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ZoeJjxa7ZMEu0tHI.png" /></figure><p>Now, I’ll draw five target points (multiply sign) close to each other in the center of the circles and remove the axis titles and the borderline</p><pre><em># target board</em><br>plot(0, 0, cex = 45, pch = 21, bg = &quot;orange&quot;, col = &quot;black&quot;, axes=F, xlab = NA, ylab = NA)<br>points(0, 0, cex = 39, pch = 21, bg = &quot;white&quot;, col = &quot;white&quot;)<br>points(0, 0, cex = 33, pch = 21, bg = &quot;orange&quot;, col = &quot;orange&quot;)<br>points(0, 0, cex = 27, pch = 21, bg = &quot;white&quot;, col = &quot;white&quot;)<br>points(0, 0, cex = 21, pch = 21, bg = &quot;orange&quot;, col = &quot;orange&quot;)<br>points(0, 0, cex = 15, pch = 21, bg = &quot;white&quot;, col = &quot;white&quot;)<br>points(0, 0, cex = 9, pch = 21, bg = &quot;orange&quot;, col = &quot;orange&quot;)<br>points(0, 0, cex = 3, pch = 21, bg = &quot;white&quot;, col = &quot;white&quot;)</pre><pre><em># target points</em><br>points(0, 0, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)<br>points(0, 0.1, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)<br>points(0, -0.1, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)<br>points(0.1, 0, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)<br>points(-0.1, 0, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pL-VwFEBfEpFDhlC.png" /></figure><p>As we have the base code to generate a target board figure, I’ll make a function using this code to use in the following sections to reduce repetition.</p><pre><em># target board function</em><br>target_board &lt;- <strong>function</strong>(title) {<br>  plot(0, 0, cex = 45, pch = 21, bg = &quot;orange&quot;, col = &quot;black&quot;, axes=F, main = title, xlab = NA, ylab = NA)<br>  points(0, 0, cex = 39, pch = 21, bg = &quot;white&quot;, col = &quot;white&quot;)<br>  points(0, 0, cex = 33, pch = 21, bg = &quot;orange&quot;, col = &quot;orange&quot;)<br>  points(0, 0, cex = 27, pch = 21, bg = &quot;white&quot;, col = &quot;white&quot;)<br>  points(0, 0, cex = 21, pch = 21, bg = &quot;orange&quot;, col = &quot;orange&quot;)<br>  points(0, 0, cex = 15, pch = 21, bg = &quot;white&quot;, col = &quot;white&quot;)<br>  points(0, 0, cex = 9, pch = 21, bg = &quot;orange&quot;, col = &quot;orange&quot;)<br>  points(0, 0, cex = 3, pch = 21, bg = &quot;white&quot;, col = &quot;white&quot;)<br>}</pre><h3>Target boards with varying accuracy and precision</h3><p>Now, I’ll use the target board function to generate four different target boards and target points at various locations to show different accuracy and precision levels.</p><pre><em># 2 by 2 grid</em><br>par(mfrow = c(2, 2))</pre><pre><em># remove extra white space</em><br>par(mar = c(1, 1, 1.5, 1)) </pre><pre><em># High Accuracy High Precision</em><br>target_board(title = &quot;High Accuracy High Precision&quot;)<br>points(0, 0, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)<br>points(0, 0.1, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)<br>points(0, -0.1, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)<br>points(0.1, 0, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)<br>points(-0.1, 0, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)</pre><pre><em># Low Accuracy High Precision</em><br>target_board(title = &quot;Low Accuracy High Precision&quot;)<br>points(0.15, 0.3, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)<br>points(0.15, 0.4, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)<br>points(0.15, 0.2, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)<br>points(0.25, 0.3, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)<br>points(0.05, 0.3, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)</pre><pre><em># High Accuracy Low Precision</em><br>target_board(title = &quot;High Accuracy Low Precision&quot;)<br>points(0, 0.15, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)<br>points(-0.1, 0.2, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)<br>points(0.09, -0.12, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)<br>points(0.15, 0, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)<br>points(-0.1, -0.1, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)</pre><pre><em># Low Accuracy Low Precision</em><br>target_board(title = &quot;Low Accuracy Low Precision&quot;)<br>points(0.3, 0.4, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)<br>points(-0.3, 0.2, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)<br>points(-0.2, -0.2, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)<br>points(0.2, -0.4, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)<br>points(-0.05, -0.5, pch = 4, lwd = 2, cex = 1.5, col = &quot;blue&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eLXpo6qgII4hzNu7.png" /></figure><p><strong>That’s it!</strong></p><p><strong>Feel free to reach me out if you got any questions.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d3534144cc91" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/art-with-r-d3534144cc91">Art with R</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Generating Spatial Patterns Using pixeltrix in R]]></title>
            <link>https://mohsinramay.medium.com/generating-spatial-patterns-using-pixeltrix-in-r-ed6d752cfa86?source=rss-21e59c6e1c3b------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed6d752cfa86</guid>
            <category><![CDATA[ggplot2]]></category>
            <category><![CDATA[spatial-analysis]]></category>
            <category><![CDATA[r]]></category>
            <category><![CDATA[matrix]]></category>
            <category><![CDATA[data-visualization]]></category>
            <dc:creator><![CDATA[Mohsin Ramay]]></dc:creator>
            <pubDate>Sat, 24 Sep 2022 19:32:18 GMT</pubDate>
            <atom:updated>2022-09-24T19:33:33.901Z</atom:updated>
            <content:encoded><![CDATA[<h3>Generating spatial patterns using pixeltrix in R</h3><p>In this blog post, I’ll show how we can easily generate different patterns in R without writing any hard code.</p><p>Last week I wrote a <a href="https://www.datawim.com/post/generating-different-patterns-in-r/">blog</a> on making different spatial patterns in R using matrix function. Today, I found this cool package “<a href="https://www.rostrum.blog/2022/09/24/pixeltrix/">pixeltrix</a>” that can help do the same thing without writing any brain-burning code and even much more.</p><p><strong>Load library</strong></p><pre>install.packages(remotes)<br>remotes::install_github(&quot;matt-dray/pixeltrix&quot;)</pre><pre>library(pixeltrix)</pre><p><strong>Generate pattern</strong></p><pre>click_pixels(<br>  n_rows = 5,<br>  n_cols = 5,<br>  n_states = 4,<br>  grid = TRUE<br>) -&gt; x</pre><p>I made a small video to briefly demonstrate the functionality. Do check this out.</p><h3>Mohsin Ramay on Twitter: &quot;This is really cool.Last week I wrote a blog on making different spatial patterns in R using matrix function. Today, I found this cool package &quot;pixeltrix&quot; that can help do the same thing without writing any brain-burning code and even much more. Thank you @mattdray #rstats pic.twitter.com/9g5kB4aVYn / Twitter&quot;</h3><p>This is really cool.Last week I wrote a blog on making different spatial patterns in R using matrix function. Today, I found this cool package &quot;pixeltrix&quot; that can help do the same thing without writing any brain-burning code and even much more. Thank you @mattdray #rstats pic.twitter.com/9g5kB4aVYn</p><p>We can do much more in generating different patterns with this package. And here is the remaining code.</p><pre># 5 by 5 matrix<br>x</pre><pre>      [,1] [,2] [,3] [,4] [,5]<br>[1,]    0    0    1    0    0<br>[2,]    0    0    2    0    0<br>[3,]    1    2    3    2    1<br>[4,]    0    0    2    0    0<br>[5,]    0    0    1    0    0</pre><p><strong>Visualization using ggplot2</strong></p><pre>library(reshape2)</pre><pre>melt(x) |&gt; <br>  ggplot(aes(x = Var2, y = Var1)) +<br>  geom_tile(aes(fill = value), color = &quot;black&quot;, size = 1) + <br>  scale_fill_viridis_c(option = &quot;F&quot;, direction = -1) +<br>  labs(fill = NULL) +<br>  theme_void()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nH0jNwgbdoPORw-vl-LNoA.png" /></figure><p><strong>That’s it!</strong></p><p><strong>Feel free to reach me out if you got any questions.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed6d752cfa86" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Generating different spatial patterns in R and their visualization using ggplot2]]></title>
            <link>https://blog.devgenius.io/generating-different-spatial-patterns-in-r-and-their-visualization-using-ggplot2-596868f4066e?source=rss-21e59c6e1c3b------2</link>
            <guid isPermaLink="false">https://medium.com/p/596868f4066e</guid>
            <category><![CDATA[spatial-analysis]]></category>
            <category><![CDATA[ggplot2]]></category>
            <category><![CDATA[matrix]]></category>
            <category><![CDATA[r]]></category>
            <category><![CDATA[random-numbers]]></category>
            <dc:creator><![CDATA[Mohsin Ramay]]></dc:creator>
            <pubDate>Sat, 17 Sep 2022 00:34:38 GMT</pubDate>
            <atom:updated>2022-09-20T10:34:12.787Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XSvBJ5B1nHTlaTUXgYX3Nw.png" /></figure><p>Spatial pattern is the arrangement of individual entities in space and the geographic relationships among them. Identification of spatial patterns is a prerequisite to understanding the complicated spatial processes underlying the distribution of a phenomenon. And this understanding is obscure without visualization.</p><p>Spatial patterns can help understand behaviors of phenomena occurring in space and time. For example, spatial patterns can help us pinpoint the source of a disease outbreak (hot spot), track the spread of an epidemic, efficiently allocate resources, risk monitoring, and urban planning, to name a few.</p><p>Spatial patterns can vary from case to case. We now have plenty of resources and analytical tools to analyze and visualize spatial data to obtain insights for solid and effective decision-making. However, we do not have much of the resources to generate patterns hypothetically for sampling and teaching purposes.</p><p>In this blog post, I’ll use basic features like matrix in R to generate different patterns and then visualize those patterns using the ggplot2 package. These examples can help design sampling schemes and educate students about spatial data and their analysis.</p><h3>Load libraries</h3><pre><strong>library</strong>(tidyverse)<br><strong>library</strong>(reshape2)</pre><p>At first, I’ll make an empty matrix using the matrix function and visualize it.</p><h3>Empty Matrix</h3><pre><em># 4x4 matrix</em><br>m &lt;- matrix(nrow = 4, ncol = 4, byrow=TRUE)</pre><pre><em># matrix visualization</em><br>melt(m) |&gt; <br>  ggplot(aes(x = Var2, y = Var1, label = &quot;NA&quot;)) +<br>  geom_tile(color = &quot;black&quot;, fill = NA, size = 1) + <br>  geom_text() +<br>  theme_void() +<br>  theme(legend.position = &quot;none&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zeQGUC8bVenJFs7s.png" /></figure><h3>Filling matrix with values</h3><p>Now, I’ll fill this matrix with only zeros.</p><h3>Zeros</h3><pre><em># 4x4 matrix with zeros</em><br>m &lt;- matrix(0, nrow = 4, ncol = 4, byrow=TRUE)</pre><pre><em># matrix visualization</em><br>melt(m) |&gt; <br>  ggplot(aes(x = Var2, y = Var1, label = value)) +<br>  geom_tile(color = &quot;black&quot;, fill = NA, size = 1) + <br>  geom_text() +<br>  labs(fill = NULL) +<br>  theme_void()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_-I1Z3bDZUxQRDqx.png" /></figure><p>Then, I’ll add the ones to generate some patterns.</p><h3>Zeros and ones</h3><pre><em># 4x4 matrix with zeros and ones</em><br>m &lt;- matrix(0:1, nrow = 4, ncol = 4, byrow=TRUE)</pre><pre><em># matrix visualization</em><br>melt(m) |&gt; <br>  ggplot(aes(x = Var2, y = Var1, fill = factor(value), label = value)) +<br>  geom_tile(color = &quot;black&quot;, size = 1) +<br>  scale_fill_manual(values = c(&quot;white&quot;, &quot;orange&quot;)) +<br>  geom_text() +<br>  labs(fill = NULL) +<br>  theme_void()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fHQsI_W0rIiAz92j.png" /></figure><p>Then, I’ll add the ones to generate some patterns.</p><h3>Each column with a distinct number</h3><pre><em># 4x4 matrix with distinct columns</em><br>m &lt;- matrix(0:3, nrow = 4, ncol = 4, byrow=TRUE)</pre><pre><em># matrix visualization</em><br>melt(m) |&gt; <br>  ggplot(aes(x = Var2, y = Var1, fill = factor(value), label = value)) +<br>  geom_tile(color = &quot;black&quot;, size = 1) + <br>  scale_fill_brewer(palette = &quot;Accent&quot;) +<br>  geom_text() +<br>  labs(fill = NULL) +<br>  theme_void()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Oioo1D-QzNtu72oa.png" /></figure><p>By using the same method, I’ll now generate a random pattern. <strong>Hint:</strong> If we increase the range of filling values (here, 0:5) to the dimensions of a matrix, the matrix function will arrange the number randomly to fill the matrix. You can increase the range and see how the pattern changes.</p><h3>Random pattern</h3><pre><em># 4x4 matrix with randomly generated number (0:5)</em><br>m &lt;- matrix(0:5, nrow = 4, ncol = 4, byrow=TRUE)</pre><pre><em># matrix visualization</em><br>melt(m) |&gt; <br>  ggplot(aes(x = Var2, y = Var1, fill = factor(value), label = value)) +<br>  geom_tile(color = &quot;black&quot;, size = 1) +<br>  scale_fill_brewer(palette = &quot;Set3&quot;) +<br>  geom_text() +<br>  labs(fill = NULL) +<br>  theme_void()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*u9ftqgYOFH2Fs3p7.png" /></figure><h3>Binomial pattern</h3><p>Like previous matrices, we can also generate a binomial matrix in R to denote binomial patterns, i.e., Yes or No and 0 or 1. And for this purpose, we can use rbinom function to generate 0 and 1 values in a random pattern.</p><h3>Random pattern</h3><pre>set.seed(1)</pre><pre><em># 6x6 matrix with randomly generated numbers (0 and 1)</em><br>m &lt;- matrix(rbinom(36, 1, 0.3), nrow = 6)</pre><pre><em># matrix visualization</em><br>melt(m) |&gt; <br>  ggplot(aes(x = Var2, y = Var1)) +<br>  geom_tile(aes(fill = factor(value)), color = &quot;black&quot;, size = 1) + <br>  scale_fill_manual(values = c(&quot;white&quot;, &quot;orange&quot;)) +<br>  labs(fill = NULL) +<br>  theme_void()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pb0X9Ykwp7W2klzu.png" /></figure><p>In the last matrix, I used probability (<em>p</em> = 0.3) to generate a random sequence of 0 and 1. However, we can also write a function to generate random patterns using different probabilities and then visualize them to see how the random pattern varies with varying probabilities.</p><pre>pp &lt;- list()</pre><pre><strong>for</strong> (i <strong>in</strong> 2:7) {<br>m &lt;- matrix(rbinom(36, 1, i/10), nrow = 6)</pre><pre>data = melt(m)</pre><pre>pp[[i]]  &lt;- data |&gt; <br>  ggplot(aes(x = Var2, y = Var1)) +<br>  geom_tile(aes(fill = factor(value)), color = &quot;black&quot;, size = 1) + <br>  scale_fill_manual(values = c(&quot;white&quot;, &quot;orange&quot;)) +<br>  labs(fill = NULL, subtitle = paste(&quot;p = &quot;, i/10)) +<br>  theme_void() +<br>  theme(plot.subtitle = element_text(hjust = 0.5, face = &quot;bold&quot;))<br>}</pre><pre><em># grid of plots</em><br><strong>library</strong>(patchwork)</pre><pre>pp[[2]] + pp[[3]] + pp[[4]] + pp[[5]] + pp[[6]] + pp[[7]] + plot_layout(ncol = 3, guides = &quot;collect&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-h_uUYl3wgF-FnIy.png" /></figure><h3>Positive spatial autocorrelation</h3><p>Positive spatial autocorrelation means that geographically nearby values of a variable tend to be similar, i.e., high values tend to be located near high values and low values near low values. We can generate this pattern using the following code.</p><h4>Code 1</h4><pre><em># 6x6 matrix </em><br>m &lt;- matrix(nrow=6, ncol = 6, byrow=TRUE)</pre><pre><em># filling values in the matrix</em><br>m[1:6, 1L:3L] = 1 <em># rows 1 to 6 in columns 1 to 3 with 1</em><br>m[1:6, 4L:6L] = 0 <em># rows 1 to 6 in columns 4 to 6 with 0</em></pre><pre><em># matrix visualization</em><br>melt(m) |&gt; <br>  ggplot(aes(x = Var2, y = Var1)) +<br>  geom_tile(aes(fill = factor(value)), color = &quot;black&quot;, size = 1) + <br>  scale_fill_manual(values = c(&quot;white&quot;, &quot;orange&quot;)) +<br>  labs(fill = NULL) +<br>  theme_void()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*F0ARTI17V_0oTwLk.png" /></figure><h4>Code 2</h4><pre><em># 6x6 matrix </em><br>m &lt;- matrix(c(1, 1, 1, 0, 0, 0), nrow=6, ncol = 6, byrow=TRUE)</pre><pre><em># matrix visualization</em><br>melt(m) |&gt; <br>  ggplot(aes(x = Var2, y = Var1)) +<br>  geom_tile(aes(fill = factor(value)), color = &quot;black&quot;, size = 1) + <br>  scale_fill_manual(values = c(&quot;white&quot;, &quot;orange&quot;)) +<br>  labs(fill = NULL) +<br>  theme_void()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IsgqgGRETQS6diph.png" /></figure><h3>Negative spatial autocorrelation</h3><p>Negative spatial autocorrelation is the opposite of positive spatial autocorrelation, i.e., when dissimilar values cluster together on a map. We can generate this pattern using the following code.</p><pre><em># 6x6 matrix </em><br>m &lt;- matrix(nrow=6, ncol = 6, byrow=TRUE)</pre><pre><em># filling values in the matrix</em><br>m[c(1, 3, 5), c(1L, 3L, 5L)] = 1<br>m[c(2, 4, 6), c(2L, 4L, 6L)] = 1<br>m[c(1, 3, 5), c(2L, 4L, 6L)] = 0<br>m[c(2, 4, 6), c(1L, 3L, 5L)] = 0</pre><pre><em># matrix visualization</em><br>melt(m) |&gt; <br>  ggplot(aes(x = Var2, y = Var1)) +<br>  geom_tile(aes(fill = factor(value)), color = &quot;black&quot;, size = 1) + <br>  scale_fill_manual(values = c(&quot;white&quot;, &quot;orange&quot;)) +<br>  labs(fill = NULL) +<br>  theme_void()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eXsCpSU7kttd3KRj.png" /></figure><h3>Hotspot pattern</h3><p>A hot spot pattern is another positive spatial autocorrelation pattern that represents clustering. We can generate a hot spot pattern using the following code.</p><pre><em># 6x6 matrix </em><br>m &lt;- matrix(nrow=11, ncol = 11, byrow=TRUE)</pre><pre><em># filling values in the matrix</em><br>m[c(1:11), c(1L:11L)] = 0<br>m[c(2:10), c(2L:10L)] = 1<br>m[c(3:9), c(3L:9L)] = 2<br>m[c(4:8), c(4L:8L)] = 3<br>m[c(5:7), c(5L:7L)] = 4<br>m[c(6), c(6L)] = 5</pre><pre><em># matrix visualization</em><br>melt(m) |&gt; <br>  ggplot(aes(x = Var2, y = Var1)) +<br>  geom_tile(aes(fill = value), color = &quot;black&quot;, size = 1) + <br>  scale_fill_viridis_c(option = &quot;F&quot;, direction = -1) +<br>  labs(fill = NULL) +<br>  theme_void()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*m8mA_1pCqmnvCIBp.png" /></figure><p><strong>That’s it!</strong></p><p><strong>Feel free to reach me out if you got any questions.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=596868f4066e" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/generating-different-spatial-patterns-in-r-and-their-visualization-using-ggplot2-596868f4066e">Generating different spatial patterns in R and their visualization using ggplot2</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Missing Data Visualization in R using ggplot2]]></title>
            <link>https://mohsinramay.medium.com/missing-data-visualization-in-r-using-ggplot2-f2d7d02e989e?source=rss-21e59c6e1c3b------2</link>
            <guid isPermaLink="false">https://medium.com/p/f2d7d02e989e</guid>
            <category><![CDATA[r]]></category>
            <category><![CDATA[missing-data]]></category>
            <category><![CDATA[ggplot2]]></category>
            <category><![CDATA[data-cleaning]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Mohsin Ramay]]></dc:creator>
            <pubDate>Sun, 04 Sep 2022 14:48:41 GMT</pubDate>
            <atom:updated>2022-09-05T14:29:09.875Z</atom:updated>
            <content:encoded><![CDATA[<p>In data science, data cleaning and dealing with missing data is one of the main issues after retrieving a big data set. Data coming from databases or archives never comes in clean and ready-to-analysis format.</p><p>Big data always contains features with missing observations. One of the main issues with missing data is deciding whether to eliminate the missing observations or impute them using information from other features.</p><p>However, in a few instances, some of the features just come empty, so we don’t need to worry about their imputation because we can simply omit or unselect those columns. But before that, we need to calculate the proportion of missing data in each feature to decide on a threshold to keep features in the data.</p><p>In this blog post, I’ll use some basic and dplyr functionality to count missingness in the data and its visualization using the ggplot2 package. After that, I’ll use some available packages that provide built-in functions to visualize the missing data. And then, I’ll show how we can remove the completely missing features from our data sets.</p><h3>Load libraries</h3><pre><strong>library</strong>(tidyverse)<br><strong>library</strong>(gt)</pre><h3>Load data</h3><p>I’ll use plant pathogen risk data from UK Risk Register for this blog post. The data is publicly available.</p><pre><em># data</em><br>data &lt;- read.csv(&quot;https://raw.githubusercontent.com/MohsinRamay/sampledata/main/Risk_Register_01_09_2022_10_47_18.csv&quot;)</pre><pre>data |&gt; <br>  glimpse()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LRgYoCN1vRTF6DrUk6v0sA.png" /></figure><ul><li>The data consists of 1409 rows and 59 columns.</li><li>And it’s evident from the data glimpse that some features are missing a considerable amount of data.</li><li>Now, I’ll calculate the percentage of missing data in each column.</li></ul><h3>Missing data count</h3><pre>miss_data &lt;- data |&gt; <br>  gather(key, value) |&gt; <br>  group_by(key) |&gt; <br>  count(na = is.na(value)) |&gt; <br>  pivot_wider(names_from = na, values_from = n, values_fill = 0) |&gt; <br>  mutate(pct_missing = (`TRUE`/sum(`TRUE`, `FALSE`))*100) |&gt; <br>  ungroup()</pre><pre>miss_data |&gt; <br>  <em>#sample_n(10) |&gt; </em><br>  gt()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W0aNE0PZyCcymVp338rYvw.png" /></figure><ul><li>Now, I’ll visualize this missing data proportion using the bar chart.</li><li>I’ll reorder the bars based on missingness so we can easily see the columns missing the maximum amount of data.</li></ul><h3>Missing data visualization</h3><pre>miss_data |&gt; <br>  mutate(Present = 100 - pct_missing) |&gt;<br>  gather(Key, value, 4:5) |&gt;    <br>  mutate(Key = recode(Key, pct_missing = &quot;Missing&quot;)) |&gt;<br>  ggplot(aes(x = reorder(key, `TRUE`), y = value, fill = Key)) +       <br>  geom_col(alpha = 0.85) +<br>  scale_fill_manual(name = &quot;&quot;,<br>                            values = c(&#39;tomato3&#39;, &#39;steelblue&#39;),<br>                            labels = c(&quot;Missing&quot;, &quot;Present&quot;)) +<br>  coord_flip() +<br>  labs(x = NULL, y = &quot;Missing (%)&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7CFLnhnPQtBFkVOX.png" /></figure><p>So, we can see that some of the columns are completely missing data, and we can remove them before moving towards further visualizations and data analysis. But before that, I’ll share another package that provides an already built function that can help us quickly visualize the amount of missing data in columns. And that function is plot_missing() from the DataExplorer. This function uses built-in criteria to create a missing profile for each column, i.e., Good, OK, Bad and Remove. The default criteria are:</p><ul><li>“Good” = 0.05</li><li>“OK” = 0.4</li><li>“Bad” = 0.8</li><li>“Remove” = 1</li></ul><h3>plot_missing function from DataExplorer package</h3><pre><strong>library</strong>(DataExplorer)<br>miss_plot &lt;- plot_missing(data)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uLnOCb1-5KV2tYfp.png" /></figure><p>This is very quick and doesn’t need any extra line of code to compute missingness in data. Also, we can edit or update the profiling criteria as per our use.</p><h3>Removing completely missing data</h3><p>Like plotting missing data, there are some accessible functions that can help us omitting the columns missing data completely. However, I’ll first show how we can write our own code to do this job.</p><ul><li>At first, I’ll extract the data from the plot_missing() function object, i.e., miss_plot, created in the last step.</li></ul><pre>miss_plot$data</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7kRmnQGCJrGuQ8ia9JQAjA.png" /></figure><p>We can see that this data object consists of four columns and 59 rows. Rows are essentially the column names from our original dataset. Therefore, we can use the pct_missing column to filter the unwanted columns.</p><pre>miss_plot$data |&gt; <br>  filter(pct_missing == 1)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rMZHzB2uJpRkFoY3ZCF4vw.png" /></figure><p>Then we can pull the names of the completely missing columns and save them as a new object for further processing.</p><pre>miss_cols &lt;- miss_plot$data |&gt; <br>  filter(pct_missing == 1) |&gt; <br>  pull(feature)</pre><pre>miss_cols</pre><pre>##  [1] <strong>High</strong>.levels.of.uncertainty.associated.with.the.risk.scores.for.this.pest ##  [2] <strong>Potential</strong>.impact.of.key.uncertainty                                      ##  [3] <strong>Potential</strong>.impact.of.Climate.uncertainty                                  ##  [4] <strong>Potential</strong>.impact.of.Current.Distribution.uncertainty                     ##  [5] <strong>Potential</strong>.impact.of.UK.Distribution.uncertainty                          ##  [6] <strong>Potential</strong>.impact.of.Hosts.uncertainty                                    ##  [7] <strong>Potential</strong>.impact.of.Impact.uncertainty                                   ##  [8] <strong>Potential</strong>.impact.of.Pathways.uncertainty                                 ##  [9] <strong>Potential</strong>.impact.of.Regulation.uncertainty                               ## [10] <strong>Potential</strong>.impact.of.Taxonomy.uncertainty                                 ## [11] <strong>Other</strong>.uncertainty                                                        ## [12] <strong>Potential</strong>.impact.of.Other.uncertainty                                    ## 59 <strong>Levels</strong>: <strong>High</strong>.levels.of.uncertainty.associated.with.the.risk.scores.for.this.pest ...</pre><p>Now we can use the select function to remove the unwanted columns.</p><pre>clean_data &lt;- data |&gt; <br>  select(-miss_cols)</pre><pre><em># clean data</em><br>glimpse(clean_data)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i-dRRQTJSeg9Q36uovtS_w.png" /></figure><pre><em># missingness in clean data</em> <br>plot_missing(clean_data)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hMWLy9CnTnGHPxzM.png" /></figure><ul><li>We can see that columns with 100% missing values have been removed.</li></ul><p>Now, I’ll share other functions we can use to remove missing features.</p><h3>remove_empty function from janitor package</h3><pre><strong>library</strong>(janitor)<br>remove_empty(data) |&gt; <br>  plot_missing()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yu3YL3psj_LN4epp.png" /></figure><h3>where function</h3><pre>data |&gt; <br>  select(<br>    where(<br>      ~sum(!is.na(.x)) &gt; 0<br>    )<br>  ) |&gt; <br>  plot_missing()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dqlpp_ibz2VPCWc0.png" /></figure><pre>data |&gt; <br>  select(<br>    where(<br>      ~!all(is.na(.x))<br>    )<br>  ) |&gt; <br>  plot_missing()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*iB1Epae73Is7n56D.png" /></figure><h3>Base functions</h3><pre>data[colSums(!is.na(data)) &gt; 0] |&gt; <br>  plot_missing()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*umM-1n1DWhOGTgqA.png" /></figure><p><strong>That’s it!</strong></p><p><strong>Feel free to reach me out if you got any questions.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f2d7d02e989e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making Publication Quality Inset Maps in R using ggplot2]]></title>
            <link>https://blog.devgenius.io/making-publication-quality-inset-maps-in-r-using-ggplot2-85359c492ac8?source=rss-21e59c6e1c3b------2</link>
            <guid isPermaLink="false">https://medium.com/p/85359c492ac8</guid>
            <category><![CDATA[maps]]></category>
            <category><![CDATA[ggplot2]]></category>
            <category><![CDATA[mapping]]></category>
            <category><![CDATA[visualization]]></category>
            <category><![CDATA[r]]></category>
            <dc:creator><![CDATA[Mohsin Ramay]]></dc:creator>
            <pubDate>Wed, 24 Aug 2022 18:20:35 GMT</pubDate>
            <atom:updated>2022-08-28T08:44:14.981Z</atom:updated>
            <content:encoded><![CDATA[<p>In this blog post, I’ll show how we can make publication quality inset maps in R using ggplot2 package.</p><p>When publishing scientific research in journals or presenting research work at a conference, showing maps of data collection sites or experimental locations is one of the key visualizations. Maps of study sites/locations can help the audience and readers to fathom the data in a better way.</p><p>Mapping sounds fancy, but it needs substantial training and skill set to make high-quality maps that are reader-friendly and visually aesthetic. Sometimes, the study sites are more dispersed and are easy to visualize in large geographic areas. However, in some cases, study sites are clustered, which makes it hard to show them on a broader scale. In that case, inset maps help us show the locations with reference to familiar geographical regions.</p><p>An inset map is a smaller map featured on the same page as the main map. Traditionally, inset maps are shown at a larger scale (smaller area) than the main map. Often, an inset map is used as a locator map that shows the area of the main map in a broader, more familiar geographical frame of reference.</p><h3>Load libraries</h3><pre><strong>library</strong>(tidyverse)<br><strong>library</strong>(readxl)<br><strong>library</strong>(ozmaps) <br><strong>library</strong>(grid)<br><strong>library</strong>(gt)</pre><h3>Load data</h3><p>For this blog post, I’ll use geographical coordinates data of different experimental sites in New South Wales, Australia. These sites consist of Adaptation and Demonstration trials on different weed grasses.</p><h3>Experiment coordinates</h3><pre><em># coordinates</em><br>data &lt;- read.csv(&quot;https://raw.githubusercontent.com/MohsinRamay/sampledata/main/GPS_coordinates_for_map.csv&quot;) %&gt;% <br>  select(-3)</pre><pre>head(data)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ao8ie2Smu7k5G_f29mbS1w.png" /></figure><h3>Map of Australia</h3><ul><li>I’ll extract the map of Australia from the ozmaps package using the ozmap function.</li></ul><pre><em># Australia map</em><br>sf_aus &lt;- ozmap(&quot;states&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Xbcyskom-Yatgx-q.png" /></figure><h3>Closest city names</h3><p>I’ll extract the closest city names and their coordinates from the original data set by filtering the first row of each grouped Town data.</p><pre><em># cities </em><br>town &lt;- data %&gt;% <br>  arrange(Town) %&gt;% <br>  group_by(Town) %&gt;% <br>  filter(row_number()==1) %&gt;% <br>  ungroup()</pre><pre>gt(town)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b3ZuFCYNa0mcrx8alNqhCw.png" /></figure><p><strong>Map</strong></p><p>At first, I’ll create a simple map for the whole country of Australia with experimental sites. I’ll use different symbols for site types and different colors for weed grasses.</p><h3>Raw Map</h3><pre>data %&gt;% <br>  ggplot() + <br>  geom_sf(data = sf_aus) +<br>  geom_point(aes(x = Longitude, y = Latitude, color = Weed.species, shape = Site)) +<br>  xlim(112, 155) +<br>  labs() +<br>  theme_bw()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*wO7MFnEqx7YIpNOb.png" /></figure><p>As you can see, the study sites are clustered (in New South Wales) when plotted on the country-wide scaled map. However, to make a better sense of the study locations with reference to nearby cities/towns, we need to plot them on a focused scale. For that purpose, we first need to identify the extent of the study sites.</p><h3>Extent</h3><p>I’ll use simple horizontal and vertical lines to visualize the extent of the study sites.</p><pre>data %&gt;% <br>  ggplot() + <br>  geom_sf(data = sf_aus) +<br>  geom_point(aes(x = Longitude, y = Latitude, color = Weed.species, shape = Site)) +<br>  xlim(112, 155) +<br>  geom_hline(yintercept = -38, lty = 2, colour = &quot;red&quot;) +<br>  geom_hline(yintercept = -29, lty = 2, colour = &quot;red&quot;) +<br>  geom_vline(xintercept = 147, lty = 2, colour = &quot;red&quot;) +<br>  geom_vline(xintercept = 153, lty = 2, colour = &quot;red&quot;) +<br>  labs() +<br>  theme_bw()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*la6Lk4V06M8gMIZb.png" /></figure><p>Using the min and max values of coordinates from the previous map, we can draw a polygon over the study sites and see if this extent can best visualize the data.</p><pre>data %&gt;% <br>  ggplot() + <br>  geom_sf(data = sf_aus) +<br>  geom_point(aes(x = Longitude, y = Latitude, color = Weed.species, shape = Site)) +<br>  xlim(112, 155) +<br>  geom_rect(aes(xmin = 147, xmax = 153, ymin = -38, ymax = -29), color = &quot;red&quot;, fill = NA) +<br>  labs() +<br>  theme_bw()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*03viSqRgFhdlu_J9.png" /></figure><h3>Focus Map</h3><p>Now, I’ll plot a focused version of the study sites using the extent coordinates determined in the previous steps.</p><pre>data %&gt;% <br>  mutate(point_size = ifelse(Site == &quot;Demonstration&quot;, 1, 0)) %&gt;% <br>  ggplot() + <br>  geom_sf(data = sf_aus) +<br>  geom_point(aes(x = Longitude, y = Latitude, color = Weed.species, shape = Site, size = point_size &gt; 0), alpha = 0.75) +<br>  scale_size_manual(values=c(2,3.5)) +<br>  xlim(147, 153) +<br>  ylim(-38, -30) +<br>  theme_test() +<br>  guides(size = &quot;none&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*acX3sVR0Gagejq8A.png" /></figure><p>This version looks better compared to the previous one. However, we need to add some information to give it a reference. Also, we need to remove the borderline of Canberra in the southern part of New South Wales. For that purpose, I’ll dissolve the boundary line using the st_union function of library sf. We’ll then use the dissolved polygon in subsequent maps.</p><pre><em># Now the dissolve</em><br><strong>library</strong>(sf)</pre><pre>AN = sf_aus %&gt;% <br>  filter(NAME %<strong>in</strong>% c(&quot;New South Wales&quot;, &quot;Australian Capital Territory&quot;))</pre><pre>NM &lt;- st_union(AN)</pre><pre>plot(NM)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1hR8cDAsJR040cE3.png" /></figure><h3>Add Map elements</h3><p>Professional maps also include some elements like North Arrow and scale etc. We’ll add these components to our map as well. Besides that, I’ll also add the closest town names and fill the non-land area with lightblue color for reference and distinction respectively.</p><pre>ss &lt;- data %&gt;% <br>  mutate(point_size = ifelse(Site == &quot;Demonstration&quot;, 1, 0)) %&gt;% <br>  ggplot() + <br>  geom_sf(data = NM) +<br>  geom_point(aes(x = Longitude, y = Latitude, color = Weed.species, shape = Site, size = point_size &gt; 0), alpha = 0.75) +<br>  scale_size_manual(values=c(2,3.5)) +<br>  xlim(147, 153) +<br>  ylim(-38, -30) +<br>  ggrepel::geom_text_repel(aes(x = Longitude, y = Latitude, label = Town), data = town, nudge_y = 0.06, nudge_x = 0.06, size = 2) +<br>  labs(color = &quot;Weed species&quot;) +<br>  theme_test() +<br>  theme(panel.background = element_rect(fill = &quot;lightblue&quot;)) +<br>  guides(size = &quot;none&quot;) +<br>  ggsn::north(location = &quot;topleft&quot;, scale = 0.8, symbol = 12,<br>               x.min = 151.5, x.max = 152.5, y.min = -36, y.max = -38) +<br>  ggsn::scalebar(location = &quot;bottomleft&quot;, dist = 100,<br>           dist_unit = &quot;km&quot;, transform = TRUE, <br>           x.min=150.5, x.max=152, y.min=-38, y.max=-30,<br>           st.bottom = FALSE, height = 0.025,<br>           st.dist = 0.05, st.size = 3)</pre><pre>ss</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*PRl69daQGLmmppLV.png" /></figure><h3>Inset Map</h3><p>Now, I’ll create a full-scale map of Australia with a red polygon showing the extent of study sites and the focused map.</p><pre><em># Map of Australia</em><br>inset &lt;- sf_aus %&gt;% <br>  ggplot() + <br>  geom_sf() +<br>  <em>#geom_sf(data = NM) +</em><br>  geom_rect(aes(xmin = 147, xmax = 153, ymin = -38, ymax = -30), color = &quot;red&quot;, fill = NA) +<br>  xlim(112, 155) +<br>  labs(x = NULL, y = NULL) +<br>  theme_test() + <br>  theme(axis.text = element_blank(),<br>        axis.ticks = element_blank(),<br>        axis.ticks.length = unit(0, &quot;pt&quot;),<br>        axis.title=element_blank(),<br>        plot.margin = margin(0, 0, 0, 0, &quot;cm&quot;),<br>        panel.background = element_rect(fill = &quot;lightblue&quot;))</pre><pre>inset</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Nbi7NB6B94sNxW4u.png" /></figure><h3>Final Map</h3><p>Now, I’ll combine both maps where the map of Australia is inset on the upper left corner.</p><pre>ss</pre><pre><em># Combining both maps</em><br>print(inset, vp = viewport(0.322, 0.859, width = 0.25, height = 0.25))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uGbM0MZXP-YmcttG.png" /></figure><p>This inset map better shows the locations of study sites with reference to the country and provinces and is more professional.</p><p><strong>That’s it!</strong></p><p><strong>Feel free to reach me out if you got any questions.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=85359c492ac8" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/making-publication-quality-inset-maps-in-r-using-ggplot2-85359c492ac8">Making Publication Quality Inset Maps in R using ggplot2</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Block Bootstrapping in R using Tidymodels]]></title>
            <link>https://mohsinramay.medium.com/block-bootstrapping-in-r-using-tidymodels-e8191f439f8d?source=rss-21e59c6e1c3b------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8191f439f8d</guid>
            <category><![CDATA[r]]></category>
            <category><![CDATA[tidyverse]]></category>
            <category><![CDATA[ggplot2]]></category>
            <category><![CDATA[bootstrap]]></category>
            <category><![CDATA[tidymodels]]></category>
            <dc:creator><![CDATA[Mohsin Ramay]]></dc:creator>
            <pubDate>Sat, 23 Jul 2022 10:49:27 GMT</pubDate>
            <atom:updated>2022-07-23T14:41:43.527Z</atom:updated>
            <content:encoded><![CDATA[<p>Often, we have situations where we want to bootstrap our analyses but in a way that all of the observations from a particular group (block) are randomly sampled than a mixture of observations from all groups. This can be achieved using block bootstrapping. Unfortunately, there is no easy or ready-made function available that can conduct block bootstrapping. However, we can use certain functions in base R and tidyverse packages to do this. In this blog post, I’ll show how we can perform block bootstrapping in R using tidyverse and tidymodels packages.</p><h3>Load libraries</h3><pre><strong>library</strong>(tidyverse)<br><strong>library</strong>(tidymodels)<br><strong>library</strong>(viridis)</pre><h3>Load data</h3><p>For this blog post, I’ll use crop yield data from the UK from different study sites (locations) and years from 2002–2019. This data also contains monthly (Jan — Dec) weather data obtained from 2002 to 2019. So, let’s suppose we want to get the bootstrapped regression coefficient of the effect of temperature on the yield gap each month. And for this purpose, when bootstrapping data, we want to block the Year data in a way that the data from one Year is randomly sampled in each bootstrap rather than random rows from mixed years.</p><pre>data &lt;- read_csv(&quot;https://raw.githubusercontent.com/MohsinRamay/yieldgap/main/ww_wt.csv&quot;)</pre><pre>glimpse(data)</pre><pre><strong>#</strong><em># Rows: 1,924</em><br><strong>#</strong><em># Columns: 20</em><br><strong>#</strong><em># $ ...1       &lt;dbl&gt; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, …</em><br><strong>#</strong><em># $ ID         &lt;chr&gt; &quot;2002_Cirencester&quot;, &quot;2002_Cirencester&quot;, &quot;2002_Cirencester&quot;,…</em><br><strong>#</strong><em># $ Year       &lt;dbl&gt; 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002,…</em><br><strong>#</strong><em># $ Location   &lt;chr&gt; &quot;Cirencester&quot;, &quot;Cirencester&quot;, &quot;Cirencester&quot;, &quot;Cirencester&quot;,…</em><br><strong>#</strong><em># $ Latitude   &lt;dbl&gt; 51.68076, 51.68076, 51.68076, 51.68076, 51.68076, 51.68076,…</em><br><strong>#</strong><em># $ Longitude  &lt;dbl&gt; -1.9580083, -1.9580083, -1.9580083, -1.9580083, -1.9580083,…</em><br><strong>#</strong><em># $ YieldTrt   &lt;dbl&gt; 9.642286, 9.642286, 9.642286, 9.642286, 9.642286, 9.642286,…</em><br><strong>#</strong><em># $ YieldUntrt &lt;dbl&gt; 6.916857, 6.916857, 6.916857, 6.916857, 6.916857, 6.916857,…</em><br><strong>#</strong><em># $ Mildew     &lt;dbl&gt; NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…</em><br><strong>#</strong><em># $ YellowRust &lt;dbl&gt; NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…</em><br><strong>#</strong><em># $ BrownRust  &lt;dbl&gt; 1.028571, 1.028571, 1.028571, 1.028571, 1.028571, 1.028571,…</em><br><strong>#</strong><em># $ Septoria   &lt;dbl&gt; 21.151429, 21.151429, 21.151429, 21.151429, 21.151429, 21.1…</em><br><strong>#</strong><em># $ gap        &lt;dbl&gt; 0.2822753, 0.2822753, 0.2822753, 0.2822753, 0.2822753, 0.28…</em><br><strong>#</strong><em># $ Month      &lt;chr&gt; &quot;September&quot;, &quot;October&quot;, &quot;November&quot;, &quot;December&quot;, &quot;January&quot;, …</em><br><strong>#</strong><em># $ temp       &lt;dbl&gt; 13.646974, 13.336226, 7.065802, 3.442173, 5.610458, 6.92963…</em><br><strong>#</strong><em># $ Date       &lt;date&gt; 2001-09-16, 2001-10-16, 2001-11-16, 2001-12-16, 2002-01-16…</em><br><strong>#</strong><em># $ rh         &lt;dbl&gt; 81.12983, 87.08586, 86.82285, 85.21759, 89.50930, 82.90430,…</em><br><strong>#</strong><em># $ sun        &lt;dbl&gt; 117.05141, 87.53126, 76.80444, 87.87857, 27.68811, 75.24004…</em><br><strong>#</strong><em># $ rnf        &lt;dbl&gt; 39.70032, 95.11663, 40.74033, 21.97640, 78.55315, 102.96212…</em><br><strong>#</strong><em># $ Season     &lt;dbl&gt; 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002,…</em></pre><h3>Study sites</h3><ul><li>Let’s plot the data on Map first to see the spatial distribution of study sites.</li></ul><pre><strong>library</strong>(maps)<br>UK &lt;- map_data(&quot;world&quot;) %&gt;% <br>  filter(region==&quot;UK&quot;)</pre><pre># Map<br>data %&gt;% <br>  distinct(Location, Longitude, Latitude) %&gt;% <br>  ggplot() +<br>  geom_polygon(data = UK, aes(x=long, y = lat, group = group), fill=&quot;grey&quot;, alpha=0.3, colour = &quot;black&quot;) +<br>  geom_point(aes(x = Longitude, y = Latitude), colour = &quot;steelblue&quot;) +<br>  coord_map() +<br>  labs(x = NULL, y = NULL, size = NULL, title = &quot;Study sites&quot;, subtitle = &quot;(2002 - 2020)&quot;) +<br>  theme_minimal() + <br>  theme(plot.title = element_text(hjust = 0.5),<br>        plot.subtitle = element_text(hjust = 0.5))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*T2nEt1p_vnn1wvC4.png" /></figure><h3>Block bootstrapping</h3><ul><li>First, I’ll select only those variables needed for final analysis, i.e., Year, Month, gap and temp.</li><li>gap is the Yield gap, a variable of interest.</li></ul><pre>data %&gt;% <br>  ungroup() %&gt;% <br>  select(Year, Month, gap, temp)</pre><pre><strong>#</strong><em># # A tibble: 1,924 × 4</em><br><strong>#</strong><em>#     Year Month       gap  temp</em><br><strong>#</strong><em>#    &lt;dbl&gt; &lt;chr&gt;     &lt;dbl&gt; &lt;dbl&gt;</em><br><strong>#</strong><em>#  1  2002 September 0.282 13.6 </em><br><strong>#</strong><em>#  2  2002 October   0.282 13.3 </em><br><strong>#</strong><em>#  3  2002 November  0.282  7.07</em><br><strong>#</strong><em>#  4  2002 December  0.282  3.44</em><br><strong>#</strong><em>#  5  2002 January   0.282  5.61</em><br><strong>#</strong><em>#  6  2002 February  0.282  6.93</em><br><strong>#</strong><em>#  7  2002 March     0.282  7.67</em><br><strong>#</strong><em>#  8  2002 April     0.282  9.42</em><br><strong>#</strong><em>#  9  2002 May       0.282 11.8 </em><br><strong>#</strong><em># 10  2002 June      0.282 14.1 </em><br><strong>#</strong><em># # … with 1,914 more rows</em><br><strong>#</strong><em># # ℹ Use `print(n = ...)` to see more rows</em></pre><ul><li>Second, I’ll widen the temperature data by Months, so we have all data for a year at a specific study site in the same row.</li></ul><pre>data %&gt;% <br>  ungroup() %&gt;% <br>  select(Year, Month, gap, temp) %&gt;%<br>  pivot_wider(names_from = Month, values_from = temp, values_fn = mean)</pre><pre><strong>#</strong><em># # A tibble: 159 × 14</em><br><strong>#</strong><em>#     Year   gap Septe…¹ October Novem…² Decem…³ January Febru…⁴ March April   May</em><br><strong>#</strong><em>#    &lt;dbl&gt; &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;</em><br><strong>#</strong><em>#  1  2002 0.282    13.6    13.3    7.07    3.44    5.61    6.93  7.67  9.42  11.8</em><br><strong>#</strong><em>#  2  2002 0.164    12.7    12.6    7.61    3.47    5.38    5.74  6.78  8.65  11.3</em><br><strong>#</strong><em>#  3  2002 0.226    13.3    13.6    7.02    3.59    5.04    6.74  7.42  9.26  12.3</em><br><strong>#</strong><em>#  4  2002 0.108    13.1    13.3    6.71    3.31    4.74    6.61  7.16  9.14  12.0</em><br><strong>#</strong><em>#  5  2002 0.311    12.9    12.9    6.69    3.01    4.83    6.51  7.17  8.98  11.6</em><br><strong>#</strong><em>#  6  2002 0.353    13.4    13.6    7.02    3.57    5.24    7.03  7.55  9.57  12.3</em><br><strong>#</strong><em>#  7  2002 0.268    13.2    13.0    7.36    3.14    5.03    6.82  7.40  9.00  11.8</em><br><strong>#</strong><em>#  8  2002 0.341    14.0    13.8    7.32    3.74    6.00    7.38  7.92  9.85  12.2</em><br><strong>#</strong><em>#  9  2002 0.309    13.4    13.9    7.05    3.40    5.11    6.91  7.46  9.35  12.2</em><br><strong>#</strong><em># 10  2003 0.139    14.8    10.4    8.55    6.04    4.44    4.32  7.77  9.97  12.7</em><br><strong>#</strong><em># # … with 149 more rows, 3 more variables: June &lt;dbl&gt;, July &lt;dbl&gt;, August &lt;dbl&gt;,</em><br><strong>#</strong><em># #   and abbreviated variable names ¹​September, ²​November, ³​December, ⁴​February</em><br><strong>#</strong><em># # ℹ Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names</em></pre><ul><li>Third, I’ll use the bootstraps function from the rsample package to replicate data. The output will consist of a list of tibbles with bootstraps classes as id and a column for the data split objects.</li></ul><pre>data %&gt;% <br>  ungroup() %&gt;% <br>  select(Year, Month, gap, temp) %&gt;%<br>  pivot_wider(names_from = Month, values_from = temp, values_fn = mean) %&gt;% <br>  bootstraps(times = 10, apparent = FALSE)</pre><pre><strong>#</strong><em># # Bootstrap sampling </em><br><strong>#</strong><em># # A tibble: 10 × 2</em><br><strong>#</strong><em>#    splits           id         </em><br><strong>#</strong><em>#    &lt;list&gt;           &lt;chr&gt;      </em><br><strong>#</strong><em>#  1 &lt;split [159/57]&gt; Bootstrap01</em><br><strong>#</strong><em>#  2 &lt;split [159/57]&gt; Bootstrap02</em><br><strong>#</strong><em>#  3 &lt;split [159/59]&gt; Bootstrap03</em><br><strong>#</strong><em>#  4 &lt;split [159/62]&gt; Bootstrap04</em><br><strong>#</strong><em>#  5 &lt;split [159/50]&gt; Bootstrap05</em><br><strong>#</strong><em>#  6 &lt;split [159/57]&gt; Bootstrap06</em><br><strong>#</strong><em>#  7 &lt;split [159/52]&gt; Bootstrap07</em><br><strong>#</strong><em>#  8 &lt;split [159/63]&gt; Bootstrap08</em><br><strong>#</strong><em>#  9 &lt;split [159/61]&gt; Bootstrap09</em><br><strong>#</strong><em># 10 &lt;split [159/54]&gt; Bootstrap10</em></pre><p><strong>Note:</strong> Here, I have used only 10 bootstrap replications. However, this number can be increased as per choice.</p><ul><li>After that, I’ll use the map function to extract the data from each bootstrap split. It will return a list of tibbles where each tibble is a bootstrap sample.</li><li>I’ll then use the unnest function from the tidyr package to unnest tibbles to a data frame.</li></ul><pre>data %&gt;% <br>  ungroup() %&gt;% <br>  select(Year, Month, gap, temp) %&gt;%<br>  pivot_wider(names_from = Month, values_from = temp, values_fn = mean) %&gt;% <br>  bootstraps(times = 10, apparent = FALSE) %&gt;% <br>  mutate(splits = purrr::map(splits, analysis)) %&gt;%<br>  unnest(splits)</pre><pre><strong>#</strong><em># # A tibble: 1,590 × 15</em><br><strong>#</strong><em>#     Year     gap September October November December January Febru…¹ March April</em><br><strong>#</strong><em>#    &lt;dbl&gt;   &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;</em><br><strong>#</strong><em>#  1  2004  0.125       14.1    9.03     8.16    4.68    4.50    5.32   6.43  9.43</em><br><strong>#</strong><em>#  2  2010 NA           13.3   11.1      7.72    3.03    2.29    2.63   5.48  8.36</em><br><strong>#</strong><em>#  3  2013  0.165       12.5    9.01     5.91    4.15    2.73    2.38   1.99  6.99</em><br><strong>#</strong><em>#  4  2015  0.276       13.4   10.4      7.20    3.75    3.14    3.52   5.32  7.49</em><br><strong>#</strong><em>#  5  2008  0.330       14.5   11.2      7.63    5.36    6.80    5.72   6.53  8.59</em><br><strong>#</strong><em>#  6  2004  0.232       13.5    8.27     6.85    3.56    3.64    4.44   5.19  8.44</em><br><strong>#</strong><em>#  7  2003  0.130       14.7   10.4      8.71    5.95    4.32    4.07   7.78  9.60</em><br><strong>#</strong><em>#  8  2015  0.386       14.1   11.3      8.04    5.58    4.57    4.88   6.56  8.09</em><br><strong>#</strong><em>#  9  2017  0.351       13.3    8.53     3.41    4.75    2.50    3.80   6.00  6.87</em><br><strong>#</strong><em># 10  2010  0.0700      11.9    8.77     5.77    0.863  -0.355   0.164  3.88  6.87</em><br><strong>#</strong><em># # … with 1,580 more rows, 5 more variables: May &lt;dbl&gt;, June &lt;dbl&gt;, July &lt;dbl&gt;,</em><br><strong>#</strong><em># #   August &lt;dbl&gt;, id &lt;chr&gt;, and abbreviated variable name ¹​February</em><br><strong>#</strong><em># # ℹ Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names</em></pre><ul><li>At this point, block bootstrapping has been done. However, we need the data in a long format for further data analysis and visualizations.</li><li>Therefore, I’ll use pivot_longer function to transform this wide data into a long format.</li><li>To keep track of observations in each bootstrapped sample, I’ll create a row ID before converting the data to the long format.</li></ul><pre>set.seed(123)</pre><pre><em># Block bootstrapping</em><br>boots &lt;- data %&gt;% <br>  ungroup() %&gt;% <br>  select(Year, Month, gap, temp) %&gt;%<br>  pivot_wider(names_from = Month, values_from = temp, values_fn = mean) %&gt;% <br>  bootstraps(times = 10, apparent = FALSE) %&gt;% <br>  mutate(splits = purrr::map(splits, analysis)) %&gt;%<br>  unnest(splits) %&gt;% <br>  group_by(id) %&gt;% <br>  mutate(row = row_number()) %&gt;% <br>  pivot_longer(names_to = &quot;Month&quot;, values_to = &quot;temp&quot;, cols = September:August)</pre><pre>boots</pre><pre><strong>#</strong><em># # A tibble: 19,080 × 6</em><br><strong>#</strong><em># # Groups:   id [10]</em><br><strong>#</strong><em>#     Year    gap id            row Month      temp</em><br><strong>#</strong><em>#    &lt;dbl&gt;  &lt;dbl&gt; &lt;chr&gt;       &lt;int&gt; &lt;chr&gt;     &lt;dbl&gt;</em><br><strong>#</strong><em>#  1  2020 0.0979 Bootstrap01     1 September 15.2 </em><br><strong>#</strong><em>#  2  2020 0.0979 Bootstrap01     1 October   11.8 </em><br><strong>#</strong><em>#  3  2020 0.0979 Bootstrap01     1 November   8.15</em><br><strong>#</strong><em>#  4  2020 0.0979 Bootstrap01     1 December   7.54</em><br><strong>#</strong><em>#  5  2020 0.0979 Bootstrap01     1 January    7.37</em><br><strong>#</strong><em>#  6  2020 0.0979 Bootstrap01     1 February   7.65</em><br><strong>#</strong><em>#  7  2020 0.0979 Bootstrap01     1 March      7.89</em><br><strong>#</strong><em>#  8  2020 0.0979 Bootstrap01     1 April     12.0 </em><br><strong>#</strong><em>#  9  2020 0.0979 Bootstrap01     1 May       13.3 </em><br><strong>#</strong><em># 10  2020 0.0979 Bootstrap01     1 June      15.0 </em><br><strong>#</strong><em># # … with 19,070 more rows</em><br><strong>#</strong><em># # ℹ Use `print(n = ...)` to see more rows</em></pre><h3>Visualize Block Bootstrapped Data</h3><ul><li>Now, we can visualize the bootstrapped data to check whether the data has been correctly bootstrapped, i.e., within each bootstrap, data from each Year is being randomly sampled with replacement.</li></ul><pre><em># Bootstraps</em><br>boots %&gt;%<br>  ggplot(aes(x = id, y = row, fill = Year)) +<br>  geom_tile() +<br>  scale_fill_viridis(option = &quot;B&quot;, direction = 1) +<br>  labs(x = NULL) +<br>  facet_wrap(~Month) +<br>  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.4))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*L5jlR_BGo5ueI9Bf.png" /></figure><p>The above plots indicate that, within each bootstrap, data from each Year is being randomly sampled with replacement. Thus, we have bootstrapped data ready for subsequent analysis.</p><h3>Regression Analysis</h3><h3>1000 bootstraps</h3><ul><li>I’ll rerun the block bootstrapping with 1000 replicates for regression analysis.</li></ul><pre>boots &lt;- data %&gt;% <br>  ungroup() %&gt;% <br>  select(Year, Month, gap, temp) %&gt;%<br>  pivot_wider(names_from = Month, values_from = temp, values_fn = mean) %&gt;% <br>  bootstraps(times = 1000, apparent = FALSE) %&gt;% <br>  mutate(splits = purrr::map(splits, analysis)) %&gt;%<br>  unnest(splits) %&gt;% <br>  group_by(id) %&gt;% <br>  mutate(row = row_number()) %&gt;% <br>  pivot_longer(names_to = &quot;Month&quot;, values_to = &quot;temp&quot;, cols = September:August)</pre><h3>Model</h3><ul><li>I’ll use the tidymodels functionality to fit the regression model between yield gap and temperature.</li></ul><pre>boot_reg &lt;- boots %&gt;% <br>  nest(data = -c(Month, id)) %&gt;% <em># grouping the Month and Bootstrap samples</em><br>  mutate(model = purrr::map(data, ~lm(gap ~ temp, data = .x)), <br>         coefs = purrr::map(model, tidy)) <em># performing regression</em><br>  <br>reg &lt;- boot_reg %&gt;% <br>  unnest(coefs) %&gt;% <em># unnesting tidied data frames</em><br>  select(-data, -id, -model)</pre><pre>reg</pre><pre><strong>#</strong><em># # A tibble: 24,000 × 7</em><br><strong>#</strong><em># # Groups:   id [1,000]</em><br><strong>#</strong><em>#    id            Month     term        estimate std.error statistic     p.value</em><br><strong>#</strong><em>#    &lt;chr&gt;         &lt;chr&gt;     &lt;chr&gt;          &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;       &lt;dbl&gt;</em><br><strong>#</strong><em>#  1 Bootstrap0001 September (Intercept)  0.261     0.104       2.50  0.0136     </em><br><strong>#</strong><em>#  2 Bootstrap0001 September temp        -0.00312   0.00750    -0.416 0.678      </em><br><strong>#</strong><em>#  3 Bootstrap0001 October   (Intercept)  0.0154    0.0572      0.269 0.789      </em><br><strong>#</strong><em>#  4 Bootstrap0001 October   temp         0.0186    0.00519     3.58  0.000466   </em><br><strong>#</strong><em>#  5 Bootstrap0001 November  (Intercept)  0.186     0.0417      4.46  0.0000164  </em><br><strong>#</strong><em>#  6 Bootstrap0001 November  temp         0.00446   0.00569     0.783 0.435      </em><br><strong>#</strong><em>#  7 Bootstrap0001 December  (Intercept)  0.108     0.0223      4.85  0.00000305 </em><br><strong>#</strong><em>#  8 Bootstrap0001 December  temp         0.0224    0.00422     5.30  0.000000421</em><br><strong>#</strong><em>#  9 Bootstrap0001 January   (Intercept)  0.105     0.0284      3.69  0.000313   </em><br><strong>#</strong><em># 10 Bootstrap0001 January   temp         0.0249    0.00598     4.17  0.0000524  </em><br><strong>#</strong><em># # … with 23,990 more rows</em><br><strong>#</strong><em># # ℹ Use `print(n = ...)` to see more rows</em></pre><h3>Violin charts</h3><ul><li>I’ll use violin charts to plot the distribution of bootstrapped regression coefficients.</li></ul><pre>reg %&gt;%<br>  filter(term == &quot;temp&quot;) %&gt;% <br>  group_by(Month) %&gt;%<br>  mutate(ci.95 = ifelse(estimate &gt; quantile(estimate, 0.025) &amp;  estimate &lt; quantile(estimate, 0.975), &quot;yes&quot;, &quot;No&quot;)) %&gt;% <br>  mutate(Month = factor(Month, levels=c(&quot;September&quot;,&quot;October&quot;,&quot;November&quot;,&quot;December&quot;,&quot;January&quot;,&quot;February&quot;,&quot;March&quot;,&quot;April&quot;,&quot;May&quot;,&quot;June&quot;,&quot;July&quot;, &quot;August&quot;))) %&gt;% <br>  ggplot(aes(x = Month, y = estimate)) +<br>  geom_violin(draw_quantiles = c(.25, .50, .75), fill = &quot;orange&quot;, alpha = 0.5) +<br>  geom_hline(yintercept = 0, lty = 2, colour = &quot;red&quot;) +<br>  labs(x = NULL, y = &quot;Bootstrapped estimates&quot;) +<br>  theme_test() +<br>  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.4))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*AuJ-4To6qCEoo4WS.png" /></figure><p>In this graph, Violin charts above the dashed red line indicate that temperature in the month of December, January, February and March has a significant positive relationship with the yield gap. This means the yield gap is increasing with rising winter temperatures, which is bad for food security. In other months, the distribution of bootstrapped estimates intersects the 0 line, thus indicating no statistically significant association.</p><p><strong>That’s it!</strong></p><p><strong>Feel free to reach me out if you got any questions.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8191f439f8d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>