the next is a copy of the scip i am working on,as you can see, i try to compare models, i fit them with the same s variables, and i get this error mensagge, why?
mod3<-glm.nb(IBDQ32~sexo+edad+audit)
mod4<-glm.nb(IBDQ32~sexo+edad+audit+caa)
mod5<-glm.nb(IBDQ32~sexo+edad+audit+caa+imc)
mod6<-glm.nb(IBDQ32~sexo+edad+imc)
library(MuMIn)
# Warning message:
# package ‘MuMIn’ was built under R version 3.4.3
ModelSel.NB<-model.sel(mod3,mod4,mod5,mod6, rank="AICc")
# Warning message:
# In model.sel.default(mod3, mod4, mod5, mod6, rank = "AICc") :
# models are not all fitted to the same data
First, I think you are getting a warning, not an error. Nevertheless, you are probably not getting the result you want.
But more importantly, in your code snippet you don't specify data argument when you fit the model. Are you sure you are not failing at this stage? Where are you taking glm.nb from? In all model fitting function I've came across data is a compulsory argument, so I would imagine your error happens there, not at model selection stage.
Or/additionally, you may have missing values in your data. For instance, if caa has 3 missing values, the number observations (the data) will be different between mod3 and mod4.
Great! I'm glad it works. This does demonstrate the need for a reprex; otherwise people might have spent a lot of time trying to fix something that wasn't really an issue.
The fact that you are not specifying the data argument to glm.nb() indicates that the variables (IBDQ32, sexo, etc.) are in your global environment. While this can "work", this approach is error-prone because it is easy to modify some variables and not others (for instance, if you decide to filter your data before modeling).
If you read in data from an external source (for example a csv file or a database), the variables will already be found within a data.frame (and not in the global environment).
The warning message generated from MuMIn::model.sel() indicates the models you are comparing include different observations. The most common reason for this is missing values. Before modeling, it's worthwhile to understand the extent of the missingness in the data. You can do this on a per-variable approach by:
library(MASS)
library(dplyr)
# update 'Eth' field of 'quine' data to have two missing values
quine$Eth[c(3, 5)] <- NA
summarise_all(quine, funs(sum(is.na(.))))
#> Eth Sex Age Lrn Days
#> 1 2 0 0 0 0
If you use the modified version of quine above for modeling, then the number of observations will differ depending on whether you include Eth as a predictor.
Differing observations is problematic for statistical comparisons (e.g. F-tests, AIC) of multiple models.
There are sophisticated approaches to handling missing values, such as imputation. However, if you have a small number of missing values, you can just omit rows that have any missing predictor before fitting any of the models. (If you want a detailed discussion on modeling with missing values, Frank Harrell's "Regression Modeling Strategies" is an excellent source).