[R] RE: more on lm(y~x) question: removing NA´s

Thomas Lumley tlumley at u.washington.edu
Tue May 4 16:14:08 CEST 2004


On Tue, 4 May 2004, Liaw, Andy wrote:

> 1. If your code actually runs, you should upgrade R, and quit using `_' for
> assignment... 8-)
>
> 2. You seem to have an extraneous `]' after the na.exclude.  Could that be
> the problem?

More seriously, the for() loop over k will mess up the value of k that you
want to use for lm.

	-thomas


>
> Andy
>
>
> > From: Christoph Scherber
> >
> > actually, the situation is much more complicated. I am producing
> > multiple graphs within a "for" loop. For some strange reason, the
> > plotting routine always stops once lm(y~x) encounters more than one
> > missing value (I have marked the important bit with "***********"):
> >
> > par(mfrow=c(5,5))
> > p_seq(3,122,2)
> > i_0
> > k_0
> > number_0
> > for (i in p) {
> >    j_foranalysis[93:174,i+1]
> >    k_foranalysis[93:174,i]
> >    df_data.frame(j,k)
> >    mainlab1_substring(names(foranalysis[i]),2,8)
> >    mainlab2_"; corr.:"
> >    mainlab3_round(cor(j,k,na.method="available"),4)
> >    mainlab4_"; excl.Mono:"
> >    mainlab5_round(cor(j[j<0.9],k[j<0.9],na.method="available"),4)
> >    mainlab_paste(mainlab1,mainlab2,mainlab3,mainlab4,mainlab5)
> >    plot(k,j,main=mainlab,xlab="% of total biomass",ylab="% of total
> > cover",pch="n")
> >    for (k in 1:length(foranalysis[93:174,i]))
> > number[k]_substring(plotcode[foranalysis[k,1]],1,5)
> >    text(foranalysis[93:174,i],foranalysis[93:174,i+1],number)
> > **********************************
> >    model_lm(j~k,na.action=na.exclude])
> > **********************************
> >    abline(model)
> >    abline(0,1,lty=2)
> >     }
> >
> > Does anyone have any suggestions on this?
> >
> > Best regards
> > Chris.,
> >
> >
> >
> >
> > Liaw, Andy wrote:
> >
> > >By (`factory') default that's done for you automagically, because
> > >options("na.action") is `na.omit'.
> > >
> > >If you really want to do it `by hand', and have the data in
> > a data frame,
> > >you can use something like:
> > >
> > >lm(y ~ x, df[complete.cases(df),])
> > >
> > >HTH,
> > >Andy
> > >
> > >
> > >
> > >>From: Christoph Scherber
> > >>
> > >>Dear all,
> > >>
> > >>I have a data frame with different numbers of NA´s in each
> > >>column, e.g.:
> > >>
> > >>x       y
> > >>1      2
> > >>NA  3
> > >>NA  4
> > >>4     NA
> > >>1     5
> > >>NA NA
> > >>
> > >>
> > >>I now want to do a linear regression on y~x with all the NA´s
> > >>removed.
> > >>The problem now is that is.na(x) (and is.na(y) obviously
> > >>gives vectors
> > >>with different lengths. How could I solve this problem?
> > >>
> > >>Thank you very much for any help.
> > >>
> > >>Best regards
> > >>Chris
> > >>
> >
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle




More information about the R-help mailing list