[R] weird question about lm() and data.frame

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri May 5 09:10:35 CEST 2006


On Thu, 4 May 2006, Guojun Zhu wrote:

> I am still battling with my project :(
>
> I have a line like this
>
> lm(month.model$next.ret~.,month.model,na.action=na.omit)

Yourexamplesmightactuallybereeadableifyouusedyourspacebar.

next.ret ~ .  would suffice here.

> month.model is a data frame with lots of columns.  One
> of them is "Oth", a dummy variable. I checked
> month.model, there are two rows with number 1 and
> others are either 0 or NA.  the two rows with Oth==1
> have no NA.  I have totally about 900 rows.  But after
> the regression, the Oth's coefficient is NA.  I do not
> understand.

You don't show us how you reach that conclusion, but probably aliasing is 
the cause.  See ?alias.

> Another issue is also about NA.  I offen use something
> like "model[model$Oth==1,]", but it will not only
> gives rows with oth==1, but also gives some rows with
> all item as NA.  I then changed this into "
> month.model[(month.model$Oth==1)&(!is.na(month.model$next.ret)),]"
> and it gives the same result.  Finally I change it
> into
> "
> month.model[(month.model$Oth==1)&(!is.na(month.model$Oth)),]"
> and it works.  next.ret is a column for sure.  Can
> anyone explain this to me?

You want month.model$Oth %in% 1

Such things are covered with detailed examples in chapter 2 of MASS4 (see 
the FAQ).


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list