码迷,mamicode.com
首页 > 编程语言 > 详细

吴裕雄--天生自然 R语言开发学习:处理缺失数据的高级方法(续一)

时间:2019-07-12 18:58:46      阅读:33      评论:0      收藏:0      [点我收藏+]

标签:data   code   which   cti   sum   case   package   obs   png   

技术图片

技术图片

技术图片

技术图片

技术图片

#-----------------------------------#
# R in Action (2nd ed): Chapter 18  #
# Advanced methods for missing data #
# requires packages VIM, mice       #
# install.packages(c("VIM", mice))  #
#-----------------------------------#

par(ask=TRUE)


# load the dataset
data(sleep, package="VIM")


# list the rows that do not have missing values
sleep[complete.cases(sleep),]


# list the rows that have one or more missing values
sleep[!complete.cases(sleep),]


# tabulate missing values patters
library(mice)
md.pattern(sleep)


# plot missing values patterns
library("VIM")
aggr(sleep, prop=FALSE, numbers=TRUE)
matrixplot(sleep)
marginplot(sleep[c("Gest","Dream")], pch=c(20), 
           col=c("darkgray", "red", "blue"))


# use correlations to explore missing values
x <- as.data.frame(abs(is.na(sleep)))
head(sleep, n=5)
head(x, n=5)
y <- x[which(apply(x,2,sum)>0)]
cor(y)
cor(sleep, y, use="pairwise.complete.obs")


# complete case analysis (listwise deletion)
options(digits=1)
cor(na.omit(sleep))
fit <- lm(Dream ~ Span + Gest, data=na.omit(sleep))
summary(fit)


# multiple imputation
options(digits=3)
library(mice)
data(sleep, package="VIM")
imp <- mice(sleep, seed=1234)
fit <- with(imp, lm(Dream ~ Span + Gest))
pooled <- pool(fit)
summary(pooled)
imp

 

吴裕雄--天生自然 R语言开发学习:处理缺失数据的高级方法(续一)

标签:data   code   which   cti   sum   case   package   obs   png   

原文地址:https://www.cnblogs.com/tszr/p/11177656.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!