library(data.table)
library(ggplot2)
library(plyr)
D_cj <- read.table('./训练/成绩.txt',header=T,fileEncoding='utf8')
D_js <- read.table('./训练/借书.txt',header=T,fileEncoding='utf8')
D_mj <- read.table('./训练/图书馆门禁.txt',header=T,fileEncoding='utf8')
D_xf <- read.table('./训练/消费.txt',header=T,fileEncoding='GBK')
summary(D_cj)
## 学期 学号 排名
## Min. :1 Min. : 1 Min. : 1
## 1st Qu.:1 1st Qu.:135 1st Qu.:135
## Median :2 Median :270 Median :270
## Mean :2 Mean :270 Mean :270
## 3rd Qu.:3 3rd Qu.:404 3rd Qu.:404
## Max. :3 Max. :538 Max. :538
summary(D_js)
## 学期 学号 书号 日期
## Min. :1.00 Min. : 1 Min. :1000232 Min. : 102
## 1st Qu.:1.00 1st Qu.:133 1st Qu.:1363560 1st Qu.: 411
## Median :1.00 Median :272 Median :2586051 Median : 918
## Mean :1.72 Mean :264 Mean :2156135 Mean : 767
## 3rd Qu.:2.00 3rd Qu.:388 3rd Qu.:2819687 3rd Qu.:1111
## Max. :3.00 Max. :537 Max. :3070276 Max. :1231
summary(D_mj)
## 学期 学号 日期 时间
## Min. :1 Min. : 1 Min. : 102 Min. : 64633
## 1st Qu.:1 1st Qu.:138 1st Qu.: 407 1st Qu.:102938
## Median :2 Median :278 Median : 902 Median :145608
## Mean :2 Mean :273 Mean : 739 Mean :143657
## 3rd Qu.:3 3rd Qu.:404 3rd Qu.:1114 3rd Qu.:182630
## Max. :3 Max. :538 Max. :1231 Max. :225254
summary(D_xf)
## 学期 学号 地点 日期
## Min. :1.00 Min. : 1 超市 : 37135 1224 : 3352
## 1st Qu.:2.00 1st Qu.:131 打印 : 1336 0107 : 2864
## Median :2.00 Median :254 交通 : 4218 1218 : 2856
## Mean :2.19 Mean :265 教室 : 25216 0105 : 2845
## 3rd Qu.:3.00 3rd Qu.:403 食堂 :281509 0108 : 2811
## Max. :3.00 Max. :538 图书馆: 7129 0106 : 2762
## 宿舍 :101547 (Other):440600
## 时间 金额
## Min. : 1 Min. :0.0e+00
## 1st Qu.:112113 1st Qu.:1.0e+00
## Median :140751 Median :3.0e+00
## Mean :143364 Mean :3.0e+01
## 3rd Qu.:180200 3rd Qu.:5.0e+00
## Max. :235959 Max. :1.2e+07
##
从这里可以大致看出我们要分析的数据,包括三个学期和538个学生
先看看每个学生三个学期的平均排名
x <- aggregate(D_cj[[3]],by=list(D_cj[[2]]),FUN=mean)
colnames(x)<-c('学号','平均排名')
qplot(x=x$学号,y=x$平均排名,geom="path",main="平均排名")
从图中可以看到排名随学号的分布是均匀的。。。
然后看看借书量随排名的分布
D_js[[5]] <- 1
y1 <- aggregate(D_js[[5]],by=list(D_js[[2]]),FUN=sum)
colnames(y1)<-c('学号','总借书量')
y1 <- join(x,y1)
y1[[3]][is.na(y1[[3]])] <- 0
qplot(y1$总借书量,y1$平均排名,geom=c('point','smooth'),main="借书量与排名")
借书量和排名之间还是有比较强烈的关系的,看图有点像直线←_←
考虑到有可能有些人是学霸但是不爱去图书馆借书,我把借书次数为0的童鞋们去掉再画了一次图
y1_1 <- y1[y1[[3]]!=0,]
qplot(y1_1$总借书量,y1_1$平均排名,geom=c('point','smooth'),main="借书量与排名(去掉从来不去图书馆的同学)")
看起来变化不大。。
看看两种情况的回归系数以及斜率
summary(lm(平均排名~总借书量,data=y1))
##
## Call:
## lm(formula = 平均排名 ~ 总借书量, data = y1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -303.62 -107.27 0.16 118.26 260.51
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 311.441 8.109 38.41 < 2e-16 ***
## 总借书量 -1.385 0.181 -7.65 9.6e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 139 on 536 degrees of freedom
## Multiple R-squared: 0.0984, Adjusted R-squared: 0.0967
## F-statistic: 58.5 on 1 and 536 DF, p-value: 9.59e-14
summary(lm(平均排名~总借书量,data=y1_1))
##
## Call:
## lm(formula = 平均排名 ~ 总借书量, data = y1_1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -299.73 -109.64 -1.44 120.37 263.05
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 307.362 8.696 35.35 < 2e-16 ***
## 总借书量 -1.323 0.188 -7.05 5.8e-12 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 139 on 500 degrees of freedom
## Multiple R-squared: 0.0905, Adjusted R-squared: 0.0887
## F-statistic: 49.7 on 1 and 500 DF, p-value: 5.85e-12
发现前者的拟合程度反而高些,果然删掉不去图书馆的同学是没有必要的。
D_mj[[5]] <- 1
y2 <- aggregate(D_mj[[5]],by=list(D_mj[[2]]),FUN=sum)
colnames(y2)<-c('学号','去图书馆总次数')
y2 <- join(x,y2)
y2[[3]][is.na(y2[[3]])] <- 0
qplot(y2$去图书馆总次数,y2$平均排名,geom=c('point','smooth'),main="去图书馆的次数与排名")
发现一个BT的人(成绩还一般)把整个图都拉长了,删掉它再做了一次分析
y2_2 <- y2[y2[[3]]!=max(y2[[3]]),]
qplot(y2_2$去图书馆总次数,y2_2$平均排名,geom=c('point','smooth'),main="去图书馆的次数与排名(修正)")
图形理想了很多,明显也是比较线性的关系。
下面是两者的线性回归模型
summary(lm(平均排名~去图书馆总次数,data=y2))
##
## Call:
## lm(formula = 平均排名 ~ 去图书馆总次数, data = y2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -323.3 -99.9 4.6 107.0 317.9
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 336.3541 9.1800 36.64 <2e-16 ***
## 去图书馆总次数 -0.5894 0.0625 -9.42 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 135 on 536 degrees of freedom
## Multiple R-squared: 0.142, Adjusted R-squared: 0.141
## F-statistic: 88.8 on 1 and 536 DF, p-value: <2e-16
summary(lm(平均排名~去图书馆总次数,data=y2_2))
##
## Call:
## lm(formula = 平均排名 ~ 去图书馆总次数, data = y2_2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -326.7 -99.1 5.8 106.0 330.1
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 340.5076 9.3091 36.58 <2e-16 ***
## 去图书馆总次数 -0.6314 0.0648 -9.75 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 135 on 535 degrees of freedom
## Multiple R-squared: 0.151, Adjusted R-squared: 0.149
## F-statistic: 95 on 1 and 535 DF, p-value: <2e-16
先让我们再来看一次这一坨数据是什么情况
summary(D_xf)
## 学期 学号 地点 日期
## Min. :1.00 Min. : 1 超市 : 37135 1224 : 3352
## 1st Qu.:2.00 1st Qu.:131 打印 : 1336 0107 : 2864
## Median :2.00 Median :254 交通 : 4218 1218 : 2856
## Mean :2.19 Mean :265 教室 : 25216 0105 : 2845
## 3rd Qu.:3.00 3rd Qu.:403 食堂 :281509 0108 : 2811
## Max. :3.00 Max. :538 图书馆: 7129 0106 : 2762
## 宿舍 :101547 (Other):440600
## 时间 金额
## Min. : 1 Min. :0.0e+00
## 1st Qu.:112113 1st Qu.:1.0e+00
## Median :140751 Median :3.0e+00
## Mean :143364 Mean :3.0e+01
## 3rd Qu.:180200 3rd Qu.:5.0e+00
## Max. :235959 Max. :1.2e+07
##
主要关注地点,发现食堂和宿舍居多,而这两者应该关系不是很大(宿舍可能有负面影响,暂时先不管它吧),其他的几项里面觉得也就打印店和图书馆能有点联系了。
此外还发现一个大土豪把整个平均值都拉高了N多,于是删掉该土豪再做一次
D_xf <- D_xf[D_xf[[6]]!=max(D_xf[[6]]),]
summary(D_xf)
## 学期 学号 地点 日期
## Min. :1.00 Min. : 1 超市 : 37135 1224 : 3352
## 1st Qu.:2.00 1st Qu.:131 打印 : 1336 0107 : 2864
## Median :2.00 Median :254 交通 : 4218 1218 : 2856
## Mean :2.19 Mean :265 教室 : 25216 0105 : 2845
## 3rd Qu.:3.00 3rd Qu.:403 食堂 :281508 0108 : 2811
## Max. :3.00 Max. :538 图书馆: 7129 0106 : 2762
## 宿舍 :101547 (Other):440599
## 时间 金额
## Min. : 1 Min. : 0.01
## 1st Qu.:112113 1st Qu.: 1.00
## Median :140751 Median : 2.60
## Mean :143363 Mean : 3.50
## 3rd Qu.:180200 3rd Qu.: 5.00
## Max. :235959 Max. :100.00
##
删掉其它的项目,只留打印和图书,然后将消费金额加在一起作为我们的第三个参数
y3 <- D_xf[D_xf[[3]] %in% c('打印','图书馆'),]
y3 <- aggregate(y3[[6]],by=list(y3[[2]]),FUN=sum)
colnames(y3)<-c('学号','打印与图书消费')
y3 <- join(x,y3)
y3[[3]][is.na(y3[[3]])] <- 0
qplot(y3$打印与图书消费,y3$平均排名,geom=c('point','smooth'),main="打印与图书消费与排名")
还是发现一些人把整个图都拉长了,所以将金额限制在500以内再看一次
y3_2 <- y3[y3[[3]]<500,]
qplot(y3_2$打印与图书消费,y3_2$平均排名,geom=c('point','smooth'),main="打印与图书消费与排名(修正)")
还是感觉不太理想,仔细观察发现在50左右的地方有个大折点,于是干脆限制在50以内看看
y3_3 <- y3[y3[[3]]<50,]
qplot(y3_3$打印与图书消费,y3_3$平均排名,geom=c('point','smooth'),main="打印与图书消费与排名(修正2)")
尝试去掉消费为0的点
y3_4 <- y3[y3[[3]]<50 & y3[[3]]>0,]
qplot(y3_4$打印与图书消费,y3_4$平均排名,geom=c('point','smooth'),main="打印与图书消费与排名(修正3)")
此外再做一个不同的尝试,因为限制在50以内去掉了太多的点,而不限制又把图拉长了,所以试着把消费金额取个根号
y3_5 <- y3[y3[[3]]<500 & y3[[3]]>0,]
y3_5[[3]] <- sqrt(y3_5[[3]])
qplot(y3_5$打印与图书消费,y3_5$平均排名,geom=c('point','smooth'),main="打印与图书消费与排名(修正4)")
不管怎么搞看起来都不怎么完美,姑且就这样吧。以下是刚才所有尝试的回归模型信息:
summary(lm(平均排名~打印与图书消费,data=y3))
##
## Call:
## lm(formula = 平均排名 ~ 打印与图书消费, data = y3)
##
## Residuals:
## Min 1Q Median 3Q Max
## -279.81 -119.74 7.28 116.23 262.84
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 283.4753 7.1150 39.8 < 2e-16 ***
## 打印与图书消费 -0.2285 0.0571 -4.0 7.2e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 144 on 536 degrees of freedom
## Multiple R-squared: 0.029, Adjusted R-squared: 0.0272
## F-statistic: 16 on 1 and 536 DF, p-value: 7.23e-05
summary(lm(平均排名~打印与图书消费,data=y3_2))
##
## Call:
## lm(formula = 平均排名 ~ 打印与图书消费, data = y3_2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -284.19 -114.89 6.89 117.52 285.98
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 287.8545 7.6198 37.78 < 2e-16 ***
## 打印与图书消费 -0.3276 0.0828 -3.96 8.6e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 144 on 530 degrees of freedom
## Multiple R-squared: 0.0287, Adjusted R-squared: 0.0269
## F-statistic: 15.7 on 1 and 530 DF, p-value: 8.56e-05
summary(lm(平均排名~打印与图书消费,data=y3_3))
##
## Call:
## lm(formula = 平均排名 ~ 打印与图书消费, data = y3_3)
##
## Residuals:
## Min 1Q Median 3Q Max
## -300.92 -117.57 3.79 115.41 271.91
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 304.592 10.140 30.0 <2e-16 ***
## 打印与图书消费 -1.235 0.536 -2.3 0.022 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 144 on 339 degrees of freedom
## Multiple R-squared: 0.0154, Adjusted R-squared: 0.0125
## F-statistic: 5.31 on 1 and 339 DF, p-value: 0.0218
summary(lm(平均排名~打印与图书消费,data=y3_4))
##
## Call:
## lm(formula = 平均排名 ~ 打印与图书消费, data = y3_4)
##
## Residuals:
## Min 1Q Median 3Q Max
## -283.49 -123.84 0.88 115.83 269.45
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 297.373 17.161 17.33 <2e-16 ***
## 打印与图书消费 -0.992 0.715 -1.39 0.17
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 147 on 210 degrees of freedom
## Multiple R-squared: 0.00907, Adjusted R-squared: 0.00435
## F-statistic: 1.92 on 1 and 210 DF, p-value: 0.167
summary(lm(平均排名~打印与图书消费,data=y3_5))
##
## Call:
## lm(formula = 平均排名 ~ 打印与图书消费, data = y3_5)
##
## Residuals:
## Min 1Q Median 3Q Max
## -275.00 -119.46 -0.32 114.54 278.18
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 295.62 14.25 20.74 <2e-16 ***
## 打印与图书消费 -5.17 1.70 -3.03 0.0026 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 144 on 401 degrees of freedom
## Multiple R-squared: 0.0224, Adjusted R-squared: 0.02
## F-statistic: 9.2 on 1 and 401 DF, p-value: 0.00258
最后把其他的几个消费也都分别看看吧
y4 <- D_xf[D_xf[[3]]=='超市',]
y4 <- aggregate(y4[[6]],by=list(y4[[2]]),FUN=sum)
colnames(y4)<-c('学号','超市消费')
y4 <- join(x,y4)
y4[[3]][is.na(y4[[3]])] <- 0
qplot(y4$超市消费,y4$平均排名,geom=c('point','smooth'),main="超市消费与排名")
summary(lm(平均排名~超市消费,data=y4))
##
## Call:
## lm(formula = 平均排名 ~ 超市消费, data = y4)
##
## Residuals:
## Min 1Q Median 3Q Max
## -319.8 -122.9 -1.9 118.4 282.3
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.53e+02 8.23e+00 30.78 <2e-16 ***
## 超市消费 2.45e-02 8.11e-03 3.02 0.0026 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 145 on 536 degrees of freedom
## Multiple R-squared: 0.0168, Adjusted R-squared: 0.0149
## F-statistic: 9.14 on 1 and 536 DF, p-value: 0.00262
看起来貌似也有点关系的样子。。不过可以看到斜率的显著性只有两颗星,还不算太显著
y4 <- D_xf[D_xf[[3]]=='交通',]
y4 <- aggregate(y4[[6]],by=list(y4[[2]]),FUN=sum)
colnames(y4)<-c('学号','交通消费')
y4 <- join(x,y4)
y4[[3]][is.na(y4[[3]])] <- 0
qplot(y4$交通消费,y4$平均排名,geom=c('point','smooth'),main="交通消费与排名")
summary(lm(平均排名~交通消费,data=y4))
##
## Call:
## lm(formula = 平均排名 ~ 交通消费, data = y4)
##
## Residuals:
## Min 1Q Median 3Q Max
## -267.2 -119.4 4.9 122.1 276.4
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 270.8352 7.0341 38.50 <2e-16 ***
## 交通消费 -0.0758 0.1788 -0.42 0.67
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 146 on 536 degrees of freedom
## Multiple R-squared: 0.000335, Adjusted R-squared: -0.00153
## F-statistic: 0.18 on 1 and 536 DF, p-value: 0.672
这个的显著性就很低了,可以认为没多大关系
y4 <- D_xf[D_xf[[3]]=='教室',]
y4 <- aggregate(y4[[6]],by=list(y4[[2]]),FUN=sum)
colnames(y4)<-c('学号','教室消费')
y4 <- join(x,y4)
y4[[3]][is.na(y4[[3]])] <- 0
qplot(y4$教室消费,y4$平均排名,geom=c('point','smooth'),main="教室消费与排名")
summary(lm(平均排名~教室消费,data=y4))
##
## Call:
## lm(formula = 平均排名 ~ 教室消费, data = y4)
##
## Residuals:
## Min 1Q Median 3Q Max
## -288.47 -104.82 5.42 110.83 255.69
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 292.13 6.51 44.86 < 2e-16 ***
## 教室消费 -37.67 4.52 -8.33 6.8e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 137 on 536 degrees of freedom
## Multiple R-squared: 0.115, Adjusted R-squared: 0.113
## F-statistic: 69.4 on 1 and 536 DF, p-value: 6.8e-16
意外发现教室消费和排名其实还是有关系的,可以稍后再做进一步分析(首先应该把最右边几个点去掉)
y4 <- D_xf[D_xf[[3]]=='食堂',]
y4 <- aggregate(y4[[6]],by=list(y4[[2]]),FUN=sum)
colnames(y4)<-c('学号','食堂消费')
y4 <- join(x,y4)
y4[[3]][is.na(y4[[3]])] <- 0
qplot(y4$食堂消费,y4$平均排名,geom=c('point','smooth'),main="食堂消费与排名")
summary(lm(平均排名~食堂消费,data=y4))
##
## Call:
## lm(formula = 平均排名 ~ 食堂消费, data = y4)
##
## Residuals:
## Min 1Q Median 3Q Max
## -280.7 -122.3 5.9 123.4 273.1
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 284.59565 9.47321 30.04 <2e-16 ***
## 食堂消费 -0.00711 0.00335 -2.12 0.034 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 145 on 536 degrees of freedom
## Multiple R-squared: 0.00835, Adjusted R-squared: 0.0065
## F-statistic: 4.51 on 1 and 536 DF, p-value: 0.0341
果不其然,不太相关
y4 <- D_xf[D_xf[[3]]=='宿舍',]
y4 <- aggregate(y4[[6]],by=list(y4[[2]]),FUN=sum)
colnames(y4)<-c('学号','宿舍消费')
y4 <- join(x,y4)
y4[[3]][is.na(y4[[3]])] <- 0
qplot(y4$宿舍消费,y4$平均排名,geom=c('point','smooth'),main="宿舍消费与排名")
summary(lm(平均排名~宿舍消费,data=y4))
##
## Call:
## lm(formula = 平均排名 ~ 宿舍消费, data = y4)
##
## Residuals:
## Min 1Q Median 3Q Max
## -276.88 -123.76 8.28 119.37 271.09
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 280.5480 8.4056 33.38 <2e-16 ***
## 宿舍消费 -0.0949 0.0481 -1.97 0.049 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 145 on 536 degrees of freedom
## Multiple R-squared: 0.00721, Adjusted R-squared: 0.00535
## F-statistic: 3.89 on 1 and 536 DF, p-value: 0.0491
同样,显著性只有一星,图也很平
经过简单的探索,我们发现借书和图书馆门禁都与排名有比较明显的相关关系,而消费里面图书、打印以及教室消费关系较大,其他消费和排名之间的关系不太密切