Hi

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
function. 

--------------------------------


#Mean imp and results
regimp <-
function(fullsim,fullsim1,fullsim2,reps=75,opt=0,rand=0,weight=0,match=0){
        results <-
data.frame(RIB=rep(0,reps),ARIB=rep(0,reps),Conf=rep(0,reps),Dks=rep(0,reps),PL=rep(0,reps),REL=rep(0,reps),VARS=rep(0,reps))
        #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 <-
lm(stepAIC(lm(new~-1+SIC,s1,weights=w),scope=list(upper=~1+SIC+turn+exturn+new+exnew+hand+excise+merch+exmerch+tob+rub+water+turn1+exturn1+new1+exnew1+hand1+excise1+
                        
merch1+exmerch1+tob1+rub1+water1+turn2+exturn2+new2+exnew2+hand2+excise2+merch2+exmerch2+tob2+rub2+water2,lower=~-1+SIC),
                                direction="both"))
                        sigdata <-
cbind(new=s1$new,SIC=s1$SIC,s1[,coeffs[which(summary(fit)$coeff[-c(1:128),4]
<= 0.05)]])
                        finalfit <- lm(new~.-1,data=sigdata,weights=w)
                }
                else {
                        fit <-
lm(stepAIC(lm(new~-1+SIC3+emp,data=s1),scope=list(upper=~1+SIC3+emp+turn+exturn+new+exnew+hand+excise+merch+exmerch+tob+rub+water+turn1+exturn1+new1+exnew1+
                        
hand1+excise1+merch1+exmerch1+tob1+rub1+water1+turn2+exturn2+new2+exnew2+hand2+excise2+merch2+exmerch2+tob2+rub2+water2,lower=~-1+SIC3+emp),
                                direction="both"))
                        sigdata <-
cbind(new=s1$new,SIC3=s1$SIC3,emp=s1$emp,s1[,coeffs[which(summary(fit)$coeff[-c(1:70),4]
<= 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.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to