You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The data frame passed to mice() contains a variable called state that consists of either: (a) a character vector (of potentially different values) or (b) only a single (repeated) value (of any type), AND
There is a variable called state available in the environment (either the global environment or an attached data frame),
then mice() throws the error:
Error in s$it : $ operator is invalid for atomic vectors
Examples:
library(mice) # version 3.15.0mynhanes<-mice::nhanesstate<-"zen"mynhanes$state<- rnorm(25)
imp<- mice(mynhanes) # No errormynhanes$state<- sample(c("WA", "OR", "CA"), 25, replace=T)
imp<- mice(mynhanes) # Errormynhanes$state<-3.1415imp<- mice(mynhanes) # Error
rm(state)
imp<- mice(mynhanes) # No error (warning about logged events)
attach(mynhanes)
imp<- mice(mynhanes) # Error
because the call to ma_exists("state", ...) on either line 100 or 103 is apparently accessing the wrong variable in the environment through some kind of iterated search of parent environments here:
The intended state variable (wherever it comes from) should perhaps be encapsulated a little more explicitly in a mice-specific data structure, rather than doing an open search of the environment. But, as I'm not familiar with mice()'s innards, I'm not sure what the best fix would be. Maybe it's as simple as renaming state to something a little less generic, like mice_internal_state_ so there is less likely to be a conflict with user variable names.
The text was updated successfully, but these errors were encountered:
mice uses a list named "state" for logging. I never realised that its name could clash with a variable named "state", which is quite common. Renaming it to something less used could be a quick and practical fix. Need to think about side effects renaming may have.
Hi, I was trying to use on my dataset without variable name of "state" and still having this issue. May I know how should I solve that?
It could run before but suddenly there's an error today.
A somewhat obscure error—if:
state
that consists of either: (a) a character vector (of potentially different values) or (b) only a single (repeated) value (of any type), ANDstate
available in the environment (either the global environment or an attached data frame),then
mice()
throws the error:Examples:
The error is coming from here:
mice/R/internal.R
Line 107 in 3e3e3ca
because the call to
ma_exists("state", ...)
on either line 100 or 103 is apparently accessing the wrong variable in the environment through some kind of iterated search of parent environments here:mice/R/internal.R
Line 140 in 3e3e3ca
The intended
state
variable (wherever it comes from) should perhaps be encapsulated a little more explicitly in a mice-specific data structure, rather than doing an open search of the environment. But, as I'm not familiar withmice()
's innards, I'm not sure what the best fix would be. Maybe it's as simple as renamingstate
to something a little less generic, likemice_internal_state_
so there is less likely to be a conflict with user variable names.The text was updated successfully, but these errors were encountered: