1
$\begingroup$

I had created contrast variables for few categorical variables in the data set. I split the data into Training and Test data. The contrast levels are dropped when the model is run. I could not proceed with the predict because the model is failed. Below is the code. Month & day are categorical variables and I have contrasted them using Simple coding method.

# Split the data
Train <- dat[1:80,] #First 80 records - Training data
Test <- dat[81:111,] # Rest of the records - Test data

# Regression
fit <- lm(Ozone~Wind+Solar.R+Month+Day+Temp, data=Train)
summary(fit)

When I run the model with Train data set, I get the error 'contrasts dropped from factor Month due to missing levels'. Can you please clarify how do we train the model with the contrast variable present and predict for the test set?

$\endgroup$
0
$\begingroup$

Some general comments: Your total sample size seems to small to split the data in train and validation set. You would probably be better off using cross-validation. Some discussion is here.

But with your small sample size, and using fold cross validation, the problem would likely reappear! You simply do have too many factor levels, so some fold is bound to loose levels. Maybe some kind of balanced simulation for choosing folds could help.

But looking at your concrete model, you are modeling a physical measurement Ozone and say "Month & day are categorical variables". That is strange! Modeling time as categorical variables that way doesn't look right for a physical measurement! So in your case, the solution seems to be to represent time in a physically meaningful way.

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.