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

#-----------------------------------#
# 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

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

时间: 2024-08-26 12:28:07

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

吴裕雄--天生自然 R语言开发学习:使用ggplot2进行高级绘图

#----------------------------------------------------------# # R in Action (2nd ed): Chapter 19 # # Advanced graphics with ggplot2 # # requires packages ggplot2, RColorBrewer, gridExtra, # # and car (for datasets) # # install.packages(c("ggplot2"

吴裕雄--天生自然 R语言开发学习:图形初阶(续一)

# ----------------------------------------------------# # R in Action (2nd ed): Chapter 3 # # Getting started with graphs # # requires that the Hmisc and RColorBrewer packages # # have been installed # # install.packages(c("Hmisc", "RColorB

吴裕雄--天生自然 R语言开发学习:基本图形

#---------------------------------------------------------------# # R in Action (2nd ed): Chapter 6 # # Basic graphs # # requires packages vcd, plotrix, sm, vioplot to be installed # # install.packages(c("vcd", "plotrix", "sm"

吴裕雄--天生自然 R语言开发学习:基本图形(续二)

#---------------------------------------------------------------# # R in Action (2nd ed): Chapter 6 # # Basic graphs # # requires packages vcd, plotrix, sm, vioplot to be installed # # install.packages(c("vcd", "plotrix", "sm"

吴裕雄--天生自然 R语言开发学习:高级数据管理(续三)

#-----------------------------------# # R in Action (2nd ed): Chapter 5 # # Advanced data management # # requires that the reshape2 # # package has been installed # # install.packages("reshape2") # #-----------------------------------# # Class R

吴裕雄--天生自然 R语言开发学习:基本图形(续一)

#---------------------------------------------------------------# # R in Action (2nd ed): Chapter 6 # # Basic graphs # # requires packages vcd, plotrix, sm, vioplot to be installed # # install.packages(c("vcd", "plotrix", "sm"

吴裕雄--天生自然 R语言开发学习:高级数据管理

#-----------------------------------# # R in Action (2nd ed): Chapter 5 # # Advanced data management # # requires that the reshape2 # # package has been installed # # install.packages("reshape2") # #-----------------------------------# # Class R

吴裕雄--天生自然 R语言开发学习:功效分析

#----------------------------------------# # R in Action (2nd ed): Chapter 10 # # Power analysis # # requires packages pwr to be installed # # install.packages("pwr") # #----------------------------------------# par(ask=TRUE) library(pwr) # t te

吴裕雄--天生自然 R语言开发学习:功效分析(续一)

#----------------------------------------# # R in Action (2nd ed): Chapter 10 # # Power analysis # # requires packages pwr to be installed # # install.packages("pwr") # #----------------------------------------# par(ask=TRUE) library(pwr) # t te

吴裕雄--天生自然 R语言开发学习:时间序列(续一)

#-----------------------------------------# # R in Action (2nd ed): Chapter 15 # # Time series # # requires forecast, tseries packages # # install.packages("forecast", "tseries") # #-----------------------------------------# par(ask=TR