前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >相关分析的简单示例

相关分析的简单示例

作者头像
jeanron100
发布2019-06-15 15:15:17
6820
发布2019-06-15 15:15:17
举报
文章被收录于专栏:杨建荣的学习笔记

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

今天

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

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

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

我们把下载得到的文件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

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

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

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

> 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")])

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

> library(psych)

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

比如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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-06-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 杨建荣的学习笔记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档