-2

I'm working with imputation with some data in R. I found a code online to perform imputation and then modeling the imputed data and the original data. The code is this:

# Using airquality dataset
data <- airquality
data[4:10,3] <- rep(NA,7)
data[1:5,4] <- NA

# Removing categorical variables
    data <- airquality[-c(5,6)]
    summary(data)

# Impute missing data using mice
library(mice)

tempData <- mice(data,m=5,maxit=50,meth='pmm',seed=500)
summary(tempData)

# Get completed datasets (observed and imputed)
completedData <- complete(tempData,1)
summary(completedData)

# Plots

# Density plot original vs imputed dataset
densityplot(tempData)

This is my syntax:

library(readr)
input_preg<- read_csv("datasurvey.csv")
summary(input_preg)
imput<- input_preg

#Imputation
library(mice)
temporal <- mice(imput,m=5,maxit=50,meth='pmm',seed=500)

#example imputed
temporal$imp$`52bcalif`

#I selected a dataset for imputation
completos<-complete(temporal,1)

#Ploting
densityplot(temporal)

So i'm doing almost exactly what the code indicates and when I'm doing the densityplot it doesnt work stating:

Error in `[.data.frame`(r, , xvar) : undefined columns selected

But with the original code, it has no problems to do the densityplot. So I dont know if it is because of the large number of imputations or that original data had 4 variables and I have 29.

Colonel G
  • 29
  • 6
  • 2
    Please read https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example ... then **edit your question!** – jogo Feb 16 '18 at 08:50
  • 2
    I know this is a longshot, but if the error message tells you `undefined columns selected` it is usually because you selected columns that are not defined. Sorry for the sarcasm :-) – Joyvalley Feb 16 '18 at 09:03
  • Is that `survival::complete()`, or something else? (Please include all libraries used.) Does your error happen after the `densityplot(..)`? It's not clear. – r2evans Feb 16 '18 at 09:38
  • yes, after densityplot. – Colonel G Feb 17 '18 at 00:24

1 Answers1

1

Change the name of that column,temporal$imp$52bcalif, I think the mistake is there. You used a number. I tested myself.

Tim Carter
  • 11
  • 1