jwb133 / smcfcs

R package implementing Substantive Model Compatible Fully Conditional Specification Multiple Imputation
11 stars 7 forks source link

Factors don't work with data imported via haven #12

Open ljwright opened 5 years ago

ljwright commented 5 years ago

Hi,

I've been trying to use smcfcs in R on a Stata dataset I've imported with haven. I think the class of the dataset is producing errors when trying to impute factor variables. (haven adds the following to the class attribute: "tbl_df" "tbl" "data.frame".)

In the example below, the first callof smcfcs succeeds while the second one fails with the warning "Variables to be imputed using method modds must be stored as factors.". Changing the method to podds also fails.

Thanks,

Liam

set.seed(123)
df <- iris
df$Sepal.Length[runif(nrow(df))<0.05] <- NA
df$Petal.Width[runif(nrow(df))<0.05] <- NA
df$Species[runif(nrow(df))<0.05] <- NA
class(df)
imp <- smcfcs(df,
             smtype = "lm",
             smformula = "Sepal.Length ~ Petal.Width + Species",
             method = c("","","","norm","mlogit"),
             m=1)
class(df) <- c("tbl_df","tbl","data.frame")   
imp <- smcfcs(df,
              smtype = "lm",
              smformula = "Sepal.Length ~ Petal.Width + Species",
              method = c("","","","norm","mlogit"),
              m=1)
jwb133 commented 5 years ago

Thanks Liam. When I run your code though, I don't get an error with either run. I get lots of warning messages about checkwz and tfun, but I think these are caused by issues with the mlogit fit for the Species variable in this dataset.

Are you using the latest version of R and smcfcs?

ljwright commented 5 years ago

I'm running R v3.5.1 and smcfcs v1.4.0