[R] linear fit function with NA values

arun smartpink111 at yahoo.com
Sat Jul 27 17:33:30 CEST 2013



HI,
set.seed(28)
dat1<- as.data.frame(matrix(sample(c(NA,1:20),100,replace=TRUE),ncol=10))

set.seed(49)
dat2<- as.data.frame(matrix(sample(c(NA,40:80),100,replace=TRUE),ncol=10))
 lapply(seq_len(ncol(dat1)),function(i) {lm(dat2[,i]~dat1[,i])}) #works bcz the default setting removes NA
Regarding the options:
?lm()
na.action: a function which indicates what should happen when the data
          contain ‘NA’s.  The default is set by the ‘na.action’ setting
          of ‘options’, and is ‘na.fail’ if that is unset.  The
          ‘factory-fresh’ default is ‘na.omit’.  Another possible value
          is ‘NULL’, no action.  Value ‘na.exclude’ can be useful.

 lapply(seq_len(ncol(dat1)),function(i) {lm(dat2[,i]~dat1[,i],na.action=na.exclude)})
#or
 lapply(seq_len(ncol(dat1)),function(i) {lm(dat2[,i]~dat1[,i],na.action=na.omit)})

lapply(seq_len(ncol(dat1)),function(i) {lm(dat2[,i]~dat1[,i],na.action=na.fail)})
#Error in na.fail.default(list(`dat2[, i]` = c(54L, 59L, 50L, 64L, 40L,  : 
 # missing values in object

In your case, the error is different.  It could be something similar to the below case:
dat1[,1]<- NA

lapply(seq_len(ncol(dat1)),function(i) {lm(dat2[,i]~dat1[,i],na.action=na.omit)})
#Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
 # 0 (non-NA) cases # here it is different

 lapply(seq_len(ncol(dat1)),function(i) {try(lm(dat2[,i]~dat1[,i]))}) #works in the above case.  It may not work in your case.

You need to provide a reproducible example to understand the situation better.
A.K.













----- Original Message -----
From: iza.ch1 <iza.ch1 at op.pl>
To: r-help at r-project.org
Cc: 
Sent: Saturday, July 27, 2013 8:47 AM
Subject: [R] linear fit function with NA values

Hi

Quick question. I am running a multiple regression function for each column of two data sets. That means as a result I get several coefficients. I have a problem because data that I use for regression contains NA. How can I ignore NA in lm function. I use the following code for regression: 
OLS<-lapply(seq_len(ncol(es.w)),function(i) {lm(es.w[,i]~es.median[,i])})
as response I get
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  all values NA

thanks for help :)

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list