Created
November 16, 2018 14:09
-
-
Save korkridake/3def48baf9174d0ff690b1c9e4b9ac4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Stackoverflow: https://stackoverflow.com/questions/53338606/combine-imputed-and-non-imputed-data | |
# Combine imputed and non imputed data | |
id <- c(1,2,3,4,5,6,7,8,9,10) | |
age <- c(60,NA,90,55,60,61,77,67,88,90) | |
bmi <- c(30,NA,NA,23,24,NA,27,23,26,21) | |
time <- c(62,88,85,NA,68,62,89,62,70,99) | |
dat <- data.frame(id, age, bmi, time) | |
dat | |
id <- c(1,2,3,4,5,6,7,8,9,10) | |
m1 <- c(60,78,90,55,60,61,77,67,88,90) | |
m2 <- c(30,44,35,23,24,22,27,23,26,21) | |
m3 <- c(62,88,85,78,68,62,89,62,70,99) | |
dat2 <- data.frame(id, m1, m2, m3) | |
dat2 | |
# install.packages("mice") | |
library(mice) | |
impdat <- mice(dat, | |
seed = 2018, | |
maxit = 10, | |
m = 5) | |
impdat | |
# Class: mids | |
# Number of multiple imputations: 5 | |
# Imputation methods: | |
# id age bmi time | |
# "" "pmm" "pmm" "pmm" | |
# PredictorMatrix: | |
# id age bmi time | |
# id 0 1 1 1 | |
# age 1 0 1 1 | |
# bmi 1 1 0 1 | |
# time 1 1 1 0 | |
impdat = complete(impdat) | |
impdat | |
# id age bmi time | |
# 1 1 60 30 62 | |
# 2 2 60 24 88 | |
# 3 3 90 24 85 | |
# 4 4 55 23 89 | |
# 5 5 60 24 68 | |
# 6 6 61 24 62 | |
# 7 7 77 27 89 | |
# 8 8 67 23 62 | |
# 9 9 88 26 70 | |
# 10 10 90 21 99 | |
final_data = cbind(impdat, dat2) | |
final_data | |
# id age bmi time id m1 m2 m3 | |
# 1 1 60 30 62 1 60 30 62 | |
# 2 2 60 24 88 2 78 44 88 | |
# 3 3 90 24 85 3 90 35 85 | |
# 4 4 55 23 89 4 55 23 78 | |
# 5 5 60 24 68 5 60 24 68 | |
# 6 6 61 24 62 6 61 22 62 | |
# 7 7 77 27 89 7 77 27 89 | |
# 8 8 67 23 62 8 67 23 62 | |
# 9 9 88 26 70 9 88 26 70 | |
# 10 10 90 21 99 10 90 21 99 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment