Skip to content

model.matrix and missing values

2 messages · Jarrod Hadfield, John Fox

#
Hi,

Does anyone know an easy way of retaining rows in a model.matrix where  
missing values are present in the predictors.  Ideally I'd be able to  
retain these rows as zeros.

Thanks,

Jarrod
#
Dear Jarrod,

If you use na.action=na.exclude in fitting the model, then you can produce
rows of NAs in the model matrix with na.resid(); e.g., for a model m,

	mm <- naresid(m$na.action, model.matrix(m))

Then if you really want to convert the rows of NAs in mm to 0s, you could
use

	mm[is.na(mm)] <- 0

I hope this helps,
 John

------------------------------
John Fox, Professor
Department of Sociology
McMaster University
Hamilton, Ontario, Canada
web: socserv.mcmaster.ca/jfox
On
http://www.R-project.org/posting-guide.html