• 主页
  • 课程

    关于课程

    • 课程归档
    • 成为一名讲师
    • 讲师信息
    教学以及管理操作教程

    教学以及管理操作教程

    ¥1,000.00 ¥100.00
    阅读更多
  • 特色
    • 展示
    • 关于我们
    • 问答
  • 事件
  • 个性化
  • 博客
  • 联系
  • 站点资源
    有任何问题吗?
    (00) 123 456 789
    weinfoadmin@weinformatics.cn
    注册登录
    恒诺新知
    • 主页
    • 课程

      关于课程

      • 课程归档
      • 成为一名讲师
      • 讲师信息
      教学以及管理操作教程

      教学以及管理操作教程

      ¥1,000.00 ¥100.00
      阅读更多
    • 特色
      • 展示
      • 关于我们
      • 问答
    • 事件
    • 个性化
    • 博客
    • 联系
    • 站点资源

      R语言

      • 首页
      • 博客
      • R语言
      • 【数据分析】 处理缺失值的常规方法总结

      【数据分析】 处理缺失值的常规方法总结

      • 发布者 weinfoadmin
      • 分类 R语言
      • 日期 2016年5月10日
      测试开头

      前言

      • 现实生活中的数据是纷繁杂乱的,收集来的数据有缺失和录入错误司空见惯,所以学习如果处理这些常见问题是每一个数据人必须掌握的技能,俗话说巧妇 难为无米之炊,不能很好的处理原始数据会给后来的建模带来麻烦,甚至引入不必要的偏差和错误,数据科学家都熟悉“垃圾进垃圾出”的说法,今天让我们来学习 成为合格数据人的必修课 —- 处理缺失值的常规办法。

      数据介绍

      • 我们继续使用上一节的贷款数据集(数据内容请见上篇介绍)

      1. library(foreign)

      2. library(gmodels)

      3. library(mice)

      4. library(VIM)

      5. library(Hmisc)

      6. library(DMwR)

      7. loan.data = read.spss("Loan_ROC.sav", to.data.frame=TRUE)

      8. levels(loan.data$education)

      9. loan.data$education=factor(loan.data$education)

      10. levels(loan.data$education)

      引入缺失值

      1. set.seed(1)

      2. data.copy = loan.data

      3. data.copy$debt_income[sample(1:nrow(data.copy), 10)] <- NA

      4. data.copy$education[sample(1:nrow(data.copy), 10)] <- NA

      5. levels(data.copy$education)

      6. data.copy$education=factor(data.copy$education)

      7. levels(data.copy$education)

      MICE 包 —— 检查缺失值分布

      • 注意连续型数据和分类型数据的区别

      • 两类缺失值

        • MCAR: missing completely at random (desirable scenario; safe maximum threshold — 5% of total large dataset)

        • MNAR: missing not at random (wise and worthwhile to check data gathering process)

      • 检查行与列(自建公式)

      1. summary(data.copy)

      2. pMiss <- function(x){sum(is.na(x))/length(x)*100}

      3. apply(data.copy, 2, pMiss)

      4. apply(data.copy, 1, pMiss)

      5. md.pattern(data.copy)

      • r names(md.pattern(data.copy)[,1][1]) 个完整值

      • r names(md.pattern(data.copy)[,1][2]) 个观察值只缺失r names(md.pattern(data.copy)[1,][12])

      • r names(md.pattern(data.copy)[,1][3]) 个观察值只缺失r names(md.pattern(data.copy)[1,][13])

      • r names(md.pattern(data.copy)[,1][4]) 个观察值缺失前两个变量

      VIM 包 + box plot 箱线图 —- 缺失值视觉化

      1. aggr_plot <- aggr(data.copy, col=c('blue','red'), numbers=TRUE, sortVars=TRUE, labels=names(data.copy), cex.axis=.7, gap=3, ylab=c("Missing data histogram","Missing value pattern"))

      2. marginplot(data.copy[,c(5,2)], main='Missing value box plotn Constrain: 2 variables')

      • debt_income 的红 - 缺失 education 的分布

      • debt_income 的蓝 - 其余不缺失 education 的分布

      • 希望看到两个变量分别的红和蓝条分布相似(注意数据类型的不同)

      1. 处理方法 —- 删除行/列 (na.action=na.omit)

      适用条件:


        1. 足够大数据集 (model doesn’t lose power)


        1. 不会引入偏差 (no disproportionate or non-representation of classes)


        1. 删列:不起重要预测作用的变量

      2. 处理方法 —- 替换 (mean / median / mode)

      适用条件:


        1. 不需要非常精确的估算


        1. 变量 variation is low,或者 low leverage over response variable

      1. impute(data.copy$debt_income, mean)  # replace with mean, indicated by star suffix

      2. impute(data.copy$debt_income, 20)  # replace with specific number

      3. # data.copy$debt_income[is.na(data.copy$debt_income)] <- mean(data.copy$debt_income, na.rm = T)  # impute manually

      计算正确率 —- 替换 mean

      1. actuals <- loan.data$debt_income[is.na(data.copy$debt_income)]

      2. predicteds <- rep(mean(data.copy$debt_income, na.rm=T), length(actuals))

      3. regr.eval(actuals, predicteds)

      3. 处理方法 —- 推测 (高大上的方法:kNN, rpart, and mice)

      3.1. kNN

      1. # 推测方法:identify ‘k’ closest observations based on euclidean distance and computes the weighted average (weighted based on distance) of these ‘k’ obs.

      2. # 优点:can impute all missing values in all variables with one call to function. It takes whole dataframe as argument and don’t have to specify which variable to impute.

      3. # 注意:not to include response variable.

      1. knnOutput <- knnImputation(data.copy)

      2. anyNA(knnOutput)

      3. actuals <- loan.data$debt_income[is.na(data.copy$debt_income)]

      4. predicteds <- knnOutput[is.na(data.copy$debt_income), "debt_income"]

      5. regr.eval(actuals, predicteds)

      • The mean absolute percentage error (mape) has improved by ~ 65% compared to the imputation by mean.

      3.2. rpart

      1. # 优点:比较于 kNN,rpart 和 mice 可以用于分类型数据;而且 rpart 仅需要一个 predictor variable to be non-NA

      2. # 注意:not to include response variable.

      3. # `method=class` for factor variable

      4. # `method=anova` for numeric variable

      1. library(rpart)

      2. class_mod <- rpart(education ~ ., data=data.copy[!is.na(data.copy$education), ], method="class", na.action=na.omit)

      3. anova_mod <- rpart(debt_income ~ ., data=data.copy[!is.na(data.copy$debt_income), ], method="anova", na.action=na.omit)

      4. education_pred <- predict(class_mod, data.copy[is.na(data.copy$education), ],type='class')

      5. debt_income_pred <- predict(anova_mod, data.copy[is.na(data.copy$debt_income), ])

      6. actuals <- loan.data$debt_income[is.na(data.copy$debt_income)] # debt_income accuracy

      7. predicteds <- debt_income_pred

      8. regr.eval(actuals, predicteds)


      9. actuals <- loan.data$education[is.na(data.copy$education)]

      10. mean(actuals != education_pred)  # misclass error for education accuracy

      • debt_income’s accuracy is slightly worse than kNN but still much better than imputation by mean

      3.3. mice

      1. # 方法:2-step:mice() to build multiple models; complete() to generate one/several completed data (default is first).

      1. miceMod <- mice(data.copy[, !names(data.copy) %in% "Loan"], method="rf")  # based on random forests

      2. miceOutput <- complete(miceMod)  # generate completed data.

      3. anyNA(miceOutput)

      4. actuals <- loan.data$debt_income[is.na(data.copy$debt_income)]

      5. predicteds <- miceOutput[is.na(data.copy$debt_income), "debt_income"]

      6. regr.eval(actuals, predicteds)   # debt_income accuracy


      7. actuals <- loan.data$education[is.na(data.copy$education)]

      8. predicteds <- miceOutput[is.na(data.copy$education), "education"]

      9. mean(actuals != predicteds)  # misclass error education accuracy

      • The mean absolute percentage error (mape) has improved by ~ 62% compared to the imputation by mean.

      • Mis-classification error is the same as rpart’s 60%, which may be contributed to imbalanced classes and small sample.

      1. miceMod <- mice(data.copy[, !names(data.copy) %in% "Loan"], method="pmm")  # based on predictive mean matching

      2. miceOutput <- complete(miceMod)  # generate completed data.

      3. anyNA(miceOutput)

      4. actuals <- loan.data$debt_income[is.na(data.copy$debt_income)]

      5. predicteds <- miceOutput[is.na(data.copy$debt_income), "debt_income"]

      6. regr.eval(actuals, predicteds)   # debt_income accuracy


      7. actuals <- loan.data$education[is.na(data.copy$education)]

      8. predicteds <- miceOutput[is.na(data.copy$education), "education"]

      9. mean(actuals != predicteds)  # misclass error education accuracy

      • The mean absolute percentage error (mape) has improved by ~ 61% compared to the imputation by mean.

      • But mis-classification error is 50%, better than previous two methods.

      4. 数据视觉化检测 —- 对比原始数据和推测值

      4.1. scatterplot

      1. xyplot(miceMod,debt_income ~ age + year_emp + income, pch=18,cex=0.8)

      • Desirable scenario: magenta points (imputed) matches shape of blue (observed). Matching shape 说明 imputed values are indeed “plausible values”.

      4.2. density plot

      1. densityplot(miceMod)

      • Magenta – density of imputed data for each imputed dataset

      • Blue – density of observed data

      • Desirable: similar distributions

      4.3. stripplot plot

      1. stripplot(miceMod, pch = 20, cex = 1.2)

      • 点状图

      5. 推测处理后的数据建模 —- pooling

      1. modelFit1 <- with(miceMod,lm(debt_income ~ age + year_emp + income))

      2. summary(pool(modelFit1))

      • fmi —- fraction of missing information

      • lambda —- proportion of total variance that is attributable to missing data

      6. 备注(额外介绍)—- 处理 random seed initialization

      • mice function is initialed with a specific seed, so results are dependent on initial choice. To reduce this effect, we impute a higher number of dataset, by changing default m=5 parameter in mice() function

        
        
        
        
        1. modelFit2 <- with(tempData2,lm(debt_income ~ age + year_emp + income))

        2. summary(pool(modelFit2))

        1. tempData2 <- mice(data.copy[, !names(data.copy) %in% "Loan"], method='pmm',m=50, maxit=10, seed=0)


      今日数据人网精选推荐:

      《机器学习的巨大精彩和影响》

      《Netflix的影片推荐系统》

      《人工智能是当今地球上最重要的科技》

      点击【阅读原文】,立刻阅读精选。

      测试结尾

      请关注“恒诺新知”微信公众号,感谢“R语言“,”数据那些事儿“,”老俊俊的生信笔记“,”冷🈚️思“,“珞珈R”,“生信星球”的支持!

      • 分享:
      作者头像
      weinfoadmin

      上一篇文章

      【数据人网】致数据人的一封信
      2016年5月10日

      下一篇文章

      【数据人网】干货资料第一期大派送
      2016年5月12日

      你可能也喜欢

      3-1665801675
      R语言学习:重读《R数据科学(中文版)》书籍
      28 9月, 2022
      6-1652833487
      经典铁死亡,再出新思路
      16 5月, 2022
      1-1651501980
      R语言学习:阅读《R For Everyone 》(第二版)
      1 5月, 2022

      搜索

      分类

      • R语言
      • TCGA数据挖掘
      • 单细胞RNA-seq测序
      • 在线会议直播预告与回放
      • 数据分析那些事儿分类
      • 未分类
      • 生信星球
      • 老俊俊的生信笔记

      投稿培训

      免费

      alphafold2培训

      免费

      群晖配置培训

      免费

      最新博文

      Nature | 单细胞技术揭示衰老细胞与肌肉再生
      301月2023
      lncRNA和miRNA生信分析系列讲座免费视频课和课件资源包,干货满满
      301月2023
      如何快速批量修改 Git 提交记录中的用户信息
      261月2023
      logo-eduma-the-best-lms-wordpress-theme

      (00) 123 456 789

      weinfoadmin@weinformatics.cn

      恒诺新知

      • 关于我们
      • 博客
      • 联系
      • 成为一名讲师

      链接

      • 课程
      • 事件
      • 展示
      • 问答

      支持

      • 文档
      • 论坛
      • 语言包
      • 发行状态

      推荐

      • iHub汉语代码托管
      • iLAB耗材管理
      • WooCommerce
      • 丁香园论坛

      weinformatics 即 恒诺新知。ICP备案号:粤ICP备19129767号

      • 关于我们
      • 博客
      • 联系
      • 成为一名讲师

      要成为一名讲师吗?

      加入数以千计的演讲者获得100%课时费!

      现在开始

      用你的站点账户登录

      忘记密码?

      还不是会员? 现在注册

      注册新帐户

      已经拥有注册账户? 现在登录

      close
      会员购买 你还没有登录,请先登录
      • ¥99 VIP-1个月
      • ¥199 VIP-半年
      • ¥299 VIP-1年
      在线支付 激活码

      立即支付
      支付宝
      微信支付
      请使用 支付宝 或 微信 扫码支付
      登录
      注册|忘记密码?