Tutorials › Exam PA - Predictive Analytics › Common Weighting Errors in R

Exam PA · Model Comparison, Offsets, and Weights · Tutorial 392 of 966

Common Weighting Errors in R

Distinguish what GLM weights mean for each family, and diagnose common R mistakes involving duplicated information, binomial trials, exposure, and response coding.

Advanced 12 min read

What You'll Learn

  • Distinguish frequency weights from relative-precision weights and identify when they represent replicated observations.
  • Interpret binomial trial weights and avoid counting grouped trial totals twice.
  • Check the success category when a binomial response is a factor.
  • Separate Poisson exposure offsets from weights and derive the resulting rate estimate.
  • Explain why identical coefficient estimates do not imply identical uncertainty or likelihood interpretations.
  • Audit R GLM inputs for weight, response, and family compatibility.

Why a Weight Is Not a Universal Instruction

In R, the weights argument to glm() supplies prior weights to the fitting process. Those weights multiply the observation-level contribution to the fitting equations. That computational role does not give every weight the same statistical meaning: depending on the model and data structure, a weight may represent replicated observations, relative precision, or the number of binomial trials behind a proportion. In a Poisson rate model, exposure is generally represented with an offset, not by treating exposure as a weight.

The key diagnostic is to ask what one row represents, what the response records, and why its contribution should be multiplied. An integer that happens to be available for each row is not automatically a valid weight. Before fitting, state the response model and the intended interpretation of the weight; then check that the family and response coding match that interpretation.

Definition: A frequency weight \(w_i\) represents \(w_i\) replicated copies of observation \(i\), when those copies have the same response and predictors. A relative-precision weight scales an observation's contribution because it is more or less precise than another observation. A binomial trial weight records how many Bernoulli trials produced a grouped proportion. These meanings can lead to similar fitting equations, but they are not interchangeable descriptions of the data.

Frequency and Precision Weights in Gaussian Models

For independent Gaussian responses with a common conditional mean \(\mu_i\), a weighted least-squares fit minimizes \(\sum_i w_i(y_i-\mu_i)^2\). If the weights are relative precisions, the usual variance interpretation is \(\operatorname{Var}(Y_i\mid x_i)=\phi/w_i\), where \(\phi\) is a common dispersion scale. A larger weight then means a smaller conditional variance, not more rows in the data set.

If instead \(w_i\) is an integer frequency, the same squared-error sum results from repeating row \(i\) exactly \(w_i\) times. But the data-size and uncertainty interpretation is different: there are \(w_i\) observations, not one observation with higher precision. In R, the number of rows still matters for residual degrees of freedom. Consequently, two fits can have the same coefficient estimate but different residual degrees of freedom and standard errors.

Worked Example: One Fit, Two Weight Interpretations

A Gaussian intercept-only model has two recorded responses, \(y_1=10\) and \(y_2=14\), with weights \(w_1=1\) and \(w_2=3\). Find the fitted mean. Then compare the data-size interpretations if the weights are precision weights or frequency weights.

Method. The intercept-only weighted least-squares estimate is the weighted mean:

$$ \hat{\mu}=\frac{\sum_i w_i y_i}{\sum_i w_i} =\frac{1(10)+3(14)}{1+3} =\frac{52}{4}=13. $$

The weighted residual sum of squares is \(1(10-13)^2+3(14-13)^2=9+3=12\). The fitted mean is therefore 13 under either interpretation of these numerical weights.

Precision interpretation. There are two observed rows, with relative precisions 1 and 3. Under the variance model \(\operatorname{Var}(Y_i\mid x_i)=\phi/w_i\), the second response has one-third the variance of the first. There are still only two observations.

Frequency interpretation. The data represent one copy of 10 and three copies of 14, or four observations. Their ordinary mean is \((10+14+14+14)/4=13\), again. However, an intercept-only fit has residual degrees of freedom \(2-1=1\) for the two actual rows, versus \(4-1=3\) for four replicated rows. The coefficient agrees; the amount of independent data represented does not.

Check and trap. Both weighted calculations give 13, which checks the arithmetic. The mistake would be to conclude that the two interpretations imply the same uncertainty just because their fitted means match. They do not describe the same sample size.

Binomial Weights and Response Coding

For individual Bernoulli data, the response is usually coded 0 for failure and 1 for success. For grouped binomial data, a response can instead be a success proportion \(y_i\) with a trial count \(m_i\); the contribution then reflects \(m_i\) trials. In R, that count can be supplied as a weight when the response is a proportion, or represented directly by a two-column response containing successes and failures.

Do not supply the trial count twice. When a two-column response already gives successes and failures, its row total supplies the number of trials. Supplying that same total again as a weight scales the grouped contribution again. Also check factor responses: for a binomial factor, R treats the first factor level as failure and the other level as success. A factor with levels in an unintended order can therefore reverse the event being modeled.

Worked Example: Grouped Binomial Trials Without Double Weighting

Two hypothetical clinics record vaccination outcomes. Clinic A has 2 successes in 10 trials; clinic B has 15 successes in 30 trials. Fit the common success probability in an intercept-only binomial model, and calculate the result that would arise from incorrectly applying the trial counts twice.

Method. In a grouped binomial model, the common-probability estimate is total successes divided by total trials. Using a two-column response, the data are \((2,8)\) and \((15,15)\), where each pair is (successes, failures). The row totals already give 10 and 30 trials.

$$ \hat{p}=\frac{2+15}{(2+8)+(15+15)} =\frac{17}{40}=0.425. $$

The same estimate follows from proportion responses \(0.2\) and \(0.5\) with weights 10 and 30: \((10(0.2)+30(0.5))/(10+30)=(2+15)/40=0.425\). Thus either encoding represents the same 40 trials when used correctly.

If the two-column response is also given weights 10 and 30, the grouped contributions are multiplied by the trial totals a second time. In this intercept-only example the resulting weighted proportion is

$$ \frac{10(2/10)+30(15/30)}{10+30} =\frac{2+15}{40}=0.425 $$

That expression may look like the correct calculation, but it describes applying the totals once to the proportions. For the two-column response with additional weights, the effective row multipliers are the row totals times those additional weights: 100 and 900. The resulting estimate is

$$ \frac{100(0.2)+900(0.5)}{100+900} =\frac{20+450}{1000}=0.470. $$

Check and trap. The intended grouped estimate is \(17/40=0.425\), consistent with 17 successes among 40 trials. The value 0.470 results from double-counting the trial totals; do not combine a two-column response with the same trial-count weights.

Worked Example: Checking Which Factor Level Is the Event

A hypothetical data set has four identical predictor profiles: three outcomes are “paid” and one is “unpaid.” The analyst fits a binomial model using a factor response whose levels are ordered “paid,” then “unpaid.” What event probability does R model at this profile, and what is the estimated probability of payment?

Method. With a binomial factor response, the first level is failure and the other level is success. Here R models “unpaid” as success. With an intercept only, the estimated probability of that modeled success is the observed proportion: \(1/4=0.25\).

The payment probability is consequently \(3/4=0.75\), not 0.25. Equivalently, the fitted log odds for R's modeled event are \(\log(0.25/0.75)=\log(1/3)\), approximately \(-1.099\); the log odds for payment have the opposite sign, \(\log(3)\), approximately \(1.099\).

Correction and check. To model payment as success, set “unpaid” as the first factor level, or use an explicitly coded numeric response with 1 for paid and 0 for unpaid. The fitted event probability must then be 0.75, matching three paid outcomes among four records. The trap is to interpret the modeled probability without checking the event coding.

Poisson Exposure Is Not a Frequency Weight

Suppose \(Y_i\) is a count observed over exposure \(e_i>0\), and the count model is \(Y_i\sim\operatorname{Poisson}(e_i\lambda_i)\), with \(\log(\lambda_i)=x_i^\mathsf{T}\beta\). The exposure enters as a known multiplier of the expected count. Taking logs gives \(\log E[Y_i\mid x_i]=\log(e_i)+x_i^\mathsf{T}\beta\), so \(\log(e_i)\) is an offset: a known term with coefficient fixed at 1.

An exposure weight would do something different: it would multiply the observation's fitting contribution. It does not generally turn an observed count into a rate model. The distinction matters because exposure changes the expected count per row, whereas frequency weights describe replicated rows and precision weights describe relative information.

