I originally posted this in stackoverflow (as given here) but was told to try here since it might be more relevant here. I am very new to statistics and R in general so my question might be a bit dumb, but since I cannot find my solutions online I thought I should try ask it here.
I have a data frame dataset
of a whole lot of different variables very similar to as follows:
Item | Size | Value | Town
----------------------------------
A | 10 | 800 | 1
B | 11 | 100 | 2
A | 17 | 900 | 2
D | 13 | 200 | 3
B | 15 | 500 | 1
C | 12 | 250 | 3
E | 14 | NA | 2
A | | 800 | 1
C | | 800 | 2
Basically, I have to try and 'guess' the Size based on the type of Item, it's Value, and the Town it was sold in, so I think a regression method would be a good idea.
I now use a regression as follows:
lm(Size ~ factor(Item) + factor(Town) + Value,...)
The problem however, occurs when I try and predict the Size using this model. I have the following lines of code:
pmodel <- lm(Size ~ factor(Item) + factor(Town) + Value,...)
prediction <- predict(pmodel, dataset2)
(where dataset2
is a subset of dataset
which has all the empty "Size" values which I want to predict)
But this now comes up with the error:
Error in model.frame.default(Terms, newdata, na.action=na.action, xlev = object$xlevels): factor factor(Town) has new levels
Is there any way around this, or is there any way I can get the model to make prediction based on the other values if it encounters a 'level' which is not in the original dataset?