Code: Select all
install.packages("DMwR") # for use of knnImputation.
require(DMwR)
x1<-3*rnorm(100)
x2<-round(abs(x1),2)
y2<-x2
y2[sample(1:100,25)]<-NA
M<-cbind(x1,y2)
sum(complete.cases(M))
Code: Select all
#Central inputation
M1 <- centralImputation(M)
plot(x1,x2,type="p",pch=19)
plot(M1[,1],M1[,2],type="p",pch=19)
cbind(x1,y2,M1[,2])
Code: Select all
#knn imputation
M<-cbind(x1,x1,y2)
sum(complete.cases(M))
M2 <- knnImputation(M, 3)
plot(x1,x2,type="p",pch=19)
plot(M2[,1],M2[,3],type="p",pch=19)
Ind<-is.na(y2)
points(M2[Ind,1],M2[Ind,3],pch=19,col="red")
cbind(x1,y2,M2[,3])