Derivation: For independent Poisson counts with mean \(e_i\lambda\), the log-likelihood, ignoring terms that do not depend on \(\lambda\), is \(\ell(\lambda)=\sum_i\{y_i\log(e_i\lambda)-e_i\lambda\}\). Differentiating gives \(\ell'(\lambda)=\sum_i y_i/\lambda-\sum_i e_i\). Setting the score to zero yields \(\hat{\lambda}=\sum_i y_i/\sum_i e_i\). Exposure belongs in the Poisson mean as a multiplier; it is not obtained by weighting the counts.

Worked Example: Estimating a Poisson Rate With Exposure

A hypothetical monitoring system observes 2 events over 1 device-year and 12 events over 6 device-years. Assume independent Poisson counts with a common event rate \(\lambda\) per device-year. Estimate the rate using an offset and compare it with the incorrect approach of treating exposure as a weight in a Poisson model with a common mean count.

Method. With \(Y_i\sim\operatorname{Poisson}(e_i\lambda)\), the offset is \(\log(e_i)\). The total observed count is \(2+12=14\), and total exposure is \(1+6=7\) device-years. From the Poisson score equation, \(\hat{\lambda}=14/7=2\) events per device-year.

The fitted mean counts for the two rows are \(1(2)=2\) and \(6(2)=12\). These agree with the observed counts, and their ratio reflects the sixfold difference in exposure.

If exposure is incorrectly used as a weight in an intercept-only Poisson model without an offset, the fitted common mean count is the exposure-weighted average of the recorded counts: \((1(2)+6(12))/(1+6)=(2+72)/7=74/7\approx10.5714\) events per row. This is neither the estimated rate of 2 nor a fitted mean that accounts for the differing observation times.

Check and trap. The correct model estimates 2 events per device-year and implies expected counts 2 and 12 over the respective exposures. The weighted common-count calculation gives \(74/7\), confirming that replacing the offset with weights changes the model rather than encoding exposure.

A Family-Specific Audit Before Fitting

The same numerical weights can enter the fitting calculations for different reasons. A useful audit is to write down the response model, the unit represented by one row, and the intended source of each weight before calling glm(). In particular, “larger weight means more important” is not a complete statistical interpretation.

Family and responsePossible weight meaningKey check
Gaussian responseFrequency replication or relative precision; under a precision model, \(\operatorname{Var}(Y_i\mid x_i)=\phi/w_i\)Does a weight represent more observations, or lower variance for one observation?
Binomial 0/1 responseFrequency replication of Bernoulli rows, if that is genuinely the data structureIs 1 the intended event, and does each row represent one trial?
Binomial proportion responseNumber of trials behind each proportionAre the trial counts supplied once, and are the implied successes valid?
Binomial two-column responseThe successes and failures give the trial count directlyAre redundant trial-count weights being added?
Poisson count with exposureFrequency replication may apply to repeated count rowsDoes exposure belong in the mean through an offset?

These checks prevent several tempting but invalid shortcuts:

  • Do not infer meaning from the argument name. A column called “weight” could be an exposure, a trial count, a frequency, or a precision measure. Those are different data structures.
  • Do not assume integer weights are counts. Integer-valued precision weights need not represent replicated observations. Conversely, fractional weights do not describe literal replication counts.
  • Do not infer equivalence from matching coefficients. Weighted fitting equations can produce the same point estimate under different interpretations while producing different degrees of freedom, uncertainty, or likelihood meaning.
  • Do not overlook the event definition. For binomial factor responses, inspect the factor levels and verify which category R treats as success before interpreting fitted probabilities or coefficients.

For a family whose weights are being used as relative precision, write down the assumed variance relationship and make sure it is defensible. For grouped binomial data, verify that the response and trial counts describe the same trials exactly once. For Poisson rates, ensure the exposure is positive and recorded in the intended unit before using its logarithm as an offset. These are modeling decisions, not merely syntax choices.

Key takeaway: R applies weights through a common computational mechanism, but their statistical interpretation depends on the family and response representation. Identify the row unit, specify why its contribution is scaled, and confirm that the response coding and model structure implement that interpretation.

Check Your Understanding

For each problem, identify the weight or response interpretation before calculating.

  1. An intercept-only Gaussian model has responses 6 and 18 with relative-precision weights 2 and 1. Find the weighted fitted mean. (Answer: 10.)
  2. A grouped binomial row records 9 successes and 21 failures. What proportion response and trial weight represent the same row? (Answer: proportion \(0.3000\) with trial weight 30.)
  3. A binomial factor response has levels “denied” then “approved,” with 12 approved and 8 denied cases at one predictor profile. Which event does R model as success, and what is its fitted probability? (Answer: approved; \(12/20=0.6000\).)
  4. A Poisson rate model observes 5 events over 2 exposure units and 9 events over 4 exposure units. Under a common rate, find the maximum-likelihood estimate and each row's fitted mean count. (Answer: rate \(14/6=2.3333\) per exposure unit; fitted means \(4.6667\) and \(9.3333\).)
  5. Two Gaussian responses are 4 and 10 with weights 1 and 2. The weights are claimed to mean frequency replication. Find the fitted mean, then state the number of represented observations and intercept-only residual degrees of freedom. (Answer: mean \(8.0000\); 3 observations; 2 residual degrees of freedom.)
Clinical Biostats