📚 Steps for Processing Experimental Data with R in Mathematical Statistics | 用R软件处理实验数据的步骤
In mathematical statistics, experimental data rarely arrive in a ready-to-analyze format. Systematic steps—from importing raw data to reporting final results—are essential to ensure valid and reproducible conclusions. R is a versatile, open-source environment widely used for statistical computing and graphics, making it an ideal tool for processing experimental data.
在数学统计中,实验数据很少以可直接分析的格式出现。从导入原始数据到报告最终结果的系统化步骤,对于确保有效且可重复的结论至关重要。R 是一个功能强大且开源的环境,广泛用于统计计算和图形展示,是处理实验数据的理想工具。
1. Setting Up the Environment and Importing Data | 设置环境与导入数据
Before any analysis, you need to install R and, preferably, RStudio. RStudio provides an integrated development environment that makes scripting, viewing data, and plotting easier. For specific tasks, load packages such as readr, dplyr, and ggplot2.
在进行任何分析之前,你需要安装 R,并最好同时安装 RStudio。RStudio 提供了一个集成开发环境,使脚本编写、数据查看和绘图更加便捷。对于特定任务,需加载如 readr、dplyr 和 ggplot2 等包。
Import data with base R functions or the readr package. For CSV files, use read.csv("file.csv", header = TRUE) or read_csv("file.csv"); for Excel files, use readxl::read_excel(); for text files with custom delimiters, use read.table().
使用基础 R 函数或 readr 包导入数据。对于 CSV 文件,使用 read.csv("file.csv", header = TRUE) 或 read_csv("file.csv");对于 Excel 文件,使用 readxl::read_excel();对于自定义分隔符的文本文件,使用 read.table()。
After importing, inspect the data structure with str(), class(), and summary(). Use head() and tail() to view the first and last rows, and names() to check column names.
导入后,使用 str()、class() 和 summary() 检查数据结构。使用 head() 和 tail() 查看首尾行,使用 names() 检查列名。
2. Data Cleaning and Preprocessing | 数据清洗与预处理
Raw experimental data may contain missing values, duplicate records, outliers, or inconsistent categorical labels. Cleaning improves data quality and prevents misleading results.
原始实验数据可能包含缺失值、重复记录、异常值或不一致的分类标签。清洗能提高数据质量,避免误导性结果。
Handle missing values by removing rows with na.omit() or complete.cases(), or by imputing with the mean, median, or a model-based method such as mice. Choose imputation carefully to avoid bias.
通过 na.omit() 或 complete.cases() 删除含缺失值的行,或使用均值、中位数或基于模型的方法(如 mice)进行缺失值填补。应谨慎选择填补方法,以避免偏差。
Detect duplicates with duplicated() and remove them using unique() or distinct() from dplyr. For inconsistent factor levels, use factor() or recode() to standardize labels.
使用 duplicated() 检测重复项,并通过 unique() 或 dplyr 包的 distinct() 删除。对于不一致的因子水平,使用 factor() 或 recode() 统一标签。
Outliers can be identified using the interquartile range (IQR) rule or z-scores. For a variable, an outlier is often defined as a value beyond Q1 − 1.5 × IQR or Q3 + 1.5 × IQR, or with |z| > 3.
异常值可以通过四分位距(IQR)规则或 z 分数识别。对于变量,异常值通常定义为超出 Q1 − 1.5 × IQR 或 Q3 + 1.5 × IQR 的值,或满足 |z| > 3 的值。
3. Descriptive Statistics | 描述性统计
Descriptive statistics summarise the main features of the data, including measures of central tendency, dispersion, and shape.
描述性统计概括数据的主要特征,包括集中趋势、离散程度和分布形态的度量。
Use summary() to obtain the minimum, first quartile, median, mean, third quartile, and maximum for each numeric variable. For grouped statistics, use aggregate() or tapply().
使用 summary() 获得每个数值变量的最小值、第一四分位数、中位数、均值、第三四分位数和最大值。对于分组统计,使用 aggregate() 或 tapply()。
For more detailed summaries, use the psych package’s describe() function, which gives the mean, standard deviation, skewness, and kurtosis in one output.
若要更详细的汇总,可使用 psych 包中的 describe() 函数,它同时输出均值、标准差、偏度和峰度。
-
Mean and median: measure central tendency
均值和中位数:度量集中趋势
-
Standard deviation and variance: measure spread
标准差和方差:度量离散程度
-
Skewness and kurtosis: measure distribution shape
偏度和峰度:度量分布形态
4. Data Visualization | 数据可视化
Visualisation helps uncover patterns, trends, and anomalies that are not obvious from numerical summaries. Base R and ggplot2 provide a wide range of plotting options.
可视化有助于发现数字汇总中不明显的模式、趋势和异常。基础 R 和 ggplot2 提供了丰富的绘图选项。
A histogram hist(data$value) displays the frequency distribution of a continuous variable. A boxplot boxplot(value ~ group, data) compares distributions across groups. A scatterplot plot(data$x, data$y) shows the relationship between two variables.
直方图 hist(data$value) 显示连续变量的频数分布。箱线图 boxplot(value ~ group, data) 比较各组分布。散点图 plot(data$x, data$y) 展示两个变量之间的关系。
Graphs created with ggplot2 allow easy layering of elements. For example:
使用 ggplot2 创建的图形可以轻松地叠加各种元素。例如:
library(ggplot2)
ggplot(data, aes(x = group, y = value)) +
geom_boxplot(fill = "lightblue") +
geom_jitter(width = 0.2) +
labs(title = "Boxplot by Group")
This code produces a boxplot with individual points overlaid, making the sample distribution transparent.
此代码生成带叠加点的箱线图,使样本分布更加直观。
5. Hypothesis Testing | 假设检验
Hypothesis testing allows you to make inferences about population parameters from sample data. In R, the base function t.test() is used for comparing means.
假设检验允许你根据样本数据对总体参数进行推断。在 R 中,基础函数 t.test() 用于均值比较。
For one sample, test whether the mean equals a specified value μ₀:
对于单样本,检验均值是否等于指定值 μ₀:
t = (x̄ − μ₀) / (s / √n)
where x̄ is the sample mean, s is the sample standard deviation, and n is the sample size. The command is t.test(data$value, mu = μ₀).
其中 x̄ 是样本均值,s 是样本标准差,n 是样本量。命令为 t.test(data$value, mu = μ₀)。
For independent samples, use t.test(value ~ group, data); for paired samples, use t.test(before, after, paired = TRUE). Non-parametric alternatives include wilcox.test() and kruskal.test().
对于独立样本,使用 t.test(value ~ group, data);对于配对样本,使用 t.test(before, after, paired = TRUE)。非参数替代方法包括 wilcox.test() 和 kruskal.test()。
6. Analysis of Variance (ANOVA) | 方差分析
When comparing means of three or more groups, use analysis of variance (ANOVA). In R, fit an ANOVA model with aov() or lm().
当比较三个及以上组的均值时,使用方差分析(ANOVA)。在 R 中,使用 aov() 或 lm() 拟合方差分析模型。
For a one-way ANOVA, use model <- aov(value ~ group, data), then summary(model) to obtain the F-statistic and p-value.
对于单因素方差分析,使用 model <- aov(value ~ group, data),然后 summary(model) 获得 F 统计量和 p 值。
If the overall test is significant, perform post-hoc comparisons with TukeyHSD(model) to identify which group means differ.
若整体检验显著,使用 TukeyHSD(model) 进行事后多重比较,以确定哪些组均值之间存在差异。
For two-way ANOVA, include an interaction term: aov(value ~ factorA * factorB, data). Check assumptions using plot(model) and Levene’s test from the car package.
对于双因素方差分析,添加交互项:aov(value ~ factorA * factorB, data)。使用 plot(model) 和 car 包中的 Levene 检验检查假设条件。
7. Regression Modeling | 回归建模
Regression analysis models the relationship between a response variable and one or more explanatory variables. The linear model is the most common starting point.
回归分析模拟响应变量与一个或多个解释变量之间的关系。线性模型是最常见的起点。
Fit a simple linear regression with lm(y ~ x, data) and display coefficients, R², and p-values using summary(model).
使用 lm(y ~ x, data) 拟合简单线性回归,并通过 summary(model) 显示系数、R² 和 p 值。
Add polynomial terms with lm(y ~ x + I(x^2), data) to model curvature. Include interactions using lm(y ~ x1 * x2, data), which is equivalent to x1 + x2 + x1:x2.
使用 lm(y ~ x + I(x^2), data) 添加多项式项以模拟曲线。使用 lm(y ~ x1 * x2, data) 包含交互作用,这等价于 x1 + x2 + x1:x2。
For multiple regression, use lm(y ~ x1 + x2 + x3, data). Compare nested models with anova(model_reduced, model_full) to test whether adding terms significantly improves the fit.
对于多元回归,使用 lm(y ~ x1 + x2 + x3, data)。通过 anova(model_reduced, model_full) 比较嵌套模型,以检验添加项是否显著改善拟合。
8. Model Diagnostics and Comparison | 模型诊断与比较
After fitting a model, check whether the underlying assumptions are satisfied. Residual analysis is a key step.
拟合模型后,需要检查基本假设是否满足。残差分析是一个关键步骤。
Use plot(model) to generate four diagnostic plots: residuals versus fitted values, a Q-Q plot of residuals, scale-location, and residuals versus leverage. These help detect non-linearity, non-normality, heteroscedasticity, and influential points.
使用 plot(model) 生成四个诊断图:残差对拟合值图、残差 Q-Q 图、尺度-位置图和残差对杠杆图。这些图有助于检测非线性、非正态性、异方差性和强影响点。
Formal tests include the Shapiro-Wilk test for normality on residuals (shapiro.test(resid(model))) and the Breusch-Pagan test for heteroscedasticity (lmtest::bptest(model)).
正式检验包括对残差进行正态性 Shapiro-Wilk 检验(shapiro.test(resid(model)))和异方差性 Breusch-Pagan 检验(lmtest::bptest(model))。
For model comparison, use AIC or BIC: AIC(model1, model2) and BIC(model1, model2). Smaller values indicate a better trade-off between goodness of fit and complexity.
对于模型比较,使用 AIC 或 BIC:AIC(model1, model2) 和 BIC(model1, model2)。数值越小表明拟合优度与复杂度之间的权衡越优。
9. Reporting and Exporting Results | 报告与导出结果
Clear reporting of statistical results is crucial for reproducibility and communication. R Markdown (via rmarkdown) is an excellent tool for combining code, output, and narrative text.
清晰报告统计结果对于可重复性和交流至关重要。R Markdown(通过 rmarkdown)是将代码、输出和叙述文本结合的优异工具。
Export cleaned data to a CSV file with write.csv(data, "cleaned_data.csv", row.names = FALSE). Save plots using ggsave("plot.png", width = 8, height = 6, dpi = 300) for publication-quality images.
使用 write.csv(data, "cleaned_data.csv", row.names = FALSE) 将清洗后的数据导出为 CSV 文件。使用 ggsave("plot.png", width = 8, height = 6, dpi = 300) 保存图形,以获得出版质量的图片。
In the final report, include the research question, data collection summary, descriptive statistics, inferential test results (with test statistic, degrees of freedom, and p-value), and effect sizes or confidence intervals.
在最终报告中,应包含研究问题、数据收集概述、描述性统计、推断检验结果(包括检验统计量、自由度和 p 值)以及效应量或置信区间。
10. Reproducibility and Best Practices | 可重复性与最佳实践
Reproducible research ensures that your analysis can be rerun by others or by yourself at a later date. Use RStudio Projects to manage file paths and settings.
可重复研究确保你的分析可以在之后由他人或你自己重新运行。使用 RStudio 项目管理文件路径和设置。
Set a random seed with set.seed(123) before any simulation or random sampling so that results are identical across runs. Record session information with sessionInfo() to document package versions.
在任何模拟或随机抽样前,使用 set.seed(123) 设置随机种子,以确保每次运行结果一致。使用 sessionInfo() 记录会话信息,以记录包版本。
Write modular code by creating functions for repetitive tasks. Avoid hard-coding absolute paths; instead, use here::here("data", "file.csv") for portable paths. Finally, clean up your workspace with rm(list = ls()) when necessary to avoid accidental conflicts.
通过创建函数将重复性任务模块化。避免硬编码绝对路径,而应使用 here::here("data", "file.csv") 获得可移植路径。最后,在必要时使用 rm(list = ls()) 清理工作区,以避免意外冲突。
Published by TutorHao | Mathematics Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply