[R] Problem running stepAIC within a function.

uzuria winging_it at hotmail.com
Sat Mar 3 15:23:41 CET 2012


I need to a function that automatically fits a regression to data, using the
stepAIC. I've ran the code manually and it works fine. However, when I run
the function on the same data, the following error occurs:

Problem in regimp(fullsim = simt, fullsim1 = simt1,..: Length of (weights)
(variable 4) is 4271 != length of others (4278) 

I got the function to output the length of the dataset and weights, which
were both 4271.

When I remove "weights=w" from the code. StepAIC start's running, but then I
get the following error:

Problem in stepAIC(lm(new ~ -1 + SIC3 + emp, data =..: number of rows in use
has changed: remove missing values? 

I checked the data for missing values and there are none. 

I can't understand why the code works fine on it's own, but not in a


#Mean imp and results
regimp <-
	results <-
	#run simulations of missing data and impute on each
	#do reps
	for (j in 1:reps){
		#bind data
		alldata <- cbind(fullsim,fullsim1,fullsim2)
		cols <- paste(names(alldata),c(rep("",16),rep(1,16),rep(2,16)),sep="")
		names(alldata) <- cols
		alldata <- alldata[,-c(3,17:21,33:37)]
		#create missing values
		missings <- createmiss(alldata)
		simmiss <- missings$missdata
		#sample of complete data
		s1 <- missings$samp
		s1$SIC <- factor(s1$SIC)
		s1$SIC3 <- factor(s1$SIC3)
		s1$emp <- factor(s1$emp)
		newfull <- rep(0,5120)
		#set weighting
		if (weight==1){w <- s1$weight/sum(s1$weight)}
		else {w <- rep(1/nrow(s1),nrow(s1))}
		#do regression
		if (opt==1){
			fit <-
			sigdata <-
<= 0.05)]])
			finalfit <- lm(new~.-1,data=sigdata,weights=w)
		else {
			fit <-
			sigdata <-
<= 0.05)]])
			finalfit <- lm(new~.-1,data=sigdata,weights=w)

View this message in context: http://r.789695.n4.nabble.com/Problem-running-stepAIC-within-a-function-tp4441487p4441487.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list