4

I have been trying to do stepwise selection on my variables with R. This is my code:

library(lattice)#to get the matrix plot, assuming this package is already installed
library(ftsa) #to get the out-of sample performance metrics, assuming this package is already installed
library(car) 

mydata=read.csv("C:/Users/jgozal1/Desktop/Multivariate Project/Raw data/FINAL_alldata_norowsunder90_subgroups.csv")

names(mydata)
str(mydata)

mydata$country_name=NULL
mydata$country_code=NULL
mydata$year=NULL
mydata$Unemployment.female....of.female.labor.force...modeled.ILO.estimate.=NULL
mydata$Unemployment.male....of.male.labor.force...modeled.ILO.estimate.=NULL
mydata$Life.expectancy.at.birth.male..years.= NULL
mydata$Life.expectancy.at.birth.female..years. = NULL

str(mydata)

Full_model=lm(mydata$Fertility.rate.total..births.per.woman. + mydata$Immunization.DPT....of.children.ages.12.23.months. + mydata$Immunization.measles....of.children.ages.12.23.months. + mydata$Life.expectancy.at.birth.total..years. + mydata$Mortality.rate.under.5..per.1000.live.births. + mydata$Improved.sanitation.facilities....of.population.with.access. ~ mydata$Primary.completion.rate.female....of.relevant.age.group. + mydata$School.enrollment.primary....gross. + mydata$School.enrollment.secondary....gross. + mydata$School.enrollment.tertiary....gross. + mydata$Internet.users..per.100.people. + mydata$Primary.completion.rate.male....of.relevant.age.group. + mydata$Mobile.cellular.subscriptions..per.100.people. + mydata$Foreign.direct.investment.net.inflows..BoP.current.US.. + mydata$Unemployment.total....of.total.labor.force...modeled.ILO.estimate., data= mydata)

summary(Full_model) #this provides the summary of the model

Reduced_model=lm(mydata$Fertility.rate.total..births.per.woman. + mydata$Immunization.DPT....of.children.ages.12.23.months. + mydata$Immunization.measles....of.children.ages.12.23.months. + mydata$Life.expectancy.at.birth.total..years. + mydata$Mortality.rate.under.5..per.1000.live.births. + mydata$Improved.sanitation.facilities....of.population.with.access. ~1,data= mydata)

step(Reduced_model,scope=list(lower=Reduced_model, upper=Full_model), direction="forward", data=mydata)

step(Full_model, direction="backward", data=mydata)

step(Reduced_model,scope=list(lower=Reduced_model, upper=Full_model), direction="both", data=mydata)

This is the link to the dataset that I am using: http://speedy.sh/YNXxj/FINAL-alldata-norowsunder90-subgroups.csv

After setting the scope for my stepwise I get this error:

Error in step(Reduced_model, scope = list(lower = Reduced_model, upper = Full_model), : number of rows in use has changed: remove missing values? In addition: Warning messages: 1: In add1.lm(fit, scope$add, scale = scale, trace = trace, k = k, : using the 548/734 rows from a combined fit 2: In add1.lm(fit, scope$add, scale = scale, trace = trace, k = k, : using the 548/734 rows from a combined fit

I have looked at other posts with the same error and the solutions usually is to omit the NAs from the data used, but that hasn't solved my problem and I am still getting exactly the same error.

jgozal
  • 1,359
  • 3
  • 14
  • 38
  • 1
    When you say "that hasn't solved my problem" do you mean you get the exact same error? It would be helpful to include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) here so we can confirm your results. – MrFlick Apr 18 '15 at 18:13
  • Yes, it gave me the same error. I will include the reproducible example soon. Thank you – jgozal Apr 18 '15 at 19:22
  • I just edited my post in order to be completely reproducible. – jgozal Apr 18 '15 at 19:35
  • 3
    I'm not sure what you did, but doing `mydata – MrFlick Apr 18 '15 at 19:56
  • Thank you MrFlick. It worked for me too. I put the na.omit when reading the data, and also when referring to the data in the full and reduced models. It's interesting because it would show how my number of observations decreased with the previous ways of omitting NAs but it still gave me an error – jgozal Apr 19 '15 at 00:08

0 Answers0