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: Create new variable with betacoeff of reg model

From   Maarten Buis <>
Subject   Re: st: Create new variable with betacoeff of reg model
Date   Mon, 20 Aug 2012 12:20:15 +0200

On Sun, Aug 19, 2012 at 5:12 PM,   Mehmet Altun wrote:
> I want to generate a new variable containing the value of the
> beta-coefficient for each country, each year. The dataset includes 81
> countries for the period 2001-2007.  My regression looks like
> reg performance firmvariables i.countryID i.year.
> The problem I have is, I have country-dummies and year dummies (using
> factor variable operator i.). I want the beta-coefficient to be calculated
> for each year AND each country. Based on the beta-coefficients i will
> generate a new independant variable (country-year) for another model. The
> new variable should only have one value for each country and year. e.g.: a
> betacoefficient value based on my regression model for Germany 2001,
> Germany 2002, Germany 2003...USA 2001, USA 2002, USA 2003...

*-------------------- begin example -------------------
sysuse nlsw88, clear

// black and occ play the role of country and year
// c_grade and ttl_exp the role of firmvariables
// I needed to coarsen these a bit to avoid empty cells
gen byte black = race == 2 if race < 3
gen byte occ = occupation
recode occ (9=10) (12=11)

// for the requested beta coefficients to make sense
// the value 0 on all "firmvariables" have to make sense
// and be in the observed range of the variable
gen byte c_grade = grade - 12

// this is the model you asked
reg wage grade ttl_exp i.occ

// this is the model you probably wanted
reg wage grade ttl_exp

// this is the same model, but giving you directly
// the requested coefficients
reg wage c_grade ttl_exp, hascons

// the beta coefficients are the predictions when
// c_grade and ttl_exp are 0
// add observations such that this is true for
// all combinations of black and occ
tempfile orig
gen byte added = 0
save `orig'
keep if !missing(black, occ)
bys black occ: keep if _n == 1
fillin black occ
drop _fillin
replace c_grade = 0
replace ttl_exp = 0
replace added = 1
append using `orig'

// predict xb for those added observations
predict beta1 if added, xb

// move the values to all observations with
// the appropriate values on black and occ
bys black occ: egen beta = mean(beta1)

// added observation are no longer needed
drop if added

// beta2 and added no longer need
drop beta1 added

// see the results
list occ black beta in 245/254

// compare with last regression
*--------------------- end example --------------------
(For more on examples I sent to the Statalist see: )

Also see: M.L. Buis (2012) "Stata tip 106: With or without reference",
The Stata Journal, 12(1), pp. 162-164.

Hope this helps,

Maarten L. Buis
Reichpietschufer 50
10785 Berlin
*   For searches and help try:

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