0

When I was working on my R project the other day and tried to optimize my linear model based on the AIC (using backward selection), I was stopped by an error message like this:

Error in stepAIC(lm(x ~ y + z), direction = "back") : 
number of rows in use has changed: remove missing values?

Do you have any idea what may be the problem here?

camille
  • 16,432
  • 18
  • 38
  • 60
Hala
  • 41
  • 1
  • 8
  • 9
    see the `Note` in `?stepAIC`: "We suggest you remove the missing values first". See `?na.omit`. – Ben Bolker Dec 11 '12 at 13:31
  • How can I do the na.omit. Within this r command. > fit5<-lm(healthscore~sex+age.centalized+education+migration+unemployed+logIncome+BMI+smoking+sport) > summary(fit5) – Hala Dec 13 '12 at 19:20
  • 1
    you should put your data into a data frame, run `na.omit()` on the data frame, and pass it to `lm` via the `data` argument. – Ben Bolker Dec 13 '12 at 19:41
  • Thank you very much for helping me ,, it worked and finally I fined my best model .. – Hala Dec 14 '12 at 15:00
  • 1
    if you found the answer correct and useful, you can upvote it and/or click on the check mark next to it to accept it ... – Ben Bolker Dec 14 '12 at 16:02

1 Answers1

4
dat <- data.frame(healthscore,sex,age.centralized,education,
    migration,unemployed,
    logIncome,BMI,smoking,sport)
fit5 <- lm(healthscore~.,data=na.omit(dat))
summary(fit5)
stepAIC(fit5)
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453