# [R] use of "effects coding" in instrumental variables estimation

Franckx Laurent laurent.franckx at vito.be
Fri Jan 31 12:35:44 CET 2014

```Dear all,

I am estimating a model with instrumental variables using ivreg() (from the "AER" library). When the regressors and the instruments are limited to continuous variables, things work fine.

However, I have some regressors, such as "income", for which I only have categorical data (very low income, low income etc).

I have used the following procedure for "effects coding" (where " one_car_dist_car_fam " is the dataframe with the sample observations and  " totink_ovg" is the categorical variable):

one_car_dist_car_fam\$totink_ovg <- as.factor(one_car_dist_car_fam\$totink_ovg)

contrasts(one_car_dist_car_fam\$totink_ovg)  <- contr.sum(7)

See http://www.ats.ucla.edu/stat/r/library/contrast_coding.htm for a refernece.

Ordinary least squares works fine with this approach (where "voorby" is the dependent variable en "totpr11" is a continuous regressor):

> common_from <- formula(voorby ~ totpr11    + totink_ovg)

> dist_ols <- lm(common_from, data =  one_car_dist_car_fam)

However, when I apply this to ivreg(), I get the following result (I have now added an endogenous variable " verbrgem" and the instruments " gewleeg + cyl + kw"):

> common_from <- formula(voorby ~ totpr11  +  totink_ovg   + verbrgem )

> form_for_iv <- update.formula(common_from, ~ . | totpr11    + totink_ovg            + gewleeg + cyl + kw  )

> dist_iv <-ivreg( form_for_iv, data =  one_car_dist_car_fam)

Error in lm.fit(xz, y, offset = offset, ...) : 0 (non-NA) cases

1: In Ops.factor(totpr11, totink_ovg) : + not meaningful for factors

2: In Ops.factor(totpr11, totink_ovg) : + not meaningful for factors

Does anyone have an idea why this occurs with ivreg() and not with lm()?

Of course, I still have the backup of doing the "effects coding" manually, but I would first like to know whether there is an elegant way out of this specific problem.

Laurent Franckx, PhD
Senior researcher sustainable mobility
VITO NV | Boeretang 200 | 2400 Mol
Tel. ++ 32 14 33 58 22| mob. +32 479 25 59 07 | Skype: laurent.franckx | laurent.franckx at vito.be | Twitter @LaurentFranckx

VITO Disclaimer: http://www.vito.be/e-maildisclaimer

```