Create new variable after imputation #356
Replies: 15 comments 1 reply
-
Hi Tobias, thanks for your mail. The As this behaviour is confusing in the context of |
Beta Was this translation helpful? Give feedback.
-
You should be able to run your code in |
Beta Was this translation helpful? Give feedback.
-
Hi Stef, To echo Tobias’s original post, thanks very much for creating MICE, I am still developing my R skills (so please excuse any stupid question) and am using your programme to try and carry out MI on a large repeated measures data set looking at outcomes for Chronic Pain patients at a UK hospital. All of the (108) variables have missing data at various points, and so it presents challenges that Tobias may not have faced… I am able to run the MI process successfully on the raw data using:
But then run in to difficulty… I need to create a number of subscales from the MICE output that can then be used for further pooled analysis. I understand that I am not able to do this directly from the pooled data set (the MICE output) and instead need to create an R object(s) that can be manipulated by the necessary functions (e.g. in Tobais’s case he converted to a Long format). However, as I have applied MICE to a large data set I have ‘viewed’ , rather than ‘#Generating data’ in the console as Tobias has done; my individual raw variables are not present as individual in R as objects which can be included in then computing a subscale score. In short: Is it possible to calculate the mean score for a group of variables from the MICE output, and then have this score created as a new variable? If so what is the best way to approach. If I manage this successfully, do I then need to convert back to a MIDS object (using the ‘imput.short <- as.mids’) before carrying out any further analysis? Many thanks in advance, Alistair |
Beta Was this translation helpful? Give feedback.
-
Hi Alistair, The issue you raise is straightforward to solve with passive imputation in
This example calculates
If you'd like to know more about the specifics and caveats of passive imputation, please have a look at the corresponding vignette in the miceVignettes repository All the best, Gerko |
Beta Was this translation helpful? Give feedback.
-
Hi Gerko, Thanks very much for your help, much appreciated! I have tried to implement your code and have had some success, however I have encountered 25 'warnings' : In Ops.factor(Rsf1, Rsf2) : ‘+’ not meaningful for factors do you know why this is? many thanks, Alistair |
Beta Was this translation helpful? Give feedback.
-
Yes, some of the variables you are trying to add together are factors, i.e. categorical variables where categories (labels) are represented by values. R is simply warning you that adding categorical variables may not be what you desire to prevent you from making an accidental error. All the best, Gerko |
Beta Was this translation helpful? Give feedback.
-
Hello Stef and Gerko, I added a ID variable to nhanes and create a sum score based on chl and hyp.
Then, I modified the predictor matrix because ID and New variable (sum score of chl+hyp) should not be predicted by other variables and should not be the predictor of other variables.
Then, I run mice.
I got strange result for the "New" variable (New=chl+hyp).
After I removed the this line of code: pred["New",] <- 0, the result seems to be reasonable.
Here are my questions:
|
Beta Was this translation helpful? Give feedback.
-
Hi Gerko, thanks for the useful explanation of passive imputation with mice. Could I add a question to this: would passive imputation also be applicable to change scores (i.e. outcome - baseline)? I can imagine a problem with this as we are assuming a correlation between the dependent and independent variable. However, perhaps I'm interpreting this issue incorrectly in the context of imputation. |
Beta Was this translation helpful? Give feedback.
-
Can’t find it on github…
Van: sophar <[email protected]>
Beantwoorden - Aan: stefvanbuuren/mice <[email protected]>
<[email protected]>
Onderwerp: Re: [stefvanbuuren/mice] Create new variable after imputation (#34)
When I'm trying to generate the long dataset to create new variables after imputation, I get the following error message:
```r
# Convert to Long
long <- mice::complete(df2, "long",include = TRUE)
Fehler: Column "pCare_doc" can't be converted from logical to numeric
```
I'm sorry, I did not manage to create a reproducible example for this, it just happens with my (large) dataset. But maybe you still have an idea what this could be? So pCare_doc is a logical variable, but why should it be converted?
|
Beta Was this translation helpful? Give feedback.
-
Unable to replicate. library(mice)
data <- data.frame(nhanes2,
flags = rep(c(TRUE, FALSE, FALSE, NA, TRUE), 5))
imp <- mice(data, m = 1, print = FALSE)
long <- mice::complete(imp, "long", include = TRUE)
str(long)
imp2 <- as.mids(long)
imp2
# force logical
long2 <- long
long2$flags <- as.logical(long2$flags)
str(long2)
imp3 <- as.mids(long2) |
Beta Was this translation helpful? Give feedback.
-
Hello Stef, thanks a lot for your help, really appreciated. I deleted my question when I realized that the error is related to the automatic conversion of logicals (which I did not know before). So I've made a workaround to convert all logicals to factors before
However, I'm not sure I understood your solution, as the error occurs when using |
Beta Was this translation helpful? Give feedback.
-
Hello, I am trying to do passive imputation as in @gerkovink 's example but in my case I need to use ifelse() function. I.e., instead of calculating the sum of two previous variables for the new varible, I need the new variable to be "0" if another previous variable is "0" and "1" otherwise. Is it possible to do this? If so, how? Thanks a lot! |
Beta Was this translation helpful? Give feedback.
-
Yes sure. The library(mice)
meth <- make.method(nhanes)
meth["bmi"] <- "~ ifelse(age == 1, 0, 1)"
imp <- mice(nhanes, method = meth, m = 1, maxit = 1, seed = 1)
head(complete(imp)) |
Beta Was this translation helpful? Give feedback.
-
Hello, again and thank you! I tried this but it keeps the new variable as NA.
This is my output:
Maybe I am tipying something wrong? I am sorry! |
Beta Was this translation helpful? Give feedback.
-
Hello. I have a similar query to @anamgreco where I need to use passive imputation to create two new binary variables as part of a multiple imputation call in I have successfully altered the imp$meth to create the
However,
Where sex is an unordered, two level factor: 1 "Female", 2 "Male". However, this imputed all missing values as zero - including where e.g. a male had whr >0.9. I've tried adapting the syntax with no luck - but my assumption is I'm probably making a very basic error which is causing this not to work.? Any help would be much appreciated! Thanks |
Beta Was this translation helpful? Give feedback.
-
This is a mail I got from Tobias Rolfes:
Datum: 20 mei 2017 15:48:53 GMT+5:30
Onderwerp: Mice: Create new variable after imputation
Hello Stef,
Thank you very much for creating such an useful package for multiple imputation.
Currently, I am facing the problem that I want to create a new variable after calculating imputations (e.g., sum scores of items) and calculate regressions with the new variabel. However, when I am doing so (cf., programm code below), the originally missing cases are deleted in the regression due to missings. Do you have an idea how I can solve the problem?
Many thanks in advance for your answer.
Best,
Tobias
Beta Was this translation helpful? Give feedback.
All reactions