Created
June 20, 2018 23:54
-
-
Save chrissyhroberts/2b2d247cd0c21fad4bc51546ae63c48e to your computer and use it in GitHub Desktop.
Multiple imputation with MICE - very simple version for rough completion of a dataset (useful for things like doing a dirty PCA)
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
data(cars) | |
#make some missing values | |
cars[sample(1:50,size = 10),1]<-NA | |
cars[sample(1:50,size = 10),2]<-NA | |
require(mice) | |
#impute a bunch of data sets | |
cars.mice<-mice(cars,m=5,maxit=50,meth='pmm',seed=500) | |
#look at the completions data set | |
cars.mice$imp$speed | |
#plot density of values imputed to see how well they worked on average | |
plot(density(cars.mice$imp$speed[,1])) | |
lines(density(cars.mice$imp$speed[,2]),col="red") | |
lines(density(cars.mice$imp$speed[,3]),col="red") | |
lines(density(cars.mice$imp$speed[,3]),col="blue") | |
lines(density(cars.mice$imp$speed[,4]),col="green") | |
lines(density(cars.mice$imp$speed[,4]),col="pink") | |
#grab one you like and complete the data | |
cars.completed1<-complete(cars.mice,action = 1) | |
cars.completed2<-complete(cars.mice,action = 2) | |
cars.completed3<-complete(cars.mice,action = 3) | |
cars.completed4<-complete(cars.mice,action = 4) | |
cars.completed5<-complete(cars.mice,action = 5) | |
#do whatever you want to do with them. | |
plot(cars.completed1$speed,cars.completed2$speed,pch=16) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment