相关分析的简单示例
发布日期:2021-06-30 13:21:48 浏览次数:2 分类:技术文章

本文共 2826 字,大约阅读时间需要 9 分钟。

这是学习笔记的第 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?

转载地址:https://jeanron100.blog.csdn.net/article/details/102479261 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:一些用户画像数据
下一篇:推荐几个统计数据的网站

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月28日 14时06分05秒