'Error: `t.haven_labelled()` not supported while attempting MICE package in R

Here is my sample code:

library(haven) community_surveys <- read_sav("community_surveys.sav")

diss_data <- as.data.frame(community_surveys)

diss_data$FOC_1 <- as.factor(diss_data$FOC_1)
diss_data$DR_1 <- as.factor(diss_data$DR_1)
diss_data$IR_1 <- as.factor(diss_data$IR_1)
diss_data$HAITI <- as.factor(diss_data$HAITI)
diss_data$TREATMENT <- as.factor(diss_data$TREATMENT)

library(mice)

mice(diss_data, maxit = 10, m = 10)

I get this error below:

Error: `t.haven_labelled()` not supported

As far as the level of comprehension, I am a newbie R user with a couple intro classes and some reading under my belt. Any assistance much appreciated.



Solution 1:[1]

Labelled data from haven leads to all sorts of weird problems. You could try one of the following:

If your data should be numeric: sapply(diss_data, haven::zap_labels)

For factors: sapply(diss_data, haven::as_factor)

You could also just try to replace the command in your code like this:

diss_data$FOC_1 <- haven::as_factor(diss_data$FOC_1)
diss_data$DR_1 <- haven::as_factor(diss_data$DR_1)
diss_data$IR_1 <- haven::as_factor(diss_data$IR_1)
diss_data$HAITI <- haven::as_factor(diss_data$HAITI)
diss_data$TREATMENT <- haven::as_factor(diss_data$TREATMENT)

Solution 2:[2]

You could remove value labels by using remove_val_labels() from labelled library.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Ka
Solution 2 Issa Chi