Repository navigation
Description
freq_weights for GLM has been merged in #2835
the summ issue is #2848
the PR #2835 was testing compared to the model with expanded, repeated data
I spot checked against Stata
issues to check
- optimization: what's the behavior if weights are None
- residuals and their usage: unclear, see below
- applicability or reusability for other weight types
- margins: not checked yet at all.
Stata has a list ofmarginsnotokafter fweight. How do weights affectmargins? Currently we only have margins for change in mean, equivalent topredict. Aggregation, i.e. averaged margins and margins at average exog, would need to take weights into account.
compared to SUMM issue, I looking specifically at the comparison to Poisson and GLM-Poisson in Stata
residuals and similar
checking anscombe and deviance residuals, they both agree with Stata fweights
correction only anscombe agrees, deviance residuals: we use weighted, Stata uses unweighted, AFAICS.
Calling model.family.resid_dev without freq_weights produces the same deviance residuals as Stata, i.e. it is unweighted.
Using aweights in Stata produces exactly the same resid_dev as using fweights.
related issue: fittedvalues is on a per observation basis, i.e. not weighted. checking an example we have the same fittedvalues with freq_weights, as Stata has with aweights (should be the same as with fweights)
Maybe predict should get weights. AFAICS, WLS predict does not allow weights. Not needed in predicting mean but would be needed for prediction/confidence interval.
-> check get_prediction behavior for se and intervals.
Currently, with PR #2835, the definitions are a bit inconsistent
e.g. resid_reponse is weighted, endog - fittedvalues are unweighted.
possible solution: make all resid_xxx unweighted, add wresid_xxx but don't cache them. (analogous to wresid). (What we need depends on the use case.)
reusing for other weights
comparing fweights to Stata aweights:
- params are the same
- bse differ by sqrt(nobs / wnobs) -> need to reset wnobs
- Stata docs mention that aweights and pweights use fweights after normalizing the weights to nobs.
- not clear for auxiliary results: e.g. resid_pearson should take account of var_weights but not of freq_weights, I guess.
- reweighting freq_weights as use to sum to nobs agrees with Stata aweights results for bse, llf, deviance and pearson (
(resid_pearson**2).sum())
pweights:
- I'm not able to get the same bse as Stata, they don't differ by a constant factor for HC1 in any version with weights as in the aweight equivalent experiments.
paramsagree and are the same in all version of weights.
update the issue here is not pweights itself. The calculation of HC1 assumes freq_weights, and using freq_weights normalized to sum to nobs is not the same as aweights - llf, pearson and deviance in Stata with pweights look like they correspond to fweights (larger sample) while reported nobs and df_resid uses the small nobs=number of rows.
cov_type
- using freq_weights: HC1 corresponds to Stata fweights
vce(robust)with the usual correction ( for bse: by factorsqrt((wnobs - 1) / nobs))
Activity
josef-pkt commentedon Mar 10, 2016
Related aside: Stata has removed aweights from Logit and Poisson (and maybe other discrete)
http://www.stata.com/statalist/archive/2008-07/msg01156.html
but they are available for GLM.
another observation: using cluster robust standard errors in Stata glm poisson produces the same bse with aweights, fweights and pweights, even though the summary shows different number of observations, and different llf and similar.
The meaning of cluster robust fweights might not be clear if weights are on observations and not on clusters as in GEE. (Note, small sample correction depends on number of clusters not on number of observations.)
Also, Stata might not have a consistent "story" for glm poisson with aweights, so maybe it's not a "verifying benchmark" for the extra results.
Similar for time series, what does fweights mean if we have a time series and want to use HAC robust standard errors. Frequency weights don't really make sense since we need a sequence of observations. panel robust cov_types could be interpreted the same way as clusters for repeated, identical panels.
aweights would still work in the different contexts to correct for general heteroscedasticity. pweights might or might not make sense with clustered or correlated data/cov_params.
josef-pkt commentedon Mar 10, 2016
suggestions so far (I'm not sure yet)
HCx] with frequ_weights, purely computational implementation that might not have a meaning but I don't want to raise and exception.weightsinstead offreq_weightsin those places where all weights agree on the calculationvar_weightssupport mainly by adding wnobs = nobs, and checking cov_type='HC'(I need those for treatment effects, and we need it in preparation for
surveysupport)josef-pkt commentedon Mar 11, 2016
browsing some Stata user questions
freq_weights with cluster robust standard errors does make sense when we have large clusters (like geographic area, states, schools) and repeated observations within a cluster, e.g. individuals or students with identical observations (endog and exog).
related for aweights or prob weights: http://www.statalist.org/forums/forum/general-stata-discussion/general/1290771-diagnostics-tests-for-iv-regression-with-clustered-robust-standard-errors-and-weight for ivreg2
josef-pkt commentedon Mar 11, 2016
related small sample factor in reg versus svyreg with cluster robust
http://stats.stackexchange.com/questions/190741/cluster-robust-se-in-stata-when-using-a-survey-design
It looks like they svy has the same difference to plain cluster robust as we. (I didn't check details.)
edit: but question is on linear regression where Stata uses different definition from GLM or Poisson.
kshedden commentedon Mar 11, 2016
Related: I have a stalled effort to enhance our GEE weights (which are
pweights) to allow individual-specific weights. They currently must be
constant for each group (cluster). One application here is to use the
weights to handle informative dropout in longitudinal studies.
I struggled with how to incorporate the weights into the method-of-moments
update of the dependence parameters. These moment estimates usually
involve iterating over pairs of observations in a group. It turns out that
taking the geometric means of the weights for the two observations is the
right way to do the weighting here.
On Thu, Mar 10, 2016 at 7:42 PM, Josef Perktold [email protected]
wrote:
josef-pkt commentedon Mar 11, 2016
@kshedden Do the weights also affect the calculation of the robust covariance, or is the problem mainly in the estimation of the dependence parameters?
I haven't been able to replicate Stata's cluster robust standard errors with weights yet by trial and error. So, I will have to look more systematically at the calculations for this.
It looks like that there are additional differences in the way weights enter the cluster robust calculations, compared to the adjustment of score_obs and hessian that I did for heteroscedasticity (HC1) robust covariances.
kshedden commentedon Mar 11, 2016
This was a year or so ago, but after quite a struggle I believe I was able
to exactly match Stata's robust covariance with weights. IIRC there is a
cluster-specific scaling factor that was hard to reconstruct but eventually
I worked it out. This is in the context of xtgee, I haven't looked at
other places in Stata with robust covariance. I hope to get back to this
in a few weeks.
On Thu, Mar 10, 2016 at 8:03 PM, Josef Perktold [email protected]
wrote:
josef-pkt commentedon Mar 11, 2016
another check in Stata: cluster robust with every observation a unique cluster produces the same bse as robust (HC1) when using aweights, but not when using fweights.
edit:
that's not the case for us: cluster bse are proportional to HC1 (with each obs/row is a cluster), 1.7 in the simple example with 17 rows and wnobs=33. Note, we have 17 clusters because each row is a cluster, but with frequencies we have 33 observations. All observations that are combined in a row are assumed to belong to the same cluster.
If I normalize weights to nobs, then the I get the same ratio 1.7 between bse in cov_type cluster and cov_type HC1
1.7 = res.nobs / res.resid in this case
(Do we have information about how many clusters are used in the cluster robust cov_params calculations?
res.n_groupsis 17)Note: I haven't fixed the small sample correction for cluster robust yet to take wnobs into account, it still uses exog.shape[0] or similar for determining nobs.
Also: Stata documentation for P:
_robustmentions that the weight formulas for sandwiches are different for freq_weights and aweights.josef-pkt commentedon Mar 13, 2016
Bug in current weight plus cluster robust:
We are adding the score_obs before taking the cross product. Addition with freq_weights requires full weights not sqrt(weights).
Repeating observations agrees with Stata's fweights for cluster robust, subject to the usual scaling factor (nobs - 1) / df_resid
edit:
if I use cov_cluster directly for stats with score and hessian tuple, then I get the cov_params proportional to Stata, i.e. don't use the fweight correction that I added _get_sandwich_arrays. There is still the incorrect sample size in it.
(for aweight, normalized freq_weights, it's the usual scaling factor compared to R)
using
use_correction=Falseproduces the same cluster robust bse this way as the repeated data.