<?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 Jonathan Morán on Medium]]></title>
        <description><![CDATA[Stories by Jonathan Morán on Medium]]></description>
        <link>https://medium.com/@jon77mon?source=rss-364086d70592------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-BJnpfC9qwEMcUHxDRon2g.jpeg</url>
            <title>Stories by Jonathan Morán on Medium</title>
            <link>https://medium.com/@jon77mon?source=rss-364086d70592------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 09:37:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jon77mon/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[Calibration of Survey Weights with survey in R]]></title>
            <link>https://medium.com/@jon77mon/calibration-of-survey-weights-with-survey-in-r-54b2860a04c6?source=rss-364086d70592------2</link>
            <guid isPermaLink="false">https://medium.com/p/54b2860a04c6</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[r]]></category>
            <category><![CDATA[surveys]]></category>
            <category><![CDATA[social-science-research]]></category>
            <category><![CDATA[statistics]]></category>
            <dc:creator><![CDATA[Jonathan Morán]]></dc:creator>
            <pubDate>Wed, 02 Apr 2025 22:05:11 GMT</pubDate>
            <atom:updated>2025-04-07T16:08:10.436Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="“Don’t Trust Raw Survey Data! Here’s How to Adjust Weights in R”" src="https://cdn-images-1.medium.com/max/1024/1*_ZibEyRMXXVXWSdbzJEGKA.jpeg" /></figure><p>Often, it is not only of interest to analyze the sample data but also to extrapolate the data to the population in order to have a more objective analysis. However, extrapolating with weights from a known population (sex, race, city, social status…) can be tedious, as the difference between known totals and the survey totalization does not match.</p><p>In this article I will show how to calibrate the weights of a survey so that the sample tabulations match the sample frame. We will use the BigCity framework and draw a sample proportional to the expenditure (Expenditure variable) of the citizens.</p><p>Although the sample drawn is generally proportional, some imbalances remain — most notably, the distribution of the Poverty variable does not perfectly align with that of the sampling frame. Calibration allows us to adjust these discrepancies so that the sample more accurately reflects the population.</p><h3>1. Exploration of the Sampling Frame and Sample Collection</h3><p>First, we loaded the sample frame and explored its main characteristics. We calculate the expenditure proportion to use as a weight in the sample selection:</p><pre><br>library(dplyr) #data manipulation<br>library(janitor)#Display marginals and totals <br>#This library contains data on the total population of a hypothetical city.<br>library(TeachingSampling)#specialized bookstore to extract samples <br>library(survey)#library to analyze surveys<br><br>options(scipen = 999, digits = 4)<br><br>data(&quot;BigCity&quot;)  # We load the sample frame<br>glimpse(BigCity) # We explore the framework<br>Rows: 150,266<br>Columns: 12<br>$ HHID        &lt;chr&gt; &quot;idHH00001&quot;, &quot;idHH00001&quot;, &quot;idH…<br>$ PersonID    &lt;chr&gt; &quot;idPer01&quot;, &quot;idPer02&quot;, &quot;idPer03…<br>$ Stratum     &lt;chr&gt; &quot;idStrt001&quot;, &quot;idStrt001&quot;, &quot;idS…<br>$ PSU         &lt;chr&gt; &quot;PSU0001&quot;, &quot;PSU0001&quot;, &quot;PSU0001…<br>$ Zone        &lt;chr&gt; &quot;Rural&quot;, &quot;Rural&quot;, &quot;Rural&quot;, &quot;Ru…<br>$ Sex         &lt;chr&gt; &quot;Male&quot;, &quot;Female&quot;, &quot;Female&quot;, &quot;M…<br>$ Age         &lt;int&gt; 38, 40, 20, 19, 18, 35, 29, 14…<br>$ MaritalST   &lt;fct&gt; Married, Married, Single, Sing…<br>$ Income      &lt;dbl&gt; 555.0, 555.0, 555.0, 555.0, 55…<br>$ Expenditure &lt;dbl&gt; 488.3, 488.3, 488.3, 488.3, 48…<br>$ Employment  &lt;fct&gt; Employed, Employed, Inactive, …<br>$ Poverty     &lt;fct&gt; NotPoor, NotPoor, NotPoor, Not…<br><br># We calculate the proportion of expenditure<br>BigCity$x &lt;- BigCity$Expenditure / sum(BigCity$Expenditure)<br><br># We visualize the distribution of spending in the framework<br>hist(BigCity$x, col = &quot;skyblue1&quot;, main = &quot;Proportion of expenditure&quot;)<br><br># We draw a sample proportional to the expenditure<br>smple &lt;- BigCity %&gt;%<br>  sample_frac(size = 9000 / nrow(BigCity), replace = FALSE, weight = x)<br><br>hist(smple$x, col = &quot;skyblue2&quot;, main = &quot;Proportion of expenditure (sample)&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*A-95baz8jGiYMKMenA3AaQ.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*4QkJZUgPH4JvgReFoQnQLg.jpeg" /><figcaption>Proportional distribution of population vs. sample</figcaption></figure><p>It is observed that the sample presents certain differences in the distribution; this imbalance is the first bias we face.</p><h3>2. Tabulations: Population vs. Sample</h3><p>Before calibrating, we compare the tabulations of the variable Poverty (and others, such as Sex) in the frame and in the sample.</p><pre># Poverty tabulations in the sample frame<br>tabyl(BigCity$Poverty) %&gt;% adorn_totals()<br><br>BigCity$Poverty      n percent<br>         NotPoor  96144 0.63983<br>         Extreme  12748 0.08484<br>        Relative  41374 0.27534<br>           Total 150266 1.00000<br><br># Tables of Poverty in the sample<br>tabyl(smple$Poverty) %&gt;% adorn_totals()<br>smple$Poverty    n percent<br>       NotPoor 6625 0.73611<br>       Extreme  461 0.05122<br>      Relative 1914 0.21267<br>         Total 9000 1.00000</pre><p>Although the grand total coincides, the subtotals show differences in distribution.</p><h3>3. Estimation of Initial Weights and Sample Design</h3><p>We first compute initial weights according to the proportion of observations in each stratum:</p><pre># Calculation of totals by stratum in the frame<br>Nh &lt;- BigCity %&gt;%<br>  group_by(Stratum) %&gt;%<br>  count() %&gt;%<br>  rename(Nh = n)<br><br># We incorporate the total by stratum in the sample and calculate the inverse weight of the sample proportion.<br>smple &lt;- smple %&gt;%<br>  left_join(Nh, by = &quot;Stratum&quot;) %&gt;%<br>  group_by(Stratum) %&gt;%<br>  mutate(aux = 1, nh = sum(aux)) %&gt;%<br>  ungroup() %&gt;%<br>  select(-aux) %&gt;%<br>  mutate(ph1 = nh / Nh,<br>         pw  = 1 / ph1)<br><br># We define the sample design<br>dsg &lt;- svydesign(ids = ~1,<br>                 strata = ~Stratum,<br>                 weights = ~pw,<br>                 data = smple)</pre><h3>4. Calibration of Weights: Methods and Application</h3><p>The calibrate function of the survey package is used to adjust the weights. The objective is to match the sample-weighted totals to the sample frame totals for the Sex and Poverty variables. We calculated the population totals and applied three calibration methods:</p><pre># We define the calibration formula and the population totals.<br>formula &lt;- ~Sex + Poverty <br>T &lt;- apply(model.matrix(formula, data = BigCity), 2, sum)<br><br># Calibration with raking<br>raking &lt;- calibrate(dsg, formula, population = T, calfun = &quot;raking&quot;)<br>smple$pw_raking &lt;- weights(raking)<br><br># Linear calibration<br>linear &lt;- calibrate(dsg, formula, population = T, calfun = &quot;linear&quot;)<br>smple$pw_linear &lt;- weights(linear)<br><br># Calibración logit (útil para acotar pesos y evitar extremos)<br>logit &lt;- calibrate(dsg, formula, population = T, calfun = &quot;logit&quot;)<br>smple$pw_logit &lt;- weights(logit)</pre><h3>When to use each method?</h3><ul><li><strong>Raking:</strong><br>It is similar to proportional prorating; weights are iteratively adjusted so that the marginal totals (e.g., Sex and Poverty) match those of the frame. It is ideal when the marginal distributions are known and a simple and robust fit is sought.</li><li><strong>Linear:</strong><br>Used when a linear relationship between auxiliary variables and weights is assumed. It may be preferable when a straightforward solution is desired, especially if collinearity is not an issue.</li><li><strong>Logit:</strong><br>It is the method of choice when it is necessary to limit the weights within predefined limits to avoid extreme values, which is especially useful in situations where some variables may generate very large or small adjustments.</li></ul><blockquote>Note: When the data are sparse, fitting with the linear or logit argument causes some estimated weights to be negative or requires finite bounds. No one wants a negative weight! In this case, it is advisable to define in the argument of the calibrate function the minimum and maximum intervals of the calibrated weight.</blockquote><pre>#Example: define bounds<br>calibrate(desing, formula, population, calfun = &quot;linear&quot;, bounds=c(-Inf,Inf))</pre><h3>5. Comparison of Post-Calibration Tables</h3><p>Finally, the tabulations of the calibrated sample are compared with those of the sample frame. For example, for the variable Sex:</p><pre># Sex tabulation in the framework<br>BigCity %&gt;% <br>group_by(Sex) %&gt;% <br>count() %&gt;% <br>adorn_totals()<br> Sex      n<br> Female  79190<br>   Male  71076<br>  Total 150266<br>#Tabulation of Sex before calibrated weights <br>smple %&gt;% <br>group_by(Sex) %&gt;% <br>summarise(y = sum(pw)) %&gt;% <br>adorn_totals()<br>    Sex         y<br> Female  78225.74<br>   Male  72040.26<br>  Total 150266.00<br># Tabulation of Sex with calibrated weights (e.g., raking method)<br>smple %&gt;% <br>group_by(Sex) %&gt;% <br>summarise(y = sum(pw_raking)) %&gt;% <br>adorn_totals()<br>Sex      y<br> Female  79190<br>   Male  71076<br>  Total 150266</pre><p>It can be seen that, although the grand total is maintained, the subtotals are adjusted to match the actual distribution of the frame. This procedure is key to obtaining representative estimates and reducing bias in subsequent analyses.</p><h3>6. Comparison of estimator accuracy before and after calibration</h3><p>Let us compare the estimation error relative in the tabulated <em>Poverty</em> variable:</p><pre>#cv before calibrating<br>cv(svytotal(~Poverty, dsg))*100<br> PovertyNotPoor  PovertyExtreme PovertyRelative <br>     0.68710534      4.59988811      2.05540409          <br><br>#cv after calibrating<br>cv(svytotal(~Poverty, linear))*100<br>         PovertyNotPoor          PovertyExtreme         PovertyRelative <br>0.000000000000014409305 0.000000000000013448326 0.000000000000022013082 </pre><h3>Conclusion</h3><p>Weight calibration allows correcting imbalances in the sample and ensuring that the results are consistent with the sampling frame. The raking method works as a proportional iterative prorating, while linear and logit offer alternatives depending on linearity and the need to bound the weights, respectively. This tool is essential in survey analysis, as it optimizes the representativeness and precision of the estimates.</p><blockquote><strong>However, none of this is possible if there are no records of a known population. This is the limit of the method.</strong></blockquote><h3>References</h3><p><a href="https://psirusteam.github.io/HHS-Handbook/estimadores-de-calibraci%C3%B3n.html">https://psirusteam.github.io/HHS-Handbook/estimadores-de-calibraci%C3%B3n.html</a></p><p><a href="https://cvxr.rbind.io/cvxr_examples/cvxr_survey_calibration/">https://cvxr.rbind.io/cvxr_examples/cvxr_survey_calibration/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54b2860a04c6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>