Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R error which says "Models were not all fitted to the same size of dataset"

Tags:

r

glm

lm

anova

I have created two generalised linear models as follows:

glm1 <-glm(Y ~ X1 + X2 + X3, family=binomial(link=logit))

glm2 <-glm(Y ~ X1 + X2, family=binomial(link=logit))

I then use the anova function:

anova(glm2,glm1)

but get an error message:

"Error in anova.glmlist(c(list(object),dotargs), dispersion = dispersion, :
models were not all fitted to the same size of dataset"

What does this mean and how can I fix this? I have attached the dataset at the start of my code so both models are working off of the same dataset.

like image 716
REnthusiast Avatar asked Aug 22 '13 17:08

REnthusiast


1 Answers

The main cause of that error is when there are missing values in one or more of the predictor variables. In recent versions of R the default action is to omit all rows that have any values missing (the previous default was to produce an error). So for example if the data frame has 100 rows and there is one missing value in X3 then your model glm1 will be fit to 99 rows of data (dropping the row where X3 is missing), but the glm2 object will be fit to the full 100 rows of data (since it does not use X3, no rows need to be deleted).

So then the anova function gives you an error because the 2 models were fit to different datasets (and how do you compute degrees of freedom, etc.).

One solution is to create a new data frame that has only the columns that will be used in at least one of your models and remove all the rows with any missing values (the na.omit or na.exclude function will make this easy), then fit both models to the same data frame that does not have any missing values.

Other options would be to look at tools for multiple imputation or other ways of dealing with missing data.

like image 111
Greg Snow Avatar answered Oct 08 '22 18:10

Greg Snow