Why does the number of rows change during AIC in R? How to ensure that this doesn't happen?

前端 未结 3 1886
春和景丽
春和景丽 2021-01-12 09:27

I\'m trying to find a minimal adequate model using AIC in R. I keep getting the following error: Error in step(model) : number of rows in use has changed: remove mi

3条回答
  •  佛祖请我去吃肉
    2021-01-12 09:52

    From the Warnings section of ?step:

    The model fitting must apply the models to the same dataset. This may be a problem if there are missing values and R's default of na.action = na.omit is used. We suggest you remove the missing values first.

    So you should do:

    no.na.data <- na.omit(data[c(predictors, response)])
    model <- lm(formula=as.formula(paste(paste(response,'~', sep=''),
                                         paste(predictors,collapse='+'), sep='')),
                no.na.data)
    step(model)
    

提交回复
热议问题