I get error for object "weight" not found whereas it exists in the data frame

78 Views Asked by At

I am using SIMPUTE package in R to impute a continuous variable. This is my dataset

'data.frame':   6000 obs. of  11 variables:
 $ age           : chr  "1" "1" "1" "1" ...
 $ GI            : num  5.29 12.52 2.03 NA 31.51 ...
 $ sex           : chr  "2" "1" "1" "1" ...
 $ Weight        : num  1.2 1.2 1.2 1.2 1.2 ...

I am using the following command from simpute package to impute "GI" variable using age and sex to create imputation cell. I have a weight variable to use for "prob"

imp_simpuatation<-impute_rhd(
  data,
  GI~age+sex,
  pool = "univariate",
  prob = Weight
)

but i get the following error: Error: object 'Weight' not found and when I tried the prob= data$Weight instead, i got the following error:

Error in impute_rhd(data, GI ~ age + sex, pool = "univariate", prob = data$Weight,  : 
  length(prob) != nrow(dat) is not TRUE

whereas

> length(data$Weight)
[1] 6000
> nrow(data)
[1] 6000
1

There are 1 best solutions below

2
alexwhitworth On

I suggest using a different imputation package. The simputation package is buggy.

The correct usage is data$Weight not Weight. But the error is a bug in simputation. The author has corrected the error you identified, but not submitted the update to CRAN. However, the code is still buggy. Use a different package.

  • CRAN version = 0.2.8
  • GitHub/Dev version = 0.2.8.2

Install the dev version and you'll get past your error, but note that the code is still buggy:

install.packages("devtools")
devtools::install_github("markvanderloo/simputation/pkg")
library(simputation)

# example data per package intro
data(iris)
dat <- iris
dat[1:3,1] <- dat[3:7,2] <- dat[8:10,5] <- NA
dat$wt <- rnorm(n=nrow(dat))

impute_rhd(dat, Species ~ Septal.Length + Sepal.Width + Petal.Length, prob=dat$wt)

Error in [.data.frame(dat, predictors) : undefined columns selected