英国留学 R 语言数据分析作业完整指南:从入门到高分报告(2026)
R 语言是英国高校统计学、经济学、心理学和商科数据分析专业的主流工具。和 Python 作业相比,R 作业的评分更看重统计解读的准确性和报告的可读性,而不只是代码能跑。
为什么 R 在英国高校这么常见?
- 统计学专业:R 是统计建模的黄金标准
- 心理学/教育学:大量心理统计包(如
psych、lavaan) - 经济学/计量经济学:
AER、plm等包专为经济数据设计 - 商科数据分析:
tidyverse生态极其完整
第一步:建立规范的 R 项目结构
很多学生把所有代码写在一个几百行的脚本里,这在 R 作业中是严重的减分项。规范结构:
my_assignment/
├── data/
│ ├── raw/ # 原始数据,不要修改
│ └── processed/ # 清洗后的数据
├── scripts/
│ ├── 01_data_cleaning.R
│ ├── 02_eda.R
│ └── 03_modelling.R
├── output/
│ ├── figures/ # 图表
│ └── tables/ # 统计表格
├── report.Rmd # R Markdown 报告
└── README.md
第二步:数据清洗(Data Cleaning)
数据清洗是你展示统计思维的第一个机会。
library(tidyverse)
df <- read_csv("data/raw/survey_data.csv")
# 1. 查看数据结构
glimpse(df)
summary(df)
# 2. 检查缺失值
df %>%
summarise(across(everything(), ~ sum(is.na(.)))) %>%
pivot_longer(everything(), names_to = "variable", values_to = "missing_count") %>%
filter(missing_count > 0)
# 3. 处理缺失值(须在报告中说明处理方式和理由)
df_clean <- df %>%
# 删除缺失比例 > 30% 的变量
select(where(~ mean(is.na(.)) < 0.3)) %>%
# 对连续变量用中位数填充
mutate(across(where(is.numeric), ~ ifelse(is.na(.), median(., na.rm = TRUE), .)))
# 4. 处理异常值(报告中须说明标准)
df_clean <- df_clean %>%
filter(
age >= 18 & age <= 100, # 合理年龄范围
income > 0 # 收入为正
)
cat("Rows removed:", nrow(df) - nrow(df_clean), "\n")
报告写作要点: 不只是写代码,要在报告里说明每个数据清洗决策的理由(为什么删除这些行?为什么用中位数而不是均值填充?)
第三步:探索性分析(EDA)与 ggplot2 可视化
高分 ggplot2 图表规范
library(ggplot2)
# 基础 ggplot2 高分模板
income_plot <- ggplot(df_clean, aes(x = education_level, y = income, fill = gender)) +
geom_boxplot(outlier.alpha = 0.3) +
scale_fill_manual(
values = c("#2563EB", "#059669"),
labels = c("Female", "Male")
) +
labs(
title = "Income Distribution by Education Level and Gender",
subtitle = "Based on 2023 UK Labour Force Survey (N = 2,847)",
x = "Education Level",
y = "Annual Income (£)",
fill = "Gender",
caption = "Source: ONS Labour Force Survey, 2023"
) +
theme_minimal(base_size = 12) +
theme(
plot.title = element_text(face = "bold"),
legend.position = "bottom",
axis.text.x = element_text(angle = 45, hjust = 1)
)
# 保存高清图(提交 PDF 报告用)
ggsave("output/figures/income_by_education.png",
plot = income_plot, width = 8, height = 5, dpi = 300)
评分要点:
- 必须有标题(Title)和轴标签(Axis Labels)
- 颜色选择要对色盲友好(避免红绿组合)
- 注明数据来源(Caption)
- 图表尺寸合适,字体清晰可读(导师打印时不会模糊)
第四步:统计检验与解读
这是 R 作业最容易失分的地方——很多学生只汇报 p 值,没有解读统计意义。
独立样本 t 检验
# 检验男女收入是否有显著差异
t_result <- t.test(income ~ gender, data = df_clean, var.equal = FALSE)
# 打印并解读
print(t_result)
# 计算效应量(Effect Size)— 很多学生忘记报告
library(effectsize)
cohens_d(income ~ gender, data = df_clean)
报告写作示例:
An independent samples Welch's t-test revealed a statistically significant difference in annual income between male (M = £38,420, SD = £12,103) and female (M = £32,891, SD = £11,847) respondents, t(1,842) = 8.34, p < .001. The effect size was moderate (Cohen's d = 0.46, 95% CI [0.37, 0.55]), indicating that gender accounts for approximately 5% of variance in income. These findings align with previous research on the gender pay gap (ONS, 2023).
注意:
- 汇报均值(M)和标准差(SD)
- 写出检验统计量(t(df) = 值)
- p 值用 "< .001" 而不是精确值(APA 格式)
- 必须汇报效应量(Cohen's d / eta squared),这是区分 First 和 2:1 的关键
线性回归(Linear Regression)
# 多元线性回归
model <- lm(income ~ age + education_level + gender + experience_years,
data = df_clean)
# 检查基本假设
library(performance)
check_model(model) # 生成诊断图(Residuals, Q-Q plot, etc.)
# 格式化输出
library(broom)
tidy(model, conf.int = TRUE) %>%
mutate(across(where(is.numeric), ~ round(., 3)))
回归报告要包含:
- 模型方程(写出所有预测变量)
- R² 和 Adjusted R²(模型解释力)
- F 检验结果(模型整体显著性)
- 各预测变量的 β 系数、t 值、p 值、95% 置信区间
- 假设检验结果(残差正态性、同方差性——用图表展示)
第五步:R Markdown 报告
英国高校 R 作业通常要求提交 R Markdown(.Rmd) 格式报告,这能把代码、输出和文字融为一体。
高分 R Markdown 模板头部:
---
title: "Data Analysis Assignment: UK Labour Market"
author: "Student ID: 1234567"
date: "`r Sys.Date()`"
output:
pdf_document:
toc: true
toc_depth: 3
number_sections: true
fig_caption: true
keep_tex: false
html_document:
theme: cosmo
toc: true
bibliography: references.bib
---
代码块(Chunk)规范:
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE, # 显示代码(除非另行指定)
warning = FALSE, # 不显示警告
message = FALSE, # 不显示消息
fig.align = "center",
fig.width = 7,
fig.height = 4
)
```
常见失分点清单
- 统计检验只汇报 p 值,没有效应量(Cohen's d / eta² / R²)
- 图表缺少标题、轴标签或数据来源
- 没有检验统计假设(回归残差正态性、方差齐性)
- 数据清洗没有在报告中说明理由
- R Markdown 没有正确设置
knitr::opts_chunk,导致输出混乱 - 使用了学校数据库才能访问的数据,但没有提供数据文件
需要帮助?
无论是统计检验的解读、回归模型的报告格式还是 ggplot2 可视化,学霸留学生辅导 有统计学和数据科学方向的学长学姐:
