实验4:数据探索
一:实验目的与要求
1:熟悉和掌握数据探索,学习数据质量分类、数据特征分析和R语言的主要数据探索函数。
二:实验内容
1:数据质量分析
2:统计量分析
3:贡献度分析
4:相关性分析
5:统计特征函数
三:课堂练习
【练习1】PPT-06第12页------检测餐饮销售数据缺失值和异常值
第一步:用setwd设置工作空间
|--------------------------------------------------|
| setwd('C:\\Users\\86158\\Desktop') getwd() |
第二步:读入数据
|----------------------------------------------------------------------------------------------|
| saledata <- read.csv(file="catering_sale.csv", header=TRUE, fileEncoding="GB2312") saledata |
完整运行结果:
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 日期 销量 1 2015/3/1 51.00 2 2015/2/28 2618.20 3 2015/2/27 2608.40 4 2015/2/26 2651.90 5 2015/2/25 3442.10 6 2015/2/24 3393.10 7 2015/2/23 3136.60 8 2015/2/22 3744.10 9 2015/2/21 6607.40 10 2015/2/20 4060.30 11 2015/2/19 3614.70 12 2015/2/18 3295.50 13 2015/2/16 2332.10 14 2015/2/15 2699.30 15 2015/2/14 NA 16 2015/2/13 3036.80 17 2015/2/12 865.00 18 2015/2/11 3014.30 19 2015/2/10 2742.80 20 2015/2/9 2173.50 21 2015/2/8 3161.80 22 2015/2/7 3023.80 23 2015/2/6 2998.10 24 2015/2/5 2805.90 25 2015/2/4 2383.40 26 2015/2/3 2620.20 27 2015/2/2 2600.00 28 2015/2/1 2358.60 29 2015/1/31 2682.20 30 2015/1/30 2766.80 31 2015/1/29 2618.80 32 2015/1/28 2714.30 33 2015/1/27 2280.80 34 2015/1/26 2414.00 35 2015/1/25 3130.60 36 2015/1/24 2716.90 37 2015/1/23 2930.80 38 2015/1/22 2504.90 39 2015/1/21 2559.50 40 2015/1/20 2168.60 41 2015/1/19 2436.40 42 2015/1/18 3234.30 43 2015/1/17 3061.00 44 2015/1/16 2900.10 45 2015/1/15 2646.80 46 2015/1/14 2615.20 47 2015/1/13 2124.40 48 2015/1/12 1958.00 49 2015/1/8 2259.10 50 2015/1/7 2419.80 51 2015/1/6 2775.00 52 2015/1/5 2594.90 53 2015/1/4 2468.30 54 2015/1/3 3004.30 55 2015/1/2 3313.30 56 2015/1/1 3613.60 57 2014/12/31 2655.90 58 2014/12/30 2644.30 59 2014/12/29 2565.30 60 2014/12/27 2525.90 61 2014/12/26 2778.00 62 2014/12/25 2542.10 63 2014/12/24 2473.30 64 2014/12/23 2240.10 65 2014/12/22 2575.00 66 2014/12/21 3802.80 67 2014/12/18 2274.70 68 2014/12/17 2687.20 69 2014/12/16 2577.80 70 2014/12/15 2583.00 71 2014/12/14 3282.60 72 2014/12/13 3113.70 73 2014/12/12 2661.40 74 2014/12/11 2553.20 75 2014/12/10 2511.30 76 2014/12/9 2710.30 77 2014/12/8 2468.10 78 2014/12/7 3041.50 79 2014/12/6 3178.90 80 2014/12/5 2594.40 81 2014/12/4 2381.10 82 2014/12/3 2415.00 83 2014/12/2 2236.40 84 2014/11/30 3207.20 85 2014/11/29 3059.50 86 2014/11/28 3039.10 87 2014/11/26 2817.50 88 2014/11/25 2891.80 89 2014/11/24 2470.10 90 2014/11/23 3556.60 91 2014/11/22 3397.70 92 2014/11/20 2761.60 93 2014/11/19 2618.20 94 2014/11/18 2758.30 95 2014/11/17 2614.30 96 2014/11/16 3437.10 97 2014/11/15 3250.00 98 2014/11/14 3063.70 99 2014/11/13 2839.20 100 2014/11/12 2360.90 101 2014/11/11 2158.50 102 2014/11/10 2005.50 103 2014/11/9 3236.40 104 2014/11/8 22.00 105 2014/11/7 2452.60 106 2014/11/6 2265.00 107 2014/11/5 2566.10 108 2014/11/4 2527.20 109 2014/11/3 2326.50 110 2014/11/2 2941.90 111 2014/11/1 60.00 112 2014/10/31 2520.90 113 2014/10/30 2446.20 114 2014/10/29 2549.40 115 2014/10/28 2449.30 116 2014/10/27 2162.50 117 2014/10/26 2781.30 118 2014/10/25 3060.60 119 2014/10/24 2064.00 120 2014/10/22 2439.70 121 2014/10/21 2476.20 122 2014/10/20 2478.30 123 2014/10/19 2826.20 124 2014/10/18 2924.80 125 2014/10/17 2417.50 126 2014/10/16 2450.10 127 2014/10/15 2533.00 128 2014/10/14 2238.70 129 2014/10/13 2388.80 130 2014/10/12 3291.30 131 2014/10/11 2738.80 132 2014/10/10 2344.10 133 2014/10/9 2068.80 134 2014/10/8 3185.30 135 2014/10/7 2778.60 136 2014/10/6 2921.10 137 2014/10/5 2524.30 138 2014/10/4 3057.10 139 2014/10/3 3039.60 140 2014/10/2 3193.40 141 2014/10/1 3075.40 142 2014/9/30 2847.60 143 2014/9/29 2311.40 144 2014/9/28 2327.30 145 2014/9/27 9106.44 146 2014/9/26 2616.60 147 2014/9/25 2620.20 148 2014/9/24 2616.40 149 2014/9/23 2655.80 150 2014/9/22 2310.70 151 2014/9/21 2935.80 152 2014/9/20 3017.90 153 2014/9/19 2625.50 154 2014/9/18 2752.70 155 2014/9/17 2181.50 156 2014/9/16 2440.50 157 2014/9/15 2422.80 158 2014/9/14 2583.60 159 2014/9/13 2728.90 160 2014/9/12 2525.30 161 2014/9/11 2531.70 162 2014/9/10 2300.50 163 2014/9/9 2097.50 164 2014/9/8 4065.20 165 2014/9/7 3555.20 166 2014/9/6 3462.50 167 2014/9/5 3033.10 168 2014/9/4 2926.10 169 2014/9/3 2431.40 170 2014/9/2 2706.00 171 2014/9/1 3049.90 172 2014/8/31 3494.70 173 2014/8/30 3691.90 174 2014/8/29 2929.50 175 2014/8/28 2760.60 176 2014/8/27 2593.70 177 2014/8/26 2884.40 178 2014/8/25 2591.30 179 2014/8/24 3022.60 180 2014/8/23 3052.10 181 2014/8/22 2789.20 182 2014/8/21 2909.80 183 2014/8/20 2326.80 184 2014/8/19 2453.10 185 2014/8/18 2351.20 186 2014/8/17 3279.10 187 2014/8/16 3381.90 188 2014/8/15 2988.10 189 2014/8/14 2577.70 190 2014/8/13 2332.30 191 2014/8/12 2518.60 192 2014/8/11 2697.50 193 2014/8/10 3244.70 194 2014/8/9 3346.70 195 2014/8/8 2900.60 196 2014/8/7 2759.10 197 2014/8/6 2915.80 198 2014/8/5 2618.10 199 2014/8/4 2993.00 200 2014/8/3 3436.40 201 2014/8/2 2261.70 |
第三步:缺失值检测并打印结果
|-------------------------------------------------------------------------------------------------------------------------------------|
| sum(complete.cases(saledata)) sum(!complete.cases(saledata)) mean(!complete.cases(saledata)) saledata[!complete.cases(saledata),] |
第四步:异常值检测箱线图(扩展)
|------------------------------------------------------------------|
| boxplot(saledata$销量, main="销售数据异常值检测", ylab="销售额", na.rm = TRUE) |
第五步:带有数值的异常值检测箱线图
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| # 异常值检测箱线图 sp <- boxplot(saledata"销量", boxwex = 0.7) title("销量异常值检测箱线图") xi \<- 1.1 sd.s \<- sd(saledata\[complete.cases(saledata), \]"销量") mn.s <- mean(saledata[complete.cases(saledata), ]"销量") points(xi, mn.s, col = "red", pch = 18) arrows(xi, mn.s - sd.s, xi, mn.s + sd.s, code = 3, col = "pink", angle = 75, length = .1) text(rep(c(1.05, 1.05, 0.95, 0.95), length = length(spout)), labels = spout\[order(spout)], spout\[order(spout)] + rep(c(150, -150, 150, -150), length = length(sp$out)), col = "red") |
【练习2】PPT-06第32页------餐饮销售数据统计量分析
第一步:读入数据
|---------------------------------|
| sales <- saledata[, 2] sales |
第二步:计算均值
|----------------------------------------|
| mean_ <- mean(sales, na.rm = T) mean_ |
第三步:计算中位数
|----------------------------------------------|
| median_ <- median(sales, na.rm = T) median_ |
第四步:计算极差
|-----------------------------------------------------------------|
| range_ <- max(sales, na.rm = T) - min(sales, na.rm = T) range_ |
第五步:计算标准差
|-------------------------------------------|
| std_ <- sqrt(var(sales, na.rm = T)) std_ |
第六步:计算变异系数
|----------------------------------------|
| variation_ <- std_ / mean_ variation_ |
第七步:计算四分位数间距
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| q1 <- quantile(sales, 0.25, na.rm = T) q3 <- quantile(sales, 0.75, na.rm = T) distance <- q3 - q1 a <- matrix(c(mean_, median_, range_, std_, variation_, q1, q3, distance), 1, byrow = T) colnames(a) <- c("均值", "中位数", "极差", "标准差", "变异系数", "1/4分位数", "3/4分位数", "四分位间距") print(a) |
【练习3】PPT-06第37页------菜品盈利贡献度统计量分析
第一步:读取菜品数据
|-----------------------------------------------------------------------------------------|
| dishdata <- read.csv(file = "catering_dish_profit.csv",fileEncoding="GB2312") dishdata |
第二步:绘制帕累托图
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| barplot(dishdata[, 3], col = "blue1", names.arg = dishdata[, 2], width = 1, space = 0, ylim = c(0, 10000), xlab = "菜品", ylab = "盈利:元") accratio <- dishdata[, 3] for ( i in 1:length(accratio)) { accratio[i] <- sum(dishdata[1:i, 3]) / sum(dishdata[, 3]) } par(new = T, mar = c(4, 4, 4, 4)) points(accratio * 10000 ~ c((1:length(accratio) - 0.5)), new = FALSE, type = "b", new = T) axis(4, col = "red", col.axis = "red", at = 0:10000, label = c(0:10000 / 10000)) mtext("累积百分比", 4, 2) points(6.5, accratio[7] * 10000, col="red") text(7, accratio[7] * 10000,paste(round(accratio[7] + 0.00001, 4) * 100, "%")) |
【练习4】PPT-06第45页------餐饮销售数据相关性分析
第一步:读取数据
|---------------------------------------------------------------------------------------------------|
| cordata <- read.csv(file = "catering_sale_all.csv", header = TRUE,fileEncoding="GB2312") cordata |
完整运行结果:
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 日期 百合酱蒸凤爪 翡翠蒸香茜饺 金银蒜汁蒸排骨 乐膳真味鸡 蜜汁焗餐包 1 2015/1/1 17 6 8 24 13 2 2015/1/2 11 15 14 13 9 3 2015/1/3 10 8 12 13 8 4 2015/1/4 9 6 6 3 10 5 2015/1/5 4 10 13 8 12 6 2015/1/6 13 10 13 16 8 7 2015/1/7 9 7 13 8 5 8 2015/1/8 9 12 13 6 7 9 2015/1/12 6 8 8 3 NA 10 2015/1/13 9 11 13 6 8 11 2015/1/14 6 7 8 9 4 12 2015/1/15 5 9 4 7 8 13 2015/1/16 9 7 11 9 11 14 2015/1/17 10 8 10 6 14 15 2015/1/18 13 12 12 10 9 16 2015/1/19 4 8 12 11 9 17 2015/1/20 6 12 10 9 11 18 2015/1/21 9 15 4 12 7 19 2015/1/22 3 10 13 13 13 20 2015/1/23 8 7 9 20 5 21 2015/1/24 11 6 11 8 7 22 2015/1/25 11 6 5 15 7 23 2015/1/26 4 7 10 7 6 24 2015/1/27 7 5 6 7 12 25 2015/1/28 8 8 12 14 8 26 2015/1/29 4 10 12 9 7 27 2015/1/30 6 7 7 11 7 28 2015/1/31 8 5 11 10 8 29 2015/2/1 8 6 7 9 6 生炒菜心 铁板酸菜豆腐 香煎韭菜饺 香煎罗卜糕 原汁原味菜心 1 13 18 10 10 27 2 10 19 13 14 13 3 3 7 11 10 9 4 9 9 13 14 13 5 10 17 11 13 14 6 9 12 11 5 9 7 7 10 8 10 7 8 8 6 12 11 5 9 4 5 5 7 10 10 7 6 9 8 9 11 7 8 5 3 10 12 9 15 9 13 9 13 8 14 9 9 15 14 13 16 9 4 14 15 11 8 12 9 15 16 7 10 6 11 11 17 4 8 14 6 13 18 9 1 5 12 8 19 8 13 5 11 11 20 12 8 7 8 11 21 8 9 7 10 9 22 14 14 7 6 8 23 10 9 12 7 5 24 15 6 12 9 4 25 11 7 12 10 6 26 8 8 10 10 11 27 7 9 16 10 11 28 11 8 10 10 9 29 4 6 11 6 9 |
第二步:求出相关系数矩阵
|------------------------|
| cor(cordata[, 2:11]) |
【练习5】PPT-06第51页
Eg1:计算两个列向量的相关系数,采用Spearman方法
|--------------------------------------------------------|
| x = c(1:8) y = c(2:9) R = cor(x,y,method="spearman") R |
Eg2:计算20×5随机矩阵的协方差矩阵
|------------------------------------------|
| X = matrix(rnorm(100),20,5) R = cor(X) R |
四:实验知识点总结
1:数据质量分析是数据预处理的前提,其主要任务是检查原始数据中是否存在脏数据。脏数据主要包括:缺失值、异常值、不一致的值、重复数据和含有特殊符号的数据。
2:缺失值分析包括:(1)统计缺失值的变量个数;(2)统计每个变量的未缺失数;(3)统计变量的缺失数和缺失率;
3:异常值分析方法包括:(1)简单统计量分析;(2)3σ原则;(3)箱型图分析;
4:数据的不一致性是数据的矛盾性和不相容性,不一致数据的产生主要发生在数据集成的过程中。
5:数据特征分析的方法包括:(1)分布分析;(2)对比分析;(3)统计量分析;(4)周期性分析;(5)贡献度分析;(6)相关性分析;
6:常用的统计特征函数。
五:遇到的问题和解决方法
**问题1:**在读入csv数据时,出现以下报错。这个报错信息表明在尝试读取CSV文件时遇到了编码问题,即文件中可能包含了非标准ASCII字符。
**解决1:**直接指定读取文件的确切编码,此处使用GB2312。