Skip to content

Improve documentation#632

Open
nalimilan wants to merge 2 commits into
masterfrom
nl/docs
Open

Improve documentation#632
nalimilan wants to merge 2 commits into
masterfrom
nl/docs

Conversation

@nalimilan

Copy link
Copy Markdown
Member

Develop documentation about weights. Add docstrings for vcov and stderror. Move implementation and comparison with R to separate page and mention weights. Show using lines in examples instead of running them in a hidden block so that users can reproduce them. Also improve various details.

Develop documentation about weights. Add docstrings for `vcov` and `stderror`.
Move implementation and comparison with R to separate page and mention weights.
Show `using` lines in examples instead of running them in a hidden block so
that users can reproduce them. Also improve various details.
@nalimilan
nalimilan requested a review from andreasnoack January 3, 2026 21:26
@codecov

codecov Bot commented Jan 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.94%. Comparing base (8976b53) to head (4b538fa).
⚠️ Report is 7 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #632   +/-   ##
=======================================
  Coverage   96.94%   96.94%           
=======================================
  Files           8        8           
  Lines        1213     1213           
=======================================
  Hits         1176     1176           
  Misses         37       37           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/linpred.jl
Comment on lines +320 to +321
When the model is weighted with `ProbabilityWeights`, the sandwich estimator is used,
scaling by `nobs(x)/(nobs(x) - 1)` degrees of freedom.

@nalimilan nalimilan Jan 3, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gragusa It's not clear to me what justifies these degrees of freedom, nor why Stata uses a different approach. Would you have references about this in R's survey and Stata, and/or would you suggest wording improvements? These would be useful for users.

Am I right that this is completely different from the df.resid argument that can be passed to summary.svyglm?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gragusa Any chance you could have a look?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, there's no unique principled choice. svyglm's n/(n-1) follows the survey-statistics tradition (PSU-count-based, analogous to the (n-1) in the sample variance). Stata's pweights uses n/(n-k), which matches its standard HC1 robust SE in the equally-weighted limit. Asymptotically, all of n, n-1, n-k are equivalent, so the choice only matters in small samples.

For the df.resid: these are two completely separate things. df.resid in summary.svyglm is the denominator degrees of freedom for the t/Wald reference distribution used to compute p-values and confidence intervals. The n/(n-1) factor we're discussing here is a finite-sample correction that multiplies the sandwich variance estimate itself. The former affects only the reference distribution; the latter affects the variance estimate.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks. So you think we should keep the formulation I proposed? Any references we could cite to help understand this choice?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. The classic reference is Lumley's book "Complex Surveys". Also, STATA has two commands (ways) to run regression with pweights. One is my doing regress y x [pweights] (which defaults to n-k corrections). To other (https://www.stata.com/manuals/svyvarianceestimation.pdf) is to use the svy set of commands, which default to n-1 corrections.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah interesting, I'll see whether I can mention these somewhere in the docs. The fact that we follow at least one of Stata's definitions is good as it means both R and Stata users can replicate our results.

Comment thread docs/src/index.md
Comment on lines +152 to +159
Using analytic weights corresponds to weighted least squares.
This gives the same results as R and Stata.

Probability weights give the same point estimates as analytic weights, but standard errors
and p-values are based on a sandwich (heteroskedasticity-robust) estimator.
This gives the same results as the R `survey` package with a simple survey design
without strata nor clustering, but differs from Stata with the `pweights` option, which
adopts the same approach but with a different assumption regarding degrees of freedom.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gragusa This paragraph is worth checking too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is correct once we fix #638

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants