Rita Carreira
2011-Apr-01 22:49 UTC
[R] package MICE, squeeze function, calling several variables at once
Hello everyone!I have a data set with missing observations that I am trying to impute. I am using MICE and I would like the imputed values to all be positive. I have two types of variables: prices (P1 to P136) and quantities (Q1 to Q136) and I also want the range of these two types to be different. Besides these variables. I am using the squeeze function but I am unable to set it such that I don't have to write the names of all the variables. Also note that I do not have all the 136 Ps and Qs because since some were completely missing, I had to drop those variables. But my first Q is called Q1 and the last one Q136 and similarly for P. I apologize if I am making a silly little error since I am still learning R. This is the code that I have and the error that I am getting: #####Program starts here#####Read data into Rdf <- read.table("F:\\testC2S4\\new_C2-S4_head_data.txt", header = TRUE); #, stringsAsFactors = TRUEdfPZ <- df[ ,1:3]dfDV <- df[ ,277:311]dfPtemp <- df[ ,seq(4, 275, by = 2)]dfP <- dfPtemp[, sapply(dfPtemp, function(x) !all(is.na(x)))]dfQtemp <- df[ ,seq(5, 275, by = 2)]dfQ <- dfQtemp[, sapply(dfQtemp, function(x) !all(is.na(x)))]remove(dfPtemp, dfQtemp) NewPrimal <- data.frame(c(dfPZ, dfQ, dfP, dfDV), row.names = NULL, check.rows = F, check.names = F) remove(dfPZ, dfQ, dfP, dfDV) library(mice)# setting up the default settings for the imputationini <- mice(NewPrimal, seed = 52275, imputationMethod = "norm", maxit=0, pred = quickpred(NewPrimal, mincor = 0.25, minpuc = 0.95, include = c("blnNewYearsDay", "blnValentinesDay", "blnStPatricksDay", "blnEaster", "blnMothersDay", "blnMemorialDay", "blnFathersDay", "blnIndependenceDay", "blnLaborDay", "blnThanksgiving", "blnChristmas", "Qtr1", "Qtr2", "Qtr3", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), exclude = c("ActualWeekEndingDate", "CompanyID", "PriceZoneID", "Date", "Year")), print=FALSE) meth <- ini$meth # naming the default method in inipred <- ini$pred # naming the matrix of default predictors in inipost <- ini$post # naming the default post processing method in inipost["Q1":"Q136"] <- "imp[[j]][,i] <- squeeze(imp[[j]][,i],c(0,351000))" #setting the range for quantitypost["P1":"P136"] <- "imp[[j]][,i] <- squeeze(imp[[j]][,i],c(0,50))" #setting the range for price # calling mice to impute given default settings:impNP <- mice(NewPrimal, meth = meth, maxit=5, pred = pred, post = post) ######My error warning####Error in "Q1":"Q136" : NA/NaN argument In addition: Warning messages: 1: In post["Q1":"Q136"] <- "imp[[j]][,i] <- squeeze(imp[[j]][,i],c(0,351000))" : NAs introduced by coercion 2: In post["Q1":"Q136"] <- "imp[[j]][,i] <- squeeze(imp[[j]][,i],c(0,351000))" : NAs introduced by coercion Any ideas? Thank you and have a great weekend! Rita ________________________________________ "If you think education is expensive, try ignorance"--Derek Bok [[alternative HTML version deleted]]
Maybe Matching Threads
- Function for deleting variables with >=50% missing obs from a data frame
- Subsetting a data frame by dropping correlated variables
- Package mice: Error in if (meth[j] != "") { : argument is of length zero
- How do I delete multiple blank variables from a data frame?
- nlme: augPred.lme for factor covariates