当前位置: 首页 > news >正文

相关分析的简单示例

这是学习笔记的第 2004 篇文章


  今天

美国人口普查局(U.S. Census Bureau)的人口统计资料,该数据集包含了美国病人的医疗费用,在github中可以下载相应的数据。 

https://github.com/stedy/Machine-Learning-with-R-datasets/find/master

整个模型的数据类似下面的形式:

640?wx_fmt=png

我们把下载得到的文件insurance.csv放在D盘下,加载数据

> insurance <- read.csv("d:\\insurance.csv",stringsAsFactors = TRUE)

使用str函数来确认该数据转换了我们之前所期望的形式:

>  str(insurance)

'data.frame':   1338 obs. of  7 variables:

 $ age     : int  19 18 28 33 32 31 46 37 37 60 ...

 $ sex     : Factor w/ 2 levels "female","male": 1 2 2 2 2 1 1 1 2 1 ...

 $ bmi     : num  27.9 33.8 33 22.7 28.9 ...

 $ children: int  0 1 3 0 0 0 1 3 2 0 ...

 $ smoker  : Factor w/ 2 levels "no","yes": 2 1 1 1 1 1 1 1 1 1 ...

 $ region  : Factor w/ 4 levels "northeast","northwest",..: 4 3 3 2 2 3 3 2 1 2 ...

 $ charges : num  16885 1726 4449 21984 3867 ...

我们查看因变量charges的数据分布情况:

> summary(insurance$charges)

   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 

   1122    4740    9382   13270   16640   63770 

平均数远大于中位数,表明保险费用的分布是右偏的,使用直方图查看的情况如下:

640?wx_fmt=png

到目前为止,我们已经得到了一个整体的数据分布情况。 

我们需要做相关性分析,首先需要使用相关系数矩阵来查看。

> cor(insurance[c("age","bmi","children","charges")])

               age       bmi   children    charges

age      1.0000000 0.1092719 0.04246900 0.29900819

bmi      0.1092719 1.0000000 0.01275890 0.19834097

children 0.0424690 0.0127589 1.00000000 0.06799823

charges  0.2990082 0.1983410 0.06799823 1.00000000

当然这种表格化的数据我们也可以做出一些基本的分析,比如age和bmi,age和charges,bmi和charges等。

我们可以使用散点图矩阵来得到可视化的数据效果,

> pairs(insurance[c("age","bmi","children","charges")])

640?wx_fmt=png

也可以基于psych得到更丰富的散点图矩阵,

> library(psych)

>  pairs.panels(insurance[c("age","bmi","children","charges")])

640?wx_fmt=png

比如age和charges之间的关系呈现出几条相对的直线,而bmi和charges的散点图构成了两个不同的群体。

在对角线上,直方图描绘了每个特征的数值分布。最后,对角线下方的散点图带有额外的可视化信息。

接下来我们尝试得到回归模型,用R对数据拟合一个线性回归模型.

> ins_model <- lm(charges~age+children+bmi+sex+smoker+region,data=insurance)

> ins_model

Call:

lm(formula = charges ~ age + children + bmi + sex + smoker + 

    region, data = insurance)

Coefficients:

    (Intercept)              age         children              bmi  

       -11938.5            256.9            475.5            339.2  

        sexmale        smokeryes  regionnorthwest  regionsoutheast  

         -131.3          23848.5           -353.0          -1035.0  

regionsouthwest  

         -960.1  

>  summary(ins_model)

Call:

lm(formula = charges ~ age + children + bmi + sex + smoker + 

    region, data = insurance)

Residuals:

     Min       1Q   Median       3Q      Max 

-11304.9  -2848.1   -982.1   1393.9  29992.8 

Coefficients:

                Estimate Std. Error t value Pr(>|t|)    

(Intercept)     -11938.5      987.8 -12.086  < 2e-16 ***

age                256.9       11.9  21.587  < 2e-16 ***

children           475.5      137.8   3.451 0.000577 ***

bmi                339.2       28.6  11.860  < 2e-16 ***

sexmale           -131.3      332.9  -0.394 0.693348    

smokeryes        23848.5      413.1  57.723  < 2e-16 ***

regionnorthwest   -353.0      476.3  -0.741 0.458769    

regionsoutheast  -1035.0      478.7  -2.162 0.030782 *  

regionsouthwest   -960.0      477.9  -2.009 0.044765 *  

---

Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 6062 on 1329 degrees of freedom

Multiple R-squared:  0.7509,    Adjusted R-squared:  0.7494 

F-statistic: 500.8 on 8 and 1329 DF,  p-value: < 2.2e-16

相关链接:

640?

相关文章:

  • 一些用户画像数据
  • 使用Shell脚本来解析MySQL元数据变化
  • 初来北京几年的精神状态
  • 磨刀不误砍柴工-流程梳理
  • 一个MySQL连接问题的优化过程
  • 认知的偏差
  • 迁移到MySQL的架构演进(一)
  • K-Means算法原理和简单测试
  • 如何让你的工作能够大量输出
  • 数据生命周期管理的初步实现
  • MySQL分布式高可用的一个补充
  • MySQL锁
  • 难忘的三件苦差事
  • 千与千寻,真是一部给大人看的动画片
  • 聊聊高考分数线和选择
  • 4个实用的微服务测试策略
  • Android系统模拟器绘制实现概述
  • Angular数据绑定机制
  • JavaScript 基础知识 - 入门篇(一)
  • JSONP原理
  • Python学习之路16-使用API
  • spring security oauth2 password授权模式
  • sublime配置文件
  • 对象管理器(defineProperty)学习笔记
  • 关于springcloud Gateway中的限流
  • 前端存储 - localStorage
  • 前端知识点整理(待续)
  • 如何进阶一名有竞争力的程序员?
  • 深入浅出Node.js
  • 腾讯优测优分享 | 你是否体验过Android手机插入耳机后仍外放的尴尬?
  • 微信小程序--------语音识别(前端自己也能玩)
  • 问:在指定的JSON数据中(最外层是数组)根据指定条件拿到匹配到的结果
  • 一些基于React、Vue、Node.js、MongoDB技术栈的实践项目
  • No resource identifier found for attribute,RxJava之zip操作符
  • LevelDB 入门 —— 全面了解 LevelDB 的功能特性
  • ​如何防止网络攻击?
  • $var=htmlencode(“‘);alert(‘2“); 的个人理解
  • (12)Hive调优——count distinct去重优化
  • (ISPRS,2023)深度语义-视觉对齐用于zero-shot遥感图像场景分类
  • (java)关于Thread的挂起和恢复
  • (翻译)Entity Framework技巧系列之七 - Tip 26 – 28
  • (四)【Jmeter】 JMeter的界面布局与组件概述
  • (转)c++ std::pair 与 std::make
  • (转)Groupon前传:从10个月的失败作品修改,1个月找到成功
  • (转)shell调试方法
  • (转)利用ant在Mac 下自动化打包签名Android程序
  • .net 4.0发布后不能正常显示图片问题
  • .NET Core 实现 Redis 批量查询指定格式的Key
  • .NET delegate 委托 、 Event 事件,接口回调
  • .NET Framework 服务实现监控可观测性最佳实践
  • .NET NPOI导出Excel详解
  • .NET 使用 XPath 来读写 XML 文件
  • .NET 中什么样的类是可使用 await 异步等待的?
  • .Net(C#)自定义WinForm控件之小结篇
  • .NET企业级应用架构设计系列之结尾篇