[R] 数据科学流程(未完成)

《R 数据科学》

一、探索性分析

1 前言

https://s2.loli.net/2022/05/07/teoK5m6TNv18WxC.png

作者根据数据科学的整体构架书写的本书。

  1. 数据处理 1.整洁的数据要求: 每行是一个观测,每列是一个变量
  2. 进行数据转化,挑选出感兴趣观测
  3. 现有变量创建新的变量
  4. 统计变量
  5. 生成知识的方式:可视化与建模
  • 反复横跳,完善分析。
  1. 模型作为数学工具,是对可视化的补充。
  2. 沟通
  • 即写文章输出)
  1. 编程
  • 自动化处理
  • 解决新的问题

准备工作

install.packages("tidyverse")
library(tidyverse)

获取更多资源

  • google 搜索报错和加上 R 语言就行
  • reprex
  • R 博客,Rbloggers

源代码下载地址 源代码

引用信息 R for Data Science by Hadley Wickham and Garrett Grolemund (O’Reilly). Copyright 2017 Garrett Grolemund, Hadley Wickham, 978-1-491-91039-9.”

2 第一章 ggplot2 进行数据可视化

2.1 概述

“`Plain Text ggplot2(data=mpg) 可以添加一个坐标系 +ggeom_point 点图层 mapping 参数 aes 函数

**课后运行结果**

6. 直接运行 ggplot(data = mpg)没有结果,因为只是建立坐标系

7. 234 行,11 列
答案:可以用 dim()、str()、ncol()、nrow()

8. drv 是 the type of drive train, where f = front-wheel drive, r = rear wheel drive, 4 = 4wd

![https://s2.loli.net/2022/05/07/dOqIyUGta6PhA4f.png](https://s2.loli.net/2022/05/07/dOqIyUGta6PhA4f.png)

9. 展示的结果是,什么车用什么驱动。 没有任何可视化的必要

**课后运行代码**

R library(tidyverse) ggplot(data = mpg) ggplot(data = mpg) + geompoint(mapping = aes(x = displ, y = hwy)) mpg ?mpg ggplot(data = mpg) + geompoint(mapping = aes(x = hwy, y = cyl))

ggplot(data = mpg) + geom_point(mapping = aes(x = class, y = drv))

### 2.2 属性映射

Plain Text size 大小 color 颜色 alpha 透明度 shape 形状(默认最多 6 种)

![https://s2.loli.net/2022/05/07/oNI8vyUdknH7S9i.png](https://s2.loli.net/2022/05/07/oNI8vyUdknH7S9i.png)

![https://s2.loli.net/2022/05/07/Xw3clPkInUY4uZB.png](https://s2.loli.net/2022/05/07/Xw3clPkInUY4uZB.png)

**课后运行结果**

10. color 是 geom 的函数,所以括号放错地方了

11. glimpse(mpg)可以看其是不是分类变量

### 2.3 分面

单个变量对图进行分面,可以使用函数 facet_wrap()

R ggplot(data = mpg) + geompoint(mapping = aes(x = displ, y = hwy)) + facetwrap(~ class, nrow = 2)

两行,class分类

两个变量分面

ggplot(data = mpg) + geompoint(mapping = aes(x = displ, y = hwy)) + facetgrid(drv ~ cyl)

### 2.4 几何对象

R ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geompoint() + geomsmooth()

ggplot(data = mpg) + geompoint(mapping = aes(x = displ, y = hwy)) + geomsmooth(mapping = aes(x = displ, y = hwy))

以上可以展示一样的图

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geompoint(mapping = aes(color = class)) + geomsmooth( data = filter(mpg, class == “subcompact”), se = FALSE )

filter 可以筛选部分数据

### 2.5 ggplot2_cheatsheet

- color,fill 等函数都是 aes 功能里面的

- fill 可以填充里面更合适

- posiiton=“fill","dodge","identity",可以柱状图不同的排列方式

- position="jitter"可以抖动

### 2.6 坐标系

- coord_flip(坐标系的转换

  - 非常适合横坐标标签过长,可以转化为横向的

R ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + geomboxplot() + coordflip()

coord_polar() 函数使用极坐标系。

### 2.7 新函数模板

R ggplot(data = ) + ( mapping = aes(), stat = , position = ) + + “`

3 第二章工作流

4 使用 dplyr 进行数据转化

5 工作流脚本

6 探索性数据分析

7 工作流项目

二、数据处理

1 tibble 实现简单数据框

2 readr 进行数据导入

3 dplyr 处理关系数据

4 stringr 处理字符串

5 forcats 处理因子

6 lubridate 处理日期和时间

三、编程

1 magrittr 进行管道操作

2 函数

3 向量

4 使用 purrr 实现迭代

四、模型

1 使用 modelr 实现基础模型

2 模型构建

3 purrr 和 broom 处理多模型

五、沟通

1 Rmarkdown

2 ggplot2 图形化沟通

3 Rmarkdown 输出

4 Rmarkdown 工作流

Posted in R

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

wpse_http_request_args: Array ( [method] => GET [timeout] => 0.01 [redirection] => 5 [httpversion] => 1.0 [user-agent] => WordPress/6.8.1; https://zhangmingxu.com [reject_unsafe_urls] => [blocking] => [headers] => Array ( ) [cookies] => Array ( ) [body] => [compress] => [decompress] => 1 [sslverify] => [sslcertificates] => /home/ftp/s/s0964322/wwwroot/wp-includes/certificates/ca-bundle.crt [stream] => [filename] => [limit_response_size] => [wpse_http_request_args_modified] => 1 ) https://zhangmingxu.com/index.php?rest_route=%2Fjetpack%2Fv4%2Fsync%2Fspawn-sync&time=1751908408&request_lock_id=1751908408.9214