Also - FYI setting na.rm to TRUE, it does exactly what you indicate you want it to do, it ignores the values in the calculations. It doesn't actually remove them from your dataframe.

On Dec 2, 2018 13:54, "Kyzar,Tricia E" <[log in to unmask]> wrote:

Thank you for replying Geraldine.  I will admit Im a bit unsure how to set these options up.  I thought that setting na.rm=FALSE meant that it would not delete the rows where there are NAs, because I do want to keep the rows, but ignore those cells.

 

Ive also tried running it the way you suggest and still get the same error message:

> lm.fit=lm(TN_2~., Subs_TN, na.rm=TRUE)

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :

  contrasts can be applied only to factors with 2 or more levels

 

Thank you for any help!

 

Tricia Kyzar

Ph: 352-392-7260

Email: [log in to unmask]

 

From: UF R Users List <[log in to unmask]> On Behalf Of Klarenberg,Geraldine
Sent: Sunday, December 2, 2018 1:47 PM
To: [log in to unmask]
Subject: Re: lm() with NA's

 

In the first version, have you tried setting "na.rm=TRUE"? Because in the way you wrote it, you're telling R to keep the NAs, which is not what you want (na.rm stands for "remove NA", so if you set it to TRUE, you are removing them).

However, the error message seems to be related to factor levels, so there might be a different problem there. The error message implies you don't have more than 1 level?

 

Geraldine

 

On Dec 2, 2018 13:24, "Kyzar,Tricia E" <[log in to unmask]> wrote:

I have a dataset: Subs_TN this is a subset of my full dataset, in this subset there are no NAs for my dependent variable (TN_2)

But there ARE NAs in places in all my other predictor columns.

 

I want to run a linear regression model using all of my predictor columns (this is a real world dataset and sporadic NAs will always be there), but Im having problems figuring out how to work around this. 

 

Below are examples of how Ive set up the lm() function and the errors Ive gotten back.  How can I perform linear regression when there are NAs in my predictor variables?

 

 

> lm.fit=lm(TN_2~., Subs_TN, na.rm=FALSE)

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :

  contrasts can be applied only to factors with 2 or more levels

 

> lm.fit=lm(TN_2~., Subs_TN, na.action=na.exclude)

 Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :

  contrasts can be applied only to factors with 2 or more levels

 

> lm.fit=lm(TN_2~., Subs_TN, na.action=na.omit)

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :

  contrasts can be applied only to factors with 2 or more levels

 

 

Thank you in advance for your advice!

~ Tricia Kyzar

 

This list strives to be beginner friendly. However, we still ask that you PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

 

This list strives to be beginner friendly. However, we still ask that you PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

This list strives to be beginner friendly. However, we still ask that you PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
This list strives to be beginner friendly. However, we still ask that you PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.