Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: holding variables to weighted means with prvalue

From   Christine Brickman <>
Subject   Re: st: holding variables to weighted means with prvalue
Date   Wed, 11 Aug 2010 10:18:31 -0700 (PDT)

Thanks, Steve.

Moments ago, I just discovered I can accomplish this with margins, which is new 
to Stata 11, I believe.

svyset [pweight=weight]
svy: regress i.male [list of remaining variables]
margins i.male
If you weight the data in the regression command, margins retains the weight.  
But there’s also an option to turn it off if you choose.

----- Original Message ----
From: Steve Samuels <>
Sent: Wed, August 11, 2010 12:06:24 PM
Subject: Re: st: holding variables to weighted means with prvalue

Not with -prvalue-.  Some of the holding specifications, like -max-
and -min- have no survey meaning.  So, apparently, none of them do.

Below is a do file which uses -adjust- which creates predictions at
the observed values for all covariates, holding others at their
weighted means.  It optionally plots the adjusted predictions.


Steven Samuels
18 Cantine's Island
Saugerties NY 12477
Voice: 845-246-0774
Fax:    206-202-4783

On Wed, Aug 11, 2010 at 10:43 AM, Christine Brickman
<> wrote:
> I want to compute predicted values while holding variable at their weighted
> means.  Even though I'm weighting the regression, STATA is holding the 
> at their unweighted means.  Here's my syntax:
> svyset [pweight=weight]
> svy: regress [VARLIST]
> forvalues i = 0(1)1 {
>                 prvalue, x(male=`i')
>     }
> Is there a way to hold all variables except male at their weighted means?
> Thanks!
> Christine

/**************************START CODE***********************************/
This do file e creates predicted values from survey regression
which vary in one predictor at a time, holding others at their
estimated population means.
Also computed are upper and lower confidence interval endpoints for
the prediction.
An optional graphics section plots predictions and CI endpoints for
each variable against that variable.
di c(level) // current confidence level . To change, -set level-.

sysuse auto,clear
svyset _n [pweight=rep78]

Define a predictor list for the right-hand-side of the svy: logit equation
local rhs mpg weight trunk

svy: mean `rhs' //outputs weighted means in _b[varname]

For each varying predictor, build up the -adjust- command.
For the adjusted predictor which varies mpg, the adjust command starts:

"adjust weight = 2941.106382978724 trunk = 13.73191489361702"

foreach v of varlist `rhs' {
local less : list rhs -v //Remove variable v from the rhs
local list_`v' " "

foreach w of varlist `less' {
local lw "`w' = `=_b[`w']'" // e.g. "weight = 2941.106382978724"
local list_`v' `list_`v'' `lw' // Augment the list
di "`v'"
di "`list_`v''"

svy: reg foreign `rhs'

For each predictor variable v (e.g. mpg), use -adjust- to compute:
pred_v : linear prediction
se_v : the se of the linear prediction
llim_v : lower 95% confidence interval endpoint
ulim_v : upper 95% confidence interval endpoint

foreach v of varlist `rhs' {
adjust "`list_`v''" , g(pred_`v' se_`v') se //The key line

gen llim_`v' = pred_`v' - (invttail(e(df_r), (1- c(level)/100)/2))*se_`v'
gen ulim_`v' = pred_`v' + (invttail(e(df_r), (1- c(level)/100)/2))*se_`v'

label var pred_`v' "Predicted Prob"
label var llim_`v' "Lower CI"
label var ulim_`v' "Upper CI"
// save (choose a new name)

// Optional Graph Module
foreach v of varlist `rhs' {
twoway scatter pred_`v' llim_`v' ulim_`v' `v', msymbol(o o o)
title("Plot of Prediction against `:variable label `v''" "Other
Predictors at their Mean") note("Program: `pname'. Data: ")
saving(graph_`v', replace)
/************************END CODE ***********************************/

*  For searches and help try:


*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index