0

I am new to R coding. I am trying to build a linear model as:

lmmodel <- lm(DV ~ IV1 + IV2 + IV3 + IV4)

I have a dependent variable with money as data type (decimal value) and 4 independent variables, a mixture of categorical and date:

Issuancedate Region Account ClientName Price 
01-01-2015 South Account1 ABC
02-01-2015 North Account2 NA
03-01-2015 NorthEast Account3 BCD
04-01-2015 SouthEast Account4 NA
05-01-2015 NA Account5 M/sBedf
06-01-2015 West Account6 Campus ltd
07-01-2015 SouthWest Account7 Offshoreltd
08-01-2015 NorthWest Account8 Sitenew 

Price is to be predicted. I see the below error:

Error in contrasts<-(tmp, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
RCoder
  • 1
  • 1
  • 1
    to make it easier for SO to help you, you include a reproducible example. Include your dataset and the code that produce this error. – MLavoie Jan 16 '16 at 16:06
  • In support of Lavoie’s point. Please take the time to read [this SO post](http://stackoverflow.com/help/mcve) on how to make a great reproducible example in R. – Eric Fail Jan 16 '16 at 16:16
  • thanks for the reply,here is the simple code I execute on the sample 8 rows of data.'train<-read.csv("Data.csv") head(train) attach(train) lmmodel<-lm(Price~Account+Region+Issuancedate+ClientName)' Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels – RCoder Jan 16 '16 at 16:36
  • 2
    please update your post instead of using comments – mtoto Jan 16 '16 at 16:56
  • have you looked at http://stackoverflow.com/questions/18171246/error-in-contrasts-when-defining-a-linear-model-in-r ? – Ben Bolker Jan 16 '16 at 20:13

1 Answers1

2

You haven't given us Price in your example data, but: the problem is that once all the rows containing NA are removed from your data set, one of the categorical dependent variables no longer has more than a single level. You can't fit a model using a categorical variable with a single level ...

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453