<?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 infoart.ca on Medium]]></title>
        <description><![CDATA[Stories by infoart.ca on Medium]]></description>
        <link>https://medium.com/@infoart?source=rss-e57d1215ee0a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*AvoYi-ynFXEYSKIGagjXEQ.png</url>
            <title>Stories by infoart.ca on Medium</title>
            <link>https://medium.com/@infoart?source=rss-e57d1215ee0a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 12:05:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@infoart/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[Essential Housekeeping Functions for R Programmers]]></title>
            <link>https://infoart.medium.com/essential-housekeeping-functions-for-r-programmers-9058f17ad4ae?source=rss-e57d1215ee0a------2</link>
            <guid isPermaLink="false">https://medium.com/p/9058f17ad4ae</guid>
            <category><![CDATA[rstudio]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[infoart.ca]]></dc:creator>
            <pubDate>Thu, 02 Apr 2026 05:36:23 GMT</pubDate>
            <atom:updated>2026-04-02T05:36:23.369Z</atom:updated>
            <content:encoded><![CDATA[<p>R is less known for its vast repository of housekeeping methods/functions, but keeping them in your arsenal can help create highly efficient workflow, and of course … ease the fighting with the GUI. Below is a list of my favorite tools that you might helpful:</p><p>PLEASE NOTE THAT SOME OF THE METHODS MIGHT LOOK DIFFERENT IN WINDOWS SYSTEMS</p><h4>Workspace &amp; Environment Management</h4><pre>ls() - List all objects in the current environment<br>objects() - Alias for ls()<br>ls.str() - List objects with their structure<br>ls(all.names = TRUE) - Show hidden objects (starting with .)<br>rm() / remove() - Remove specific objects<br>rm(list = ls()) - Clear everything (use with care!)<br>rm(list = ls(all.names = TRUE)) - Nuclear option: clear hidden objects too<br>search() - Show the search path (loaded packages + environments)<br>attach() - Attach a data frame or list to the search path<br>detach() - Remove from search path<br>environment() - Get current environment<br>parent.env() - Get parent environment<br>new.env() - Create a new environment<br>assign() - Assign a value to an environment<br>get() / exists() - Retrieve or check existence in an environment</pre><h4>Working Directory &amp; File System</h4><pre>getwd() - Get current working directory<br>setwd() - Set working directory<br>list.files() / dir() - List files in directory<br>list.dirs() - List subdirectories<br>file.exists() - Check if a file/folder exists<br>file.info() - Get file metadata (size, modification time, etc.)<br>file.size() - Get file size in bytes<br>file.rename() - Rename a file<br>file.copy() - Copy a file<br>file.remove() - Delete a file<br>unlink() - Delete files or directories (recursive option)<br>dir.create() - Create a new directory<br>normalizePath() - Clean up and expand path<br>path.expand() - Expand ~ to home directory<br>file.path() - Safely construct cross-platform paths<br>basename() - Extract filename from path<br>dirname() - Extract directory from path<br>tempfile() - Generate a temporary file name<br>tempdir() - Get temporary directory path<br>normalizePath(getwd()) - Clean current working directory path</pre><blockquote>Pro tip: Always use file.path() instead of pasting strings with / or \</blockquote><h4>Graphics &amp; Device Management</h4><pre>dev.off() - Close the current graphics device<br>graphics.off() - Close all graphics devices<br>dev.list() - List all open devices<br>dev.cur() - Get current device number<br>dev.set() - Switch to a specific device<br>dev.new() - Open a new graphics device<br>par() - Set/query graphical parameters (use with withr::with_par for safety)<br>png() / pdf() / jpeg() / svg() - Open specific devices (pair with dev.off())<br>recordPlot() / replayPlot() - Save and replay a plot<br>dev.capture() - Capture plot as raster (newer R versions)</pre><blockquote><strong>Golden rule: </strong>End every plotting script with dev.off() or wrap in on.exit(dev.off())</blockquote><h4>Package Management</h4><pre>library() - Load a package<br>require() - Load quietly (returns FALSE on failure)<br>requireNamespace() - Check without attaching<br>install.packages() - Install from CRAN (or repos)<br>update.packages() - Update all installed packages<br>remove.packages() - Uninstall a package<br>installed.packages() - List all installed packages<br>available.packages() - List packages available on CRAN<br>.libPaths() - View or set library paths<br>find.package() - Find location of an installed package<br>.packages() - List currently attached packages<br>sessionInfo() - Full session info (R version + packages)<br>packageVersion() - Get version of a specific package<br>citation() - Get citation for a package<br>news(package = &quot;dplyr&quot;) - View package news/changelog</pre><h4>Session Information &amp; System Details</h4><pre>R.version - R version string<br>getRversion() - R version as package_version object<br>Sys.info() - Operating system and user info<br>.Platform - Platform-specific details<br>capabilities() - What features R was compiled with<br>R.home() - R installation directory<br>Sys.getenv() - Get environment variables<br>Sys.setenv() - Set environment variables<br>Sys.setlocale() - Set locale (for dates, numbers, etc.)<br>options() / getOption() - Global options (e.g., digits, warn, scipen)</pre><h4>Memory &amp; Performance</h4><pre>gc() - Trigger garbage collection<br>gcinfo() - Get/set garbage collection info<br>object.size() - Size of an object in bytes<br>memory.size() / memory.limit() - Windows-specific memory info<br>pryr::mem_used() - (from pryr) Human-readable memory usage</pre><h4>Console Output &amp; Logging</h4><pre>cat() - Concatenate and print<br>print() - Print with class methods<br>message() - Print diagnostic message<br>warning() - Issue a warning<br>stop() - Stop with error<br>sink() - Redirect output to file<br>capture.output() - Capture printed output as character vector<br>invisible() - Return value without printing<br>suppressWarnings() / suppressMessages() - Silence warnings/messages<br>warnings() - Show last warnings</pre><h4>Saving &amp; Loading State</h4><pre>save.image() - Save entire workspace to .RData<br>load() - Load .RData file<br>save() - Save specific objects<br>saveRDS() / readRDS() - Save/read single objects (preferred!)<br>history() - Show command history<br>savehistory() - Save history to file<br>loadhistory() - Load history file</pre><h4><strong>System Interaction &amp; OS Commands</strong></h4><pre>system() - Run shell command and capture output<br>system2() - More flexible version of system()<br>shell() - Windows shell command<br>shell.exec(getwd()) - Open current folder in Windows Explorer<br>system(&quot;open .&quot;) / system(&quot;xdg-open .&quot;) - Open current folder on Mac/Linux</pre><h4>Reproducibility, Debugging &amp; RStudio-Specific</h4><pre>set.seed() - Set random seed (reproducibility)<br>rstudioapi::restartSession() - Clean restart of R session (RStudio only)<br>rstudioapi::filesPaneNavigate(getwd()) - Navigate Files pane to current folder (RStudio)</pre><h4>And some bonus everyday commands</h4><pre>.rs.restartR() (alternative restart in older RStudio)<br>on.exit() - Clean up automatically when a function exits<br>withr package helpers (with_dir(), with_options(), with_par()) for temporary changes</pre><p>Feel free to shae your favorite housekeeping trick in the comments …. what did I miss that belongs in the list?</p><p>Happy Housekeeping in R!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9058f17ad4ae" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Survival Analysis in Stata (Part 2: Cox Regression)]]></title>
            <link>https://infoart.medium.com/survival-analysis-in-stata-part-2-cox-regression-106781313b51?source=rss-e57d1215ee0a------2</link>
            <guid isPermaLink="false">https://medium.com/p/106781313b51</guid>
            <category><![CDATA[survival-analysis]]></category>
            <category><![CDATA[stata]]></category>
            <category><![CDATA[data-analytics]]></category>
            <dc:creator><![CDATA[infoart.ca]]></dc:creator>
            <pubDate>Thu, 12 Mar 2026 22:07:21 GMT</pubDate>
            <atom:updated>2026-03-12T22:07:21.779Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*spIJIboQ68prjCiw" /><figcaption>Photo by <a href="https://unsplash.com/@alelmes?utm_source=medium&amp;utm_medium=referral">Al Elmes</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>The Kaplan-Meier curves we drew in Part 1 gave us the “when” story. We saw that most young men in Zambia have their first sexual experience between ages 16 and 20, and that those living in rural areas tend to start slightly earlier than those in cities. But the Kaplan-Meier curve has one important limitation: it can only look at one variable at a time.</p><p>This raises an obvious question. When we saw that rural men have earlier first sex, was that really about where they live? Or is it because rural men also tend to have less education and lower wealth, and it is actually those factors doing the work? The Kaplan-Meier curve simply cannot tell us. Cox regression can.</p><p>Cox regression can dothe same job as any regression model we already know, like logistic or linear regression, but built specifically for time-to-event data. It adjusts for all variables at the same time and tells us which factors are independently associated with the timing of the event, after accounting for everything else in the model. This is what makes it so useful in practice.</p><p>The main output is a <strong>hazard ratio</strong>. This is the key number to understand:</p><ul><li>A hazard ratio above 1.0 means the event happens <strong>sooner</strong> in that group</li><li>A hazard ratio below 1.0 means the event happens <strong>later</strong></li><li>A hazard ratio of exactly 1.0 means <strong>no difference</strong> from the reference group</li></ul><p>The reference group is simply the baseline category that everything else is compared against. In our model it will be urban residence, no education, poorest wealth quintile, and Central province.</p><p>Because we already declared the data with stset in Part 1, we can run the model straight away. If we have closed Stata since then, we just need to re-run stset time_sex, failure(event_sex == 1) first.</p><pre>stset time_sex, failure(event_sex == 1)<br>stcox i.v025 i.v106 i.v190 i.v024, nolog cformat(%3.2f)</pre><p>The i. prefix tells Stata to treat each variable as categorical. The nolog option suppresses the iteration log to keep the output tidy, and cformat(%3.2f) rounds numbers to two decimal places.</p><pre>Cox regression with Breslow method for ties<br>No. of subjects =  12,585                              Number of obs = 12,585<br>No. of failures =  10,925<br>Time at risk    = 217,428<br>                                                       LR chi2(17)   = 938.43<br>Log likelihood = -93503.653                            Prob &gt; chi2   = 0.0000<br>--------------------------------------------------------------------------------<br>            _t | Haz. ratio   Std. err.      z    P&gt;|z|     [95% conf. interval]<br>---------------+----------------------------------------------------------------<br>          v025 |<br>        rural  |       1.05       0.03     1.54   0.124         0.99        1.11<br>               |<br>          v106 |<br>      primary  |       1.15       0.05     3.02   0.003         1.05        1.26<br>    secondary  |       1.09       0.05     1.90   0.057         1.00        1.20<br>       higher  |       0.84       0.05    -2.86   0.004         0.75        0.95<br>               |<br>          v190 |<br>       poorer  |       1.04       0.03     1.20   0.230         0.98        1.10<br>       middle  |       1.01       0.03     0.28   0.782         0.95        1.07<br>       richer  |       0.89       0.04    -2.96   0.003         0.82        0.96<br>      richest  |       0.82       0.04    -4.28   0.000         0.74        0.90<br>               |<br>          v024 |<br>   copperbelt  |       0.72       0.03    -7.96   0.000         0.66        0.78<br>      eastern  |       1.09       0.04     2.35   0.019         1.01        1.18<br>      luapula  |       0.73       0.03    -7.37   0.000         0.67        0.79<br>       lusaka  |       0.83       0.03    -4.36   0.000         0.77        0.91<br>     muchinga  |       0.71       0.03    -7.70   0.000         0.65        0.77<br>     northern  |       0.83       0.03    -4.51   0.000         0.76        0.90<br>north western  |       1.27       0.05     5.59   0.000         1.17        1.38<br>     southern  |       1.02       0.04     0.58   0.563         0.95        1.10<br>      western  |       1.53       0.07     9.92   0.000         1.41        1.66<br>--------------------------------------------------------------------------------</pre><p><strong>Reading the results</strong></p><p>The model included all 12,585 men, of whom 10,925 had their first sexual experience during the observation period. The overall model is highly significant (p = 0.0000), which tells us that these four variables together explain a meaningful amount of the variation in timing.</p><p><em>Residence: does urban or rural living matter?</em></p><p>Rural men have a hazard ratio of 1.05. On the surface this suggests they have first sex about 5% sooner than urban men. But the p-value is 0.124, which means this difference is not statistically significant. We cannot rule out that it is simply due to chance.</p><p>This is one of the most instructive findings in the whole analysis. Let’s remember that the Kaplan-Meier curve in Part 1 showed a visible gap between rural and urban men. Cox regression now tells us that gap was largely an illusion caused by the fact that rural men also tend to have less education and lower wealth. Once we hold those factors constant, the residence difference shrinks to almost nothing. This is exactly why we need Cox regression: the Kaplan-Meier curve can show us a pattern, but it cannot tell us what is actually driving it.</p><p><em>Education: does staying in school delay first sex?</em></p><p>Compared to men with no education, men with primary education actually have first sex slightly earlier (HR = 1.15, p = 0.003). This may seem surprising. men with no education are a small and unusual group in this sample, which makes them a somewhat unstable reference category. men with secondary education show no statistically significant difference (p = 0.057). Men with higher education, however, clearly have later first sex (HR = 0.84, p = 0.004), meaning about a 16% lower hazard at any given age. The takeaway is straightforward: it is tertiary education in particular that is associated with delayed sexual debut, not just any schooling.</p><p><em>Wealth: does money change the picture?</em></p><p>The poorer, middle, and richer quintiles all show no significant difference from the poorest group. It is only the richest men who stand out, with a hazard ratio of 0.82 (p = 0.000), meaning about an 18% lower hazard compared to the poorest. Wealth only really matters at the top of the distribution. Men in the middle of the wealth spectrum behave very similarly to the poorest men once education and region are accounted for.</p><p><em>Region: where in Zambia does it happen earliest and latest?</em></p><p>This is the most striking part of the results. Even after adjusting for education, wealth, and residence, there are enormous differences across provinces. Western province has a hazard ratio of 1.53, meaning men there have first sex about 53% sooner than men in Central province at any given age. That is a very large effect. North Western (HR = 1.27) and Eastern (HR = 1.09) also show earlier timing. On the other side, Copperbelt (HR = 0.72), Luapula (HR = 0.73), and Muchinga (HR = 0.71) all show considerably later first sex than Central province. These regional patterns likely reflect cultural, religious, and community-level factors that our individual-level variables simply do not capture.</p><p><strong>Lets now visualise the results using a forest plot as it </strong>communicates the same information much more clearly and is the standard way to present Cox regression results in academic papers. Each row shows one category, the dot is the hazard ratio, and the horizontal line through it is the 95% confidence interval. When the confidence interval crosses the vertical line at 1.0, the result is not statistically significant.</p><p>First we install the coefplot package if we have not already done so:</p><pre>ssc install coefplot, replace</pre><p>Then we re-run the Cox model and immediately create the plot:</p><pre>stcox i.v025 i.v106 i.v190 i.v024, nolog cformat(%3.2f)</pre><pre>coefplot, eform drop(_cons) xline(1, lcolor(gray) lpattern(dash)) xlabel(0.5(0.25)2, format(%3.1f)) xtitle(&quot;Hazard ratio (95% CI)&quot;) title(&quot;Adjusted hazard ratios for age at first sex&quot; &quot;Zambia DHS 2024&quot;) headings(2.v025 = &quot;{bf:Residence (ref: urban)}&quot; 1.v106 = &quot;{bf:Education (ref: no education)}&quot; 2.v190 = &quot;{bf:Wealth (ref: poorest)}&quot; 2.v024 = &quot;{bf:Province (ref: Central)}&quot;) mcolor(navy) ciopts(lcolor(navy) lwidth(med))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p7He59WX-ACxnAsNsUtCeA.png" /></figure><p>The vertical dashed line at 1.0 is the no difference line. Anything to the right of it means earlier first sex; anything to the left means later. The wider the confidence interval bar, the less precise our estimate is. A bar that crosses the dashed line means we cannot confidently say the effect is real.</p><p><strong>Lastly, one important assumption to check</strong></p><p>Cox regression rests on a critical assumption: the hazard ratio between any two groups must stay roughly constant over time. This is called the <strong>proportional hazards assumption</strong>. In practical terms it means that if men in Western province have first sex at a faster rate than men in Central province at age 16, that same relative difference should hold at age 25 and age 35. The ratio should stay proportional across time, hence the name.</p><p>We test this with one line of code run immediately after the model:</p><pre>. estat phtest, detail<br><br>Test of proportional-hazards assumption<br><br>Time function: Analysis time<br>--------------------------------------------------------<br>             |        rho     chi2       df    Prob&gt;chi2<br>-------------+------------------------------------------<br>     1b.v025 |          .        .        1           .<br>      2.v025 |   -0.03403    12.88        1       0.0003<br>     0b.v106 |          .        .        1           .<br>      1.v106 |   -0.00397     0.17        1       0.6792<br>      2.v106 |    0.02529     6.96        1       0.0083<br>      3.v106 |    0.05791    36.02        1       0.0000<br>     1b.v190 |          .        .        1           .<br>      2.v190 |   -0.01327     1.91        1       0.1668<br>      3.v190 |   -0.01266     1.75        1       0.1857<br>      4.v190 |    0.00179     0.04        1       0.8507<br>      5.v190 |   -0.00282     0.09        1       0.7672<br>     1b.v024 |          .        .        1           .<br>      2.v024 |    0.00952     0.99        1       0.3198<br>      3.v024 |   -0.02113     4.88        1       0.0272<br>      4.v024 |    0.00561     0.34        1       0.5573<br>      5.v024 |   -0.01922     4.04        1       0.0445<br>      6.v024 |    0.03006     9.85        1       0.0017<br>      7.v024 |    0.00097     0.01        1       0.9192<br>      8.v024 |   -0.05436    32.04        1       0.0000<br>      9.v024 |   -0.03090    10.44        1       0.0012<br>     10.v024 |   -0.07096    54.28        1       0.0000<br>-------------+------------------------------------------<br> Global test |              376.18       17       0.0000<br>--------------------------------------------------------<br><br></pre><p>The global test gives p = 0.0000, which means the assumption is violated. For several variables in our model, the hazard ratio is not stable over time. The model has given us one average number where the true effect is actually shifting as men get older. We cannot fully trust the hazard ratios above without addressing this.</p><p>The variables with the clearest violations are rural residence, secondary education, higher education, and several provinces. This does not mean our analysis is wrong; it means we need to take one more step before our results are fully trustworthy.</p><p>There are three common ways to handle this. We can <strong>stratify</strong> by the offending variable, which means allowing the baseline hazard to differ freely across groups without estimating a hazard ratio for that variable. This is the simplest fix. We can add a <strong>time-varying interaction</strong>, which explicitly models how the effect of a variable changes over time. Or we can switch to a different type of model called an <strong>accelerated failure time model</strong>, which does not make the proportional hazards assumption at all and instead estimates how much sooner or later the event happens in one group compared to another.</p><p>For a dataset like this one where several variables are affected, stratifying on residence is a clean and commonly used solution:</p><pre>stcox i.v106 i.v190 i.v024, strata(v025) nolog cformat(%3.2f)</pre><p>This removes residence from the hazard ratio table but allows the model to account for it in the background, making the remaining hazard ratios more honest and reliable.</p><p>In Part 3 we will work through how to handle this violation properly, compare results across all five outcomes, and produce a final publication-ready table.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=106781313b51" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Survival Analysis in Stata (Part 1: Kaplan-Meier Curve)]]></title>
            <link>https://infoart.medium.com/survival-analysis-in-stata-part-1-kaplan-meier-curve-0b979a5ead05?source=rss-e57d1215ee0a------2</link>
            <guid isPermaLink="false">https://medium.com/p/0b979a5ead05</guid>
            <category><![CDATA[big-data-analytics]]></category>
            <category><![CDATA[survival-analysis]]></category>
            <category><![CDATA[epidemiology]]></category>
            <category><![CDATA[stata]]></category>
            <dc:creator><![CDATA[infoart.ca]]></dc:creator>
            <pubDate>Thu, 12 Mar 2026 19:54:34 GMT</pubDate>
            <atom:updated>2026-03-12T19:54:34.691Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7FdVnpjglmUsCyFi" /><figcaption>Photo by <a href="https://unsplash.com/@alelmes?utm_source=medium&amp;utm_medium=referral">Al Elmes</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>We will do a quick tutorial on how to perform a survival analysis in Stata, using the Zambia DHS dataset.</p><p>In this first part, we’ll keep things simple and focus only on the <strong>Kaplan-Meier curve, or </strong>basically the “when does it happen?” story. We’ll load the data, create the time-to-event variables, declare the survival data, and plot some nice-looking survival curves (including the urban/rural comparison).</p><p>In the next tutorial we’ll move on to the more exciting part: <strong>Cox regression</strong>, where we start adjusting for education, wealth, region, and actually test which factors make first sex happen earlier or later.</p><p>Below are the codes to load the dataset and prepare the variables for analysis:</p><pre>use &quot;/Users/Downloads/ZMMR81DT/ZMMR81FL.dta&quot; <br><br>keep v013 v024 v025 v106 v169a v190 v012 v483a v483f v212 v511 v531<br><br>gen event_sex = 0<br>replace event_sex = 1 if v531 &gt; 0 &amp; v531 &lt; 90<br>gen time_sex = v012<br>replace time_sex = v531 if event_sex == 1<br>label var event_sex &quot;Had first sex (1=yes, 0=censored)&quot;<br>label var time_sex &quot;Time to first sex (years)&quot;<br><br>gen event_cohab = 0<br>replace event_cohab = 1 if v511 != .<br>gen time_cohab = v012<br>replace time_cohab = v511 if event_cohab == 1<br>label var event_cohab &quot;Ever cohabited (1=yes, 0=censored)&quot;<br>label var time_cohab &quot;Time to first cohabitation (years)&quot;<br><br>gen event_birth = 0<br>replace event_birth = 1 if v212 != .<br>gen time_birth = v012<br>replace time_birth = v212 if event_birth == 1<br>label var event_birth &quot;Had first birth (1=yes, 0=censored)&quot;<br>label var time_birth &quot;Time to first birth (years)&quot;<br><br>gen tc_age = v483a<br>replace tc_age = 3 if v483a == 95<br>replace tc_age = . if v483a == 98<br>gen event_tcirc = 0<br>replace event_tcirc = 1 if tc_age != .<br>gen time_tcirc = v012<br>replace time_tcirc = tc_age if event_tcirc == 1<br>label var event_tcirc &quot;Traditionally circumcised (1=yes, 0=censored)&quot;<br>label var time_tcirc &quot;Time to traditional circumcision (years)&quot;<br><br>gen mc_age = v483f<br>replace mc_age = 3 if v483f == 95<br>replace mc_age = . if v483f == 98<br>gen event_mcirc = 0<br>replace event_mcirc = 1 if mc_age != .<br>gen time_mcirc = v012<br>replace time_mcirc = mc_age if event_mcirc == 1<br>label var event_mcirc &quot;Medically circumcised (1=yes, 0=censored)&quot;<br>label var time_mcirc &quot;Time to medical circumcision (years)&quot;</pre><p>Notice that for each outcome we create two variables: a time variable (how old the person was when the event happened, or how old they are now if it has not happened yet) and an event indicator (1 if the event happened, 0 if it did not). People coded 0 are called <strong>censored</strong>, which simply means we ran out of observation time for them. The event had not happened by the time of the survey, but it might still happen in the future. There is nothing wrong or unusual about being censored; it is a normal part of survival data.</p><p>We are now ready to declare the dataset as a time-to-event one and plot the first curve:</p><pre>stset time_sex, failure(event_sex == 1)<br>sts graph, title(&quot;Time to first sex&quot;) xtitle(&quot;Age (years)&quot;) ytitle(&quot;Survival probability&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C7N2HUc_L0PBZzSNohrnuA.png" /></figure><p>Let’s first read the chart. A Kaplan-Meier curve is essentially a survival story told over time. Here are the key elements:</p><p>The y-axis (vertical) shows the proportion of people who have not yet had the event. At age 0, the value is 1.0, meaning nobody has had sex yet. The x-axis (horizontal) shows age in years.</p><p>The blue line starts at 1.0 and steps downward every time someone in the study has their first sexual experience. It is not smooth. It has flat sections where nothing is happening and sudden drops where a number of people experienced the event around the same age.</p><p>The line stays almost perfectly flat until around age 14 or 15, which tells us that first sexual experience before the mid-teens is very rare. The curve then begins falling sharply. By age 16 or 17 it is already down to about 85 to 90%. This is the age window where a large share of people have their first sexual experience for the first time.</p><p>The steepest part of the curve runs from roughly age 17 to age 20, dropping almost like a staircase going down fast. At around age 18, roughly half the sample still has not had sex. By age 20, only about 25% have not, meaning that three quarters of people have had their first sexual experience before turning 20.</p><p>After age 20 the drops become smaller and less frequent. By age 40 to 60 the curve is almost flat again, hovering just above zero. By middle age, nearly everyone in the study has had their first sexual experience, with only around 1 to 2% reporting never having had sex.</p><p>The single most useful number to read from a Kaplan-Meier curve is the <strong>median</strong>, which is the age where the curve crosses the 50% line. Half the people have already had the event, half have not. Here, the median age at first sex is approximately 18 years.</p><p>A few tips for reading any Kaplan-Meier curve: a flat line means no events are happening in that period; a sudden big drop means many people experienced the event around the same age; the higher the line, the more people are still waiting for the event; and when the line flattens near zero, almost everyone has experienced the event.</p><p>In this sample, first sexual experience is rare before the mid-teens, becomes common in late adolescence, and is nearly universal by the mid-twenties. This is a classic right-skewed distribution, where most people experience the event within a relatively narrow window during adolescence and young adulthood.</p><p>We can now check whether the curve looks the same for people living in urban and rural areas:</p><pre>sts graph, by(v025) title(&quot;Time to First Sex by Residence&quot;) xtitle(&quot;Age (years)&quot;) ytitle(&quot;Survival probability&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HnPJ8pAccndo6B2IfMXb0w.png" /></figure><p>This graph tells the same story as before but now splits participants into two groups: those currently living in urban areas and those currently living in rural areas. Both lines start together at 1.0, because nobody has had sex yet at birth, and they stay close together until around age 14 or 15.</p><p>From there, the rural line begins dropping a little faster than the urban line. This means that on average, people in rural areas tend to have their first sexual experience slightly earlier than those in cities. Looking at the 50% mark, the rural line crosses it just before age 18, while the urban line crosses it a few months later. By age 20, roughly 80 to 85% of people in rural areas have already had sex, compared to around 75% in urban areas.</p><p>After about age 25, both lines flatten and converge near zero. Almost everyone, regardless of where they live, eventually has their first sexual experience by their early thirties. The place of residence shifts the timing by a year or two at most, but does not change whether the event happens at all.</p><p>But how do we know this difference is not just due to chance? We can run a formal test to find out:</p><pre>. sts test v025<br><br>Failure _d: event_sex==1<br>Analysis time _t: time_sex<br><br>Equality of survivor functions<br>Log-rank test<br><br>Observed       Expected<br>v025      events         events<br><br>urban       4110        4849.98<br>rural       6815        6075.02<br><br>Total      10925       10925.00<br><br>chi2(1) = 254.63<br>Pr&gt;chi2 = 0.0000</pre><p>The answer is, the differences are real (p&lt;0.05). Rural people started having sex noticeably earlier than urban people, and that gap is not luck , it’s highly statistically significant (the chi-square is huge at 254.63, and the p-value is 0.0000, which basically means there is almost zero chance this happened by accident.</p><p>To understand what the test is actually doing: it compared how many events were observed in each group against how many we would expect if the two groups had identical timing. Rural participants had 6,815 first-sex events but would have been expected to have around 6,075 if there were no difference. Urban participants had 4,110 events but would have been expected to have nearly 4,850. Rural participants had far more early events than chance would predict, and urban participants had far fewer. The chi-square statistic of 254.63 reflects how large that gap is.</p><p>In short, people currently living in rural areas really do have their first sexual experience earlier than those in urban areas, and the data confirm that this difference is strong and not a result of random variation.</p><h3>A few things to keep in mind:</h3><ul><li>Some researchers plot <strong>1 minus survival probability</strong> (called cumulative incidence or cumulative event probability). The line then <strong>starts at 0 and goes up</strong> instead of starting at 1 and going down. But time is still on the x-axis and probability on the y-axis. It basicaly shows the “bad thing happened” side instead of the “still surviving” side. Here is what it looks like using our example:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HOvqkRdW_He6s87tnpWJlg.png" /></figure><pre>sts graph, failure title(&quot;Cumulative Probability of First Sex&quot;)   xtitle(&quot;Age (years)&quot;) ytitle(&quot;Cumulative incidence&quot;)</pre><ul><li>Why no id() in stset? DHS individual data is already one row per person, so we do not need a panel ID. If there is a case ID variable (usually caseid), we can add id(caseid), but it is not required.</li><li>Why no origin()? The natural origin is birth (age 0), and our time variables are already measured in years of age, so the default works fine.</li><li>The y-axis of a Kaplan-Meier curve shows the probability of not yet experiencing the event, so it starts at 1.0 and declines over time. This is what statisticians call the survival function, written as S(t).</li><li>On truncation versus censoring: this dataset only includes people currently aged 15 to 59. People who experienced events before age 15 are captured (for example, a value of 10 in v212 is valid), but we have no information about people who died before the survey was conducted. This is a standard feature of DHS data and is generally acceptable for the kinds of outcomes we are analysing here.</li></ul><h3>And finally a few tips to understand a Kaplan-Meier curve quicker:</h3><ul><li><strong>Y-axis (vertical)</strong>: The survival probability (also called the survival function S(t)). It always starts at 1.00 (or 100%) at the beginning and steps down toward 0 as time goes on. This shows the proportion of people who have <strong>not yet</strong> experienced the event (like first sex, death, marriage, etc.).</li><li><strong>X-axis (horizontal)</strong>: Time — it can be age in years (like in our DHS graphs), days since diagnosis, months of follow-up, or any other time unit. It always starts at 0.</li><li><strong>Flat line</strong> = no events happening in that time period.</li><li><strong>Sudden big drop</strong> = a lot of people experienced the event at that exact age/time.</li><li><strong>The higher the line</strong> = the more people are still “waiting” for the event.</li><li><strong>The line eventually flattening near zero</strong> = almost everyone eventually experiences the event.</li><li>The <strong>median</strong> (50% point) is the most important single number — it’s like the “typical” age/time for the event.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0b979a5ead05" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Easily Transfer All Your R Packages to a New Computer]]></title>
            <link>https://infoart.medium.com/easily-transfer-all-your-r-packages-to-a-new-computer-14170c5c1ec9?source=rss-e57d1215ee0a------2</link>
            <guid isPermaLink="false">https://medium.com/p/14170c5c1ec9</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[rstudio]]></category>
            <dc:creator><![CDATA[infoart.ca]]></dc:creator>
            <pubDate>Tue, 30 Dec 2025 16:46:13 GMT</pubDate>
            <atom:updated>2025-12-30T16:46:13.251Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’ve ever set up a new computer, you know the pain of reinstalling all your R packages. Dozens or hundreds of packages, each with its own dependencies, can make the process tedious. Luckily, there’s a simple way to export your installed packages from one computer and install them all on another. Here’s how to do it like a pro.</p><h3>Step 1: Export Your Installed Packages</h3><p>First, on your old computer, open R and run:</p><pre># Get a list of all installed packages<br>installed_packages &lt;- installed.packages()[, &quot;Package&quot;]</pre><pre># Save the list to a text file<br>writeLines(installed_packages, &quot;installed_packages.txt&quot;)</pre><p>This creates a file called installed_packages.txt in your working directory. Each line contains the name of one package. Easy to transfer via email, cloud storage, or a USB drive.</p><h3>Step 2: Transfer the File</h3><p>Move installed_packages.txt to your new computer. Put it somewhere easy to access, like your Downloads folder.</p><h3>Step 3: Install All Packages on the New Computer</h3><p>On the new computer, open R and run:</p><pre># Set the working directory to where you placed the file<br>setwd(&quot;/Users/yourusername/Downloads/&quot;)  # change path as needed</pre><pre># Read the list of package names<br>packages_to_install &lt;- readLines(&quot;installed_packages.txt&quot;)</pre><pre># Install them all at once<br>install.packages(packages_to_install)</pre><p>If you like using the pacman package for convenience, you can also do:</p><pre># Install pacman if needed<br>install.packages(&quot;pacman&quot;)<br>library(pacman)</pre><pre># Install packages from the list<br>p_load(char = packages_to_install)</pre><blockquote><em>Tip: </em><em>pacman will automatically skip packages that are already installed, so it’s handy for updating or syncing environments.</em></blockquote><h3>Bonus: The One-Liner Shortcut</h3><p>If you’re a fan of shortcuts, you can skip creating a file and directly install all packages from one computer to another using this one-liner:</p><pre>install.packages(installed.packages()[, &quot;Package&quot;])</pre><p>…but using the text file method is safer if you want a permanent record of your packages.</p><p>Enjoy!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=14170c5c1ec9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Host Your Website Free Using Firebase — Step by Step]]></title>
            <link>https://infoart.medium.com/how-to-host-your-website-free-using-firebase-step-by-step-14811ca2e21c?source=rss-e57d1215ee0a------2</link>
            <guid isPermaLink="false">https://medium.com/p/14811ca2e21c</guid>
            <category><![CDATA[web-app-development]]></category>
            <dc:creator><![CDATA[infoart.ca]]></dc:creator>
            <pubDate>Sun, 02 Nov 2025 03:44:21 GMT</pubDate>
            <atom:updated>2025-11-02T03:44:21.131Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to Host Your Website Free Using Firebase — Step by Step</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*lGceleD0oJAkPttA" /><figcaption>Photo by <a href="https://unsplash.com/@domenicoloia?utm_source=medium&amp;utm_medium=referral">Domenico Loia</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Today we are going to see you <strong>how to put a website online for free using Firebase</strong>. Do not worry, I will explain everything in very simple words, step by step. The steps were tested on a Mac computer, but they should be same/similar on other OS.</p><p>Firebase is service from Google. It allows us to <strong>host websites</strong>, use database, analytics and many more. But today we focus only on <strong>hosting static website</strong>.</p><h3>Step 1 — Install Node.js</h3><p>Firebase CLI (command line tool) works using Node.js. So first we need Node.js on our computer.</p><ol><li>Go to <a href="https://nodejs.org/">https://nodejs.org</a></li><li>Download <strong>LTS version</strong> (long-term support)</li><li>Install it by default options (Next → Next → Finish)</li></ol><p>Check installation by opening terminal (or command prompt) and typing:</p><pre>node -v<br>npm -v</pre><p>You should see version numbers. This means Node.js and npm installed correctly.</p><h3>Step 2 — Install Firebase Tools</h3><p>Open terminal and type:</p><pre>npm install -g firebase-tools</pre><p>It will install Firebase CLI globally. This is tool to connect your computer with Firebase.</p><h3>Step 3 — Login Firebase</h3><p>Type in terminal:</p><pre>firebase login</pre><p>It will open browser. Choose your <strong>Google account</strong>. Allow all permissions.</p><p>This connects your computer to Firebase.</p><h3>Step 4 — Create Firebase Project</h3><p>Go to <a href="https://console.firebase.google.com/">https://console.firebase.google.com</a></p><ol><li>Click <strong>Add project</strong></li><li>Give name (for example: mywebsite)</li><li>Turn off Google Analytics (for now) → Finish</li></ol><p>Your Firebase project is ready!</p><h3>Step 5 — Prepare Your Website</h3><p>Make a folder on your computer for your website. Example:</p><pre>mywebsite/<br>   index.html<br>   style.css<br>   images/</pre><ul><li>index.html is your homepage.</li><li>All CSS, JS, images stay in this folder.</li></ul><p>✅ Important: Keep folder clean and ready.</p><h3>Step 6 — Initialize Firebase Hosting</h3><p>Open terminal <strong>inside your website folder</strong>. Type:</p><pre>firebase init hosting</pre><p>It will ask questions:</p><ol><li><strong>Pick project:</strong> Choose the project you just created</li><li><strong>Public directory:</strong> Type . (dot, meaning this folder itself)</li><li><strong>Single page app (rewrite urls to /index.html)?</strong> Type No</li><li><strong>GitHub deploy?</strong> Type No (for now)</li></ol><p>Firebase will create small files like firebase.json in your folder.</p><h3>Step 7 — Deploy Your Website</h3><p>Finally, type:</p><pre>firebase deploy</pre><p>After few seconds, Firebase will show:</p><pre>Hosting URL: https://your-project-name.web.app</pre><p>Open this link in browser. Congratulations! Your website is now online.</p><h3>Step 8 — Update Your Website</h3><p>When you make any changes:</p><ol><li>Save files in your folder</li><li>Open terminal inside folder</li><li>Type:</li></ol><pre>firebase deploy</pre><p>Your website updates automatically.</p><h3>Notes</h3><ul><li>Firebase free plan gives <strong>10 GB storage and 10 GB monthly bandwidth</strong>. Enough for personal or small websites.</li><li>If homepage is not index.html, Firebase will not load it by default. Rename your main file to index.html.</li><li>You can add <strong>custom domain</strong> later in Firebase Hosting.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=14811ca2e21c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding ROC Curves in Regression Models: A Gentle Guide Using NHANES Data in R]]></title>
            <link>https://infoart.medium.com/understanding-roc-curves-in-regression-models-a-gentle-guide-using-nhanes-data-in-r-ca83a9a574b4?source=rss-e57d1215ee0a------2</link>
            <guid isPermaLink="false">https://medium.com/p/ca83a9a574b4</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[data-analysis]]></category>
            <dc:creator><![CDATA[infoart.ca]]></dc:creator>
            <pubDate>Wed, 29 Oct 2025 11:12:24 GMT</pubDate>
            <atom:updated>2025-10-29T11:12:24.579Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XLbGuYHSEPJL_xX6" /><figcaption>Photo by <a href="https://unsplash.com/@lauchmaedchen?utm_source=medium&amp;utm_medium=referral">Laura Lauch</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>When we build a regression model to predict something like diabetes, we often want to know: <strong>how good is the model really?</strong> Accuracy alone doesn’t tell the full story, especially when the data has many more “no” cases than “yes.” And that’s where the <strong>ROC curves</strong> (Receiver Operating Characteristic curves) come into the discussion.</p><h4>So, what is an ROC Curve after all?</h4><p>An ROC curve is a simple but powerful way to check how well a model can separate two groups, for example, people <strong>with diabetes (1)</strong> and <strong>without diabetes (0)</strong>.</p><p>Specifically, it shows a trade-off between:</p><ul><li><strong>True Positive Rate (Sensitivity)</strong> — how many of the real positives the model correctly finds.</li><li><strong>False Positive Rate (1 — Specificity)</strong> — how many of the negatives it mistakenly calls positive.</li></ul><p>A perfect model would have a curve that hugs the <strong>top-left corner</strong> (high sensitivity, low false positives), while a useless model lies on the diagonal line (random guessing).</p><p>The <strong>AUC (Area Under the Curve)</strong> tells us how much area is under the ROC curve.<br> It ranges from:</p><ul><li><strong>0.5</strong> → no better than chance</li><li><strong>1.0</strong> → perfect prediction</li></ul><p>So, the higher the AUC, the better the model distinguishes between positive and negative cases.</p><p>First, let’s look at the following chart to understand the concept better. We look at the codes to create the chart later on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-Sdt-BIeyI5PJyDKTn35sg.png" /></figure><p>We used data from the <strong>NHANES</strong> dataset in R to predict diabetes. We started simple , just age , and then added more predictors step by step.</p><ul><li><strong>Model 1 (Age only)</strong> has an AUC of around 0.76 — not bad, age alone explains something.</li><li><strong>Model 2 (+BMI)</strong> jumps to about 0.80 — weight clearly matters.</li><li>Adding <strong>Blood Pressure</strong> and <strong>Cholesterol</strong> gives small improvements.</li><li>By the time we include <strong>Physical Activity</strong>, AUC reaches around <strong>0.81–0.82</strong>, meaning the model is quite good at separating diabetic and non-diabetic individuals.</li></ul><p>And here is the code to replicate the chart:</p><pre><br>library(pROC)<br>library(NHANES)<br>library(ggplot2)<br><br># Load data and remove missing values for Diabetes outcome<br>data(NHANES)<br>nhanes_clean &lt;- NHANES[complete.cases(NHANES[, c(&quot;Diabetes&quot;, &quot;Age&quot;, &quot;BMI&quot;, &quot;BPSysAve&quot;, &quot;TotChol&quot;, &quot;PhysActive&quot;)]), ]<br><br># Convert Diabetes to binary (0/1)<br>nhanes_clean$Diabetes_binary &lt;- ifelse(nhanes_clean$Diabetes == &quot;Yes&quot;, 1, 0)<br><br># Train 5 different logistic regression models<br>model1 &lt;- glm(Diabetes_binary ~ Age, data = nhanes_clean, family = binomial)<br>model2 &lt;- glm(Diabetes_binary ~ Age + BMI, data = nhanes_clean, family = binomial)<br>model3 &lt;- glm(Diabetes_binary ~ Age + BMI + BPSysAve, data = nhanes_clean, family = binomial)<br>model4 &lt;- glm(Diabetes_binary ~ Age + BMI + BPSysAve + TotChol, data = nhanes_clean, family = binomial)<br>model5 &lt;- glm(Diabetes_binary ~ Age + BMI + BPSysAve + TotChol + PhysActive, data = nhanes_clean, family = binomial)<br><br># Get predictions<br>pred1 &lt;- predict(model1, type = &quot;response&quot;)<br>pred2 &lt;- predict(model2, type = &quot;response&quot;)<br>pred3 &lt;- predict(model3, type = &quot;response&quot;)<br>pred4 &lt;- predict(model4, type = &quot;response&quot;)<br>pred5 &lt;- predict(model5, type = &quot;response&quot;)<br><br># Create ROC objects<br>roc1 &lt;- roc(nhanes_clean$Diabetes_binary, pred1, quiet = TRUE)<br>roc2 &lt;- roc(nhanes_clean$Diabetes_binary, pred2, quiet = TRUE)<br>roc3 &lt;- roc(nhanes_clean$Diabetes_binary, pred3, quiet = TRUE)<br>roc4 &lt;- roc(nhanes_clean$Diabetes_binary, pred4, quiet = TRUE)<br>roc5 &lt;- roc(nhanes_clean$Diabetes_binary, pred5, quiet = TRUE)<br><br># Prepare data for ggplot<br>roc_data &lt;- data.frame(<br>    FPR = c(1 - roc1$specificities, 1 - roc2$specificities, 1 - roc3$specificities,<br>            1 - roc4$specificities, 1 - roc5$specificities),<br>    TPR = c(roc1$sensitivities, roc2$sensitivities, roc3$sensitivities,<br>            roc4$sensitivities, roc5$sensitivities),<br>    Model = c(<br>        rep(paste0(&quot;Model 1: Age (AUC=&quot;, round(auc(roc1), 3), &quot;)&quot;), length(roc1$specificities)),<br>        rep(paste0(&quot;Model 2: +BMI (AUC=&quot;, round(auc(roc2), 3), &quot;)&quot;), length(roc2$specificities)),<br>        rep(paste0(&quot;Model 3: +BP (AUC=&quot;, round(auc(roc3), 3), &quot;)&quot;), length(roc3$specificities)),<br>        rep(paste0(&quot;Model 4: +Cholesterol (AUC=&quot;, round(auc(roc4), 3), &quot;)&quot;), length(roc4$specificities)),<br>        rep(paste0(&quot;Model 5: +PhysActive (AUC=&quot;, round(auc(roc5), 3), &quot;)&quot;), length(roc5$specificities))<br>    )<br>)<br><br># Create ggplot<br>ggplot(roc_data, aes(x = FPR, y = TPR, color = Model)) +<br>    geom_line(size = 1.5) +<br>    geom_abline(intercept = 0, slope = 1, linetype = &quot;dashed&quot;, color = &quot;gray50&quot;, size = 1) +<br>    scale_color_manual(values = c(&quot;#FF6B9D&quot;, &quot;#FFD93D&quot;, &quot;#FF8C42&quot;, &quot;#667eea&quot;, &quot;#764ba2&quot;)) +<br>    labs(<br>        title = &quot;ROC Curves - Diabetes Prediction Models (NHANES)&quot;,<br>        x = &quot;False Positive Rate (1 - Specificity)&quot;,<br>        y = &quot;True Positive Rate (Sensitivity)&quot;,<br>        color = &quot;Model&quot;<br>    ) +<br>    theme_minimal(base_size = 12) +<br>    theme(<br>        plot.title = element_text(size = 16, face = &quot;bold&quot;, hjust = 0.5),<br>         legend.title = element_text(face = &quot;bold&quot;),<br>        panel.grid.major = element_line(color = &quot;gray90&quot;),<br>        panel.grid.minor = element_line(color = &quot;gray95&quot;)<br>    ) +<br>    coord_equal()</pre><p>Keep ROCking!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ca83a9a574b4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Linear regression in R]]></title>
            <link>https://infoart.medium.com/linear-regression-in-r-8ffe8631e4ba?source=rss-e57d1215ee0a------2</link>
            <guid isPermaLink="false">https://medium.com/p/8ffe8631e4ba</guid>
            <category><![CDATA[linear-regression]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-analysis]]></category>
            <category><![CDATA[rstudio]]></category>
            <dc:creator><![CDATA[infoart.ca]]></dc:creator>
            <pubDate>Wed, 08 Oct 2025 02:38:17 GMT</pubDate>
            <atom:updated>2025-10-08T02:49:33.041Z</atom:updated>
            <content:encoded><![CDATA[<h4>What is Linear Regression?</h4><p>At its core, linear regression is a statistical method used to understand the relationship between an outcome (DV) and one or more independent variables (IV).</p><p>Imagine you want to predict a student’s exam score based on the number of hours they studied. Linear regression helps us find the “best-fit” straight line that describes how the exam score (our <strong>dependent variable</strong>) changes as the hours of study (our <strong>independent variable</strong>) increase or decrease.</p><p>When we have more than one independent variable, it’s called <strong>multiple linear regression</strong>. For instance, we could try to predict that same exam score using hours of study, hours of sleep, and the student’s previous grades. This is the type of regression we’ll be looking at today.</p><h4>When to use linear regression?</h4><ul><li><strong>Predict a continuous outcome variable:</strong> For example, predicting the price of a house based on its size, number of bedrooms, and location.</li><li><strong>Understand the strength of relationships:</strong> Linear regression models help us how strong the relationship is between the DV and IVs, like rainfall and crop yield.</li></ul><p>A key assumption is that there is a linear relationship between the variables. This means that as one variable changes, the other changes at a relatively constant rate.</p><h4>A Example using the swiss dataset</h4><p>We’ll be using the built-in swiss dataset in R. This dataset contains information on fertility and socio-economic indicators for 47 French-speaking provinces of Switzerland around the year 1888. Our goal is to understand what factors influenced the fertility rate (Fertility) in these provinces.</p><p>The other variables in the dataset are:</p><ul><li>Agriculture: % of males involved in agriculture.</li><li>Examination: % of draftees receiving the highest mark on an army examination.</li><li>Education: % of draftees with education beyond primary school.</li><li>Catholic: % of the population that is Catholic.</li><li>Infant.Mortality: % of live births who die in the first year.</li></ul><p>Let’s have a quick view of the data:</p><pre>&gt; head(swiss)<br>             Fertility Agriculture Examination Education Catholic<br>Courtelary        80.2        17.0          15        12     9.96<br>Delemont          83.1        45.1           6         9    84.84<br>Franches-Mnt      92.5        39.7           5         5    93.40<br>Moutier           85.8        36.5          12         7    33.77<br>Neuveville        76.9        43.5          17        15     5.16<br>Porrentruy        76.1        35.3           9         7    90.57<br>             Infant.Mortality<br>Courtelary               22.2<br>Delemont                 22.2<br>Franches-Mnt             20.2<br>Moutier                  20.3<br>Neuveville               20.6<br>Porrentruy               26.6<br>&gt; str(swiss)<br>&#39;data.frame&#39;: 47 obs. of  6 variables:<br> $ Fertility       : num  80.2 83.1 92.5 85.8 76.9 76.1 83.8 92.4 82.4 82.9 ...<br> $ Agriculture     : num  17 45.1 39.7 36.5 43.5 35.3 70.2 67.8 53.3 45.2 ...<br> $ Examination     : int  15 6 5 12 17 9 16 14 12 16 ...<br> $ Education       : int  12 9 5 7 15 7 7 8 7 13 ...<br> $ Catholic        : num  9.96 84.84 93.4 33.77 5.16 ...<br> $ Infant.Mortality: num  22.2 22.2 20.2 20.3 20.6 26.6 23.6 24.9 21 24.4 ...</pre><p>Let’s now run a linear regression model and nicely format the output using the summ function from thejtools package. This model looked at how five different social factors (Agriculture, Examination, Education, Catholic, and Infant Mortality) are related to the fertility rate in 47 Swiss provinces in the late 19th century:</p><pre>lm(Fertility ~ ., swiss) %&gt;% jtools::summ()<br><br>MODEL INFO:<br>Observations: 47<br>Dependent Variable: Fertility<br>Type: OLS linear regression <br><br>MODEL FIT:<br>F(5,41) = 19.76, p = 0.00<br>R² = 0.71<br>Adj. R² = 0.67 <br><br>Standard errors:OLS<br>------------------------------------------------------<br>                          Est.    S.E.   t val.      p<br>---------------------- ------- ------- -------- ------<br>(Intercept)              66.92   10.71     6.25   0.00<br>Agriculture              -0.17    0.07    -2.45   0.02<br>Examination              -0.26    0.25    -1.02   0.32<br>Education                -0.87    0.18    -4.76   0.00<br>Catholic                  0.10    0.04     2.95   0.01<br>Infant.Mortality          1.08    0.38     2.82   0.01<br>------------------------------------------------------</pre><p>The summ functions comes with a multitude of options that you can explore to get a customized output:</p><pre>&gt; lm(Fertility ~ ., swiss) %&gt;% jtools::summ(confint = TRUE, digits = 3, pvals = FALSE)<br>MODEL INFO:<br>Observations: 47<br>Dependent Variable: Fertility<br>Type: OLS linear regression <br><br>MODEL FIT:<br>F(5,41) = 19.761, p = 0.000<br>R² = 0.707<br>Adj. R² = 0.671 <br><br>Standard errors:OLS<br>----------------------------------------------------------<br>                           Est.     2.5%    97.5%   t val.<br>---------------------- -------- -------- -------- --------<br>(Intercept)              66.915   45.294   88.536    6.250<br>Agriculture              -0.172   -0.314   -0.030   -2.448<br>Examination              -0.258   -0.771    0.255   -1.016<br>Education                -0.871   -1.241   -0.501   -4.758<br>Catholic                  0.104    0.033    0.175    2.953<br>Infant.Mortality          1.077    0.306    1.848    2.822<br>----------------------------------------------------------</pre><p>The plot_summs function creates a nice forestplot of the coefficients and CI:</p><pre>lm(Fertility ~ ., swiss) %&gt;% jtools::plot_summs()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3N6I7iP2CMcxvIOWIM8WHA.png" /></figure><p>Lets now try to make sense of the model output:</p><p>First things first:</p><p>The model does a reasonably good job of explaining fertility. The five factors together explain about 70.7% of the differences in fertility rates between these provinces. This is a fairly strong relationship.</p><p>Second, the F-test shows the model is statistically significant overall (p-value = 0.000), meaning the predictors collectively have a meaningful relationship with fertility.</p><p>Now the coefficients:</p><p><strong>Education</strong>: Regarding model coefficients, for every one-unit increase in the education level, the fertility rate decreases by about 0.87 units, on average (95%CI= -1.24 to -0.50). In short, higher education is associated with lower fertility.</p><p><strong>Infant.Mortality</strong>: For every one-unit increase in the infant mortality rate, the fertility rate increases by about 1.08 units (95%CI=0.31 to 1.85) i.e. provinces with higher infant mortality tend to have higher fertility rates.</p><p><strong>Agriculture</strong>: As the percentage of men involved in agriculture increases by one unit, the fertility rate decreases by about 0.17 units (95%CI=-0.31 to -0.03).</p><p><strong>Catholic</strong>: As the percentage of the population that is Catholic increases by one unit, the fertility rate increases by about 0.10 units, i.e. more Catholic populations are linked to higher fertility.</p><p><strong>Examination</strong>: This factor does not show a statistically significant relationship with fertility. The estimated effect is negative (-0.26), but the range of plausible values goes from a strong negative effect (-0.77) to a positive one (0.26). Since this range includes <em>zero</em>, we cannot be confident that examination scores have a real relationship with fertility once the other factors are considered.</p><p>Before saying goodbye, let’s quickly check the VIF values to rule out the presence of multicollinearity. We will do so using the good old car package:</p><pre>lm(Fertility ~ ., swiss) %&gt;% car::vif()<br>     Agriculture      Examination        Education         Catholic Infant.Mortality <br>        2.284129         3.675420         2.774943         1.937160         1.107542<br><br><br><br>## we could get them using summ as well:<br>lm(Fertility ~ ., swiss) %&gt;% <br>    jtools::summ(<br>        confint = TRUE, <br>        digits = 3, <br>        pvals = FALSE,<br>        vif = TRUE)<br><br><br>MODEL INFO:<br>Observations: 47<br>Dependent Variable: Fertility<br>Type: OLS linear regression <br><br>MODEL FIT:<br>F(5,41) = 19.761, p = 0.000<br>R² = 0.707<br>Adj. R² = 0.671 <br><br>Standard errors:OLS<br>------------------------------------------------------------------<br>                           Est.     2.5%    97.5%   t val.     VIF<br>---------------------- -------- -------- -------- -------- -------<br>(Intercept)              66.915   45.294   88.536    6.250        <br>Agriculture              -0.172   -0.314   -0.030   -2.448   2.284<br>Examination              -0.258   -0.771    0.255   -1.016   3.675<br>Education                -0.871   -1.241   -0.501   -4.758   2.775<br>Catholic                  0.104    0.033    0.175    2.953   1.937<br>Infant.Mortality          1.077    0.306    1.848    2.822   1.108<br>------------------------------------------------------------------</pre><p>Looks like we are safe (VIF &gt; 5 or 10 indicates problematic multicollinearity).</p><p>And finally a cute looking barplot to visualize the VIF values:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EKx--hyqWfM3vuhFPU6Itg.png" /></figure><pre> <br>lm(Fertility ~ ., swiss) %&gt;%<br>    car::vif() %&gt;%<br>    tibble::enframe(name = &quot;Variable&quot;, value = &quot;VIF&quot;) %&gt;%<br>    arrange(VIF) %&gt;%<br>    ggplot(aes(x = reorder(Variable, VIF), y = VIF)) +<br>    geom_col(fill = &quot;#4F6D7A&quot;, width = 0.7) +<br>    coord_flip() +<br>    theme_minimal(base_size = 13) +<br>    labs(title = &quot;Variance Inflation Factor (VIF) by Predictor&quot;,<br>        x = &quot;Predictor Variable&quot;,<br>        y = &quot;VIF Value&quot;) +<br>    theme( plot.title = element_text(face = &quot;bold&quot;, size = 14, hjust = 0.5),<br>        axis.text = element_text(color = &quot;salmon&quot;),<br>        panel.grid.minor = element_blank())+ hrbrthemes::theme_ipsum()</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ffe8631e4ba" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding path analysis: paths, standardized coefficients and causality]]></title>
            <link>https://infoart.medium.com/understanding-path-analysis-paths-standardized-coefficients-and-causality-acd52d3d847c?source=rss-e57d1215ee0a------2</link>
            <guid isPermaLink="false">https://medium.com/p/acd52d3d847c</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[path-analysis]]></category>
            <category><![CDATA[causal-inference]]></category>
            <category><![CDATA[data-analysis]]></category>
            <category><![CDATA[rstudio]]></category>
            <dc:creator><![CDATA[infoart.ca]]></dc:creator>
            <pubDate>Sat, 08 Feb 2025 21:27:46 GMT</pubDate>
            <atom:updated>2025-02-08T21:27:46.592Z</atom:updated>
            <content:encoded><![CDATA[<p>Path analysis is part of the family of <strong>structural equation modeling (SEM)</strong> techniques, but it focuses solely on observed variables (not latent variables). It is a statistical technique that extends multivariable regression methods to model the direct and indirect relationships among the variables within a predefined causal framework. In this article, we will explore the basics of path analysis and interpretation techniques, including coefficients, standardization, and causality. We will use example dataset to build a causal model in R.</p><blockquote>Path analysis requires that the relationships between variables are specified a priori based on theory or prior evidence.</blockquote><blockquote>What is path analysis?</blockquote><p>Path analysis is a commonly used method in modelling causal relationships between variables. At its core, this method helps understand how variables interact directly and indirectly within a given model. Path analysis produces path coefficients, which quantify the strength of these relationships. Specifically, it estimate <strong>direct effects</strong> (relationships between variables connected by a single arrow), assess <strong>indirect effects</strong> (relationships mediated through intermediate variables), and estimate thus the <strong>total effect</strong> of one variable on another (direct + indirect effects).</p><p><strong><em>Total effects = direct effects + indirect effects</em></strong></p><blockquote>Path analysis differs from regular regression methods by clearly distinguishing between direct effects (the immediate relationships between variables) and indirect effects (the relationships that are mediated through other variables). Unlike simple correlations, path coefficients are derived from a causal model, reflecting hypothesized cause-and-effect relationships.</blockquote><blockquote>What is path in path analysis?</blockquote><p>In path analysis, a path refers to the directional relationship between variables in a model, represented by arrows in a path diagram. These paths signify hypothesized causal effects, based on theory or prior research. The single-headed arrows (→) indicate direct effects of one variable on another, while double-headed arrows (↔) indicate correlations or covariances between variables without implying causation.</p><p>For example, in a path model:</p><ul><li>X → Y represents a direct effect of variable X on variable Y.</li><li>X ↔ Z suggests that X and Z are correlated but do not necessarily have a causal relationship.</li></ul><blockquote>What is a path coefficient and how to interprete them?</blockquote><p>Path analysis produces path coefficient which are standardized regression coefficient (also called a beta coefficient). A coefficient measures the direct effect of one variable on another in a causal model, while accounting for the influence of other variables in the model. These coefficients are standardized, meaning they are expressed in terms of standard deviations, which allows for comparison across variables with different units of measurement.</p><p>Interpreting path coefficients involves evaluating their magnitude, direction, and statistical significance:</p><ol><li><strong>Magnitude (strength of the effect)</strong>:</li></ol><ul><li><strong>|0.0–0.1|</strong>: very weak effect</li><li><strong>|0.1–0.3|</strong>: weak effect</li><li><strong>|0.3–0.5|</strong>: moderate effect</li><li><strong>|0.5 and above|</strong>: strong effect</li></ul><p><strong>2. Sign (direction of the effect)</strong>:</p><ul><li><strong>Positive (+)</strong>: A positive path coefficient indicates that an increase in the IV leads to an increase in the DV.</li><li><strong>Negative (-)</strong>: A negative path coefficient indicates the opposite- an increase in the IV leads to a decrease in the DV.</li></ul><p><strong>3. Statistical Significance</strong>:</p><p>A p-value that accompanies each coefficient indicates whether the coefficient is statistically significant. A p-value less than 0.05 suggests statistical significance.</p><blockquote>Path coefficients tell us how strongly one variable influences another and in which direction.</blockquote><p>Let’s use a hypothetical example with the following relationships to better understand the numbers:</p><p><strong>Direct Effects</strong>:</p><pre>Education → Income (path coefficient = 0.6)<br>A 1 SD increase in Education leads to a 0.6 SD increase in Income.<br><br>Income → Job Satisfaction (path coefficient = 0.5)<br>A 1 SD increase in Income leads to a 0.5 SD increase in Job Satisfaction.<br><br>Education → Job Satisfaction (path coefficient = 0.3)<br>A 1 SD increase in Education directly leads to a 0.3 SD increase in Job Satisfaction.</pre><p><strong>Indirect effects</strong></p><pre>Indirect Effect = (Education → Income) × (Income → Job Satisfaction)<br>                = 0.6 × 0.5<br>                = 0.3</pre><p><strong>Total Effect</strong>:</p><pre>The total effect of Education on Job Satisfaction is the sum of the direct <br>and indirect effects:<br><br>TE = Direct Effect + Indirect Effect<br>             = 0.3 + 0.3<br>             = 0.6</pre><blockquote>Why Use Standardization in Path Analysis?</blockquote><p>Standardization plays a crucial role in path analysis. By converting variables to a common scale (mean = 0, SD= 1), it allows for a direct comparison between different path coefficients. Without standardization, coefficients could be misleading because different variables might have very different units or scales (e.g. income in thousands vs. BMI in kg/m2).</p><h4>Example analysis in R:</h4><p>We will perform an example analysis using the NHANES dataset which contains a wealth of data on socioeconomic and health status. Specifically, we will explore how income and housing onditions influence mental health and drug use through the following hypotheses:</p><p>H1: Homeownership (HomeOwn) is associated with fewer days of bad mental health (DaysMentHlthBad).</p><p>H2: Higher household income (HHIncome) is associated with fewer days of bad mental health (DaysMentHlthBad).</p><p>H3: Fewer days of bad mental health (DaysMentHlthBad) are associated with lower likelihood of using hard drugs (HardDrugs).</p><p>H4: Homeownership (HomeOwn) is associated with lower likelihood of using hard drugs (HardDrugs).</p><p>Here is the complete code we are going to use:</p><pre>library(lavaan)<br>library(lavaanExtra)<br>library(ggplot2)<br>library(dplyr)<br><br>data(NHANES)<br><br>#prep the data first: <br><br>nhanes_data &lt;- NHANES %&gt;%<br>  filter(!is.na(HHIncome), !is.na(DaysMentHlthBad), !is.na(HomeOwn), !is.na(HardDrugs)) %&gt;%<br>  mutate(HHIncome = as.numeric(factor(HHIncome)),<br>    DaysMentHlthBad = as.numeric(DaysMentHlthBad),<br>    HomeOwn = ifelse(HomeOwn == &quot;Own&quot;, 1, 0),<br>    HardDrugs = ifelse(HardDrugs == &quot;Yes&quot;, 1, 0))<br><br># Here is the path model are using to meet the objectives of the analysis<br><br>model &lt;- &#39;DaysMentHlthBad ~ HHIncome + HomeOwn<br>  HardDrugs ~ HHIncome + HomeOwn + DaysMentHlthBad&#39;<br><br># Now we can the model using the magical sem function from lavaan:<br>fit &lt;- sem(model, data = nhanes_data)<br><br><br># and visualise the model using the nice_lavaanPlot function from lavaanExtra package:<br>nice_lavaanPlot(model = fit, <br>                node_options = list(shape = &quot;box&quot;, fontname = &quot;Helvetica&quot;), <br>                edge_options = c(color = &quot;black&quot;),<br>                coefs = TRUE, <br>                stand = TRUE, <br>                covs = TRUE, <br>                stars = c(&quot;regress&quot;, &quot;latent&quot;, &quot;covs&quot;), <br>                sig = 0.05, <br>                graph_options = c(rankdir = &quot;LR&quot;))<br><br> <br># To print the model summary to get standardized estimates along with the fit statistics<br>summary(fit, standardized = TRUE, fit.measures = TRUE)<br><br></pre><p>And here is plot the code generates for us:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5l3xpzMDtcek76e6S_hr-A.png" /></figure><p>Let’s now review the results in light of the hypotheses:</p><p><strong>H1: Homeownership (HomeOwn) is associated with fewer days of bad mental health (DaysMentHlthBad).</strong><em> H1 is supported by the results, path coefficient is -0.05*** i.e. homeowners experience fewer days of poor mental health compared to non-homeowners.</em></p><p><strong>H2: Higher household income (HHIncome) is associated with fewer days of bad mental health (DaysMentHlthBad).</strong> <em>H2 is supported by the results also, path coefficient is -0.11***</em></p><p><strong>H3: Fewer days of bad mental health (DaysMentHlthBad) are associated with lower likelihood of using hard drugs (HardDrugs). </strong><em>H3 is supported by the results also, path coefficient is 0.1*** i.e. those experiencing poor mental health are more likely to turn to drug use.</em></p><p><strong>H4: Homeownership (HomeOwn) is associated with lower likelihood of using hard drugs (HardDrugs). </strong><em>H4 is supported by the results also, path coefficient is -0.08*** i.e. those who own their homes are less likely to engage in drug use.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=acd52d3d847c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using fuzzy joins in R: handling unmatched strings]]></title>
            <link>https://infoart.medium.com/using-fuzzy-joins-in-r-handling-unmatched-strings-98a6e5f32b87?source=rss-e57d1215ee0a------2</link>
            <guid isPermaLink="false">https://medium.com/p/98a6e5f32b87</guid>
            <category><![CDATA[rstudio]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data]]></category>
            <dc:creator><![CDATA[infoart.ca]]></dc:creator>
            <pubDate>Sat, 25 Jan 2025 20:06:11 GMT</pubDate>
            <atom:updated>2025-01-25T20:06:11.906Z</atom:updated>
            <content:encoded><![CDATA[<p>Data often comes with inconsistencies, especially when dealing with real-world datasets. Variations in spellings, typos, or differing formats can make merging datasets challenging. This is where fuzzy joins proves to be so useful. In this blog post, we’ll explore how to handle such issues in R using the fuzzyjoin package.</p><h3>What is a Fuzzy Join?</h3><p>A fuzzy join allows us to merge datasets even when the key columns don’t perfectly match. It uses similarity measures like string distance to determine the best matches between keys in the two datasets.</p><h3>Setting Up</h3><p>We’ll use the fuzzyjoin package for fuzzy joins. Ensure the required packages are installed:</p><pre>library(dplyr, fuzzyjoin, stringdist)</pre><p>To beging with, lets inspect the two datasets with slight mismatches in country names.</p><pre>df1 &lt;- data.frame(<br>    country = c(&quot;United States&quot;, &quot;Canada&quot;, &quot;United Kingdom&quot;, &quot;Germany&quot;, &quot;France&quot;),<br>    population = c(331002651, 37742154, 67886011, 83783942, 65273511))<br><br>&gt; df1<br>         country population<br>1  United States  331002651<br>2         Canada   37742154<br>3 United Kingdom   67886011<br>4        Germany   83783942<br>5         France   65273511<br><br>df2 &lt;- data.frame(<br>    country = c(&quot;Unitef Sates&quot;, &quot;Canadda&quot;, &quot;Unitd Kingom&quot;, &quot;Geermanny&quot;, &quot;Franccee&quot;),<br>    gdp = c(3.5, 4.0, 6.7, 1.8, 4.5))<br><br><br>&gt; df2<br>       country gdp<br>1 Unitef Sates 3.5<br>2      Canadda 4.0<br>3 Unitd Kingom 6.7<br>4    Geermanny 1.8<br>5     Franccee 4.5<br><br><br></pre><p>As the country names are not matching, we won’t be able to perform operations such as joining or merging the datasets. This is where Fuzzy Join enters the picture:</p><p>We’ll use stringdist_join from the fuzzyjoin package to merge these datasets based on approximate matches.</p><pre>stringdist_join(df1, df2, <br>                mode = &quot;inner&quot;,<br>                by = &quot;country&quot;,<br>                max_dist = 2)<br><br><br>       country.x population    country.y gdp<br>1  United States  331002651 Unitef Sates 3.5<br>2         Canada   37742154      Canadda 4.0<br>3 United Kingdom   67886011 Unitd Kingom 6.7<br>4        Germany   83783942    Geermanny 1.8<br>5         France   65273511     Franccee 4.5</pre><p>As we can see, the method correctly identified the closely matched names and joined the datasets accordingly.</p><h3>Meaning of the parameters</h3><ul><li><strong>by</strong>: Specifies the columns to join.</li><li><strong>max_dist</strong>: The maximum allowable string distance for matches.</li></ul><p>The result shows that even with minor mismatches, the datasets have been successfully joined.</p><p>Freel free to experiment with different distance metrics (&quot;osa&quot;, &quot;lv&quot;, &quot;dl&quot;, etc.) to find the best fit for your data.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=98a6e5f32b87" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding descriptive analysis for epidemiological research]]></title>
            <link>https://infoart.medium.com/understanding-descriptive-analysis-for-epidemiological-research-ed32f9583718?source=rss-e57d1215ee0a------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed32f9583718</guid>
            <category><![CDATA[rstudio]]></category>
            <category><![CDATA[data]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[infoart.ca]]></dc:creator>
            <pubDate>Wed, 27 Nov 2024 05:26:35 GMT</pubDate>
            <atom:updated>2024-11-27T05:29:11.623Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9JTmpVL9qLdZO_k-" /><figcaption>Photo by <a href="https://unsplash.com/@adamkool?utm_source=medium&amp;utm_medium=referral">Adam Kool</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Descriptive analysis is a key component of epidemiological research. This is the first step in all population survey data analysis that helps us understand the <strong>baseline characteristic</strong> of the study participants. Often referred to as <strong>Table 1, </strong>it<strong> </strong>provides a snapshot of the survey population and answers the question: <a href="https://pubmed.ncbi.nlm.nih.gov/31229583/"><strong>Who is in this study, anyway?</strong></a></p><p>Despite being a relatively straightforward analysis, many researchers struggle with interpreting the results of descriptive tables. Part of the struggle comes from an inadequate understanding of the relevant terms such as <strong>cross tabulation, 2x2 tables</strong>,and <strong>contingency tables,</strong> all of which are integral to descriptive analysis.</p><p>This tutorial will provide a beginner friendly yet comprehensive guide to understanding and interpreting Table 1. Specifically, we will be focusing on <strong>row</strong> and <strong>column</strong> percentages in <strong>cross tabulations</strong>. All the R codes used to geenrate the tables are provided at the end of the text. To keep the tutorial accessible, we will focus only on categorical variables. No special knowledge of R or quantitative research is required to benefit from this article.</p><blockquote>Let’s start with cross tabulation</blockquote><p>Cross tabulation is a popular method for displaying the distribution of two categorical variables in a matrix format. This allows a better comparisons of frequencies (and/or percentages) across their respective categories. For instance, the following table contains two cross-tabulations for the categorical variables Gender,Home Ownership and Education: one showing the distribution of Home Ownership by Gender and the other displaying the distribution of Education by Gender.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/792/1*MoGEDayRYr0FlCQKIipHzw.png" /></figure><p>Specifically, the first cross tabulation shows the frequency distribution (with row %) of male and female individuals across different categories of Home Ownership. The results indicate that a higher percentage of individuals who own homes are females compared to males (51% vs 49%).</p><p>The second cross tabulation shows that a higher percentage of College graduates are female compared to male (52% vs 48%).</p><p><em>To conclude, the row % method is suitable for understanding how a specific category of the row variable (e.g. homeowners: own /rent/ other) varies across the categories of the column variable (e.g. gender) within each row, e.g. among homeowners, a higher percentage are female.</em></p><p>However, reporting row % may not be sufficient when the goal is to understand how categories of the row variable vary within each category of the column variable, for which column percentages are used. Let’s then consider the second variation of the table by showing the column percentges:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H2-gk1_7DVM2BHJMnfSkhg.png" /></figure><p>This table indicates that among all females individuals, 65% own homes, 33% rent, and 2% fall under Other. In contrast, among males, 64% own homes, 33% rent, and 3% fall under Other. As such, <strong>column percentages</strong> focus on the distribution of the row variable within each category of the column variable (e.g. the percentage of females or males who own or rent).</p><blockquote>To conclude: row percentages help answer the question: <strong>Are homeowners more likely to be male or female? </strong>In contrast, column percentages help answer the question: <strong>What % of females own or rent homes?</strong></blockquote><blockquote>Row percentages show how each row category (e.g., “own,” “rent”) is distributed across column categories (e.g., gender), while column percentages show how each column category is distributed across row categories.</blockquote><p><strong>One last question: </strong>How the interpretations would look like if the position of row and column variables are switched?</p><p><em>Row percentages would answer the question:</em><strong><em> among males, what percentage own homes versus rent? </em></strong><em>And column percentages would answer the question:</em><strong><em> among homeowners, what percentage are male versus female?</em></strong></p><blockquote>2x2 table</blockquote><p>A specific type of cross tabulation that involves the a binary column and a binary row variables e.g. gender (male vs female )and homeownership (Own vs rent)</p><blockquote><strong>Contingency table</strong></blockquote><p>Essentially the same as a cross tabulation, but it is often used in the context of statistical tests (inferential analysis) such as the chi-2 test of independence. The term contingency reflects the idea that the table displays the dependency or contingency of one variable on another. In other words, the table allows us to observe how the distribution of the levels of one categorical variable is contingent upon the levels of another.</p><p>The chi-2 test compares the observed cell counts in the table with the expected cell counts (under the assumption of no association between the variables). Significant differences between observed and expected counts suggest a relationship between the variables. Thus, the cross tabulation provides the necessary structure to conduct the statistical tests to find out if there is a significant association between the row and column variables.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aPdzAiTfrasCmdH81IYvyg.png" /></figure><blockquote>In short: while the terms “cross-tabulation” and “contingency table” are often used interchangeably, “contingency table” are used to include an inferential component to the descriptive content of a “cross-tabulation”.</blockquote><p>As a parting gift, here is an elegant way to summarize continuous variables using the mighty gtsummary package. Although cross tabulations are generally used to summarize categorical variables, a similar structure can be used to tabulate the means and SD of a continuous column across the levels of categorical variable/s.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/706/1*EZmGA_McY02XdbmZW9VjkQ.png" /></figure><pre>######### table 1 #########<br><br>library(crosstable)<br>library(NHANES)<br><br>crosstable(NHANES, c(HomeOwn,Education), by=c(Gender), total = &quot;none&quot;,<br>           percent_pattern = &quot;{n} ({p_row})&quot;, percent_digits = 0)<br><br><br><br>######### table 2 #########<br><br>library(crosstable)<br>library(NHANES)<br><br>crosstable(NHANES, c(HomeOwn,Education), by=c(Gender), total = &quot;none&quot;,<br>           percent_pattern = &quot;{n} ({p_col})&quot;, percent_digits = 0)<br><br><br>######### table 3#########<br><br>library(crosstable)<br>library(NHANES)<br>crosstable(NHANES, c(HomeOwn,Education), by=c(Gender), total = &quot;none&quot;,<br>           percent_pattern = &quot;{n} ({p_col})&quot;, percent_digits = 0, test =T) %&gt;%<br>    as_flextable() <br><br><br><br>######### table 4 #########<br><br>NHANES %&gt;%<br>    tbl_continuous(<br>        variable = BMI,<br>        include=c(Gender, Race1),<br>        statistic = everything() ~ &quot;{mean} ({sd})&quot;,<br>        digits=everything()~1<br>    ) %&gt;%<br>    add_p(<br>        list(Gender~&quot;t.test&quot;),<br>        test.args = all_tests(&quot;t.test&quot;) ~ list(var.equal = TRUE)<br>    )</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed32f9583718" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>